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 10, 2022
1 parent 9f00269 commit 0fbd0c1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
51 changes: 45 additions & 6 deletions client_nic_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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 @@ -32,7 +34,6 @@ func TestVMNICCreation(t *testing.T) {
func TestDuplicateVMNICCreationWithSameName(t *testing.T) {
t.Parallel()
helper := getHelper(t)
nicName := "test_duplicate_name"

vm := assertCanCreateVM(
t,
Expand All @@ -59,10 +60,48 @@ func TestDuplicateVMNICCreationWithSameName(t *testing.T) {
assertNICCount(t, vm, 0)
}

func TestDuplicateVMNICCreationWithSameNameAndDiffVNICProfile(t *testing.T) {
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)
nicName := "test_duplicate_name"

vm := assertCanCreateVM(
t,
Expand All @@ -78,8 +117,8 @@ func TestDuplicateVMNICCreationWithSameNameAndDiffVNICProfile(t *testing.T) {
nicName,
ovirtclient.CreateNICParams())
assertNICCount(t, vm, 1)
DiffVNICProfile, _ := assertCanFindDiffVNICProfile(helper, helper.GetVNICProfileID())
if DiffVNICProfile == "" {
DiffVNICProfileSameNetwork, _ := assertCanFindDiffVNICProfileSameNetwork(helper, helper.GetVNICProfileID())
if DiffVNICProfileSameNetwork == "" {
assertCannotCreateNICWithSameName(
t,
helper,
Expand All @@ -91,7 +130,7 @@ func TestDuplicateVMNICCreationWithSameNameAndDiffVNICProfile(t *testing.T) {
t,
vm,
nicName,
DiffVNICProfile,
DiffVNICProfileSameNetwork,
ovirtclient.CreateNICParams())
}
assertNICCount(t, vm, 1)
Expand Down
33 changes: 30 additions & 3 deletions client_vnicprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,44 @@ func assertCanCreateVNICProfile(t *testing.T, helper ovirtclient.TestHelper) ovi
return newVNICProfile
}

func assertCanFindDiffVNICProfile(helper ovirtclient.TestHelper, vnicProfileID string) (string, error) {
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(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 vnicProfile.ID() != vnicProfileID {
if vnicID != vnicProfileID && vnicProfile.NetworkID() == networkID {
return vnicID, nil
}
}
return "", fmt.Errorf("failed to find different VNIC profile ID for testing, use the exiting one")
return "", fmt.Errorf(
"failed to find different VNIC profile ID and same Network for testing, use the exiting one")
}

0 comments on commit 0fbd0c1

Please sign in to comment.