-
Notifications
You must be signed in to change notification settings - Fork 1
/
drain_test.go
126 lines (116 loc) · 2.78 KB
/
drain_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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestDrain(t *testing.T) {
const chunkSize = 3
content := "foo"
source := createTempfile(t, content)
defer os.Remove(source)
tr, err := NewTracker(testConfig)
if err != nil {
t.Fatal(err)
}
cleanDB(t, tr.db)
go tr.Run()
defer tr.Shutdown()
_, err = tr.db.Exec("insert into zone(zoneid, name) values(1, 'zone1')")
if err != nil {
t.Fatal(err)
}
_, err = tr.db.Exec("insert into rack(rackid, zoneid, name) values(1, 1, 'rack1')")
if err != nil {
t.Fatal(err)
}
_, err = tr.db.Exec("insert into host(hostid, hostname, status, hostip, rackid) values(1, '127.0.0.1', 'alive', '127.0.0.1', 1)")
if err != nil {
t.Fatal(err)
}
_, err = tr.db.Exec("insert into device(devid, status, hostid, read_port, write_port) values(2, 'alive', 1, 8500, 8501)")
if err != nil {
t.Fatal(err)
}
tempDir, err := ioutil.TempDir("", "efes-test-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
devPath := filepath.Join(tempDir, "dev2")
err = os.Mkdir(devPath, 0700)
if err != nil {
t.Fatal(err)
}
serverConfig := *testConfig
serverConfig.Server.DataDir = devPath
srv, err := NewServer(&serverConfig)
if err != nil {
t.Fatal(err)
}
go srv.Run()
defer srv.Shutdown()
// Put a file on first server
clientConfig := NewConfig()
clientConfig.Client.ChunkSize = chunkSize
clt, err := NewClient(clientConfig)
if err != nil {
t.Fatal(err)
}
fmt.Println("waiting for tracker to become ready")
<-tr.Ready
fmt.Println("waiting for server to become ready")
<-srv.Ready
fmt.Println("writing file to first server")
err = clt.Write("k", source)
if err != nil {
t.Fatal(err)
}
// setup second server
fmt.Println("setting up second server")
_, err = tr.db.Exec("insert into device(devid, status, hostid, bytes_total, bytes_used, bytes_free, read_port, write_port) values(3, 'alive', 1, 1000000000, 500000000, 500000000, 8502, 8503)")
if err != nil {
t.Fatal(err)
}
devPath = filepath.Join(tempDir, "dev3")
err = os.Mkdir(devPath, 0700)
if err != nil {
t.Fatal(err)
}
config2 := *testConfig
config2.Server.DataDir = devPath
config2.Server.ListenAddressForRead = "0.0.0.0:8502"
config2.Server.ListenAddress = "0.0.0.0:8503"
srv2, err := NewServer(&config2)
if err != nil {
t.Fatal(err)
}
go srv2.Run()
defer srv2.Shutdown()
// Run drain
dr, err := NewDrainer(&serverConfig)
if err != nil {
t.Fatal(err)
}
dr.stopOnError = true
err = dr.Run()
if err != nil {
t.Fatal(err)
}
// Check content
copied := createTempfile(t, "")
defer os.Remove(copied)
err = clt.Read("k", copied)
if err != nil {
t.Fatal(err)
}
copyContent, err := ioutil.ReadFile(copied)
if err != nil {
t.Fatal(err)
}
if string(copyContent) != content {
t.Fatal("invalid content:", copyContent)
}
}