forked from sorintlab/stolon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy_test.go
256 lines (225 loc) · 7.29 KB
/
proxy_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2015 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package integration
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"testing"
"time"
"github.com/sorintlab/stolon/internal/cluster"
"github.com/sorintlab/stolon/internal/common"
"github.com/sorintlab/stolon/internal/store"
"github.com/satori/go.uuid"
)
func TestProxyListening(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
clusterName := uuid.NewV4().String()
tstore, err := NewTestStore(t, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
storeEndpoints := fmt.Sprintf("%s:%s", tstore.listenAddress, tstore.port)
tp, err := NewTestProxy(t, dir, clusterName, pgSUUsername, pgSUPassword, pgReplUsername, pgReplPassword, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := tp.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer tp.Stop()
t.Logf("test proxy start with store down. Should not listen")
// tp should not listen because it cannot talk with store
if err := tp.WaitNotListening(10 * time.Second); err != nil {
t.Fatalf("expecting tp not listening due to failed store communication, but it's listening.")
}
tp.Stop()
if err := tstore.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := tstore.WaitUp(10 * time.Second); err != nil {
t.Fatalf("error waiting on store up: %v", err)
}
defer func() {
if tstore.cmd != nil {
tstore.Stop()
}
}()
storePath := filepath.Join(common.StorePrefix, clusterName)
sm := store.NewKVBackedStore(tstore.store, storePath)
cd := &cluster.ClusterData{
FormatVersion: cluster.CurrentCDFormatVersion,
Cluster: &cluster.Cluster{
UID: "01",
Generation: 1,
Spec: &cluster.ClusterSpec{
InitMode: cluster.ClusterInitModeP(cluster.ClusterInitModeNew),
FailInterval: &cluster.Duration{Duration: 10 * time.Second},
},
Status: cluster.ClusterStatus{
CurrentGeneration: 1,
Phase: cluster.ClusterPhaseNormal,
Master: "01",
},
},
Keepers: cluster.Keepers{
"01": &cluster.Keeper{
UID: "01",
Spec: &cluster.KeeperSpec{},
Status: cluster.KeeperStatus{
Healthy: true,
},
},
},
DBs: cluster.DBs{
"01": &cluster.DB{
UID: "01",
Generation: 1,
ChangeTime: time.Time{},
Spec: &cluster.DBSpec{
KeeperUID: "01",
Role: common.RoleMaster,
Followers: []string{"02"},
},
Status: cluster.DBStatus{
Healthy: false,
CurrentGeneration: 1,
},
},
},
Proxy: &cluster.Proxy{
Spec: cluster.ProxySpec{
MasterDBUID: "01",
},
},
}
pair, err := sm.AtomicPutClusterData(context.TODO(), cd, nil)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
// test proxy start with the store up
t.Logf("test proxy start with the store up. Should listen")
if err := tp.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
// tp should listen
if err := tp.WaitListening(10 * time.Second); err != nil {
t.Fatalf("expecting tp listening, but it's not listening.")
}
t.Logf("test proxy error communicating with store. Should stop listening")
// Stop store
tstore.Stop()
if err := tstore.WaitDown(10 * time.Second); err != nil {
t.Fatalf("error waiting on store down: %v", err)
}
// tp should not listen because it cannot talk with the store
if err := tp.WaitNotListening(cluster.DefaultProxyTimeoutInterval * 2); err != nil {
t.Fatalf("expecting tp not listening due to failed store communication, but it's listening.")
}
t.Logf("test proxy communication with store restored. Should start listening")
// Start store
if err := tstore.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := tstore.WaitUp(10 * time.Second); err != nil {
t.Fatalf("error waiting on store up: %v", err)
}
// tp should listen
if err := tp.WaitListening(10 * time.Second); err != nil {
t.Fatalf("expecting tp listening, but it's not listening.")
}
t.Logf("test proxy error communicating with store but restored before proxy check timeout. Should continue listening")
// Stop store
tstore.Stop()
if err := tstore.WaitDown(10 * time.Second); err != nil {
t.Fatalf("error waiting on store down: %v", err)
}
// wait less than DefaultProxyTimeoutInterval
time.Sleep(cluster.DefaultProxyTimeoutInterval / 3)
// Start store
if err := tstore.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := tstore.WaitUp(10 * time.Second); err != nil {
t.Fatalf("error waiting on store up: %v", err)
}
// tp should listen
if ok := tp.CheckListening(); !ok {
t.Fatalf("expecting tp listening, but it's not listening.")
}
// wait proxy reading again from the store
time.Sleep(2 * cluster.DefaultProxyCheckInterval)
// tp should listen
if ok := tp.CheckListening(); !ok {
t.Fatalf("expecting tp listening, but it's not listening.")
}
t.Logf("test proxyConf removed. Should continue listening")
// remove proxyConf
cd.Proxy.Spec.MasterDBUID = ""
pair, err = sm.AtomicPutClusterData(context.TODO(), cd, pair)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
// tp should listen
if err := tp.WaitListening(10 * time.Second); err != nil {
t.Fatalf("expecting tp listening, but it's not listening.")
}
t.Logf("test proxyConf restored. Should continue listening")
// Set proxyConf again
cd.Proxy.Spec.MasterDBUID = "01"
pair, err = sm.AtomicPutClusterData(context.TODO(), cd, pair)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
// tp should listen
if err := tp.WaitListening(10 * time.Second); err != nil {
t.Fatalf("expecting tp listening, but it's not listening.")
}
t.Logf("test clusterView removed. Should continue listening")
// remove whole clusterview
_, err = sm.AtomicPutClusterData(context.TODO(), nil, pair)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
// tp should listen
if err := tp.WaitListening(10 * time.Second); err != nil {
t.Fatalf("expecting tp listening, but it's not listening.")
}
// simulate the store in hang by freezing its process
t.Logf("SIGSTOPping the store: %s", tstore.uid)
if err := tstore.Signal(syscall.SIGSTOP); err != nil {
t.Fatalf("unexpected err: %v", err)
}
// tp should not listen because it cannot talk with the store
if err := tp.WaitNotListening(cluster.DefaultProxyTimeoutInterval * 2); err != nil {
t.Fatalf("expecting tp not listening due to failed store communication, but it's listening.")
}
// resume the store
t.Logf("SIGCONTing the store: %s", tstore.uid)
if err := tstore.Signal(syscall.SIGCONT); err != nil {
t.Fatalf("unexpected err: %v", err)
}
// tp should listen
if err := tp.WaitListening(10 * time.Second); err != nil {
t.Fatalf("expecting tp listening, but it's not listening.")
}
}