Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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