This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
/
cli_test.go
92 lines (72 loc) · 2 KB
/
cli_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
// Copyright (c) 2012 VMware, Inc.
package gonit_test
import (
"fmt"
"github.com/bmizerany/assert"
. "github.com/cloudfoundry/gonit"
"github.com/cloudfoundry/gonit/test/helper"
"net/rpc"
"testing"
)
var mockAPI = &MockAPI{api: &API{}}
func init() {
rpc.Register(mockAPI)
}
type MockAPI struct {
api *API
}
func (*MockAPI) StopProcess(name string, r *ActionResult) error {
if name == "foo" {
return nil
}
return fmt.Errorf("Unknown service %s", name)
}
func (*MockAPI) StartGroup(name string, r *ActionResult) error {
if name == "vcap" {
return nil
}
return fmt.Errorf("Unknown service group %s", name)
}
func (*MockAPI) UnmonitorAll(unused interface{}, r *ActionResult) error {
return nil
}
func (m *MockAPI) About(args interface{}, about *About) error {
return m.api.About(args, about)
}
// run tests via RPC or local reflection
func runTests(t *testing.T, client CliClient) {
method, name := RpcArgs("stop", "foo", false)
reply, err := client.Call(method, name)
assert.Equal(t, nil, err)
method, name = RpcArgs("stop", "bar", false)
reply, err = client.Call(method, name)
assert.NotEqual(t, nil, err)
method, name = RpcArgs("unmonitor", "all", false)
reply, err = client.Call(method, name)
assert.Equal(t, nil, err)
method, name = RpcArgs("start", "vcap", true)
reply, err = client.Call(method, name)
assert.Equal(t, nil, err)
method, name = RpcArgs("start", "bar", true)
reply, err = client.Call(method, name)
assert.NotEqual(t, nil, err)
method, name = RpcArgs("about", "", false)
reply, err = client.Call(method, name)
assert.Equal(t, nil, err)
about, ok := reply.(*About)
assert.Equal(t, true, ok)
assert.Equal(t, VERSION, about.Version)
reply, err = client.Call("ENOENT", "")
assert.NotEqual(t, nil, err)
}
func TestRemote(t *testing.T) {
err := helper.WithRpcServer(func(c *rpc.Client) {
client := NewRemoteClient(c, mockAPI)
runTests(t, client)
})
assert.Equal(t, nil, err)
}
func TestLocal(t *testing.T) {
client := NewLocalClient(mockAPI)
runTests(t, client)
}