forked from intercom/dvara
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstate_manager_test.go
167 lines (152 loc) · 4.24 KB
/
state_manager_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
package dvara
import (
"fmt"
"sync"
"testing"
)
func TestManagerFindsMissingExtraMembers(t *testing.T) {
manager := newManager()
manager.addProxies("a", "b")
comparison, _ := manager.getComparison(getStatusResponse("a", "b"), getStatusResponse("a", "c"))
if _, ok := comparison.ExtraMembers["b"]; !ok {
t.Fatal("Extra member b not found")
}
if _, ok := comparison.MissingMembers["c"]; !ok {
t.Fatal("Missing member c not found")
}
if len(comparison.ExtraMembers) != 1 {
t.Fatalf("too many extra members, expecting %d, got %d", 1, len(comparison.ExtraMembers))
}
if len(comparison.MissingMembers) != 1 {
t.Fatalf("too many extra members, expecting %d, got %d", 1, len(comparison.MissingMembers))
}
}
func TestManagerAddsAndRemovesProxies(t *testing.T) {
manager := newManager()
manager.addProxies("mongoA", "mongoB")
comparison, _ := manager.getComparison(getStatusResponse("mongoA", "mongoB"), getStatusResponse("mongoA", "mongoC"))
manager.addRemoveProxies(comparison)
if _, ok := manager.proxies[manager.realToProxy["mongoA"]]; !ok {
t.Fatal("proxyA was removed")
}
if _, ok := manager.proxies[manager.realToProxy["mongoB"]]; ok {
t.Fatal("proxyB was not removed")
}
if _, ok := manager.proxies[manager.realToProxy["mongoC"]]; !ok {
t.Fatal("proxyC was not added")
}
}
func TestManagerWithMissingMembersInCurrentState(t *testing.T) {
manager := newManager()
manager.addProxies("mongoA")
comparison, _ := manager.getComparison(getStatusResponse("mongoA", "mongoB"), getStatusResponse("mongoA", "mongoC"))
if _, ok := comparison.ExtraMembers["mongoB"]; ok {
t.Fatal("mongoB is not currently running")
}
}
func TestReplicaSetMembers(t *testing.T) {
t.Parallel()
h := NewReplicaSetHarness(3, t)
defer h.Stop()
proxyMembers := h.Manager.ProxyMembers()
session := h.ProxySession()
defer session.Close()
status, err := replSetGetStatus(session)
if err != nil {
t.Fatal(err)
}
outerProxyResponseCheckLoop:
for _, m := range status.Members {
for _, p := range proxyMembers {
if m.Name == p {
continue outerProxyResponseCheckLoop
}
}
t.Fatalf("Unexpected member: %s", m.Name)
}
}
func TestProxyNotInReplicaSet(t *testing.T) {
t.Parallel()
h := NewReplicaSetHarness(3, t)
defer h.Stop()
addr := "127.0.0.1:666"
expected := fmt.Sprintf("mongo %s is not in ReplicaSet", addr)
_, err := h.Manager.Proxy(addr)
if err == nil || err.Error() != expected {
t.Fatalf("did not get expected error, got: %s", err)
}
}
func TestAddSameProxyToReplicaSet(t *testing.T) {
t.Parallel()
m := newManager()
p := &Proxy{
ProxyAddr: "1",
MongoAddr: "2",
}
if _, err := m.addProxy(p); err != nil {
t.Fatal(err)
}
expected := fmt.Sprintf("proxy %s already used in ReplicaSet", p.ProxyAddr)
_, err := m.addProxy(p)
if err == nil || err.Error() != expected {
t.Fatalf("did not get expected error, got: %s", err)
}
}
func TestAddSameMongoToReplicaSet(t *testing.T) {
t.Parallel()
m := newManager()
p := &Proxy{
ProxyAddr: "1",
MongoAddr: "2",
}
if _, err := m.addProxy(p); err != nil {
t.Fatal(err)
}
p = &Proxy{
ProxyAddr: "3",
MongoAddr: p.MongoAddr,
}
expected := fmt.Sprintf("mongo %s already exists in ReplicaSet", p.MongoAddr)
_, err := m.addProxy(p)
if err == nil || err.Error() != expected {
t.Fatalf("did not get expected error, got: %s", err)
}
}
func TestAddRemoveProxy(t *testing.T) {
t.Parallel()
m := newManager()
p := &Proxy{
ProxyAddr: "1",
MongoAddr: "2",
}
if _, err := m.addProxy(p); err != nil {
t.Fatal(err)
}
m.removeProxy(p)
if _, ok := m.proxies[p.ProxyAddr]; ok {
t.Fatal("failed to remove proxy")
}
}
func newManager() *StateManager {
replicaSet := setupReplicaSet()
return newManagerWithReplicaSet(replicaSet)
}
func newManagerWithReplicaSet(replicaSet *ReplicaSet) *StateManager {
return &StateManager{
RWMutex: &sync.RWMutex{},
replicaSet: replicaSet,
baseAddrs: replicaSet.Addrs,
proxyToReal: make(map[string]string),
realToProxy: make(map[string]string),
proxies: make(map[string]*Proxy),
}
}
func getStatusResponse(memberNames ...string) *replSetGetStatusResponse {
members := []statusMember{}
for _, name := range memberNames {
members = append(members, statusMember{Name: name, State: "state"})
}
return &replSetGetStatusResponse{
Members: members,
}
}