diff --git a/pkg/store/swarm/swarm_test.go b/pkg/store/swarm/swarm_test.go index a8af40067..a7c52c848 100644 --- a/pkg/store/swarm/swarm_test.go +++ b/pkg/store/swarm/swarm_test.go @@ -1,27 +1,72 @@ package swarm import ( - "testing" - + docker_types "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/swarm" + mock_client "github.com/docker/infrakit/pkg/mock/docker/docker/client" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" + "testing" ) -func TestEncodeDecode(t *testing.T) { +var input = map[string]interface{}{ + "Group": map[string]interface{}{ + "managers": map[string]interface{}{ + "Instance": "foo", + "Flavor": "bar", + "Allocation": []interface{}{"a", "b", "c"}, + }, + "workers": map[string]interface{}{ + "Instance": "bar", + "Flavor": "baz", + }, + }, +} + +const nodeID = "my-node-id" +const encodedInput = "eJx0jkEKwlAMRPc9xTBrT/B3bhTPIC7STxXx+yNpVVB6dymSGhE3gTeTmeTZAADXptcLE94I8CxVDp31QQO4LEWzDEetTNjOOkDhImL7jZkz7T4GV0VuakxgKxYS3NR+kJq7ydqrenr0Fd7VTj/fxbrHv7rpktc1PsdXAAAA//8CYjjK" + +var infoResponse = docker_types.Info{InfoBase: &docker_types.InfoBase{Swarm: swarm.Info{NodeID: nodeID}}} - input := map[string]interface{}{ - "Group": map[string]interface{}{ - "managers": map[string]interface{}{ - "Instance": "foo", - "Flavor": "bar", - "Allocation": []interface{}{"a", "b", "c"}, - }, - "workers": map[string]interface{}{ - "Instance": "bar", - "Flavor": "baz", - }, +func TestSaveLoadSnapshot(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + client := mock_client.NewMockAPIClient(ctrl) + snapshot, err := NewSnapshot(client) + + swarmInfo := swarm.Swarm{} + expectedSpec := swarm.Spec{ + Annotations: swarm.Annotations{ + Labels: map[string]string{SwarmLabel: encodedInput}, }, } + //Test first time save Snapshot (nil -> expectedSpec) + client.EXPECT().SwarmUpdate(gomock.Any(), swarm.Version{Index: uint64(0)}, expectedSpec, swarm.UpdateFlags{RotateWorkerToken: false, RotateManagerToken: false, RotateManagerUnlockKey: false}).Return(nil) + client.EXPECT().SwarmInspect(gomock.Any()).Return(swarmInfo, nil) + err = snapshot.Save(input) + require.NoError(t, err) + + //Test update Snapshot(unexpectedSpec -> expectedSpec) + unexpectedSpec := swarm.Spec{ + Annotations: swarm.Annotations{ + Labels: map[string]string{SwarmLabel: "dummy"}, + }, + } + swarmInfo.Spec = unexpectedSpec + client.EXPECT().SwarmUpdate(gomock.Any(), swarm.Version{Index: uint64(0)}, expectedSpec, swarm.UpdateFlags{RotateWorkerToken: false, RotateManagerToken: false, RotateManagerUnlockKey: false}).Return(nil) + client.EXPECT().SwarmInspect(gomock.Any()).Return(swarmInfo, nil) + err = snapshot.Save(input) + require.NoError(t, err) + //Test Load Snapshot + client.EXPECT().SwarmInspect(gomock.Any()).Return(swarmInfo, nil) + output := map[string]interface{}{} + err = snapshot.Load(&output) + require.NoError(t, err) + require.Equal(t, input, output) +} + +func TestEncodeDecode(t *testing.T) { encoded, err := encode(input) require.NoError(t, err) t.Log("encoded=", encoded)