-
Notifications
You must be signed in to change notification settings - Fork 2
/
config_test.go
122 lines (110 loc) · 3.04 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"bytes"
"errors"
"os"
"testing"
"time"
"github.com/hashicorp/memberlist"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
func TestCreateMemberlistConfig(t *testing.T) {
tests := []struct {
config []byte
expectedMLConfigFunc func() *memberlist.Config
}{
{
config: []byte(`
tcp_timeout: 100
push_pull_interval: 15000
probe_interval: 200
probe_timeout: 200
gossip_interval: 100
gossip_to_the_dead_time: 300
indirect_checks: 10
retransmit_mult: 10
suspicion_mult: 10
secret_key: abc
`),
expectedMLConfigFunc: func() *memberlist.Config {
mlConfig := memberlist.DefaultLocalConfig()
mlConfig.TCPTimeout = 100 * time.Millisecond
mlConfig.PushPullInterval = 15 * time.Second
mlConfig.ProbeInterval = 200 * time.Millisecond
mlConfig.ProbeTimeout = 200 * time.Millisecond
mlConfig.GossipInterval = 100 * time.Millisecond
mlConfig.GossipToTheDeadTime = 300 * time.Millisecond
mlConfig.IndirectChecks = 10
mlConfig.RetransmitMult = 10
mlConfig.SuspicionMult = 10
mlConfig.SecretKey = []byte("abc")
return mlConfig
},
},
{
config: []byte(`
secret_key: abc
`),
expectedMLConfigFunc: func() *memberlist.Config {
mlConfig := memberlist.DefaultLocalConfig()
mlConfig.SecretKey = []byte("abc")
return mlConfig
},
},
}
for _, test := range tests {
c := viper.New()
c.SetConfigType("yaml")
c.ReadConfig(bytes.NewBuffer(test.config))
mlConfig := createMemberlistConfig(c)
expectedMLConfig := test.expectedMLConfigFunc()
require.Equal(t, expectedMLConfig, mlConfig)
}
}
func TestGetConfig(t *testing.T) {
tests := []struct {
inputEnv map[string]string
expectedErr error
expectedConfigFunc func() *viper.Viper
}{
{
expectedErr: errors.New("missing required config key: discovery.domain"),
},
{
inputEnv: map[string]string{"BROKER_DISCOVERY_DOMAIN": "test"},
expectedConfigFunc: func() *viper.Viper {
c := viper.New()
d := []byte(`
mqtt:
port: 1883
subscription_size:
"cluster:message_from": 1024
"cluster:new_member": 10
discovery:
subscription_size:
"cluster:message_to": 1024
cluster:
expected_members: 3
config:
probe_interval: 500
`)
c.SetConfigType("yaml")
c.ReadConfig(bytes.NewBuffer(d))
return c
},
},
}
for _, test := range tests {
for k, v := range test.inputEnv {
os.Setenv(k, v)
}
c, err := getConfig()
require.Equal(t, test.expectedErr, err)
if test.expectedConfigFunc != nil {
expectedConfig := test.expectedConfigFunc()
require.Equal(t, expectedConfig.AllSettings(), c.AllSettings())
}
os.Clearenv()
}
}