-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_test.go
71 lines (63 loc) · 1.73 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"bytes"
"github.com/spf13/viper"
"reflect"
"testing"
"time"
)
func TestReadConfig(t *testing.T) {
var yamlContent = []byte(`
port: 9123
requestTimeout: 5s
scrapeInterval: 60s
devices:
- macAddress: "ABC12345"
displayName: "livingRoomShutter"
type: "switch25"
username: "some-user"
password: "pass123"
- macAddress: "123DEF"
displayName: "kitchenShutter"
type: "switch25"
username: "another-user"
password: "secure"
`)
viper.SetConfigType("yaml")
viper.ReadConfig(bytes.NewBuffer(yamlContent))
var givenConfig configuration
err := viper.Unmarshal(&givenConfig)
if err != nil {
t.Errorf("could not unmarshal status endpoint content: %v", err)
}
expectedConfig := configuration{
Port: 9123,
RequestTimeout: time.Second * time.Duration(5),
ScrapeInterval: time.Second * time.Duration(60),
Devices: []device{
{
MACAddress: "ABC12345",
DisplayName: "livingRoomShutter",
Type: "switch25",
Username: "some-user",
Password: "pass123",
},
{
MACAddress: "123DEF",
DisplayName: "kitchenShutter",
Type: "switch25",
Username: "another-user",
Password: "secure",
},
},
}
if !reflect.DeepEqual(givenConfig, expectedConfig) {
t.Error("given config file does not match expected config")
}
if givenConfig.Devices[0].getStatusURL() != "http://shellyswitch25-ABC12345/status" {
t.Errorf("getStatusURL() = %s; want http://shellyswitch25-ABC12345/status", givenConfig.Devices[0].getStatusURL())
}
if givenConfig.Devices[1].getStatusURL() != "http://shellyswitch25-123DEF/status" {
t.Errorf("getStatusURL() = %s; want http://shellyswitch25-123DEF/status", givenConfig.Devices[1].getStatusURL())
}
}