|
1 | 1 | package swarm
|
2 | 2 |
|
3 | 3 | 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" |
6 | 8 | "github.com/stretchr/testify/require"
|
| 9 | + "testing" |
7 | 10 | )
|
8 | 11 |
|
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}}} |
10 | 30 |
|
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}, |
22 | 41 | },
|
23 | 42 | }
|
| 43 | + //Test first time save Snapshot (nil -> expectedSpec) |
24 | 44 |
|
| 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) { |
25 | 70 | encoded, err := encode(input)
|
26 | 71 | require.NoError(t, err)
|
27 | 72 | t.Log("encoded=", encoded)
|
|
0 commit comments