forked from mendersoftware/mender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
302 lines (249 loc) · 8.07 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
// Copyright 2016 Mender Software 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/ioutil"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path"
"runtime"
"strings"
"testing"
"github.com/mendersoftware/log"
"github.com/stretchr/testify/assert"
)
func init() {
defaultConfFile = "mender-default-test.conf"
}
func TestMissingArgs(t *testing.T) {
err := doMain([]string{"-config", "mender.conf.example"})
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 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 = 9525080
programName := "mender"
built := false
statbuf, err := os.Stat(programName)
if os.IsNotExist(err) {
// Try building first
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())
}
built = true
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())
}
if built {
os.Remove(programName)
}
}
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 := NewDirStore(tdir)
assert.NotNil(t, ds)
db := NewDBStore(tdir)
defer db.Close()
assert.NotNil(t, db)
// pretend we have a tenant token
ds.WriteAll(defaultTenantTokenFile, []byte("foo-tenant-token"))
// setup test config
cpath := path.Join(tdir, "mender.config")
writeConfig(t, cpath, menderConfig{
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(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(authTokenName)
assert.NoError(t, err)
assert.Equal(t, []byte("foobar-token"), d)
// force boostrap and run again, check if key was changed
db.Remove(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(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(authTokenName)
assert.Error(t, err)
assert.True(t, os.IsNotExist(err))
}