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
39 changes: 34 additions & 5 deletions client_nic_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ 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"
nickName := fmt.Sprintf("duplicate_nic_%s", helper.GenerateRandomID(5))

vm := assertCanCreateVM(
t,
Expand All @@ -45,16 +45,45 @@ func TestDuplicateVMNICCreation(t *testing.T) {
t,
helper,
vm,
nicName,
nickName,
ovirtclient.CreateNICParams())
assertNICCount(t, vm, 1)
assertCannotCreateNIC(
assertCannotCreateNICWithSameName(
t,
helper,
vm,
nicName,
nickName,
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)
nickName := fmt.Sprintf("duplicate_nic_%s", helper.GenerateRandomID(5))

vm := assertCanCreateVM(
t,
helper,
fmt.Sprintf("nic_test_%s", helper.GenerateRandomID(5)),
ovirtclient.CreateVMParams(),
)
assertNICCount(t, vm, 0)
assertCanCreateNIC(
t,
helper,
vm,
nickName,
ovirtclient.CreateNICParams())
assertNICCount(t, vm, 1)
diffVNICProfileSameNetwork := assertCanCreateVNICProfile(t, helper)
assertCannotCreateNICWithVNICProfile(
t,
vm,
nickName,
diffVNICProfileSameNetwork.ID(),
ovirtclient.ETimeout)
assertNICCount(t, vm, 1)
}
31 changes: 28 additions & 3 deletions client_nic_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ovirtclient_test

import (
"errors"
"testing"

ovirtclient "github.com/ovirt/go-ovirt-client"
Expand Down Expand Up @@ -34,19 +35,43 @@ 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,
errorCode ovirtclient.ErrorCode) {
_, err := vm.CreateNIC(name, diffVNICProfile, ovirtclient.CreateNICParams())
if err != nil {
if errorCode == "" {
return
}
var e ovirtclient.EngineError
if !errors.As(err, &e) {
t.Fatalf("Failed to convert returned error to EngineError (%v)", err)
}
if !e.HasCode(errorCode) {
t.Fatalf("Unexpected error code: %s, instead of: %s", e.Code(), errorCode)
}
return
}
t.Fatalf("Unexectedly create 2 NICs with same name %s and different VNICProfile %s", name, diffVNICProfile)
}

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
5 changes: 2 additions & 3 deletions mock_nic_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ 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 {
return nil, newError(ENotFound, "NIC with name %s is already in use", name)
if n.name == name && m.vnicProfiles[n.vnicProfileID].networkID == m.vnicProfiles[vnicProfileID].networkID {
Copy link

Choose a reason for hiding this comment

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

I checked in my local setup, if I remove this change the test still passes. That means that there is no test that catches this bug.

Copy link

Choose a reason for hiding this comment

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

@mgold1234 this is still not resolved. If I remove this change and run the test suite the test passes. There needs to be a test that fails if this change is not added.

return nil, newError(ETimeout, "NIC with same name %s is already in use", name)
}
}

id := uuid.Must(uuid.NewUUID()).String()

nic := &nic{
Expand Down