forked from mendersoftware/mender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
183 lines (157 loc) · 5.19 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
// 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"
import "os/exec"
import "github.com/mendersoftware/log"
import mt "github.com/mendersoftware/mendertesting"
import "os"
import "strings"
import "testing"
import "io/ioutil"
import "fmt"
func TestMissingArgs(t *testing.T) {
if err := doMain([]string{}); err == nil {
t.Fatal("Calling doMain() with no arguments does not " +
"produce error")
} else {
mt.AssertErrorSubstring(t, err, errMsgNoArgumentsGiven.Error())
}
}
func TestAmbiguousArgumentsArgs(t *testing.T) {
if err := doMain([]string{"-daemon", "-commit"}); err != errMsgAmbiguousArgumentsGiven {
t.Fail()
}
}
func TestLoggingOptions(t *testing.T) {
if err := doMain([]string{"-commit", "-log-level", "crap"}); err == nil {
t.Fatal("'crap' log level should have given error")
} else {
// Should have a reference to log level.
mt.AssertErrorSubstring(t, err, "Level")
}
if err := doMain([]string{"-info", "-log-level", "debug"}); err == nil {
t.Fatal("Incompatible log levels should have given error")
} else {
mt.AssertErrorSubstring(t, err,
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")
mt.AssertTrue(t, strings.Index(buf.String(), "Should show") >= 0)
mt.AssertTrue(t, strings.Index(buf.String(), "Should not show") < 0)
mt.AssertTrue(t, strings.Index(buf.String(), "Should also not show") < 0)
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()
mt.AssertTrue(t, strings.Index(buf.String(),
"Module filter should show main_test") >= 0)
mt.AssertTrue(t, strings.Index(buf.String(),
"Module filter should show MyModule") >= 0)
mt.AssertTrue(t, strings.Index(buf.String(),
"Module filter should not show MyOtherModule") < 0)
doMain([]string{"-log-file", "test.log"})
log.Errorln("Should be in log file")
fd, err := os.Open("test.log")
mt.AssertTrue(t, err == nil)
var bytebuf [4096]byte
n, err := fd.Read(bytebuf[:])
mt.AssertTrue(t, err == nil)
mt.AssertTrue(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.
mt.AssertTrue(t, err != nil)
mt.AssertTrue(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")
if err != nil {
t.Fatal("failed to create temporary file")
}
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
if err != nil {
t.Fatal("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\n", CreateVersionString())
if string(data) != expected {
t.Fatalf("unexpected version output '%s' expected '%s'",
string(data), expected)
}
}