From 0162f9da2c333f07ad1a836d3df3d0e1fdeba159 Mon Sep 17 00:00:00 2001 From: kaiyou Date: Thu, 7 May 2020 12:27:25 +0200 Subject: [PATCH] Add unit tests for state functions Currently unit tests only succeed if the state path is writeable, since it is hardcoded. --- cluster/state_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 cluster/state_test.go diff --git a/cluster/state_test.go b/cluster/state_test.go new file mode 100644 index 0000000..af2bcb4 --- /dev/null +++ b/cluster/state_test.go @@ -0,0 +1,32 @@ +package cluster + +import ( + "net" + "reflect" + "testing" + + "github.com/costela/wesher/common" +) + +func Test_State_Save_Load(t *testing.T) { + key := "abcdefghijklmnopqrstuvwxyzABCDEF" + node := common.Node{ + Name: "node", + Addr: net.ParseIP("10.0.0.2"), + } + + cluster := Cluster{ + state: &State{ + ClusterKey: []byte(key), + Nodes: []common.Node{node}, + }, + } + + cluster.saveState() + loaded := &State{} + loadState(loaded) + + if !reflect.DeepEqual(cluster.state, loaded) { + t.Errorf("cluster state save then reload mistmatch: %s / %s", cluster.state, loaded) + } +}