-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_test.go
More file actions
127 lines (105 loc) · 3.08 KB
/
write_test.go
File metadata and controls
127 lines (105 loc) · 3.08 KB
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
package systemder
import (
"os"
"strings"
"testing"
)
type writeMock struct {
callsCount int
calls []struct {
writtenPath string
writtenData []byte
writtenPerm os.FileMode
}
}
func (w *writeMock) writeFile(path string, data []byte, perm os.FileMode) error {
if w.calls == nil {
w.calls = make([]struct {
writtenPath string
writtenData []byte
writtenPerm os.FileMode
}, 0)
}
call := struct {
writtenPath string
writtenData []byte
writtenPerm os.FileMode
}{
writtenPath: path,
writtenData: data,
writtenPerm: perm,
}
w.calls = append(w.calls, call)
w.callsCount++
return nil
}
func TestGenerateAndWriteService(t *testing.T) {
log := &NullLogger{}
write := &writeMock{}
s := &Systemder{
log: log,
writeFile: write.writeFile,
}
desc := "Test service"
name := "testservice"
err := s.GenerateAndWriteService(desc, name)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if write.callsCount != 1 {
t.Fatalf("unexpected calls count: %d", write.callsCount)
}
if write.calls[0].writtenPath != "/etc/systemd/system/"+name+".service" {
t.Fatalf("unexpected written path: %s", write.calls[0].writtenPath)
}
if !strings.Contains(string(write.calls[0].writtenData), desc) {
t.Fatalf("unexpected written data: %s", write.calls[0].writtenData)
}
binaryName := "systemder.test" // go test makes a binary with this name
if !strings.Contains(string(write.calls[0].writtenData), binaryName) {
t.Fatalf("unexpected written data: %s", write.calls[0].writtenData)
}
}
func TestGenerateAndWriteTimer(t *testing.T) {
log := &NullLogger{}
write := &writeMock{}
s := &Systemder{
log: log,
writeFile: write.writeFile,
}
desc := "Test timer"
onCalendar := "* * * * *"
name := "testtimer"
args := []string{"arg1", "arg2"}
err := s.GenerateAndWriteTimer(desc, onCalendar, name, args)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if write.callsCount != 2 {
t.Fatalf("unexpected calls count: %d", write.callsCount)
}
// oneshot
if write.calls[0].writtenPath != "/etc/systemd/system/"+name+".service" {
t.Fatalf("unexpected written path: %s", write.calls[0].writtenPath)
}
if !strings.Contains(string(write.calls[0].writtenData), desc) {
t.Fatalf("unexpected written data: %s", write.calls[0].writtenData)
}
binaryName := "systemder.test arg1 arg2" // go test makes a binary with this name
if !strings.Contains(string(write.calls[0].writtenData), binaryName) {
t.Fatalf("unexpected written data: %s", write.calls[0].writtenData)
}
// timer
if write.calls[1].writtenPath != "/etc/systemd/system/"+name+".timer" {
t.Fatalf("unexpected written path: %s", write.calls[1].writtenPath)
}
if !strings.Contains(string(write.calls[1].writtenData), desc) {
t.Fatalf("unexpected written data: %s", write.calls[1].writtenData)
}
if !strings.Contains(string(write.calls[1].writtenData), name+".service") {
t.Fatalf("unexpected written data: %s", write.calls[1].writtenData)
}
if !strings.Contains(string(write.calls[1].writtenData), onCalendar) {
t.Fatalf("unexpected written data: %s", write.calls[1].writtenData)
}
}