forked from mackerelio/mackerel-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
253 lines (217 loc) · 6.21 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"os/signal"
"syscall"
"testing"
"time"
"github.com/mackerelio/mackerel-agent/command"
"github.com/mackerelio/mackerel-agent/pidfile"
)
func TestParseFlags(t *testing.T) {
// prepare dummy config
confFile, err := ioutil.TempFile("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary config file for test")
}
confFile.WriteString(`verbose=false
root="/hoge/fuga"
apikey="DUMMYAPIKEY"
diagnostic=false
`)
confFile.Sync()
confFile.Close()
defer os.Remove(confFile.Name())
mergedConfig, _ := resolveConfig(&flag.FlagSet{}, []string{"-conf=" + confFile.Name(), "-role=My-Service:default,INVALID#SERVICE", "-verbose", "-diagnostic"})
t.Logf(" apibase: %v", mergedConfig.Apibase)
t.Logf(" apikey: %v", mergedConfig.Apikey)
t.Logf(" root: %v", mergedConfig.Root)
t.Logf(" pidfile: %v", mergedConfig.Pidfile)
t.Logf(" diagnostic: %v", mergedConfig.Diagnostic)
t.Logf("roleFullnames: %v", mergedConfig.Roles)
t.Logf(" verbose: %v", mergedConfig.Verbose)
if mergedConfig.Root != "/hoge/fuga" {
t.Errorf("Root(confing from file) should be /hoge/fuga but: %v", mergedConfig.Root)
}
if len(mergedConfig.Roles) != 1 || mergedConfig.Roles[0] != "My-Service:default" {
t.Error("Roles(config from command line option) should be parsed")
}
if mergedConfig.Verbose != true {
t.Error("Verbose(overwritten by command line option) shoud be true")
}
if mergedConfig.Diagnostic != true {
t.Error("Diagnostic(overwritten by command line option) shoud be true")
}
}
func TestDetectForce(t *testing.T) {
// prepare dummy config
confFile, err := ioutil.TempFile("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary config file for test")
}
confFile.WriteString(`apikey="DUMMYAPIKEY"
`)
confFile.Sync()
confFile.Close()
defer os.Remove(confFile.Name())
argv := []string{"-conf=" + confFile.Name()}
conf, force, _ := resolveConfigForRetire(&flag.FlagSet{}, argv)
if force {
t.Errorf("force should be false")
}
if conf.Apikey != "DUMMYAPIKEY" {
t.Errorf("Apikey should be 'DUMMYAPIKEY'")
}
argv = append(argv, "-force")
conf, force, _ = resolveConfigForRetire(&flag.FlagSet{}, argv)
if !force {
t.Errorf("force should be true")
}
if conf.Apikey != "DUMMYAPIKEY" {
t.Errorf("Apikey should be 'DUMMYAPIKEY'")
}
}
func TestResolveConfigForRetire(t *testing.T) {
confFile, err := ioutil.TempFile("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary config file for test")
}
confFile.WriteString(`apikey="DUMMYAPIKEY"
`)
confFile.Sync()
confFile.Close()
defer os.Remove(confFile.Name())
// Allow accepting unnecessary options, pidfile, diagnostic and role.
// Because, these options are potentially passed in initd script by using $OTHER_OPTS.
argv := []string{
"-conf=" + confFile.Name(),
"-apibase=https://api.mackerelio.com",
"-pidfile=hoge",
"-root=hoge",
"-verbose",
"-diagnostic",
"-apikey=hogege",
"-role=hoge:fuga",
}
conf, force, _ := resolveConfigForRetire(&flag.FlagSet{}, argv)
if force {
t.Errorf("force should be false")
}
if conf.Apikey != "hogege" {
t.Errorf("Apikey should be 'hogege'")
}
}
func TestCreateAndRemovePidFile(t *testing.T) {
file, err := ioutil.TempFile("", "")
if err != nil {
t.Errorf("failed to create tmpfile, %s", err)
}
fpath := file.Name()
defer os.Remove(fpath)
err = pidfile.Create(fpath)
if err != nil {
t.Errorf("pid file should be created but, %s", err)
}
pidfile.Remove(fpath)
if err := pidfile.Create(fpath); err != nil {
t.Errorf("pid file should be created but, %s", err)
}
pidfile.Remove(fpath)
ioutil.WriteFile(fpath, []byte(fmt.Sprint(math.MaxInt32)), 0644)
if err := pidfile.Create(fpath); err != nil {
t.Errorf("old pid file should be ignored and new pid file should be created but, %s", err)
}
}
func TestSignalHandler(t *testing.T) {
app := &command.App{}
termCh := make(chan struct{})
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
go signalHandler(c, app, termCh)
resultCh := make(chan int)
maxTerminatingInterval = 100 * time.Millisecond
c <- os.Interrupt
c <- os.Interrupt
go func() {
<-termCh
<-termCh
<-termCh
<-termCh
resultCh <- 0
}()
go func() {
time.Sleep(time.Second)
resultCh <- 1
}()
if r := <-resultCh; r != 0 {
t.Errorf("Something went wrong")
}
}
func TestConfigTestOK(t *testing.T) {
// prepare dummy config
confFile, err := ioutil.TempFile("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary config file for test")
}
confFile.WriteString(`apikey="DUMMYAPIKEY"
`)
confFile.Sync()
confFile.Close()
defer os.Remove(confFile.Name())
argv := []string{"-conf=" + confFile.Name()}
err = doConfigtest(&flag.FlagSet{}, argv)
if err != nil {
t.Errorf("configtest(ok) must be return nil")
}
}
func TestConfigTestNotFound(t *testing.T) {
// prepare dummy config
confFile, err := ioutil.TempFile("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary config file for test")
}
confFile.WriteString(`apikey="DUMMYAPIKEY"
`)
confFile.Sync()
confFile.Close()
defer os.Remove(confFile.Name())
argv := []string{"-conf=" + confFile.Name() + "xxx"}
err = doConfigtest(&flag.FlagSet{}, argv)
if err == nil {
t.Errorf("configtest(failed) must be return error")
}
}
func TestConfigTestInvalidFormat(t *testing.T) {
// prepare dummy config
confFile, err := ioutil.TempFile("", "mackerel-config-test")
if err != nil {
t.Fatalf("Could not create temporary config file for test")
}
confFile.WriteString(`apikey="DUMMYAPIKEY"
invalid!!!
`)
confFile.Sync()
confFile.Close()
defer os.Remove(confFile.Name())
argv := []string{"-conf=" + confFile.Name()}
err = doConfigtest(&flag.FlagSet{}, argv)
if err == nil {
t.Errorf("configtest(failed) must be return error")
}
}
func TestDoOnce(t *testing.T) {
err := doOnce(&flag.FlagSet{}, []string{})
if err != nil {
t.Errorf("doOnce should return nil even if argv is empty, but returns %s", err)
}
}
func TestDoVersion(t *testing.T) {
err := doVersion(&flag.FlagSet{}, []string{})
if err != nil {
t.Errorf("doVersion should return nil, but returns %s", err)
}
}