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 15, 2022
1 parent 8d94d81 commit 75a3fdd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 86 deletions.
59 changes: 9 additions & 50 deletions client_nic_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
ovirtclient "github.com/ovirt/go-ovirt-client"
)

const nicName = "test_duplicate_name"

func TestVMNICCreation(t *testing.T) {
t.Parallel()
helper := getHelper(t)
Expand All @@ -34,6 +32,7 @@ func TestVMNICCreation(t *testing.T) {
func TestDuplicateVMNICCreationWithSameName(t *testing.T) {
t.Parallel()
helper := getHelper(t)
nickName := fmt.Sprintf("duplicate_nic_%s", helper.GenerateRandomID(5))

vm := assertCanCreateVM(
t,
Expand All @@ -46,62 +45,24 @@ func TestDuplicateVMNICCreationWithSameName(t *testing.T) {
t,
helper,
vm,
nicName,
nickName,
ovirtclient.CreateNICParams())
assertNICCount(t, vm, 1)
assertCannotCreateNICWithSameName(
t,
helper,
vm,
nicName,
nickName,
ovirtclient.CreateNICParams())
assertNICCount(t, vm, 1)
assertCanRemoveNIC(t, nic1)
assertNICCount(t, vm, 0)
}

func TestDuplicateVMNICCreationWithSameNameDiffVNICProfileDiffNetwork(t *testing.T) {
t.Parallel()
helper := getHelper(t)

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)
DiffVNICProfileDiffNetwork, _ := assertCanFindDiffVNICProfileDiffNetwork(helper, helper.GetVNICProfileID())
if DiffVNICProfileDiffNetwork == "" {
assertCannotCreateNICWithSameName(
t,
helper,
vm,
nicName,
ovirtclient.CreateNICParams())
} else {
assertCannotCreateNICWithVNICProfile(
t,
vm,
nicName,
DiffVNICProfileDiffNetwork,
ovirtclient.CreateNICParams())
}
assertNICCount(t, vm, 1)
assertCanRemoveNIC(t, nic1)
assertNICCount(t, vm, 0)
}

func TestDuplicateVMNICCreationWithSameNameDiffVNICProfileSameNetwork(t *testing.T) {
t.Parallel()
helper := getHelper(t)
nickName := fmt.Sprintf("duplicate_nic_%s", helper.GenerateRandomID(5))

vm := assertCanCreateVM(
t,
Expand All @@ -110,21 +71,19 @@ func TestDuplicateVMNICCreationWithSameNameDiffVNICProfileSameNetwork(t *testing
ovirtclient.CreateVMParams(),
)
assertNICCount(t, vm, 0)
nic1 := assertCanCreateNIC(
assertCanCreateNIC(
t,
helper,
vm,
nicName,
nickName,
ovirtclient.CreateNICParams())
assertNICCount(t, vm, 1)
DiffVNICProfileSameNetwork, _ := assertCanFindDiffVNICProfileSameNetwork(t, helper, helper.GetVNICProfileID())
diffVNICProfileSameNetwork := assertCanCreateVNICProfile(t, helper)
assertCannotCreateNICWithVNICProfile(
t,
vm,
nicName,
DiffVNICProfileSameNetwork,
nickName,
diffVNICProfileSameNetwork.ID(),
ovirtclient.CreateNICParams())
assertNICCount(t, vm, 1)
assertCanRemoveNIC(t, nic1)
assertNICCount(t, vm, 0)
}
35 changes: 0 additions & 35 deletions client_vnicprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,3 @@ func assertCanCreateVNICProfile(t *testing.T, helper ovirtclient.TestHelper) ovi
})
return newVNICProfile
}

func assertCanFindDiffVNICProfileDiffNetwork(helper ovirtclient.TestHelper, vnicProfileID string) (string, error) {
client := helper.GetClient()
existVNICProfile, err := client.GetVNICProfile(vnicProfileID)
if err != nil {
return "", fmt.Errorf("failed to verify VNIC profile ID %s", vnicProfileID)
}
networkID := existVNICProfile.NetworkID()
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 vnicID != vnicProfileID && vnicProfile.NetworkID() != networkID {
return vnicID, nil
}
}
return "", fmt.Errorf(
"failed to find different VNIC profile ID and Different Network for testing, use the exiting one")
}

func assertCanFindDiffVNICProfileSameNetwork(t *testing.T, helper ovirtclient.TestHelper, vnicProfileID string) (string, error) {
client := helper.GetClient()
newVNICProfile := assertCanCreateVNICProfile(t, helper)
existVNICProfile, err := client.GetVNICProfile(vnicProfileID)
if err != nil {
return "", fmt.Errorf("failed to verify VNIC profile ID %s", vnicProfileID)
}
if newVNICProfile.NetworkID() == existVNICProfile.NetworkID() {
return newVNICProfile.ID(), nil
}
return "", fmt.Errorf(
"failed to find different VNIC profile ID and same Network for testing, use the exiting one")
}
2 changes: 1 addition & 1 deletion mock_nic_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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 {
if n.name == name && m.vnicProfiles[n.vnicProfileID].networkID == m.vnicProfiles[vnicProfileID].networkID {
return nil, newError(ENotFound, "NIC with same name %s is already in use", name)
}
}
Expand Down

0 comments on commit 75a3fdd

Please sign in to comment.