Skip to content

Commit

Permalink
This PR fixes oVirt#82 and check that vnic doesn't has same network
Browse files Browse the repository at this point in the history
  • Loading branch information
mgold1234 committed Feb 9, 2022
1 parent d1442f1 commit 8f13077
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
35 changes: 33 additions & 2 deletions client_nic_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestVMNICCreation(t *testing.T) {
assertNICCount(t, vm, 0)
}

func TestDuplicateVMNICCreation(t *testing.T) {
func TestDuplicateVMNICCreationWithSameName(t *testing.T) {
t.Parallel()
helper := getHelper(t)
nicName := "test_duplicate_name"
Expand All @@ -48,7 +48,7 @@ func TestDuplicateVMNICCreation(t *testing.T) {
nicName,
ovirtclient.CreateNICParams())
assertNICCount(t, vm, 1)
assertCannotCreateNIC(
assertCannotCreateNICWithSameName(
t,
helper,
vm,
Expand All @@ -58,3 +58,34 @@ func TestDuplicateVMNICCreation(t *testing.T) {
assertCanRemoveNIC(t, nic1)
assertNICCount(t, vm, 0)
}

func TestDuplicateVMNICCreationWithSameNameAndDiffNetwork(t *testing.T) {
t.Parallel()
helper := getHelper(t)
nicName := "test_duplicate_name"

vm := assertCanCreateVM(
t,
helper,
fmt.Sprintf("nic_test_%s", helper.GenerateRandomID(5)),
ovirtclient.CreateVMParams(),
)
assertNICCount(t, vm, 0)
nic1 := assertCanCreateNIC(
t,
helper,
vm,
nicName,
ovirtclient.CreateNICParams())
assertNICCount(t, vm, 1)
DiffVNICProfile, _ := assertCanFindDiffVNICProfile(helper, helper.GetVNICProfileID())
assertCannotCreateNICWithSameNameDiffNetwork(
t,
vm,
nicName,
DiffVNICProfile,
ovirtclient.CreateNICParams())
assertNICCount(t, vm, 1)
assertCanRemoveNIC(t, nic1)
assertNICCount(t, vm, 0)
}
15 changes: 14 additions & 1 deletion client_nic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func assertCanCreateNIC(
return nic
}

func assertCannotCreateNIC(
func assertCannotCreateNICWithSameName(
t *testing.T,
helper ovirtclient.TestHelper,
vm ovirtclient.VM,
Expand All @@ -47,6 +47,19 @@ func assertCannotCreateNIC(
}
}

func assertCannotCreateNICWithSameNameDiffNetwork(
t *testing.T,
vm ovirtclient.VM,
name string,
diffVNICProfile string,
params ovirtclient.BuildableNICParameters,
) {
nic, _ := vm.CreateNIC(name, diffVNICProfile, params)
if nic != nil {
t.Fatalf("create 2 NICs with same name %s", name)
}
}

func assertCanRemoveNIC(t *testing.T, nic ovirtclient.NIC) {
if err := nic.Remove(); err != nil {
t.Fatalf("failed to remove NIC %s (%v)", nic.ID(), err)
Expand Down
15 changes: 15 additions & 0 deletions client_vnicprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,18 @@ func assertCanCreateVNICProfile(t *testing.T, helper ovirtclient.TestHelper) ovi
})
return newVNICProfile
}

func assertCanFindDiffVNICProfile(helper ovirtclient.TestHelper, vnicProfileID string) (string, error) {
client := helper.GetClient()
vnicProfiles, err := client.ListVNICProfiles()
if err != nil {
return "", fmt.Errorf("failed to list VNIC profiles (%w)", err)
}
for _, vnicProfile := range vnicProfiles {
vnicID := vnicProfile.ID()
if vnicProfile.ID() != vnicProfileID {
return vnicID, nil
}
}
return vnicProfileID, fmt.Errorf("failed to find different VNIC profile ID for testing, use the exiting one")
}
4 changes: 2 additions & 2 deletions mock_nic_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (m *mockClient) CreateNIC(
return nil, newError(ENotFound, "VM with ID %s not found for NIC creation", vmid)
}
for _, n := range m.nics {
if n.name == name && n.vnicProfileID == vnicProfileID {
return nil, newError(ENotFound, "NIC with same name %s and same network is already in use", name)
if n.name == name {
return nil, newError(ENotFound, "NIC with same name %s is already in use", name)
}
}

Expand Down

0 comments on commit 8f13077

Please sign in to comment.