Skip to content

Commit

Permalink
ut: add unit test (#4702)
Browse files Browse the repository at this point in the history
* add unit test for GetStaticMac

---------

Signed-off-by: zcq98 <[email protected]>
  • Loading branch information
zcq98 authored Nov 6, 2024
1 parent 5734a27 commit 74b5c49
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 49 deletions.
10 changes: 10 additions & 0 deletions pkg/ipam/subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,3 +1136,13 @@ func TestPopPodNic(t *testing.T) {
subnet.popPodNic(podName, nicName)
require.Equal(t, 0, len(subnet.PodToNicList[podName]))
}

func TestGetStaticMac(t *testing.T) {
subnet, err := NewSubnet("v4Subnet", "10.0.0.0/24", nil)
require.NoError(t, err)
require.NotNil(t, subnet)
podName := "pod1.default"
nicName := "nic1"
err = subnet.GetStaticMac(podName, nicName, "", false)
require.NoError(t, err)
}
33 changes: 29 additions & 4 deletions pkg/ovs/ovn-nb-acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func (suite *OvnClientTestSuite) testCreateSgDenyAllACL() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

t.Run("normal create sg deny all acl", func(t *testing.T) {
sgName := "test_create_deny_all_acl_pg"
Expand Down Expand Up @@ -617,6 +617,11 @@ func (suite *OvnClientTestSuite) testCreateSgDenyAllACL() {
require.Error(t, err)
})

t.Run("should print log err when sg name is empty", func(t *testing.T) {
err := nbClient.CreateSgDenyAllACL("")
require.ErrorContains(t, err, "the port group name or logical switch name is required")
})

t.Run("fail nb client should log err", func(t *testing.T) {
sgName := "test_failed_client"
err := failedNbClient.CreateSgDenyAllACL(sgName)
Expand Down Expand Up @@ -719,6 +724,11 @@ func (suite *OvnClientTestSuite) testCreateSgBaseACL() {
match = fmt.Sprintf("%s == @%s && ip.proto == 112", portDirection, pgName)
expect(pg, match, ovnnb.ACLDirectionFromLport)
})

t.Run("should return no err when sg name is empty", func(t *testing.T) {
err := nbClient.CreateSgBaseACL("", ovnnb.ACLDirectionFromLport)
require.NoError(t, err)
})
}

func (suite *OvnClientTestSuite) testUpdateSgACL() {
Expand Down Expand Up @@ -832,6 +842,16 @@ func (suite *OvnClientTestSuite) testUpdateSgACL() {
require.Equal(t, expect, rulACL)
require.Contains(t, pg.ACLs, rulACL.UUID)
})

t.Run("should print log err when sg name is empty", func(t *testing.T) {
sg := &kubeovnv1.SecurityGroup{
ObjectMeta: metav1.ObjectMeta{
Name: "",
},
}
err = nbClient.UpdateSgACL(sg, ovnnb.ACLDirectionToLport)
require.ErrorContains(t, err, "the port group name or logical switch name is required")
})
}

func (suite *OvnClientTestSuite) testUpdateLogicalSwitchACL() {
Expand Down Expand Up @@ -1130,6 +1150,11 @@ func (suite *OvnClientTestSuite) testSetLogicalSwitchPrivate() {
}
}
})

t.Run("should print log err when ls name is empty", func(t *testing.T) {
err := nbClient.SetLogicalSwitchPrivate("", cidrBlock, nodeSwitchCidrBlock, allowSubnets)
require.ErrorContains(t, err, "the port group name or logical switch name is required")
})
}

