This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
/
flavor_test.go
154 lines (129 loc) · 4.65 KB
/
flavor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package combo // import "github.com/docker/infrakit/pkg/plugin/flavor/combo"
import (
"errors"
"testing"
group_controller "github.com/docker/infrakit/pkg/controller/group"
mock_flavor "github.com/docker/infrakit/pkg/mock/spi/flavor"
"github.com/docker/infrakit/pkg/plugin"
"github.com/docker/infrakit/pkg/spi/flavor"
"github.com/docker/infrakit/pkg/spi/group"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/docker/infrakit/pkg/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)
func logicalID(v string) *instance.LogicalID {
id := instance.LogicalID(v)
return &id
}
var inst = instance.Spec{
Properties: types.AnyString("{}"),
Tags: map[string]string{},
Init: "",
LogicalID: logicalID("id"),
Attachments: []instance.Attachment{{ID: "att1", Type: "nic"}},
}
func pluginLookup(plugins map[string]flavor.Plugin) group_controller.FlavorPluginLookup {
return func(key plugin.Name) (flavor.Plugin, error) {
plugin, has := plugins[key.String()]
if has {
return plugin, nil
}
return nil, errors.New("Plugin doesn't exist")
}
}
func TestMergeBehavior(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
a := mock_flavor.NewMockPlugin(ctrl)
b := mock_flavor.NewMockPlugin(ctrl)
plugins := map[string]flavor.Plugin{"a": a, "b": b}
combo := NewPlugin(pluginLookup(plugins), Options{})
flavorProperties := types.AnyString(`[
{
"Plugin": "a",
"Properties": {"a": "1"}
},
{
"Plugin": "b",
"Properties": {"b": "2"}
}
]`)
allocation := group.AllocationMethod{Size: 1}
index := group.Index{Group: group.ID("group"), Sequence: 0}
a.EXPECT().Prepare(types.AnyString(`{"a": "1"}`), inst, allocation, index).Return(instance.Spec{
Properties: inst.Properties,
Tags: map[string]string{"a": "1", "c": "4"},
Init: "init data a",
LogicalID: inst.LogicalID,
Attachments: []instance.Attachment{{ID: "a", Type: "nic"}},
}, nil)
b.EXPECT().Prepare(types.AnyString(`{"b": "2"}`), inst, allocation, index).Return(instance.Spec{
Properties: inst.Properties,
Tags: map[string]string{"b": "2", "c": "5"},
Init: "init data b",
LogicalID: inst.LogicalID,
Attachments: []instance.Attachment{{ID: "b", Type: "gpu"}},
}, nil)
result, err := combo.Prepare(flavorProperties, inst, group.AllocationMethod{Size: 1}, index)
require.NoError(t, err)
expected := instance.Spec{
Properties: inst.Properties,
Tags: map[string]string{"a": "1", "b": "2", "c": "5"},
Init: "init data a\ninit data b",
LogicalID: inst.LogicalID,
Attachments: []instance.Attachment{{ID: "att1", Type: "nic"}, {ID: "a", Type: "nic"}, {ID: "b", Type: "gpu"}},
}
require.Equal(t, expected, result)
}
func TestMergeNoLogicalID(t *testing.T) {
// Tests regression of a bug where a zero value was returned for the LogicalID despite nil being passed.
inst = instance.Spec{
Properties: types.AnyString("{}"),
Tags: map[string]string{},
Init: "",
Attachments: []instance.Attachment{{ID: "att1", Type: "nic"}},
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
a := mock_flavor.NewMockPlugin(ctrl)
b := mock_flavor.NewMockPlugin(ctrl)
plugins := map[string]flavor.Plugin{"a": a, "b": b}
combo := NewPlugin(pluginLookup(plugins), Options{})
flavorProperties := types.AnyString(`[
{
"Plugin": "a",
"Properties": {"a": "1"}
},
{
"Plugin": "b",
"Properties": {"b": "2"}
}
]`)
allocation := group.AllocationMethod{Size: 1}
index := group.Index{Group: group.ID("group"), Sequence: 0}
a.EXPECT().Prepare(types.AnyString(`{"a": "1"}`), inst, allocation, index).Return(instance.Spec{
Properties: inst.Properties,
Tags: map[string]string{"a": "1", "c": "4"},
Init: "init data a",
LogicalID: inst.LogicalID,
Attachments: []instance.Attachment{{ID: "a", Type: "nic"}},
}, nil)
b.EXPECT().Prepare(types.AnyString(`{"b": "2"}`), inst, allocation, index).Return(instance.Spec{
Properties: inst.Properties,
Tags: map[string]string{"b": "2", "c": "5"},
Init: "init data b",
LogicalID: inst.LogicalID,
Attachments: []instance.Attachment{{ID: "b", Type: "gpu"}},
}, nil)
result, err := combo.Prepare(flavorProperties, inst, group.AllocationMethod{Size: 1}, index)
require.NoError(t, err)
expected := instance.Spec{
Properties: inst.Properties,
Tags: map[string]string{"a": "1", "b": "2", "c": "5"},
Init: "init data a\ninit data b",
LogicalID: inst.LogicalID,
Attachments: []instance.Attachment{{ID: "att1", Type: "nic"}, {ID: "a", Type: "nic"}, {ID: "b", Type: "gpu"}},
}
require.Equal(t, expected, result)
}