-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_test.go
64 lines (55 loc) · 1.57 KB
/
cluster_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
package main
import (
"testing"
)
func Test_newCluster(t *testing.T) {
n := newNode("testuuid", "http://localhost:1000", true)
c := newCluster(n, "clustersecret", 60)
if c.Myself != n {
t.Error("cluster wasn't created properly")
}
if !c.CheckSecret("clustersecret") {
t.Error("doesn't accept correct secret")
}
if c.CheckSecret("wrongsecret") {
t.Error("accept's a wrong secret")
}
}
func Test_jsonSerialize(t *testing.T) {
n := newNode("testuuid", "http://localhost:1000", true)
c := newCluster(n, "clustersecret", 60)
d := "{\"uuid\":\"testuuid\",\"base_url\":\"http://localhost:1000\",\"writeable\":true,\"secret\":\"clustersecret\",\"neighbors\":null}"
if string(c.jsonSerialize()) != d {
t.Error("incorrect json")
}
// NodeMeta outputs the same
if string(c.NodeMeta(10)) != d {
t.Error("bad output from NodeMeta")
}
// LocalState outputs the same
if string(c.LocalState(true)) != d {
t.Error("bad output from NodeMeta")
}
}
func Test_addAndFind(t *testing.T) {
n := newNode("testuuid", "http://localhost:1000", true)
c := newCluster(n, "clustersecret", 60)
// add a neighbor
n2 := newNode("testuuid2", "http://localhost:1001", true)
c.AddNeighbor(*n2)
// we should be able to retrieve them
n3, ok := c.FindNeighborByUUID("testuuid2")
if !ok {
t.Error("failed to find neighbor")
}
if n2.UUID != n3.UUID {
t.Error("UUID mismatch", n2.UUID, n3.UUID)
}
// remove the neighbor
c.RemoveNeighbor(*n2)
// we should not be able to retrieve them anymore
n3, ok = c.FindNeighborByUUID("testuuid2")
if ok {
t.Error("neighbor was not removed")
}
}