-
Notifications
You must be signed in to change notification settings - Fork 4
/
patrol_test.go
156 lines (122 loc) · 4.42 KB
/
patrol_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
package patrol
import (
"log"
"sabey.co/unittest"
"testing"
)
func TestPatrolServices(t *testing.T) {
log.Println("TestPatrolServices")
unittest.Equals(t, APP_NAME_MAXLENGTH, 255)
config := &Config{}
unittest.Equals(t, config.Validate(), ERR_PATROL_EMPTY)
// Apps must be initialized, the creation of the config object will not do this for you
config.Apps = make(map[string]*ConfigApp)
unittest.Equals(t, config.Validate(), ERR_PATROL_EMPTY)
// add a service
config.Services = make(map[string]*ConfigService)
// empty Service
config.Services[""] = &ConfigService{}
// check that key exists
_, exists := config.Services[""]
unittest.Equals(t, exists, true)
unittest.Equals(t, config.Validate(), ERR_SERVICES_KEY_EMPTY)
// delete empty key
delete(config.Services, "")
_, exists = config.Services[""]
unittest.Equals(t, exists, false)
// check for invalid key
config.Services["1234567890123456790123456789012345678901234567890123456789012345"] = &ConfigService{
Management: SERVICE_MANAGEMENT_INITD,
}
unittest.Equals(t, config.Validate(), ERR_SERVICES_KEY_INVALID)
// delete invalid key
delete(config.Services, "1234567890123456790123456789012345678901234567890123456789012345")
_, exists = config.Services["1234567890123456790123456789012345678901234567890123456789012345"]
unittest.Equals(t, exists, false)
service := &ConfigService{
// empty object
}
config.Services["ssh"] = service
// we're no longer going to get a config error, so we're good!
unittest.Equals(t, config.Validate(), ERR_SERVICE_MANAGEMENT_INVALID)
// create a valid service
service.Service = "SSH"
service.Name = "SSH Service"
service.Management = SERVICE_MANAGEMENT_SERVICE
// valid config!
unittest.IsNil(t, config.Validate())
// create patrol
patrol, err := CreatePatrol(config)
unittest.IsNil(t, err)
unittest.NotNil(t, patrol)
// add duplicate case insensitive key
// we can reuse our http object
config.Services["SSH"] = service
unittest.Equals(t, len(config.Services), 2)
// Validate
unittest.Equals(t, config.Validate(), ERR_SERVICE_LABEL_DUPLICATE)
// delete service so we can Validate App
config.Services = make(map[string]*ConfigService)
unittest.Equals(t, config.Validate(), ERR_PATROL_EMPTY)
}
func TestPatrolApps(t *testing.T) {
log.Println("TestPatrolApps")
config := &Config{}
unittest.Equals(t, config.Validate(), ERR_PATROL_EMPTY)
// Apps must be initialized, the creation of the config object will not do this for you
config.Apps = make(map[string]*ConfigApp)
unittest.Equals(t, config.Validate(), ERR_PATROL_EMPTY)
// add a service
config.Services = make(map[string]*ConfigService)
// empty App
config.Apps[""] = &ConfigApp{
KeepAlive: APP_KEEPALIVE_PID_PATROL,
}
// check that key exists
_, exists := config.Apps[""]
unittest.Equals(t, exists, true)
unittest.Equals(t, config.Validate(), ERR_APPS_KEY_EMPTY)
// delete empty key
delete(config.Apps, "")
_, exists = config.Apps[""]
unittest.Equals(t, exists, false)
// check for invalid key
config.Apps["1234567890123456790123456789012345678901234567890123456789012345"] = &ConfigApp{
KeepAlive: APP_KEEPALIVE_PID_PATROL,
}
unittest.Equals(t, config.Validate(), ERR_APPS_KEY_INVALID)
// delete invalid key
delete(config.Apps, "1234567890123456790123456789012345678901234567890123456789012345")
_, exists = config.Apps["1234567890123456790123456789012345678901234567890123456789012345"]
unittest.Equals(t, exists, false)
// valid object
app := &ConfigApp{
// empty object
}
config.Apps["http"] = app
// no keep alive
unittest.Equals(t, config.Validate(), ERR_APP_KEEPALIVE_INVALID)
app.KeepAlive = APP_KEEPALIVE_PID_PATROL
unittest.Equals(t, config.Validate(), ERR_APP_NAME_EMPTY)
app.Name = "name"
unittest.Equals(t, config.Validate(), ERR_APP_WORKINGDIRECTORY_EMPTY)
app.WorkingDirectory = "/directory"
unittest.Equals(t, config.Validate(), ERR_APP_BINARY_EMPTY)
app.Binary = "file"
unittest.Equals(t, config.Validate(), ERR_APP_LOGDIRECTORY_EMPTY)
app.LogDirectory = "log-directory"
unittest.Equals(t, config.Validate(), ERR_APP_PIDPATH_EMPTY)
app.PIDPath = "pid"
// valid config!
unittest.IsNil(t, config.Validate())
// create patrol
patrol, err := CreatePatrol(config)
unittest.IsNil(t, err)
unittest.NotNil(t, patrol)
// add duplicate case insensitive key
// we can reuse our http object
config.Apps["HTTP"] = app
unittest.Equals(t, len(config.Apps), 2)
// Validate
unittest.Equals(t, config.Validate(), ERR_APP_LABEL_DUPLICATE)
}