forked from mendersoftware/mender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
209 lines (177 loc) · 6.42 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2018 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"io/ioutil"
"os"
"path"
"reflect"
"testing"
"github.com/mendersoftware/mender/client"
"github.com/stretchr/testify/assert"
)
var testConfig = `{
"ClientProtocol": "https",
"HttpsClient": {
"Certificate": "/data/client.crt",
"Key": "/data/client.key"
},
"RootfsPartA": "/dev/mmcblk0p2",
"RootfsPartB": "/dev/mmcblk0p3",
"UpdatePollIntervalSeconds": 10,
"InventoryPollIntervalSeconds": 60,
"ServerURL": "mender.io",
"ServerCertificate": "/var/lib/mender/server.crt",
"UpdateLogPath": "/var/lib/mender/log/deployment.log"
}`
var testBrokenConfig = `{
"ClientProtocol": "https",
"HttpsClient": {
"Certificate": "/data/client.crt",
"Key": "/data/client.key"
},
"RootfsPartA": "/dev/mmcblk0p2",
"RootfsPartB": "/dev/mmcblk0p3",
"PollIntervalSeconds": 60,
"ServerURL": "mender
"ServerCertificate": "/var/lib/mender/server.crt"
}`
var testMultipleServersConfig = `{
"Servers": [
{"ServerURL": "https://server.one/"},
{"ServerURL": "https://server.two/"},
{"ServerURL": "https://server.three/"}
]
}`
var testTooManyServerDefsConfig = `{
"ServerURL": "mender.io",
"ServerCertificate": "/var/lib/mender/server.crt",
"Servers": [{"ServerURL": "mender.io"}]
}`
func Test_readConfigFile_noFile_returnsError(t *testing.T) {
err := readConfigFile(nil, "non-existing-file")
assert.Error(t, err)
}
func Test_readConfigFile_brokenContent_returnsError(t *testing.T) {
configFile, _ := os.Create("mender.config")
defer os.Remove("mender.config")
configFile.WriteString(testBrokenConfig)
// fails on first call to readConfigFile (invalid JSON)
confFromFile, err := loadConfig("mender.config", "does-not-exist.config")
assert.Error(t, err)
assert.Nil(t, confFromFile)
}
func validateConfiguration(t *testing.T, actual *menderConfig) {
expectedConfig := menderConfig{
ClientProtocol: "https",
HttpsClient: struct {
Certificate string
Key string
SkipVerify bool
}{
Certificate: "/data/client.crt",
Key: "/data/client.key",
SkipVerify: false,
},
RootfsPartA: "/dev/mmcblk0p2",
RootfsPartB: "/dev/mmcblk0p3",
UpdatePollIntervalSeconds: 10,
InventoryPollIntervalSeconds: 60,
ServerURL: "mender.io",
ServerCertificate: "/var/lib/mender/server.crt",
UpdateLogPath: "/var/lib/mender/log/deployment.log",
Servers: []client.MenderServer{{ServerURL: "mender.io"}},
}
if !assert.True(t, reflect.DeepEqual(actual, &expectedConfig)) {
t.Logf("got: %+v", actual)
t.Logf("expected: %+v", expectedConfig)
}
}
func Test_loadConfig_correctConfFile_returnsConfiguration(t *testing.T) {
configFile, _ := os.Create("mender.config")
defer os.Remove("mender.config")
configFile.WriteString(testConfig)
config, err := loadConfig("mender.config", "does-not-exist.config")
assert.NoError(t, err)
assert.NotNil(t, config)
validateConfiguration(t, config)
config2, err2 := loadConfig("does-not-exist.config", "mender.config")
assert.NoError(t, err2)
assert.NotNil(t, config2)
validateConfiguration(t, config2)
}
func TestServerURLConfig(t *testing.T) {
configFile, _ := os.Create("mender.config")
defer os.Remove("mender.config")
configFile.WriteString(`{"ServerURL": "https://mender.io/"}`)
config, err := loadConfig("mender.config", "does-not-exist.config")
assert.NoError(t, err)
assert.Equal(t, "https://mender.io", config.Servers[0].ServerURL)
// Not allowed to specify server(s) both as a list and string entry.
configFile.Seek(0, os.SEEK_SET)
configFile.WriteString(testTooManyServerDefsConfig)
config, err = loadConfig("mender.config", "does-not-exist.config")
assert.Error(t, err)
assert.Nil(t, config)
}
// TestMultipleServersConfig attempts to add multiple servers to config-
// file, as well as overriding the ServerURL from the first server.
func TestMultipleServersConfig(t *testing.T) {
// create a temporary mender.conf file
tdir, _ := ioutil.TempDir("", "mendertest")
confPath := path.Join(tdir, "mender.conf")
confFile, err := os.Create(confPath)
defer os.RemoveAll(tdir)
assert.NoError(t, err)
confFile.WriteString(testMultipleServersConfig)
// load config and assert expected values i.e. check that all entries
// are present and URL's trailing forward slash is trimmed off.
conf, err := loadConfig(confPath, "does-not-exist.config")
assert.NoError(t, err)
assert.Equal(t, "https://server.one", conf.Servers[0].ServerURL)
assert.Equal(t, "https://server.two", conf.Servers[1].ServerURL)
assert.Equal(t, "https://server.three", conf.Servers[2].ServerURL)
}
func TestConfigurationMergeSettings(t *testing.T) {
var mainConfigJson = `{
"RootfsPartA": "Eggplant",
"UpdatePollIntervalSeconds": 375
}`
var fallbackConfigJson = `{
"RootfsPartA": "Spinach",
"RootfsPartB": "Lettuce"
}`
mainConfigFile, _ := os.Create("main.config")
defer os.Remove("main.config")
mainConfigFile.WriteString(mainConfigJson)
fallbackConfigFile, _ := os.Create("fallback.config")
defer os.Remove("fallback.config")
fallbackConfigFile.WriteString(fallbackConfigJson)
config, err := loadConfig("main.config", "fallback.config")
assert.NoError(t, err)
assert.NotNil(t, config)
// When a setting appears in neither file, it is left with its default value.
assert.Equal(t, "", config.ServerCertificate)
assert.Equal(t, 0, config.StateScriptTimeoutSeconds)
// When a setting appears in both files, the main file takes precedence.
assert.Equal(t, "Eggplant", config.RootfsPartA)
// When a setting appears in only one file, its value is used.
assert.Equal(t, "Lettuce", config.RootfsPartB)
assert.Equal(t, 375, config.UpdatePollIntervalSeconds)
}
func TestConfigurationNeitherFileExistsIsError(t *testing.T) {
config, err := loadConfig("does-not-exist", "also-does-not-exist")
assert.Error(t, err)
assert.Nil(t, config)
}