Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 448073d

Browse files
YujiOshimawfarner
authored andcommitted
Add test for save/load state snapshot in swarm (#323)
Signed-off-by: Yuji Oshima <[email protected]>
1 parent 13bd231 commit 448073d

File tree

1 file changed

+59
-14
lines changed

1 file changed

+59
-14
lines changed

pkg/store/swarm/swarm_test.go

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,72 @@
11
package swarm
22

33
import (
4-
"testing"
5-
4+
docker_types "github.com/docker/docker/api/types"
5+
"github.com/docker/docker/api/types/swarm"
6+
mock_client "github.com/docker/infrakit/pkg/mock/docker/docker/client"
7+
"github.com/golang/mock/gomock"
68
"github.com/stretchr/testify/require"
9+
"testing"
710
)
811

9-
func TestEncodeDecode(t *testing.T) {
12+
var input = map[string]interface{}{
13+
"Group": map[string]interface{}{
14+
"managers": map[string]interface{}{
15+
"Instance": "foo",
16+
"Flavor": "bar",
17+
"Allocation": []interface{}{"a", "b", "c"},
18+
},
19+
"workers": map[string]interface{}{
20+
"Instance": "bar",
21+
"Flavor": "baz",
22+
},
23+
},
24+
}
25+
26+
const nodeID = "my-node-id"
27+
const encodedInput = "eJx0jkEKwlAMRPc9xTBrT/B3bhTPIC7STxXx+yNpVVB6dymSGhE3gTeTmeTZAADXptcLE94I8CxVDp31QQO4LEWzDEetTNjOOkDhImL7jZkz7T4GV0VuakxgKxYS3NR+kJq7ydqrenr0Fd7VTj/fxbrHv7rpktc1PsdXAAAA//8CYjjK"
28+
29+
var infoResponse = docker_types.Info{InfoBase: &docker_types.InfoBase{Swarm: swarm.Info{NodeID: nodeID}}}
1030

11-
input := map[string]interface{}{
12-
"Group": map[string]interface{}{
13-
"managers": map[string]interface{}{
14-
"Instance": "foo",
15-
"Flavor": "bar",
16-
"Allocation": []interface{}{"a", "b", "c"},
17-
},
18-
"workers": map[string]interface{}{
19-
"Instance": "bar",
20-
"Flavor": "baz",
21-
},
31+
func TestSaveLoadSnapshot(t *testing.T) {
32+
ctrl := gomock.NewController(t)
33+
defer ctrl.Finish()
34+
client := mock_client.NewMockAPIClient(ctrl)
35+
snapshot, err := NewSnapshot(client)
36+
37+
swarmInfo := swarm.Swarm{}
38+
expectedSpec := swarm.Spec{
39+
Annotations: swarm.Annotations{
40+
Labels: map[string]string{SwarmLabel: encodedInput},
2241
},
2342
}
43+
//Test first time save Snapshot (nil -> expectedSpec)
2444

45+
client.EXPECT().SwarmUpdate(gomock.Any(), swarm.Version{Index: uint64(0)}, expectedSpec, swarm.UpdateFlags{RotateWorkerToken: false, RotateManagerToken: false, RotateManagerUnlockKey: false}).Return(nil)
46+
client.EXPECT().SwarmInspect(gomock.Any()).Return(swarmInfo, nil)
47+
err = snapshot.Save(input)
48+
require.NoError(t, err)
49+
50+
//Test update Snapshot(unexpectedSpec -> expectedSpec)
51+
unexpectedSpec := swarm.Spec{
52+
Annotations: swarm.Annotations{
53+
Labels: map[string]string{SwarmLabel: "dummy"},
54+
},
55+
}
56+
swarmInfo.Spec = unexpectedSpec
57+
client.EXPECT().SwarmUpdate(gomock.Any(), swarm.Version{Index: uint64(0)}, expectedSpec, swarm.UpdateFlags{RotateWorkerToken: false, RotateManagerToken: false, RotateManagerUnlockKey: false}).Return(nil)
58+
client.EXPECT().SwarmInspect(gomock.Any()).Return(swarmInfo, nil)
59+
err = snapshot.Save(input)
60+
require.NoError(t, err)
61+
//Test Load Snapshot
62+
client.EXPECT().SwarmInspect(gomock.Any()).Return(swarmInfo, nil)
63+
output := map[string]interface{}{}
64+
err = snapshot.Load(&output)
65+
require.NoError(t, err)
66+
require.Equal(t, input, output)
67+
}
68+
69+
func TestEncodeDecode(t *testing.T) {
2570
encoded, err := encode(input)
2671
require.NoError(t, err)
2772
t.Log("encoded=", encoded)

0 commit comments

Comments
 (0)