func (suite *OvnClientTestSuite) testNewSgRuleACL() {
Expand Down Expand Up @@ -1356,7 +1381,7 @@ func (suite *OvnClientTestSuite) testDeleteAcls() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
pgName := "test-del-acls-pg"
lsName := "test-del-acls-ls"
matchPrefix := "outport == @ovn.sg.test_del_acl_pg && ip"
Expand Down Expand Up @@ -2450,7 +2475,7 @@ func (suite *OvnClientTestSuite) testCreateBareACL() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

t.Run("create bare ACL successfully", func(t *testing.T) {
err := nbClient.CreateBareACL("test-parent", "from-lport", "1000", "ip4.src == 10.0.0.1", "allow")
Expand Down Expand Up @@ -2529,7 +2554,7 @@ func (suite *OvnClientTestSuite) testUpdateACL() {
t := suite.T()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

// nbClient := suite.ovnNBClient
pgName := "test_update_acl_pg"
Expand Down
14 changes: 14 additions & 0 deletions pkg/ovs/ovn-nb-address_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ func (suite *OvnClientTestSuite) testAddressSetUpdateAddress() {
require.Error(t, err)
require.Contains(t, err.Error(), "get address set")
})

t.Run("update address set with invalid address", func(t *testing.T) {
err := nbClient.AddressSetUpdateAddress(asName, "192.168.1.1/xx")
require.NoError(t, err)
})
}

func (suite *OvnClientTestSuite) testDeleteAddressSet() {
Expand Down Expand Up @@ -327,6 +332,15 @@ func (suite *OvnClientTestSuite) testUpdateAddressSet() {

nbClient := suite.ovnNBClient

as := newAddressSet("test_update_as", map[string]string{
sgKey: "test-sg",
})

t.Run("update with nil address set", func(t *testing.T) {
err := nbClient.UpdateAddressSet(as, nil)
require.Error(t, err)
})

t.Run("update with nil address set", func(t *testing.T) {
err := nbClient.UpdateAddressSet(nil)
require.Error(t, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/ovs/ovn-nb-bfd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (suite *OvnClientTestSuite) testListBFD() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

lrpName := "test-list-bfd"
dstIP1 := "192.168.124.2"
Expand Down Expand Up @@ -116,7 +116,7 @@ func (suite *OvnClientTestSuite) testDeleteBFD() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
lrpName := "test-del-bfd"
dstIP1 := "192.168.124.4"
dstIP2 := "192.168.124.5"
Expand Down Expand Up @@ -175,7 +175,7 @@ func (suite *OvnClientTestSuite) testListDownBFDs() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
lrpName := "test-list-down-bfd"
dstIP1 := "192.168.124.6"
dstIP2 := "192.168.124.7"
Expand Down Expand Up @@ -501,7 +501,7 @@ func (suite *OvnClientTestSuite) testBfdUpdateL3HAHandler() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

t.Run("BFD status change with wrong table", func(t *testing.T) {
t.Parallel()
Expand Down
10 changes: 5 additions & 5 deletions pkg/ovs/ovn-nb-logical_router_port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (suite *OvnClientTestSuite) testCreatePeerRouterPort() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
localLrName := "test-create-peer-lr-local"
remoteLrName := "test-create-peer-lr-remote"
localRouterPort := fmt.Sprintf("%s-%s", localLrName, remoteLrName)
Expand Down Expand Up @@ -82,7 +82,7 @@ func (suite *OvnClientTestSuite) testUpdateLogicalRouterPortRA() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
lrpName := "test-update-ra-lrp"
lrName := "test-update-ra-lr"

Expand Down Expand Up @@ -225,7 +225,7 @@ func (suite *OvnClientTestSuite) testCreateLogicalRouterPort() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
lrName := "test-create-lrp-lr"

err := nbClient.CreateLogicalRouter(lrName)
Expand Down Expand Up @@ -498,7 +498,7 @@ func (suite *OvnClientTestSuite) testCreateLogicalRouterPortOp() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
lrpName := "test-create-op-lrp"
lrName := "test-create-op-lr"

Expand Down Expand Up @@ -795,7 +795,7 @@ func (suite *OvnClientTestSuite) testAddLogicalRouterPort() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
lrName := "test-add-lrp-lr"

err := nbClient.CreateLogicalRouter(lrName)
Expand Down
26 changes: 13 additions & 13 deletions pkg/ovs/ovn-nb-logical_switch_port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (suite *OvnClientTestSuite) testCreateLogicalSwitchPort() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
lsName := "test-create-lsp-ls"
ips := "10.244.0.37,fc00::af4:25"
mac := "00:00:00:AB:B4:65"
Expand Down Expand Up @@ -308,7 +308,7 @@ func (suite *OvnClientTestSuite) testCreateVirtualLogicalSwitchPorts() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
lsName := "test-create-virtual-port-ls"
vips := []string{"192.168.33.10", "192.168.33.12"}

Expand Down Expand Up @@ -354,7 +354,7 @@ func (suite *OvnClientTestSuite) testCreateVirtualLogicalSwitchPort() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
lspName := "test-create-one-virtual-port-lsp"
lsName := "test-create-one-virtual-port-ls"
lsName2 := "test-create-one-virtual-port-ls2"
Expand Down Expand Up @@ -439,7 +439,7 @@ func (suite *OvnClientTestSuite) testSetLogicalSwitchPortVirtualParents() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

lsName := "test-update-port-virt-parents-ls"
ips := []string{"192.168.211.31", "192.168.211.32"}
Expand Down Expand Up @@ -497,7 +497,7 @@ func (suite *OvnClientTestSuite) testSetVirtualLogicalSwitchPortVirtualParents()
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

lsName := "test-update-virtual-port-virt-parents-ls"
ip := "192.168.211.31"
Expand Down Expand Up @@ -565,7 +565,7 @@ func (suite *OvnClientTestSuite) testSetLogicalSwitchPortArpProxy() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
lsName := "test-set-lsp-arp-proxy-ls"
ips := "10.244.0.37,fc00::af4:25"
mac := "00:00:00:AB:B4:65"
Expand Down Expand Up @@ -1171,7 +1171,7 @@ func (suite *OvnClientTestSuite) testEnablePortLayer2forward() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
lsName := "test-enable-port-l2-ls"
lspName := "test-enable-port-l2-lsp"
ns := "test-enable-port-l2-ns"
Expand Down Expand Up @@ -1313,7 +1313,7 @@ func (suite *OvnClientTestSuite) testUpdateLogicalSwitchPort() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

lsName := "test-update-lsp-ls"
lspName := "test-update-lsp-lsp"
Expand Down Expand Up @@ -1392,7 +1392,7 @@ func (suite *OvnClientTestSuite) testDeleteLogicalSwitchPort() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

lspName := "test-delete-port-lsp"
lsName := "test-delete-port-ls"
Expand Down Expand Up @@ -1438,7 +1438,7 @@ func (suite *OvnClientTestSuite) testDeleteLogicalSwitchPorts() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

lsName := "test-delete-ports-ls"
lspName1 := "test-delete-port-lsp1"
Expand Down Expand Up @@ -1489,7 +1489,7 @@ func (suite *OvnClientTestSuite) testListNormalLogicalSwitchPorts() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

lsName := "a-normal-ls"
lspName := "a-lsp-listed"
Expand Down Expand Up @@ -1584,7 +1584,7 @@ func (suite *OvnClientTestSuite) testListLogicalSwitchPorts() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

lsName := "test-list-lsp-ls"
err := nbClient.CreateBareLogicalSwitch(lsName)
Expand Down Expand Up @@ -1762,7 +1762,7 @@ func (suite *OvnClientTestSuite) testDeleteLogicalSwitchPortOp() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

lspName := "test-del-op-lsp"
lsName := "test-del-op-ls"
Expand Down
8 changes: 4 additions & 4 deletions pkg/ovs/ovn-nb-logical_switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (suite *OvnClientTestSuite) testLogicalSwitchAddPort() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

t.Run("add port to logical switch", func(t *testing.T) {
lsName := "test-add-port-ls"
Expand Down Expand Up @@ -162,7 +162,7 @@ func (suite *OvnClientTestSuite) testLogicalSwitchDelPort() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

lsName := "test-del-port-ls"
lspName := "test-del-port-lsp"
Expand Down Expand Up @@ -306,7 +306,7 @@ func (suite *OvnClientTestSuite) testDeleteLogicalSwitch() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient
name := "test-delete-ls"

t.Run("no err when delete existent logical switch", func(t *testing.T) {
Expand Down Expand Up @@ -724,7 +724,7 @@ func (suite *OvnClientTestSuite) testCreateBareLogicalSwitch() {
t.Parallel()

nbClient := suite.ovnNBClient
failedNbClient := suite.faiedOvnNBClient
failedNbClient := suite.failedOvnNBClient

lsName := "test-create-bare-ls"

Expand Down
Loading

0 comments on commit 74b5c49

Please sign in to comment.