This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
api_test.go
193 lines (158 loc) · 4.28 KB
/
api_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) 2012 VMware, Inc.
package gonit_test
import (
"bytes"
"github.com/bmizerany/assert"
. "github.com/cloudfoundry/gonit"
"github.com/cloudfoundry/gonit/test/helper"
"io/ioutil"
"math"
"net/rpc"
"os"
"strings"
"testing"
)
var rpcName = "TestAPI"
var config = &ConfigManager{}
func init() {
rpc.RegisterName(rpcName, NewAPI(config))
}
func isActionError(err error) bool {
return strings.HasPrefix(err.Error(), "ActionError")
}
func TestActionsNoConfig(t *testing.T) {
err := helper.WithRpcServer(func(c *rpc.Client) {
reply := &ActionResult{}
err := c.Call(rpcName+".StartAll", "", reply)
assert.Equal(t, nil, err)
// Total == 0 since no processes are configured
assert.Equal(t, 0, reply.Total)
assert.Equal(t, 0, reply.Errors)
reply = &ActionResult{}
err = c.Call(rpcName+".StartProcess", "foo", reply)
assert.NotEqual(t, nil, err)
assert.Equal(t, true, isActionError(err))
reply = &ActionResult{}
err = c.Call(rpcName+".StartGroup", "bar", reply)
assert.NotEqual(t, nil, err)
assert.Equal(t, true, isActionError(err))
})
assert.Equal(t, nil, err)
}
func TestAbout(t *testing.T) {
err := helper.WithRpcServer(func(c *rpc.Client) {
params := struct{}{}
about := About{}
err := c.Call(rpcName+".About", ¶ms, &about)
assert.Equal(t, nil, err)
assert.Equal(t, VERSION, about.Version)
// need not be same type, just requires same fields
var gabout struct {
Version string
}
err = c.Call(rpcName+".About", ¶ms, &gabout)
assert.Equal(t, nil, err)
assert.Equal(t, VERSION, gabout.Version)
})
assert.Equal(t, nil, err)
}
func tmpPidFile(t *testing.T, pid int) string {
file, err := ioutil.TempFile("", "api_test")
if err != nil {
t.Error(err)
}
if err := WritePidFile(pid, file.Name()); err != nil {
t.Fatal(err)
}
return file.Name()
}
// simple exercise of CliFormatters
func testCliPrint(t *testing.T, value CliFormatter) {
buf := new(bytes.Buffer)
value.Print(buf)
assert.NotEqual(t, 0, buf.Len())
}
func TestStatus(t *testing.T) {
nprocesses := 0
type testProcess struct {
name string
pid int
ppid int
running bool
}
// use pid/ppid of the go test process to test
// Running, sigar.ProcState, etc.
pid := os.Getpid()
ppid := os.Getppid()
groups := []struct {
name string
processes []testProcess
}{
{
name: "a_group",
processes: []testProcess{
{"a_process", pid, ppid, true},
{"x_process", math.MaxInt32, -1, false},
},
},
{
name: "b_group",
processes: []testProcess{
{"b_process", pid, ppid, true},
},
},
}
for _, group := range groups {
for _, process := range group.processes {
// write pidfile for use by process.IsRunning()
pidfile := tmpPidFile(t, process.pid)
defer os.Remove(pidfile)
config.AddProcess(group.name, &Process{
Name: process.name,
Pidfile: pidfile,
})
nprocesses++
}
}
err := helper.WithRpcServer(func(c *rpc.Client) {
statusGroup := rpcName + ".StatusGroup"
statusProcess := rpcName + ".StatusProcess"
// should get an error if group does not exist
err := c.Call(statusGroup, "enogroup", &ProcessGroupStatus{})
assert.NotEqual(t, nil, err)
// should get an error if process does not exist
err = c.Call(statusProcess, "enoprocess", &ProcessStatus{})
assert.NotEqual(t, nil, err)
for _, group := range groups {
gstat := &ProcessGroupStatus{}
err := c.Call(statusGroup, group.name, gstat)
assert.Equal(t, nil, err)
assert.Equal(t, len(group.processes), len(gstat.Group))
testCliPrint(t, gstat)
for _, process := range group.processes {
stat := &ProcessStatus{}
err := c.Call(statusProcess, process.name, stat)
assert.Equal(t, nil, err)
running := stat.Summary.Running
assert.Equal(t, process.running, running)
testCliPrint(t, stat)
if !running {
continue
}
assert.Equal(t, process.pid, stat.Pid)
assert.Equal(t, process.ppid, stat.State.Ppid)
}
}
all := &ProcessGroupStatus{}
err = c.Call(rpcName+".StatusAll", "", all)
assert.Equal(t, nil, err)
assert.Equal(t, nprocesses, len(all.Group))
testCliPrint(t, all)
summary := &Summary{}
err = c.Call(rpcName+".Summary", "", summary)
assert.Equal(t, nil, err)
assert.Equal(t, nprocesses, len(summary.Processes))
testCliPrint(t, summary)
})
assert.Equal(t, nil, err)
}