-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent_test.go
151 lines (138 loc) · 2.99 KB
/
agent_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
package gomonit
import (
"io/ioutil"
"net/http"
"os"
"testing"
"time"
)
var monit MonitStub
type MonitStub struct {
replyStatus bool
replydoAction bool
lastAction string
lastService string
}
func (m *MonitStub) Status(w http.ResponseWriter, r *http.Request) {
if m.replyStatus {
file, _ := ioutil.ReadFile("tests/status-test.xml")
_, err := w.Write(file)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
} else {
w.WriteHeader(http.StatusNotFound)
}
}
func (m *MonitStub) DoAction(w http.ResponseWriter, r *http.Request) {
if m.replydoAction {
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
w.WriteHeader(http.StatusBadRequest)
return
}
err := r.ParseForm()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusOK)
m.lastAction = r.FormValue("action")
m.lastService = r.FormValue("service")
} else {
w.WriteHeader(http.StatusNotFound)
}
}
func TestAgent(t *testing.T) {
agent, err := NewMonitAgent("http://127.0.0.1:2812", "admin:monit")
if err != nil {
t.Errorf("%v\n", err)
return
}
// Request Status
err = agent.RequestStatus()
if err != nil {
t.Errorf("%v\n", err)
return
}
// Do an action on service
err = agent.StartService("service1")
if err != nil {
t.Errorf("%v\n", err)
return
}
if monit.lastAction != "start" {
t.Errorf("last action request mismatch %s\n", monit.lastAction)
return
}
if monit.lastService != "service1" {
t.Errorf("last service request mismatch %s\n", monit.lastService)
return
}
// CmdService
err = agent.CmdService("service1", "stop")
if err != nil {
t.Errorf("%v\n", err)
return
}
if monit.lastAction != "stop" {
t.Errorf("last action request mismatch %s\n", monit.lastAction)
return
}
if monit.lastService != "service1" {
t.Errorf("last service request mismatch %s\n", monit.lastService)
return
}
}
func TestAgentErrors(t *testing.T) {
agent, err := NewMonitAgent("http://127.0.0.1:2812", "admin:monit")
if err != nil {
t.Errorf("%v\n", err)
return
}
// unknown command
err = agent.CmdService("service1", "foo")
if err == nil {
t.Errorf("%v\n", err)
return
}
// unknown service
err = agent.CmdService("bar", "monitor")
if err == nil {
t.Errorf("%v\n", err)
return
}
// Do an action on unknown service
err = agent.StartService("unknown")
if err == nil {
t.Errorf("%v\n", err)
return
}
// Request Status error
monit.replyStatus = false
err = agent.RequestStatus()
if err == nil {
t.Errorf("%v\n", err)
return
}
// Do an action on service error
monit.replydoAction = false
err = agent.StartService("service1")
if err == nil {
t.Errorf("%v\n", err)
return
}
}
func serve() {
err := http.ListenAndServe("0.0.0.0:2812", nil)
if err != nil {
os.Exit(1)
}
}
func TestMain(m *testing.M) {
monit = MonitStub{true, true, "", ""}
http.HandleFunc("/_status", monit.Status)
http.HandleFunc("/_doaction", monit.DoAction)
go serve()
time.Sleep(1 * time.Second)
os.Exit(m.Run())
}