Skip to content

Commit 43bde74

Browse files
authored
add some unit tests (#90)
2 parents 1b75921 + c93aee9 commit 43bde74

File tree

3 files changed

+3036
-0
lines changed

3 files changed

+3036
-0
lines changed

cloudstack_instances_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,78 @@ func TestNodeAddresses(t *testing.T) {
174174
})
175175
}
176176
}
177+
178+
func TestGetProviderIDFromInstanceID(t *testing.T) {
179+
cs := &CSCloud{}
180+
181+
tests := []struct {
182+
name string
183+
instanceID string
184+
want string
185+
}{
186+
{
187+
name: "valid instance ID",
188+
instanceID: "vm-123",
189+
want: "external-cloudstack://vm-123",
190+
},
191+
{
192+
name: "empty instance ID",
193+
instanceID: "",
194+
want: "external-cloudstack://",
195+
},
196+
}
197+
198+
for _, tt := range tests {
199+
t.Run(tt.name, func(t *testing.T) {
200+
got := cs.getProviderIDFromInstanceID(tt.instanceID)
201+
if got != tt.want {
202+
t.Errorf("getProviderIDFromInstanceID(%q) = %q, want %q", tt.instanceID, got, tt.want)
203+
}
204+
})
205+
}
206+
}
207+
208+
func TestGetInstanceIDFromProviderID(t *testing.T) {
209+
cs := &CSCloud{}
210+
211+
tests := []struct {
212+
name string
213+
providerID string
214+
want string
215+
}{
216+
{
217+
name: "full provider ID format",
218+
providerID: "external-cloudstack://vm-123",
219+
want: "vm-123",
220+
},
221+
{
222+
name: "instance ID only - backward compatibility",
223+
providerID: "vm-123",
224+
want: "vm-123",
225+
},
226+
{
227+
name: "empty string",
228+
providerID: "",
229+
want: "",
230+
},
231+
{
232+
name: "invalid format - no separator",
233+
providerID: "external-cloudstack-vm-123",
234+
want: "external-cloudstack-vm-123",
235+
},
236+
{
237+
name: "different provider prefix",
238+
providerID: "aws://i-1234567890abcdef0",
239+
want: "i-1234567890abcdef0",
240+
},
241+
}
242+
243+
for _, tt := range tests {
244+
t.Run(tt.name, func(t *testing.T) {
245+
got := cs.getInstanceIDFromProviderID(tt.providerID)
246+
if got != tt.want {
247+
t.Errorf("getInstanceIDFromProviderID(%q) = %q, want %q", tt.providerID, got, tt.want)
248+
}
249+
})
250+
}
251+
}

0 commit comments

Comments
 (0)