This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
66 lines (55 loc) · 1.69 KB
/
api_test.go
File metadata and controls
66 lines (55 loc) · 1.69 KB
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
package raftify
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
)
func TestAPI(t *testing.T) {
// Reserve ports for this test
ports := reservePorts(1)
// Initialize dummy node
node := initDummyNode("TestNode", 1, 1, ports[0])
// Create directory for test data
os.MkdirAll(node.workingDir+"/testing/TestNode", 0755)
defer os.RemoveAll(node.workingDir + "/testing")
// Write configuration data to raftify.json file
nodesBytes, _ := json.Marshal(node.config)
ioutil.WriteFile(node.workingDir+"/testing/TestNode/raftify.json", nodesBytes, 0755)
// Test InitNode
node, err := InitNode(node.logger, node.workingDir+"/testing/TestNode")
if err != nil {
t.Logf("Expected node to initialize successfully, instead got error: %v", err.Error())
t.FailNow()
}
// Test GetHealthScore
if node.GetHealthScore() != 0 {
t.Logf("Expected node to reach a health score of 0, instead got %v", node.GetHealthScore())
t.FailNow()
}
// Test GetMembers
members := node.GetMembers()
if _, ok := members["TestNode"]; !ok {
t.Logf("Expected to find member \"%v\", instead not found", node.config.ID)
t.FailNow()
}
if len(members) != 1 {
t.Logf("Expected length of memberlist to be 1, instead got %v", len(members))
t.FailNow()
}
// Test GetID
if node.GetID() != "TestNode" {
t.Logf("Expected id to be \"%v\", instead got %v", node.config.ID, members["id"])
t.FailNow()
}
// Test GetState
if node.GetState() != Leader {
t.Logf("Expected node to be leader, instead got %v", node.state.toString())
t.FailNow()
}
// Test Shutdown
if err := node.Shutdown(); err != nil {
t.Logf("Expected successful shutdown of %v, instead got error: %v", node.config.ID, err.Error())
t.FailNow()
}
}