Skip to content

Commit

Permalink
Move inline strings into conststants
Browse files Browse the repository at this point in the history
  • Loading branch information
yitsushi committed Oct 22, 2021
1 parent 15f75c0 commit c923a31
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 86 deletions.
120 changes: 47 additions & 73 deletions core/steps/network/interface_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ func TestNewNetworkInterface_everythingIsEmpty(t *testing.T) {
g.RegisterTestingT(t)

var status *models.NetworkInterfaceStatus
vmid, _ := models.NewVMID("testvm", "testns")
vmid, _ := models.NewVMID(vmName, nsName)
iface := &models.NetworkInterface{}
svc := mock.NewMockNetworkService(mockCtrl)
ctx := context.Background()

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Times(0)

step := network.NewNetworkInterface(vmid, iface, status, svc)
Expand All @@ -47,13 +47,13 @@ func TestNewNetworkInterface_doesNotExist(t *testing.T) {
g.RegisterTestingT(t)

var status *models.NetworkInterfaceStatus
vmid, _ := models.NewVMID("testvm", "testns")
iface := &models.NetworkInterface{GuestDeviceName: "eth0"}
vmid, _ := models.NewVMID(vmName, nsName)
iface := &models.NetworkInterface{GuestDeviceName: defaultEthDevice}
svc := mock.NewMockNetworkService(mockCtrl)
ctx := context.Background()

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Times(0)

step := network.NewNetworkInterface(vmid, iface, status, svc)
Expand All @@ -63,18 +63,18 @@ func TestNewNetworkInterface_doesNotExist(t *testing.T) {
g.Expect(shouldDo).To(g.BeTrue())

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(false, nil).
Times(1)

svc.EXPECT().
IfaceCreate(gomock.Eq(ctx), gomock.Eq(ports.IfaceCreateInput{
DeviceName: "testns_testvm_tap",
DeviceName: expectedTapDeviceName,
})).
Return(&ports.IfaceDetails{
DeviceName: "testns_testvm_tap",
DeviceName: expectedTapDeviceName,
Type: models.IfaceTypeTap,
MAC: "AA:BB:CC:DD:EE:FF",
MAC: defaultMACAddress,
Index: 0,
}, nil).
Times(1)
Expand All @@ -90,23 +90,23 @@ func TestNewNetworkInterface_existingInterface(t *testing.T) {

g.RegisterTestingT(t)

vmid, _ := models.NewVMID("testvm", "testns")
vmid, _ := models.NewVMID(vmName, nsName)
iface := &models.NetworkInterface{
GuestDeviceName: "eth0",
GuestDeviceName: defaultEthDevice,
AllowMetadataRequests: false,
GuestMAC: "AA:BB:CC:DD:EE:FF",
GuestMAC: defaultMACAddress,
Type: models.IfaceTypeTap,
}
status := &models.NetworkInterfaceStatus{
HostDeviceName: "testns_testvm_tap",
HostDeviceName: expectedTapDeviceName,
Index: 0,
MACAddress: "AA:BB:CC:DD:EE:FF",
MACAddress: defaultMACAddress,
}
svc := mock.NewMockNetworkService(mockCtrl)
ctx := context.Background()

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(true, nil).
Times(1)

Expand All @@ -117,16 +117,16 @@ func TestNewNetworkInterface_existingInterface(t *testing.T) {
g.Expect(shouldDo).To(g.BeFalse())

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(true, nil).
Times(1)

svc.EXPECT().
IfaceDetails(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceDetails(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(&ports.IfaceDetails{
DeviceName: "testns_testvm_tap",
DeviceName: expectedTapDeviceName,
Type: models.IfaceTypeTap,
MAC: "AA:BB:CC:DD:EE:FF",
MAC: defaultMACAddress,
Index: 0,
}, nil).
Times(1)
Expand All @@ -142,22 +142,13 @@ func TestNewNetworkInterface_missingInterface(t *testing.T) {

g.RegisterTestingT(t)

vmid, _ := models.NewVMID("testvm", "testns")
iface := &models.NetworkInterface{
GuestDeviceName: "eth0",
AllowMetadataRequests: true,
GuestMAC: "AA:BB:CC:DD:EE:FF",
}
status := &models.NetworkInterfaceStatus{
HostDeviceName: "testns_testvm_tap",
Index: 0,
MACAddress: "AA:BB:CC:DD:EE:FF",
}
vmid, _ := models.NewVMID(vmName, nsName)
iface, status := fullNetworkInterface()
svc := mock.NewMockNetworkService(mockCtrl)
ctx := context.Background()

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(false, nil).
Times(1)

Expand All @@ -168,27 +159,27 @@ func TestNewNetworkInterface_missingInterface(t *testing.T) {
g.Expect(shouldDo).To(g.BeTrue())

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(false, nil).
Times(1)

svc.EXPECT().
IfaceCreate(gomock.Eq(ctx), gomock.Eq(ports.IfaceCreateInput{
DeviceName: "testns_testvm_tap",
MAC: "AA:BB:CC:DD:EE:FF",
DeviceName: expectedTapDeviceName,
MAC: defaultMACAddress,
})).
Return(&ports.IfaceDetails{
DeviceName: "testns_testvm_tap",
DeviceName: expectedTapDeviceName,
Type: models.IfaceTypeTap,
MAC: "FF:EE:DD:CC:BB:AA",
MAC: reverseMACAddress,
Index: 0,
}, nil).
Times(1)

_, err = step.Do(ctx)

g.Expect(err).To(g.BeNil())
g.Expect(status.MACAddress).To(g.Equal("FF:EE:DD:CC:BB:AA"))
g.Expect(status.MACAddress).To(g.Equal(reverseMACAddress))
}

func TestNewNetworkInterface_svcError(t *testing.T) {
Expand All @@ -197,22 +188,13 @@ func TestNewNetworkInterface_svcError(t *testing.T) {

g.RegisterTestingT(t)

vmid, _ := models.NewVMID("testvm", "testns")
iface := &models.NetworkInterface{
GuestDeviceName: "eth0",
AllowMetadataRequests: true,
GuestMAC: "AA:BB:CC:DD:EE:FF",
}
status := &models.NetworkInterfaceStatus{
HostDeviceName: "testns_testvm_tap",
Index: 0,
MACAddress: "AA:BB:CC:DD:EE:FF",
}
vmid, _ := models.NewVMID(vmName, nsName)
iface, status := fullNetworkInterface()
svc := mock.NewMockNetworkService(mockCtrl)
ctx := context.Background()

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(false, errors.ErrParentIfaceRequired).
Times(2)

Expand All @@ -233,42 +215,34 @@ func TestNewNetworkInterface_fillChangedStatus(t *testing.T) {

g.RegisterTestingT(t)

vmid, _ := models.NewVMID("testvm", "testns")
iface := &models.NetworkInterface{
GuestDeviceName: "eth0",
AllowMetadataRequests: true,
GuestMAC: "AA:BB:CC:DD:EE:FF",
Type: models.IfaceTypeMacvtap,
}
status := &models.NetworkInterfaceStatus{
HostDeviceName: "testns_testvm_tap",
Index: 0,
MACAddress: "AA:BB:CC:DD:EE:FF",
}
vmid, _ := models.NewVMID(vmName, nsName)
iface, status := fullNetworkInterface()
iface.Type = models.IfaceTypeMacvtap
svc := mock.NewMockNetworkService(mockCtrl)
ctx := context.Background()

step := network.NewNetworkInterface(vmid, iface, status, svc)

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_vtap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedMacvtapDeviceName)).
Return(true, nil).
Times(1)

svc.EXPECT().
IfaceDetails(gomock.Eq(ctx), gomock.Eq("testns_testvm_vtap")).
IfaceDetails(gomock.Eq(ctx), gomock.Eq(expectedMacvtapDeviceName)).
Return(&ports.IfaceDetails{
DeviceName: "testns_testvm_vtap",
Type: models.IfaceTypeTap,
MAC: "FF:EE:DD:CC:BB:AA",
DeviceName: expectedMacvtapDeviceName,
Type: models.IfaceTypeMacvtap,
MAC: reverseMACAddress,
Index: 0,
}, nil).
Times(1)

_, err := step.Do(ctx)

g.Expect(err).To(g.BeNil())
g.Expect(status.MACAddress).To(g.Equal("FF:EE:DD:CC:BB:AA"))
g.Expect(status.MACAddress).To(g.Equal(reverseMACAddress))
g.Expect(status.HostDeviceName).To(g.Equal(expectedMacvtapDeviceName))
}

func TestNewNetworkInterface_createError(t *testing.T) {
Expand All @@ -277,14 +251,14 @@ func TestNewNetworkInterface_createError(t *testing.T) {

g.RegisterTestingT(t)

var status *models.NetworkInterfaceStatus
vmid, _ := models.NewVMID("testvm", "testns")
iface := &models.NetworkInterface{GuestDeviceName: "eth0"}
vmid, _ := models.NewVMID(vmName, nsName)
status := &models.NetworkInterfaceStatus{}
iface := &models.NetworkInterface{GuestDeviceName: defaultEthDevice}
svc := mock.NewMockNetworkService(mockCtrl)
ctx := context.Background()

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Times(0)

step := network.NewNetworkInterface(vmid, iface, status, svc)
Expand All @@ -294,13 +268,13 @@ func TestNewNetworkInterface_createError(t *testing.T) {
g.Expect(shouldDo).To(g.BeTrue())

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(false, nil).
Times(1)

svc.EXPECT().
IfaceCreate(gomock.Eq(ctx), gomock.Eq(ports.IfaceCreateInput{
DeviceName: "testns_testvm_tap",
DeviceName: expectedTapDeviceName,
})).
Return(nil, errors.ErrParentIfaceRequired).
Times(1)
Expand Down
26 changes: 13 additions & 13 deletions core/steps/network/interface_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ func TestDeleteNetworkInterface_doesNotExist(t *testing.T) {

g.RegisterTestingT(t)

vmid, _ := models.NewVMID("testvm", "testns")
vmid, _ := models.NewVMID(vmName, nsName)
iface := &models.NetworkInterface{}
svc := mock.NewMockNetworkService(mockCtrl)
ctx := context.Background()

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(false, nil).
Times(1)

Expand All @@ -37,7 +37,7 @@ func TestDeleteNetworkInterface_doesNotExist(t *testing.T) {
g.Expect(shouldDo).To(g.BeFalse())

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(false, nil).
Times(1)

Expand All @@ -52,13 +52,13 @@ func TestDeleteNetworkInterface_exists(t *testing.T) {

g.RegisterTestingT(t)

vmid, _ := models.NewVMID("testvm", "testns")
vmid, _ := models.NewVMID(vmName, nsName)
iface := &models.NetworkInterface{}
svc := mock.NewMockNetworkService(mockCtrl)
ctx := context.Background()

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(true, nil).
Times(1)

Expand All @@ -69,14 +69,14 @@ func TestDeleteNetworkInterface_exists(t *testing.T) {
g.Expect(shouldDo).To(g.BeTrue())

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(true, nil).
Times(1)

svc.EXPECT().
IfaceDelete(
gomock.Eq(ctx),
gomock.Eq(ports.DeleteIfaceInput{DeviceName: "testns_testvm_tap"}),
gomock.Eq(ports.DeleteIfaceInput{DeviceName: expectedTapDeviceName}),
).
Return(nil).
Times(1)
Expand All @@ -92,13 +92,13 @@ func TestDeleteNetworkInterface_exists_errorDeleting(t *testing.T) {

g.RegisterTestingT(t)

vmid, _ := models.NewVMID("testvm", "testns")
vmid, _ := models.NewVMID(vmName, nsName)
iface := &models.NetworkInterface{}
svc := mock.NewMockNetworkService(mockCtrl)
ctx := context.Background()

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(true, nil).
Times(1)

Expand All @@ -109,14 +109,14 @@ func TestDeleteNetworkInterface_exists_errorDeleting(t *testing.T) {
g.Expect(shouldDo).To(g.BeTrue())

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(true, nil).
Times(1)

svc.EXPECT().
IfaceDelete(
gomock.Eq(ctx),
gomock.Eq(ports.DeleteIfaceInput{DeviceName: "testns_testvm_tap"}),
gomock.Eq(ports.DeleteIfaceInput{DeviceName: expectedTapDeviceName}),
).
Return(netlink.LinkNotFoundError{}).
Times(1)
Expand All @@ -132,13 +132,13 @@ func TestDeleteNetworkInterface_IfaceExistsError(t *testing.T) {

g.RegisterTestingT(t)

vmid, _ := models.NewVMID("testvm", "testns")
vmid, _ := models.NewVMID(vmName, nsName)
iface := &models.NetworkInterface{}
svc := mock.NewMockNetworkService(mockCtrl)
ctx := context.Background()

svc.EXPECT().
IfaceExists(gomock.Eq(ctx), gomock.Eq("testns_testvm_tap")).
IfaceExists(gomock.Eq(ctx), gomock.Eq(expectedTapDeviceName)).
Return(false, errors.ErrParentIfaceRequired).
Times(2)

Expand Down
Loading

0 comments on commit c923a31

Please sign in to comment.