Skip to content

Commit

Permalink
enh: add unit test for instanceID from providerID (#2406)
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshi-1505 authored Oct 17, 2023
1 parent 4900dd0 commit e761db1
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions pkg/openstack/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -189,3 +190,74 @@ func TestSortNodeAddressesWithMultipleCIDRs(t *testing.T) {

executeSortNodeAddressesTest(t, addressSortOrder, want)
}

func Test_instanceIDFromProviderID(t *testing.T) {
type args struct {
providerID string
}
tests := []struct {
name string
args args
wantInstanceID string
wantRegion string
wantErr bool
}{
{
name: "it parses region & instanceID correctly from providerID",
args: args{
providerID: "openstack://us-east-1/testInstanceID",
},
wantInstanceID: "testInstanceID",
wantRegion: "us-east-1",
wantErr: false,
},
{
name: "it parses instanceID if providerID has empty protocol & no region",
args: args{
providerID: "/testInstanceID",
},
wantInstanceID: "testInstanceID",
wantRegion: "",
wantErr: false,
},
{
name: "it returns error in case of invalid providerID format with no region",
args: args{
providerID: "openstack://us-east-1-testInstanceID",
},
wantInstanceID: "",
wantRegion: "",
wantErr: true,
},
{
name: "it parses correct instanceID in case the region name is the empty string",
args: args{
providerID: "openstack:///testInstanceID",
},
wantInstanceID: "testInstanceID",
wantRegion: "",
wantErr: false,
},
{
name: "it appends openstack:// in case of missing protocol in providerID",
args: args{
providerID: "us-east-1/testInstanceID",
},
wantInstanceID: "testInstanceID",
wantRegion: "us-east-1",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotInstanceID, gotRegion, err := instanceIDFromProviderID(tt.args.providerID)
assert.Equal(t, tt.wantInstanceID, gotInstanceID)
assert.Equal(t, tt.wantRegion, gotRegion)
if tt.wantErr == true {
assert.ErrorContains(t, err, "didn't match expected format")
} else {
assert.NoError(t, err)
}
})
}
}

0 comments on commit e761db1

Please sign in to comment.