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

Commit

Permalink
Add test for save/load state snapshot in swarm (#323)
Browse files Browse the repository at this point in the history
Signed-off-by: Yuji Oshima <yuji.oshima0x3fd@gmail.com>
  • Loading branch information
YujiOshima authored and wfarner committed Dec 6, 2016
1 parent 13bd231 commit 448073d
Showing 1 changed file with 59 additions and 14 deletions.
73 changes: 59 additions & 14 deletions pkg/store/swarm/swarm_test.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 448073d

Please sign in to comment.