Skip to content

Commit 282ef97

Browse files
authored
Add some unit tests (#89)
2 parents db5721c + 3b90644 commit 282ef97

File tree

3 files changed

+915
-0
lines changed

3 files changed

+915
-0
lines changed

cloudstack_instances_test.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package cloudstack
21+
22+
import (
23+
"strings"
24+
"testing"
25+
26+
"github.com/apache/cloudstack-go/v2/cloudstack"
27+
corev1 "k8s.io/api/core/v1"
28+
)
29+
30+
func TestNodeAddresses(t *testing.T) {
31+
cs := &CSCloud{}
32+
33+
tests := []struct {
34+
name string
35+
instance *cloudstack.VirtualMachine
36+
wantAddrs []corev1.NodeAddress
37+
wantErr bool
38+
errContains string
39+
}{
40+
{
41+
name: "instance with internal IP only",
42+
instance: &cloudstack.VirtualMachine{
43+
Id: "vm-1",
44+
Name: "test-vm",
45+
Nic: []cloudstack.Nic{
46+
{Ipaddress: "10.0.0.1"},
47+
},
48+
},
49+
wantAddrs: []corev1.NodeAddress{
50+
{Type: corev1.NodeInternalIP, Address: "10.0.0.1"},
51+
},
52+
wantErr: false,
53+
},
54+
{
55+
name: "instance with internal IP and hostname",
56+
instance: &cloudstack.VirtualMachine{
57+
Id: "vm-1",
58+
Name: "test-vm",
59+
Hostname: "test-hostname",
60+
Nic: []cloudstack.Nic{
61+
{Ipaddress: "10.0.0.1"},
62+
},
63+
},
64+
wantAddrs: []corev1.NodeAddress{
65+
{Type: corev1.NodeInternalIP, Address: "10.0.0.1"},
66+
{Type: corev1.NodeHostName, Address: "test-hostname"},
67+
},
68+
wantErr: false,
69+
},
70+
{
71+
name: "instance with internal IP and public IP",
72+
instance: &cloudstack.VirtualMachine{
73+
Id: "vm-1",
74+
Name: "test-vm",
75+
Publicip: "203.0.113.1",
76+
Nic: []cloudstack.Nic{
77+
{Ipaddress: "10.0.0.1"},
78+
},
79+
},
80+
wantAddrs: []corev1.NodeAddress{
81+
{Type: corev1.NodeInternalIP, Address: "10.0.0.1"},
82+
{Type: corev1.NodeExternalIP, Address: "203.0.113.1"},
83+
},
84+
wantErr: false,
85+
},
86+
{
87+
name: "instance with all address types",
88+
instance: &cloudstack.VirtualMachine{
89+
Id: "vm-1",
90+
Name: "test-vm",
91+
Hostname: "test-hostname",
92+
Publicip: "203.0.113.1",
93+
Nic: []cloudstack.Nic{
94+
{Ipaddress: "10.0.0.1"},
95+
},
96+
},
97+
wantAddrs: []corev1.NodeAddress{
98+
{Type: corev1.NodeInternalIP, Address: "10.0.0.1"},
99+
{Type: corev1.NodeHostName, Address: "test-hostname"},
100+
{Type: corev1.NodeExternalIP, Address: "203.0.113.1"},
101+
},
102+
wantErr: false,
103+
},
104+
{
105+
name: "instance with no NICs returns error",
106+
instance: &cloudstack.VirtualMachine{
107+
Id: "vm-1",
108+
Name: "test-vm",
109+
Nic: []cloudstack.Nic{},
110+
},
111+
wantAddrs: nil,
112+
wantErr: true,
113+
errContains: "does not have an internal IP",
114+
},
115+
{
116+
name: "instance with nil NICs returns error",
117+
instance: &cloudstack.VirtualMachine{
118+
Id: "vm-1",
119+
Name: "test-vm",
120+
Nic: nil,
121+
},
122+
wantAddrs: nil,
123+
wantErr: true,
124+
errContains: "does not have an internal IP",
125+
},
126+
{
127+
name: "instance with multiple NICs uses first",
128+
instance: &cloudstack.VirtualMachine{
129+
Id: "vm-1",
130+
Name: "test-vm",
131+
Nic: []cloudstack.Nic{
132+
{Ipaddress: "10.0.0.1"},
133+
{Ipaddress: "10.0.0.2"},
134+
},
135+
},
136+
wantAddrs: []corev1.NodeAddress{
137+
{Type: corev1.NodeInternalIP, Address: "10.0.0.1"},
138+
},
139+
wantErr: false,
140+
},
141+
}
142+
143+
for _, tt := range tests {
144+
t.Run(tt.name, func(t *testing.T) {
145+
gotAddrs, err := cs.nodeAddresses(tt.instance)
146+
147+
if tt.wantErr {
148+
if err == nil {
149+
t.Errorf("nodeAddresses() expected error, got nil")
150+
return
151+
}
152+
if tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) {
153+
t.Errorf("nodeAddresses() error = %v, want error containing %q", err, tt.errContains)
154+
}
155+
return
156+
}
157+
158+
if err != nil {
159+
t.Errorf("nodeAddresses() unexpected error: %v", err)
160+
return
161+
}
162+
163+
if len(gotAddrs) != len(tt.wantAddrs) {
164+
t.Errorf("nodeAddresses() returned %d addresses, want %d", len(gotAddrs), len(tt.wantAddrs))
165+
return
166+
}
167+
168+
for i, want := range tt.wantAddrs {
169+
if gotAddrs[i].Type != want.Type || gotAddrs[i].Address != want.Address {
170+
t.Errorf("nodeAddresses()[%d] = {%v, %v}, want {%v, %v}",
171+
i, gotAddrs[i].Type, gotAddrs[i].Address, want.Type, want.Address)
172+
}
173+
}
174+
})
175+
}
176+
}

0 commit comments

Comments
 (0)