forked from mendersoftware/mender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
507 lines (425 loc) · 14.1 KB
/
main_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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Copyright 2019 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 (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path"
"runtime"
"strings"
"syscall"
"testing"
"time"
"github.com/mendersoftware/log"
"github.com/mendersoftware/mender/client"
"github.com/mendersoftware/mender/datastore"
"github.com/mendersoftware/mender/installer"
"github.com/mendersoftware/mender/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func init() {
defaultConfFile = "mender-default-test.conf"
}
func TestMissingArgs(t *testing.T) {
err := doMain([]string{"-config", "mender.conf.production"})
assert.Error(t, err, "calling doMain() with no arguments should produce an error")
assert.Contains(t, err.Error(), errMsgNoArgumentsGiven.Error())
}
func TestAmbiguousArgumentsArgs(t *testing.T) {
err := doMain([]string{"-daemon", "-commit"})
assert.Error(t, err)
assert.Equal(t, errMsgAmbiguousArgumentsGiven, err)
}
func TestArgsParseCheckUpdate(t *testing.T) {
args := []string{"-check-update"}
runOpts, err := argsParse(args)
assert.NoError(t, err)
assert.Equal(t, true, *runOpts.updateCheck)
}
func TestRunDaemon(t *testing.T) {
// create directory for storing deployments logs
tempDir, _ := ioutil.TempDir("", "logs")
defer os.RemoveAll(tempDir)
DeploymentLogger = NewDeploymentLogManager(tempDir)
var buf bytes.Buffer
oldOutput := log.Log.Out
log.SetOutput(&buf)
log.SetLevel(log.DebugLevel)
defer log.SetOutput(oldOutput)
ds := store.NewMemStore()
tests := map[string]struct {
signal syscall.Signal
}{
"check-update": {
signal: syscall.SIGUSR1,
},
"inventory-update": {
signal: syscall.SIGUSR2,
},
}
for name, test := range tests {
mender := newDefaultTestMender()
td := &menderDaemon{
mender: mender,
sctx: StateContext{
store: ds,
wakeupChan: make(chan bool, 1),
},
store: ds,
forceToState: make(chan State, 1),
}
go func() {
err := runDaemon(td)
require.Nil(t, err, "Daemon returned with an error code")
}()
for td.mender.GetCurrentState() != authorizeWaitState {
time.Sleep(time.Millisecond * 200)
}
proc, err := os.FindProcess(os.Getpid())
require.Nil(t, err)
require.Nil(t, proc.Signal(test.signal))
// Give the client some time to handle the signal.
time.Sleep(time.Second * 1)
td.StopDaemon()
assert.Contains(t, buf.String(), "forced wake-up", name+" signal did not force daemon from sleep")
buf.Reset()
}
}
func TestLoggingOptions(t *testing.T) {
err := doMain([]string{"-commit", "-log-level", "crap"})
assert.Error(t, err, "'crap' log level should have given error")
// Should have a reference to log level.
assert.Contains(t, err.Error(), "Level")
err = doMain([]string{"-info", "-log-level", "debug"})
assert.Error(t, err, "Incompatible log levels should have given error")
assert.Contains(t, err.Error(), errMsgIncompatibleLogOptions.Error())
var buf bytes.Buffer
oldOutput := log.Log.Out
log.SetOutput(&buf)
defer log.SetOutput(oldOutput)
// Ignore errors for now, we just want to know if the logging level was
// applied.
log.SetLevel(log.DebugLevel)
doMain([]string{"-log-level", "panic"})
log.Debugln("Should not show")
doMain([]string{"-debug"})
log.Debugln("Should show")
doMain([]string{"-info"})
log.Debugln("Should also not show")
logdata := buf.String()
assert.Contains(t, logdata, "Should show")
assert.NotContains(t, logdata, "Should not show")
assert.NotContains(t, logdata, "Should also not show")
doMain([]string{"-log-modules", "main_test,MyModule"})
log.Errorln("Module filter should show main_test")
log.PushModule("MyModule")
log.Errorln("Module filter should show MyModule")
log.PushModule("MyOtherModule")
log.Errorln("Module filter should not show MyOtherModule")
log.PopModule()
log.PopModule()
assert.True(t, strings.Index(buf.String(),
"Module filter should show main_test") >= 0)
assert.True(t, strings.Index(buf.String(),
"Module filter should show MyModule") >= 0)
assert.True(t, strings.Index(buf.String(),
"Module filter should not show MyOtherModule") < 0)
defer os.Remove("test.log")
doMain([]string{"-log-file", "test.log"})
log.Errorln("Should be in log file")
fd, err := os.Open("test.log")
assert.NoError(t, err)
var bytebuf [4096]byte
n, err := fd.Read(bytebuf[:])
assert.True(t, err == nil)
assert.True(t, strings.Index(string(bytebuf[0:n]),
"Should be in log file") >= 0)
err = doMain([]string{"-no-syslog"})
// Just check that the flag can be specified.
assert.True(t, err != nil)
assert.True(t, strings.Index(err.Error(), "syslog") < 0)
}
func TestBinarySize(t *testing.T) {
// Test that the binary does not unexpectedly increase a lot in size,
// this is intended to protect against introducing very large
// dependencies. It is perfectly okay to increase this number as the
// program grows, but the binary size should be verified before doing
// so.
//
// When increasing, use current binary size on amd64 + 1M.
const maxSize int64 = 7600000
programName := "mender"
cmd := exec.Command("go", "version")
version, err := cmd.CombinedOutput()
require.NoError(t, err)
t.Logf("Go version: %s", string(version))
programName = "/tmp/mender"
cmd = exec.Command("go", "build", "-o", programName)
err = cmd.Run()
if err != nil {
t.Fatalf("Could not build '%s': %s",
programName, err.Error())
}
defer os.Remove(programName)
cmd = exec.Command("strip", programName)
err = cmd.Run()
require.NoError(t, err)
statbuf, err := os.Stat(programName)
if err != nil {
t.Fatalf("Could not stat '%s': %s. Please build before "+
"testing.", programName, err.Error())
}
if statbuf.Size() > maxSize {
t.Fatalf("'%s' has grown unexpectedly big (%d bytes). "+
"Check that file size is ok?", programName,
statbuf.Size())
}
}
func TestVersion(t *testing.T) {
oldstdout := os.Stdout
tfile, err := ioutil.TempFile("", "mendertest")
assert.NoError(t, err)
tname := tfile.Name()
// pretend we're stdout now
os.Stdout = tfile
// running with stderr pointing to temp file
err = doMain([]string{"-version"})
// restore previous stderr
os.Stdout = oldstdout
assert.NoError(t, err, "calling main with -version should not produce an error")
// rewind
tfile.Seek(0, 0)
data, _ := ioutil.ReadAll(tfile)
tfile.Close()
os.Remove(tname)
expected := fmt.Sprintf("%s\nruntime: %s\n", VersionString(), runtime.Version())
assert.Equal(t, expected, string(data),
"unexpected version output '%s' expected '%s'", string(data), expected)
}
func writeConfig(t *testing.T, path string, conf menderConfig) {
cf, err := os.Create(path)
assert.NoError(t, err)
defer cf.Close()
d, _ := json.Marshal(conf)
_, err = cf.Write(d)
assert.NoError(t, err)
}
func writeFakeIdentityHelper(t *testing.T, path string, script string) {
f, err := os.Create(path)
assert.NoError(t, err)
defer f.Close()
_, err = f.WriteString(script)
assert.NoError(t, err)
err = os.Chmod(path, 0755)
assert.NoError(t, err)
}
// go through bootstrap procedure
func TestMainBootstrap(t *testing.T) {
var err error
// fake server first
responder := &struct {
httpStatus int
data string
headers http.Header
}{
http.StatusOK,
"foobar-token",
http.Header{},
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
responder.headers = r.Header
w.WriteHeader(responder.httpStatus)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, responder.data)
}))
defer ts.Close()
// directory for keeping test data
tdir, err := ioutil.TempDir("", "mendertest")
defer os.RemoveAll(tdir)
// setup a dirstore helper to easily access file contents in test dir
ds := store.NewDirStore(tdir)
assert.NotNil(t, ds)
db := store.NewDBStore(tdir)
defer db.Close()
assert.NotNil(t, db)
// setup test config
cpath := path.Join(tdir, "mender.config")
writeConfig(t, cpath, menderConfig{
menderConfigFromFile: menderConfigFromFile{
Servers: []client.MenderServer{{ServerURL: ts.URL}},
},
})
// override identity helper script
oldidh := identityDataHelper
defer func(old string) {
identityDataHelper = old
}(oldidh)
newidh := path.Join(tdir, "fakehelper")
writeFakeIdentityHelper(t, newidh,
`#!/bin/sh
echo mac=00:11:22:33:44:55
`)
identityDataHelper = newidh
// run bootstrap
db.Remove(datastore.AuthTokenName)
err = doMain([]string{"-data", tdir, "-config", cpath, "-debug", "-bootstrap"})
assert.NoError(t, err)
// should have generated a key
keyold, err := ds.ReadAll(defaultKeyFile)
assert.NoError(t, err)
assert.NotEmpty(t, keyold)
// and we should have a token
d, err := db.ReadAll(datastore.AuthTokenName)
assert.NoError(t, err)
assert.Equal(t, []byte("foobar-token"), d)
// force boostrap and run again, check if key was changed
db.Remove(datastore.AuthTokenName)
err = doMain([]string{"-data", tdir, "-config", cpath, "-debug", "-bootstrap", "-forcebootstrap"})
assert.NoError(t, err)
keynew, err := ds.ReadAll(defaultKeyFile)
assert.NoError(t, err)
assert.NotEmpty(t, keynew)
assert.NotEqual(t, keyold, keynew)
db.Remove(datastore.AuthTokenName)
// return non 200 status code, we should get an error as authorization has
// failed
responder.httpStatus = http.StatusUnauthorized
err = doMain([]string{"-data", tdir, "-config", cpath, "-debug", "-bootstrap", "-forcebootstrap"})
assert.Error(t, err)
_, err = db.ReadAll(datastore.AuthTokenName)
assert.Error(t, err)
assert.True(t, os.IsNotExist(err))
}
func TestPrintArtifactName(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "TestPrintArtifactName")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
require.NoError(t, os.MkdirAll(path.Join(tmpdir, "etc"), 0755))
require.NoError(t, os.MkdirAll(path.Join(tmpdir, "data"), 0755))
tfile, err := os.OpenFile(path.Join(tmpdir, "etc", "artifact_info"),
os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
require.NoError(t, err)
dbstore := store.NewDBStore(path.Join(tmpdir, "data"))
config := &menderConfig{
ArtifactInfoFile: tfile.Name(),
}
deviceManager := NewDeviceManager(nil, config, dbstore)
// no error
_, err = io.WriteString(tfile, "artifact_name=foobar")
require.NoError(t, err)
assert.Nil(t, PrintArtifactName(deviceManager))
name, err := deviceManager.GetCurrentArtifactName()
require.NoError(t, err)
assert.Equal(t, "foobar", name)
// DB should override file.
dbstore.WriteAll(datastore.ArtifactNameKey, []byte("db-name"))
assert.Nil(t, PrintArtifactName(deviceManager))
name, err = deviceManager.GetCurrentArtifactName()
require.NoError(t, err)
assert.Equal(t, "db-name", name)
// Erasing it should restore old.
dbstore.Remove(datastore.ArtifactNameKey)
assert.Nil(t, PrintArtifactName(deviceManager))
name, err = deviceManager.GetCurrentArtifactName()
require.NoError(t, err)
assert.Equal(t, "foobar", name)
// empty artifact_name should fail
err = ioutil.WriteFile(tfile.Name(), []byte("artifact_name="), 0644)
//overwrite file contents
require.NoError(t, err)
assert.EqualError(t, PrintArtifactName(deviceManager), "The Artifact name is empty. Please set a valid name for the Artifact!")
// two artifact_names is also an error
err = ioutil.WriteFile(tfile.Name(), []byte(fmt.Sprint("artifact_name=a\ninfo=i\nartifact_name=b\n")), 0644)
require.NoError(t, err)
expected := "More than one instance of artifact_name found in manifest file"
err = PrintArtifactName(deviceManager)
require.Error(t, err)
assert.Contains(t, err.Error(), expected)
}
func TestGetMenderDaemonPID(t *testing.T) {
tests := map[string]struct {
cmd *exec.Cmd
expected string
}{
"error": {
exec.Command("abc"),
"getMenderDaemonPID: Failed to run systemctl",
},
"error: no output": {
exec.Command("printf", ""),
"could not find the PID of the mender daemon",
},
"return PID": {
exec.Command("echo", "MainPID=123"),
"123",
},
}
for name, test := range tests {
pid, err := getMenderDaemonPID(test.cmd)
if err != nil && test.expected != "" {
assert.Contains(t, err.Error(), test.expected, name)
}
if pid != "" {
assert.Equal(t, test.expected, pid, name)
}
}
cmdKill := exec.Command("abc")
cmdPID := exec.Command("echo", "123")
assert.Error(t, updateCheck(cmdKill, cmdPID))
}
// Minimal init
func TestInitDaemon(t *testing.T) {
// create directory for storing deployments logs
tempDir, _ := ioutil.TempDir("", "logs")
defer os.RemoveAll(tempDir)
DeploymentLogger = NewDeploymentLogManager(tempDir)
bootstrap := false
dualRootfs := installer.NewDualRootfsDevice(nil, nil, installer.DualRootfsDeviceConfig{})
d, err := initDaemon(&menderConfig{}, dualRootfs, &installer.UBootEnv{},
&runOptionsType{dataStore: &tempDir, bootstrapForce: &bootstrap})
require.Nil(t, err)
assert.NotNil(t, d)
// Test with failing init daemon
runOpts, err := argsParse([]string{"-daemon"})
require.Nil(t, err)
assert.Error(t, handleCLIOptions(runOpts, &installer.UBootEnv{}, dualRootfs, &menderConfig{}))
}
// Tests that the client will boot with an error message in the case of an invalid server certificate.
func TestInvalidServerCertificateBoot(t *testing.T) {
tdir, err := ioutil.TempDir("", "invalidcert-test")
require.Nil(t, err)
logBuf := bytes.NewBuffer(nil)
defer func(oldLog *log.Logger) { log.Log = oldLog }(log.Log) // Restore standard logger
log.Log = log.New()
log.SetLevel(log.WarnLevel)
log.SetOutput(logBuf)
mconf := menderConfig{
menderConfigFromFile: menderConfigFromFile{
ServerCertificate: "/some/invalid/cert.crt",
},
}
bootstrap := false
_, err = initDaemon(&mconf, nil, &installer.UBootEnv{},
&runOptionsType{dataStore: &tdir, bootstrapForce: &bootstrap})
assert.NoError(t, err, "initDaemon returned an unexpected error")
assert.Contains(t, logBuf.String(), "IGNORING ERROR")
}