-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
89 lines (77 loc) · 1.69 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
package main
import (
"testing"
"time"
)
type ruletest struct {
props map[string]string
ok bool
}
type dev map[string]string
func (f dev) Syspath() string { return f["HID_NAME"] }
func (f dev) Action() string { return "add" }
func (f dev) Properties() map[string]string {
return f
}
func (f dev) PropertyValue(k string) string {
return f[k]
}
var fakedevices = []device{
dev{"HID_NAME": "foo", "SUBSYSTEM": "hid"},
dev{"HID_NAME": "bar", "SUBSYSTEM": "hid"},
}
func testConfig() *Config {
return &Config{
ScriptPath: "",
Rules: []rule{
{PropName: "HID_NAME", PropValue: "foo",
Command: "foo", Action: "add"},
},
}
}
func TestWatchLoop(t *testing.T) {
conf := testConfig()
devchan := make(chan device)
matchchan := make(chan rule)
go watchLoop(devchan, matchchan, conf)
go func() {
for _, d := range fakedevices {
devchan <- d
}
close(devchan)
}()
out := <-matchchan
if out.Command != "foo" {
t.Error("match failure, got:", out.Command, "want: foo")
}
out = <-matchchan
if out.Command != "" {
t.Error("Rule mis-match, should have got nil rule, got: ", out)
}
}
// more an intergration test, but
func TestCommandRunner(t *testing.T) {
conf := testConfig()
Workers = 1
WorkerDelay = time.Nanosecond
matchchan := commandRunners(conf)
matchchan <- rule{Command: "/bin/true"}
matchchan <- rule{Command: "/bin/false"}
time.Sleep(time.Millisecond)
}
var asstrings = []string{`---
HID_NAME = "foo"
SUBSYSTEM = "hid"
`, `---
HID_NAME = "bar"
SUBSYSTEM = "hid"
`,
}
func TestDeviceList(t *testing.T) {
for i := range fakedevices {
str := devString(fakedevices[i])
if str != asstrings[i] {
t.Error("list format is wrong, got:", str, "want:", asstrings[i])
}
}
}