-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbs_test.go
127 lines (114 loc) · 3.37 KB
/
dbs_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
package dbfailover
import (
"database/sql"
"testing"
"time"
)
func TestNewEmpty(t *testing.T) {
_, err := New(nil)
if err != ErrNoDatabases {
t.Fatalf("creating DBs with empty slice does not return ErrNoDatabases")
}
}
func TestFailover(t *testing.T) {
pool := getDockerPool(t)
network := getDockerNetwork(t, pool)
defer pool.RemoveNetwork(network)
adb, masterResource := startMasterInstance(t, pool, network)
defer pool.Purge(masterResource)
bdb, slaveResource := startSlaveInstance(t, pool, network, adb)
defer pool.Purge(slaveResource)
cdb := startOfflineInstance(t)
names := map[*sql.DB]string{
adb: "A",
bdb: "B",
cdb: "C",
}
p, err := New([]*sql.DB{adb, bdb, cdb})
if err != nil {
t.Fatalf("creating DBs failed with: %v", err)
}
defer p.Stop()
t.Run("initial setup", func(t *testing.T) {
m := p.Master()
if m != adb {
t.Fatalf("master database does not match, got %v, expected %v", names[m], names[adb])
}
s := p.Slave()
if s != bdb {
t.Fatalf("slave database does not match, got %v, expected %v", names[s], names[bdb])
}
})
// perform failover
// step 1 make B db writable
_, err = bdb.Exec("SET GLOBAL read_only = 0")
if err != nil {
t.Fatalf("making B db writable: %v", err)
}
time.Sleep(defaultCheckInterval + 100*time.Millisecond)
t.Run("step 1 B is writable", func(t *testing.T) {
m := p.Master()
if m != adb {
t.Fatalf("master database does not match, got %v, expected %v", names[m], names[adb])
}
s := p.Slave()
if s != bdb {
t.Fatalf("slave database does not match, got %v, expected %v", names[s], names[bdb])
}
})
// step 2 make A db read-only
_, err = adb.Exec("SET GLOBAL read_only = 1")
if err != nil {
t.Fatalf("making A db read-only: %v", err)
}
time.Sleep(defaultCheckInterval + 100*time.Millisecond)
t.Run("step 2 A is read-only", func(t *testing.T) {
// master is considered offline now, we are getting old master as fallback
m := p.Master()
if m != adb {
t.Fatalf("master database does not match, got %v, expected %v", names[m], names[adb])
}
s := p.Slave()
if s != bdb {
t.Fatalf("slave database does not match, got %v, expected %v", names[s], names[bdb])
}
})
// step 3 configure A db as slave
err = makeSlaveOf(adb, bdb)
if err != nil {
t.Fatalf("making B db slave of A db: %v", err)
}
time.Sleep(defaultCheckInterval + 100*time.Millisecond)
t.Run("step 3 A is slave of B", func(t *testing.T) {
// master is considered slave now, we are getting old master as fallback
m := p.Master()
if m != adb {
t.Fatalf("master database does not match, got %v, expected %v", names[m], names[adb])
}
// both servers are in slave role now
s := p.Slave()
if s != bdb && s != adb {
t.Fatalf("slave database does not match, got %v, expected %v or %v", names[s], names[bdb], names[adb])
}
})
// step 4 reset slave on B db
_, err = bdb.Exec("STOP SLAVE")
if err != nil {
t.Fatalf("stopping slave on B db: %v", err)
}
_, err = bdb.Exec("RESET SLAVE")
if err != nil {
t.Fatalf("resetting slave on B db: %v", err)
}
time.Sleep(defaultCheckInterval + 100*time.Millisecond)
t.Run("step 4 B is master", func(t *testing.T) {
m := p.Master()
if m != bdb {
t.Fatalf("master database does not match, got %v, expected %v", names[m], names[bdb])
}
s := p.Slave()
if s != adb {
t.Fatalf("slave database does not match, got %v, expected %v", names[s], names[adb])
}
})
}