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
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())
Copy link

Choose a reason for hiding this comment

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

What happens if there is no second vnic profile? We cannot assume there is a secondary profile available. Can we create a second profile? What if the secondary profile is on the same network as the primary? Does it still work?

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 , so I will change the function to catch the case that if there is no secondary profile then we will check the assertCannotCreateNICWithSameName and if we have more then 1 profile we will check this -assertCannotCreateNICWithVNICProfile.
WDYT?

Copy link

Choose a reason for hiding this comment

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

Yes. Ideally, we'll skip the test if there is no secondary profile available. Later we can create a secondary profile to test.

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(
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,
Expand All @@ -47,6 +47,19 @@ func assertCannotCreateNIC(
}
}

func assertCannotCreateNICWithSameNameDiffNetwork(
Copy link

Choose a reason for hiding this comment

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

I would suggest renaming this to assertCannotCreateNICWithVNICProfile since that more accurately covers what this function does and makes the function reusable.

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 assertCannotCreateNICWithSameVNICProfile?
it is not clear just with vnic profile.

Copy link

Choose a reason for hiding this comment

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

We could do that, however, we are trying to keep the assert functions reusable in other test cases. The naming should convey what the assert functions are doing rather than the current use case so future contributors feel that they can safely reuse the assert function. If the assert function alone does not convey the logic in the test case using the assert function we can always add comments explaining what's happening.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

NP

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

Choose a reason for hiding this comment

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

It is preferable from a readability perspective to check the error, not the returned object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

t.Fatalf("create 2 NICs with same name %s", name)
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved
}
}

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)
mgold1234 marked this conversation as resolved.
Show resolved Hide resolved
}
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")
}
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