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
/
softlayer_test.go
235 lines (219 loc) · 6.52 KB
/
softlayer_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package instance // import "github.com/docker/infrakit/pkg/provider/terraform/instance"
import (
"fmt"
"testing"
"github.com/docker/infrakit/pkg/provider/ibmcloud/client"
"github.com/docker/infrakit/pkg/spi/flavor"
"github.com/softlayer/softlayer-go/datatypes"
"github.com/softlayer/softlayer-go/filter"
"github.com/stretchr/testify/require"
)
func TestMergeLabelsIntoTagSliceEmpty(t *testing.T) {
result := mergeLabelsIntoTagSlice([]interface{}{}, map[string]string{})
require.Equal(t, []string{}, result)
}
func TestMergeLabelsIntoTagSliceTagsOnly(t *testing.T) {
result := mergeLabelsIntoTagSlice(
[]interface{}{
"tag1:val1",
"tag2:val2",
},
map[string]string{},
)
require.Len(t, result, 2)
require.Contains(t, result, "tag1:val1")
require.Contains(t, result, "tag2:val2")
}
func TestMergeLabelsIntoTagSliceLabelsOnly(t *testing.T) {
result := mergeLabelsIntoTagSlice(
[]interface{}{},
map[string]string{
"label1": "val1",
"label2": "val2",
},
)
require.Len(t, result, 2)
require.Contains(t, result, "label1:val1")
require.Contains(t, result, "label2:val2")
}
func TestMergeLabelsIntoTagSlice(t *testing.T) {
result := mergeLabelsIntoTagSlice(
[]interface{}{
"tag1:val1",
"tag2:val2",
},
map[string]string{
"label1": "val1",
"label2": "val2",
},
)
require.Len(t, result, 4)
require.Contains(t, result, "tag1:val1")
require.Contains(t, result, "tag2:val2")
require.Contains(t, result, "label1:val1")
require.Contains(t, result, "label2:val2")
}
func TestFilterVMsByTagsEmpty(t *testing.T) {
vms := []datatypes.Virtual_Guest{}
filterVMsByTags(&vms, []string{})
require.Equal(t, []datatypes.Virtual_Guest{}, vms)
}
func TestGetUniqueVMByTagsEmpty(t *testing.T) {
id, err := getUniqueVMByTags([]datatypes.Virtual_Guest{}, []string{})
require.NoError(t, err)
require.Nil(t, id)
}
func TestGetUniqueVMByTagsOneMatch(t *testing.T) {
vmID := 123
vmTagName := "some-tag"
vmTag := datatypes.Tag{Name: &vmTagName}
vms := []datatypes.Virtual_Guest{{Id: &vmID, TagReferences: []datatypes.Tag_Reference{{Tag: &vmTag}}}}
id, err := getUniqueVMByTags(vms, []string{vmTagName})
require.NoError(t, err)
require.Equal(t, vmID, *id)
}
func TestGetUniqueVMByTagsOneMatchNilID(t *testing.T) {
vmTagName := "some-tag"
vmHostname := "some-hostname"
vmTag := datatypes.Tag{Name: &vmTagName}
vms := []datatypes.Virtual_Guest{
{
Hostname: &vmHostname,
TagReferences: []datatypes.Tag_Reference{{Tag: &vmTag}},
},
}
id, err := getUniqueVMByTags(vms, []string{vmTagName})
require.Equal(t, "VM 'some-hostname' missing ID", err.Error())
require.Nil(t, id)
}
func TestGetUniqueVMByTagsTwoMatches(t *testing.T) {
vmID1 := 123
vmID2 := 234
vmTagName := "some-tag"
vmTag := datatypes.Tag{Name: &vmTagName}
vms := []datatypes.Virtual_Guest{
{Id: &vmID1, TagReferences: []datatypes.Tag_Reference{{Tag: &vmTag}}},
{Id: &vmID2, TagReferences: []datatypes.Tag_Reference{{Tag: &vmTag}}},
}
id, err := getUniqueVMByTags(vms, []string{vmTagName})
require.Equal(t,
fmt.Sprintf("Only a single VM should match tags, but VMs %v match tags: %v", []int{vmID1, vmID2}, []string{vmTagName}),
err.Error())
require.Nil(t, id)
}
// getVMs is a utility function to get datatypes.Virtual_Guest with tags
func getVMs() []datatypes.Virtual_Guest {
vmID0 := 0
vmID1 := 1
vmID2 := 2
vmID3 := 3
tag1Name := "tag1"
tag1 := datatypes.Tag{Name: &tag1Name}
tag2Name := "tag2"
tag2 := datatypes.Tag{Name: &tag2Name}
tag3Name := "tag3"
tag3 := datatypes.Tag{Name: &tag3Name}
vms := []datatypes.Virtual_Guest{
{
TagReferences: []datatypes.Tag_Reference{},
Id: &vmID0,
},
{
TagReferences: []datatypes.Tag_Reference{{Tag: &tag1}},
Id: &vmID1,
},
{
TagReferences: []datatypes.Tag_Reference{{Tag: &tag1}, {Tag: &tag2}},
Id: &vmID2,
},
{
TagReferences: []datatypes.Tag_Reference{{Tag: &tag1}, {Tag: &tag2}, {Tag: &tag3}},
Id: &vmID3,
},
}
return vms
}
func TestFilterVMsByTags(t *testing.T) {
// No tags given, everything matches
vms := getVMs()
filterVMsByTags(&vms, []string{})
require.Len(t, vms, 4)
require.Equal(t, 0, *vms[0].Id)
require.Equal(t, 1, *vms[1].Id)
require.Equal(t, 2, *vms[2].Id)
require.Equal(t, 3, *vms[3].Id)
// Empty tag, nothing matches
vms = getVMs()
filterVMsByTags(&vms, []string{""})
require.Len(t, vms, 0)
// 1 tag matches
vms = getVMs()
filterVMsByTags(&vms, []string{"tag1"})
require.Len(t, vms, 3)
require.Equal(t, 1, *vms[0].Id)
require.Equal(t, 2, *vms[1].Id)
require.Equal(t, 3, *vms[2].Id)
// 2 tags match
vms = getVMs()
filterVMsByTags(&vms, []string{"tag1", "tag2"})
require.Len(t, vms, 2)
require.Equal(t, 2, *vms[0].Id)
require.Equal(t, 3, *vms[1].Id)
// 3 tags match
vms = getVMs()
filterVMsByTags(&vms, []string{"tag1", "tag2", "tag3"})
require.Len(t, vms, 1)
require.Equal(t, 3, *vms[0].Id)
// A tag that doesn't match
vms = getVMs()
filterVMsByTags(&vms, []string{"tag1", "foo"})
require.Len(t, vms, 0)
}
func TestGetIBMCloudVMByTagAPIError(t *testing.T) {
fake := client.FakeSoftlayer{
GetVirtualGuestsStub: func(mask, filters *string) (resp []datatypes.Virtual_Guest, err error) {
return []datatypes.Virtual_Guest{}, fmt.Errorf("Custom error")
},
}
b, err := GetIBMCloudVMByTag(&fake, nil)
require.Error(t, err)
require.Equal(t, "Custom error", err.Error())
require.Empty(t, b)
}
func TestGetIBMCloudVMByTagNoVMs(t *testing.T) {
vmID1 := 1
vmID2 := 2
tag1Val := "tag1:val1"
tag1 := datatypes.Tag{Name: &tag1Val}
tagClusterIDVal := flavor.ClusterIDTag + ":my-cluster-id"
tag2 := datatypes.Tag{Name: &tagClusterIDVal}
tag3Val := "tag1:no-match"
tag3 := datatypes.Tag{Name: &tag3Val}
fake := client.FakeSoftlayer{
GetVirtualGuestsStub: func(mask, filters *string) (resp []datatypes.Virtual_Guest, err error) {
return []datatypes.Virtual_Guest{
{
TagReferences: []datatypes.Tag_Reference{{Tag: &tag1}, {Tag: &tag2}},
Id: &vmID1,
},
{
TagReferences: []datatypes.Tag_Reference{{Tag: &tag2}, {Tag: &tag3}},
Id: &vmID2,
},
}, nil
},
}
tags := []string{"tag1:val1", flavor.ClusterIDTag + ":my-cluster-id"}
id, err := GetIBMCloudVMByTag(&fake, tags)
require.NoError(t, err)
require.Equal(t, vmID1, *id)
// Verify args
expectedMask := "id,hostname,tagReferences[id,tag[name]]"
expectedFilter := filter.New(filter.Path("virtualGuests.tagReferences.tag.name").Eq(flavor.ClusterIDTag + ":my-cluster-id")).Build()
require.Equal(t,
[]struct {
Mask *string
Filters *string
}{{&expectedMask, &expectedFilter}},
fake.GetVirtualGuestsArgs)
}