-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
166 lines (147 loc) · 3.92 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
package main
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"sync"
"testing"
"time"
"github.com/m-lab/go/prometheusx"
"github.com/m-lab/go/rtx"
)
var dtSchema = `[
{
"fields": [
{
"mode": "NULLABLE",
"name": "Output",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "CommandLine",
"type": "STRING"
},
{
"mode": "NULLABLE",
"name": "Name",
"type": "STRING"
}
],
"mode": "REPEATED",
"name": "commands",
"type": "RECORD"
}
]`
func countFiles(dir string) int {
filecount := 0
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info == nil {
return nil
}
if !info.IsDir() {
filecount++
}
return nil
})
return filecount
}
func TestMainOnce(t *testing.T) {
// Reset global variables into a known-good start state.
mainCtx, mainCancel = context.WithCancel(context.Background())
defer mainCancel()
dir, err := ioutil.TempDir("", "TestMainOnce")
rtx.Must(err, "failed to create temp data dir")
defer os.RemoveAll(dir)
rtx.Must(os.MkdirAll(dir+"/data", 0o777), "failed to create data subdir")
config := `[{"Name": "uname", "Cmd": ["uname", "-a"]}]`
rtx.Must(ioutil.WriteFile(dir+"/config.json", []byte(config), 0o666), "failed to write config")
rtx.Must(ioutil.WriteFile(dir+"/nodeinfo1.json", []byte(dtSchema), 0o666), "failed to write schema")
*datadir = dir + "/data"
*schemaDir = dir + "/datatypes"
*schemaFile = dir + "/nodeinfo1.json"
*configFile = dir + "/config.json"
*once = true
*smoketest = true
*waittime = 3 * time.Second
*prometheusx.ListenAddress = ":0"
// Run main.
main()
// Verify that some files were created inside uname.
filecount := countFiles(dir + "/data")
if filecount == 0 {
t.Errorf("No files were produced when we ran main.")
}
}
func TestMainMultipleAndReload(t *testing.T) {
// Reset global variables into a known-good start state.
mainCtx, mainCancel = context.WithCancel(context.Background())
defer mainCancel()
dir, err := ioutil.TempDir("", "TestMainMultiple")
rtx.Must(err, "failed to create tempdir")
defer os.RemoveAll(dir)
rtx.Must(os.MkdirAll(dir+"/data", 0o777), "failed to create data subdir")
rtx.Must(ioutil.WriteFile(dir+"/nodeinfo1.json", []byte(dtSchema), 0o666), "failed to write schema")
*datadir = dir + "/data"
*schemaDir = dir + "/datatypes"
*schemaFile = dir + "/nodeinfo1.json"
*once = false
*smoketest = false
*waittime = time.Duration(1 * time.Millisecond)
*prometheusx.ListenAddress = ":0"
*configFile = dir + "/config.json"
config := `[
{
"Name": "uname",
"Cmd": ["uname", "-a"]
},
{
"Name": "ifconfig",
"Cmd": ["ifconfig", "-a"]
}
]
`
rtx.Must(ioutil.WriteFile(dir+"/config.json", []byte(config), 0o666), "failed to write config")
rtx.Must(err, "failed to write config")
// Run main but sleep for .5s to guarantee that the timer will go off on its
// own multiple times.
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
main()
wg.Done()
}()
start := time.Now().UTC()
unameokay := false
ifconfigokay := false
for time.Now().UTC().Sub(start) < time.Second && !(unameokay && ifconfigokay) {
unameokay = countFiles(dir+"/data") > 1
ifconfigokay = countFiles(dir+"/data") > 1
}
if !ifconfigokay || !unameokay {
t.Error("Not enough output was produced in a second")
}
newConfig := `[
{
"Name": "ls",
"Cmd": ["ls"]
}
]
`
rtx.Must(ioutil.WriteFile(dir+"/config.json", []byte(newConfig), 0o666), "failed to write newConfig")
time.Sleep(500 * time.Millisecond)
start = time.Now().UTC()
lsokay := false
for time.Now().UTC().Sub(start) < time.Second && !lsokay {
lsokay = countFiles(dir+"/data") > 1
}
if !lsokay {
t.Errorf("Not enough files were produced with the ls config.")
}
os.Remove(dir + "/config.json")
time.Sleep(100 * time.Millisecond)
// Make sure that the file disappearing does not cause a crash.
mainCancel()
wg.Wait()
}