-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcloudstack_instances_test.go
More file actions
251 lines (235 loc) · 6.16 KB
/
cloudstack_instances_test.go
File metadata and controls
251 lines (235 loc) · 6.16 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package cloudstack
import (
"strings"
"testing"
"github.com/apache/cloudstack-go/v2/cloudstack"
corev1 "k8s.io/api/core/v1"
)
func TestNodeAddresses(t *testing.T) {
cs := &CSCloud{}
tests := []struct {
name string
instance *cloudstack.VirtualMachine
wantAddrs []corev1.NodeAddress
wantErr bool
errContains string
}{
{
name: "instance with internal IP only",
instance: &cloudstack.VirtualMachine{
Id: "vm-1",
Name: "test-vm",
Nic: []cloudstack.Nic{
{Ipaddress: "10.0.0.1"},
},
},
wantAddrs: []corev1.NodeAddress{
{Type: corev1.NodeInternalIP, Address: "10.0.0.1"},
},
wantErr: false,
},
{
name: "instance with internal IP and hostname",
instance: &cloudstack.VirtualMachine{
Id: "vm-1",
Name: "test-vm",
Hostname: "test-hostname",
Nic: []cloudstack.Nic{
{Ipaddress: "10.0.0.1"},
},
},
wantAddrs: []corev1.NodeAddress{
{Type: corev1.NodeInternalIP, Address: "10.0.0.1"},
{Type: corev1.NodeHostName, Address: "test-hostname"},
},
wantErr: false,
},
{
name: "instance with internal IP and public IP",
instance: &cloudstack.VirtualMachine{
Id: "vm-1",
Name: "test-vm",
Publicip: "203.0.113.1",
Nic: []cloudstack.Nic{
{Ipaddress: "10.0.0.1"},
},
},
wantAddrs: []corev1.NodeAddress{
{Type: corev1.NodeInternalIP, Address: "10.0.0.1"},
{Type: corev1.NodeExternalIP, Address: "203.0.113.1"},
},
wantErr: false,
},
{
name: "instance with all address types",
instance: &cloudstack.VirtualMachine{
Id: "vm-1",
Name: "test-vm",
Hostname: "test-hostname",
Publicip: "203.0.113.1",
Nic: []cloudstack.Nic{
{Ipaddress: "10.0.0.1"},
},
},
wantAddrs: []corev1.NodeAddress{
{Type: corev1.NodeInternalIP, Address: "10.0.0.1"},
{Type: corev1.NodeHostName, Address: "test-hostname"},
{Type: corev1.NodeExternalIP, Address: "203.0.113.1"},
},
wantErr: false,
},
{
name: "instance with no NICs returns error",
instance: &cloudstack.VirtualMachine{
Id: "vm-1",
Name: "test-vm",
Nic: []cloudstack.Nic{},
},
wantAddrs: nil,
wantErr: true,
errContains: "does not have an internal IP",
},
{
name: "instance with nil NICs returns error",
instance: &cloudstack.VirtualMachine{
Id: "vm-1",
Name: "test-vm",
Nic: nil,
},
wantAddrs: nil,
wantErr: true,
errContains: "does not have an internal IP",
},
{
name: "instance with multiple NICs uses first",
instance: &cloudstack.VirtualMachine{
Id: "vm-1",
Name: "test-vm",
Nic: []cloudstack.Nic{
{Ipaddress: "10.0.0.1"},
{Ipaddress: "10.0.0.2"},
},
},
wantAddrs: []corev1.NodeAddress{
{Type: corev1.NodeInternalIP, Address: "10.0.0.1"},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotAddrs, err := cs.nodeAddresses(tt.instance)
if tt.wantErr {
if err == nil {
t.Errorf("nodeAddresses() expected error, got nil")
return
}
if tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) {
t.Errorf("nodeAddresses() error = %v, want error containing %q", err, tt.errContains)
}
return
}
if err != nil {
t.Errorf("nodeAddresses() unexpected error: %v", err)
return
}
if len(gotAddrs) != len(tt.wantAddrs) {
t.Errorf("nodeAddresses() returned %d addresses, want %d", len(gotAddrs), len(tt.wantAddrs))
return
}
for i, want := range tt.wantAddrs {
if gotAddrs[i].Type != want.Type || gotAddrs[i].Address != want.Address {
t.Errorf("nodeAddresses()[%d] = {%v, %v}, want {%v, %v}",
i, gotAddrs[i].Type, gotAddrs[i].Address, want.Type, want.Address)
}
}
})
}
}
func TestGetProviderIDFromInstanceID(t *testing.T) {
cs := &CSCloud{}
tests := []struct {
name string
instanceID string
want string
}{
{
name: "valid instance ID",
instanceID: "vm-123",
want: "external-cloudstack://vm-123",
},
{
name: "empty instance ID",
instanceID: "",
want: "external-cloudstack://",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := cs.getProviderIDFromInstanceID(tt.instanceID)
if got != tt.want {
t.Errorf("getProviderIDFromInstanceID(%q) = %q, want %q", tt.instanceID, got, tt.want)
}
})
}
}
func TestGetInstanceIDFromProviderID(t *testing.T) {
cs := &CSCloud{}
tests := []struct {
name string
providerID string
want string
}{
{
name: "full provider ID format",
providerID: "external-cloudstack://vm-123",
want: "vm-123",
},
{
name: "instance ID only - backward compatibility",
providerID: "vm-123",
want: "vm-123",
},
{
name: "empty string",
providerID: "",
want: "",
},
{
name: "invalid format - no separator",
providerID: "external-cloudstack-vm-123",
want: "external-cloudstack-vm-123",
},
{
name: "different provider prefix",
providerID: "aws://i-1234567890abcdef0",
want: "i-1234567890abcdef0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := cs.getInstanceIDFromProviderID(tt.providerID)
if got != tt.want {
t.Errorf("getInstanceIDFromProviderID(%q) = %q, want %q", tt.providerID, got, tt.want)
}
})
}
}