Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add check that vnic with same name doesn't has the same network #109

Closed
wants to merge 8 commits into from
76 changes: 73 additions & 3 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"
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved

func TestVMNICCreation(t *testing.T) {
t.Parallel()
helper := getHelper(t)
Expand All @@ -29,10 +31,9 @@ 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"

vm := assertCanCreateVM(
t,
Expand All @@ -48,7 +49,7 @@ func TestDuplicateVMNICCreation(t *testing.T) {
nicName,
ovirtclient.CreateNICParams())
assertNICCount(t, vm, 1)
assertCannotCreateNIC(
assertCannotCreateNICWithSameName(
t,
helper,
vm,
Expand All @@ -58,3 +59,72 @@ func TestDuplicateVMNICCreation(t *testing.T) {
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())
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved
if DiffVNICProfileDiffNetwork == "" {
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved
assertCannotCreateNICWithSameName(
t,
helper,
vm,
nicName,
ovirtclient.CreateNICParams())
} else {
assertCannotCreateNICWithVNICProfile(
t,
vm,
nicName,
DiffVNICProfileDiffNetwork,
ovirtclient.CreateNICParams())
}
assertNICCount(t, vm, 1)
assertCanRemoveNIC(t, nic1)
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved
assertNICCount(t, vm, 0)
}

func TestDuplicateVMNICCreationWithSameNameDiffVNICProfileSameNetwork(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)
DiffVNICProfileSameNetwork, _ := assertCanFindDiffVNICProfileSameNetwork(t, helper, helper.GetVNICProfileID())
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved
assertCannotCreateNICWithVNICProfile(
t,
vm,
nicName,
DiffVNICProfileSameNetwork,
ovirtclient.CreateNICParams())
assertNICCount(t, vm, 1)
assertCanRemoveNIC(t, nic1)
assertNICCount(t, vm, 0)
}
20 changes: 17 additions & 3 deletions client_nic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,33 @@ func assertCanCreateNIC(
return nic
}

func assertCannotCreateNIC(
func assertCannotCreateNICWithSameName(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name change is misleading. The error message below should also be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about -("create 2 NICs with same name %s (%v)", name, err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janosdebugs , we have some functions for create nic:

  1. assertCanCreateNIC
  2. assertCannotCreateNICWithSameName
  3. assertCannotCreateNICWithVNICProfile
    and more...

regarding 2 and 3 what do you suggest? if I change 2 as it was assertCannotCreateNIC, what is the differerent from 3?
maybe change it to 'assertCannotCreateNICWithName'?
and 3 will be assertCannotCreateNICWithVNICProfile?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would personally prefer having two functions: assertCanCreateNICWithVNICProfile and assertCannotCreateNICWithVNICProfile. This should cover everything we need, right?

t *testing.T,
helper ovirtclient.TestHelper,
vm ovirtclient.VM,
name string,
params ovirtclient.BuildableNICParameters,
) {
nic, _ := vm.CreateNIC(name, helper.GetVNICProfileID(), params)
if nic != nil {

_, err := vm.CreateNIC(name, helper.GetVNICProfileID(), params)
if err == nil {
t.Fatalf("create 2 NICs with same name %s", name)
}
}

func assertCannotCreateNICWithVNICProfile(
t *testing.T,
vm ovirtclient.VM,
name string,
diffVNICProfile string,
params ovirtclient.BuildableNICParameters,
) {
_, err := vm.CreateNIC(name, diffVNICProfile, params)
if err == nil {
t.Fatalf("create 2 NICs with same name %s and different VNICProfile (%v)", name, err)
}
}

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
35 changes: 35 additions & 0 deletions client_vnicprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,38 @@ 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)
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved
}
networkID := existVNICProfile.NetworkID()
vnicProfiles, err := client.ListVNICProfiles()
if err != nil {
return "", fmt.Errorf("failed to list VNIC profiles (%w)", err)
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved
}
for _, vnicProfile := range vnicProfiles {
vnicID := vnicProfile.ID()
if vnicID != vnicProfileID && vnicProfile.NetworkID() != networkID {
return vnicID, nil
}
}
return "", fmt.Errorf(
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved
"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)
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved
}
if newVNICProfile.NetworkID() == existVNICProfile.NetworkID() {
return newVNICProfile.ID(), nil
}
return "", fmt.Errorf(
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved
"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 @@ -22,7 +22,7 @@ func (m *mockClient) CreateNIC(
}
for _, n := range m.nics {
if n.name == name {
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved
return nil, newError(ENotFound, "NIC with name %s is already in use", name)
return nil, newError(ENotFound, "NIC with same name %s is already in use", name)
}
}

Expand Down