-
Notifications
You must be signed in to change notification settings - Fork 14
/
server_test.go
123 lines (102 loc) · 2.74 KB
/
server_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
package bamboo_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
bamboo "github.com/rcarmstrong/go-bamboo"
)
var serverState = &bamboo.TransitionStateInfo{
ServerInfo: bamboo.ServerInfo{
State: bamboo.PausedState,
ReindexInProgress: false,
},
SetByUser: "test",
}
func TestStateTransitions(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(transitionServerStateStub))
defer ts.Close()
client := bamboo.NewSimpleClient(nil, "", "")
client.SetURL(ts.URL)
var testCases = []struct {
expectedState string
function func() (*bamboo.TransitionStateInfo, *http.Response, error)
}{
{bamboo.PausedState, client.Server.Pause},
{bamboo.RunningState, client.Server.Resume},
{bamboo.PreparingForRestartState, client.Server.PrepareForRestart},
{bamboo.ReadyForRestartState, client.Server.Resume},
}
for _, c := range testCases {
transitionStateInfo, _, err := c.function()
if err != nil {
t.Error(err)
}
if transitionStateInfo.State != c.expectedState {
t.Error(fmt.Sprintf("Server state %s does not equal expected state of %s", transitionStateInfo.State, c.expectedState))
}
}
}
func TestReindex(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(reindexServerStateStub))
defer ts.Close()
client := bamboo.NewSimpleClient(nil, "", "")
client.SetURL(ts.URL)
var testCases = []struct {
expected bool
function func() (*bamboo.ReindexState, *http.Response, error)
}{
{true, client.Server.Reindex},
{true, client.Server.ReindexStatus},
}
for _, c := range testCases {
reindexState, _, err := c.function()
if err != nil {
t.Error(err)
}
if reindexState.ReindexInProgress != c.expected {
t.Error(fmt.Sprintf("Reindex method returned %t when %t was expected", reindexState.ReindexInProgress, c.expected))
}
}
}
func transitionServerStateStub(w http.ResponseWriter, r *http.Request) {
method := strings.Split(strings.Split(r.URL.String(), ".")[0], "/")[5]
switch method {
case "pause":
serverState.State = bamboo.PausedState
case "resume":
if serverState.State == bamboo.PausedState {
serverState.State = bamboo.RunningState
} else {
serverState.State = bamboo.ReadyForRestartState
}
case "prepareForRestart":
serverState.State = bamboo.PreparingForRestartState
default:
serverState.State = "Unknown"
}
bytes, err := json.Marshal(serverState)
if err != nil {
panic(err)
}
w.Write(bytes)
}
func reindexServerStateStub(w http.ResponseWriter, r *http.Request) {
resp := bamboo.ReindexState{
ReindexInProgress: true,
ReindexPending: true,
}
bytes, err := json.Marshal(resp)
if err != nil {
panic(err)
}
switch r.Method {
case "POST":
w.WriteHeader(202)
case "GET":
w.WriteHeader(200)
}
w.Write(bytes)
}