Skip to content

Commit

Permalink
ut: failed sb case
Browse files Browse the repository at this point in the history
Signed-off-by: bobz965 <[email protected]>
  • Loading branch information
bobz965 committed Dec 24, 2024
1 parent 85542c2 commit e1eeabb
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/ovs/ovn-nb-suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type OvnClientTestSuite struct {
ovnSBClient *OVNSbClient

failedOvnNBClient *OVNNbClient
failedOvnSBClient *OVNSbClient
ovnLegacyClient *LegacyClient

ovsSocket string
Expand All @@ -40,6 +41,10 @@ func emptyNbDatabaseModel() (model.ClientDBModel, error) {
return model.NewClientDBModel("OVN_Northbound", map[string]model.Model{})
}

func emptySbDatabaseModel() (model.ClientDBModel, error) {
return model.NewClientDBModel("OVN_Southbound", map[string]model.Model{})
}

func (suite *OvnClientTestSuite) SetupSuite() {
fmt.Println("set up ovn client test suite")
// setup ovn nb client schema
Expand Down Expand Up @@ -73,6 +78,21 @@ func (suite *OvnClientTestSuite) SetupSuite() {

// setup ovn sb client
sbClientSchema := ovnsb.Schema()

// setup failed case ovn sb client
emptySbDBModel, err := emptyNbDatabaseModel()
require.NoError(suite.T(), err)

fakeSBServer, sbSock1 := newOVSDBServer(suite.T(), "fake-sb", emptySbDBModel, sbClientSchema)
sbEndpoint1 := fmt.Sprintf("unix:%s", sbSock1)
require.FileExists(suite.T(), sbSock1)
failedOvnSBClient, err := newOvnSbClient(suite.T(), sbEndpoint1, 10)
require.NoError(suite.T(), err)
suite.failedOvnSBClient = failedOvnSBClient
// close the server to simulate the failed case
fakeSBServer.Close()
require.NoFileExists(suite.T(), sbSock1)

sbClientDBModel, err := ovnsb.FullDatabaseModel()
require.NoError(suite.T(), err)

Expand Down
73 changes: 73 additions & 0 deletions pkg/ovs/ovn-sb-chassis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (suite *OvnClientTestSuite) testUpdateChassis() {
t.Parallel()

sbClient := suite.ovnSBClient
failSBClient := suite.failedOvnSBClient

t.Cleanup(func() {
err := sbClient.DeleteChassis("chassis-name-3")
Expand Down Expand Up @@ -140,13 +141,21 @@ func (suite *OvnClientTestSuite) testUpdateChassis() {
require.Error(t, err)
require.Contains(t, err.Error(), "failed to generate update operations for chassis")
})

t.Run("test failed client update chassis with valid fields", func(t *testing.T) {
updatedHostname := "updated-host-name"
chassis.Hostname = updatedHostname
err := failSBClient.UpdateChassis(chassis, &chassis.Hostname)
require.Error(t, err)
})
}

func (suite *OvnClientTestSuite) testListChassis() {
t := suite.T()
t.Parallel()

sbClient := suite.ovnSBClient
failSBClient := suite.failedOvnSBClient

t.Cleanup(func() {
err := sbClient.DeleteChassis("chassis-1")
Expand Down Expand Up @@ -197,13 +206,19 @@ func (suite *OvnClientTestSuite) testListChassis() {
require.NotContains(t, names, "chassis-1")
require.NotContains(t, names, "chassis-2")
})

t.Run("test failed client list chassis with no entries", func(t *testing.T) {
_, err := failSBClient.ListChassis()
require.Error(t, err)
})
}

func (suite *OvnClientTestSuite) testGetChassisByHost() {
t := suite.T()
t.Parallel()

sbClient := suite.ovnSBClient
failSBClient := suite.failedOvnSBClient

t.Cleanup(func() {
err := sbClient.DeleteChassis("chassis-3x")
Expand Down Expand Up @@ -293,13 +308,20 @@ func (suite *OvnClientTestSuite) testGetChassisByHost() {
err = sbClient.DeleteChassis("chassis-6x-fake")
require.NoError(t, err)
})

t.Run("test failed client get chassis by host with non-existent hostname", func(t *testing.T) {
_, err := failSBClient.GetChassisByHost("non-existent-host")
require.Error(t, err)
})

}

func (suite *OvnClientTestSuite) testDeleteChassisByHost() {
t := suite.T()
t.Parallel()

sbClient := suite.ovnSBClient
failSBClient := suite.failedOvnSBClient

t.Cleanup(func() {
err := sbClient.DeleteChassis("chassis-node1-1")
Expand Down Expand Up @@ -355,13 +377,24 @@ func (suite *OvnClientTestSuite) testDeleteChassisByHost() {
err := sbClient.DeleteChassisByHost("node3")
require.ErrorContains(t, err, "chassis name is empty")
})

t.Run("test failed client delete chassis by non-existent host", func(t *testing.T) {
err := failSBClient.DeleteChassisByHost("non-existent-node")
require.Error(t, err)
})

t.Run("test failed client delete chassis by empty chassis name with ExternalIDs", func(t *testing.T) {
err := failSBClient.DeleteChassisByHost("node3")
require.Error(t, err)
})
}

func (suite *OvnClientTestSuite) testUpdateChassisTag() {
t := suite.T()
t.Parallel()

sbClient := suite.ovnSBClient
failSBClient := suite.failedOvnSBClient

t.Cleanup(func() {
err := sbClient.DeleteChassis("chassis-update-tag")
Expand Down Expand Up @@ -421,13 +454,48 @@ func (suite *OvnClientTestSuite) testUpdateChassisTag() {
require.Equal(t, "", updatedChassis.ExternalIDs["node"])
require.Equal(t, util.CniTypeName, updatedChassis.ExternalIDs["vendor"])
})

t.Run("test failed client update chassis tag with empty node name", func(t *testing.T) {
err := failSBClient.UpdateChassisTag("chassis-update-tag", "")
require.Error(t, err)
})

t.Run("test failed client update chassis tag with new node name", func(t *testing.T) {
err := failSBClient.UpdateChassisTag("chassis-update-tag", "new-node-name")
require.Error(t, err)
})

t.Run("test failed client update chassis tag with existing ExternalIDs", func(t *testing.T) {
chassis.ExternalIDs = map[string]string{"existing": "value"}
err := failSBClient.UpdateChassis(chassis, &chassis.ExternalIDs)
require.Error(t, err)

err = failSBClient.UpdateChassisTag("chassis-update-tag", "another-node-name")
require.Error(t, err)
})

t.Run("test failed client update chassis tag with non-existent chassis", func(t *testing.T) {
err := failSBClient.UpdateChassisTag("non-existent-chassis", "node-name")
require.Error(t, err)
})

t.Run("test failed client update chassis tag with empty chassis name", func(t *testing.T) {
err := failSBClient.UpdateChassisTag("", "node-name")
require.Error(t, err)
})

t.Run("test failed client update chassis tag with empty node name", func(t *testing.T) {
err := failSBClient.UpdateChassisTag("chassis-update-tag", "")
require.Error(t, err)
})
}

func (suite *OvnClientTestSuite) testGetKubeOvnChassisses() {
t := suite.T()
t.Parallel()

sbClient := suite.ovnSBClient
failSBClient := suite.failedOvnSBClient

kubeOvnChassis1 := newChassis(0, "host-1", "kube-ovn-chassis-1", nil, nil, nil, map[string]string{"vendor": util.CniTypeName}, nil)
kubeOvnChassis2 := newChassis(0, "host-2", "kube-ovn-chassis-2", nil, nil, nil, map[string]string{"vendor": util.CniTypeName}, nil)
Expand Down Expand Up @@ -469,6 +537,11 @@ func (suite *OvnClientTestSuite) testGetKubeOvnChassisses() {
require.False(t, names["non-kube-ovn-chassis"])
require.True(t, names["mixed-chassis"])

// failed client test
chassisList, err = failSBClient.GetKubeOvnChassisses()
require.Error(t, err)
require.Nil(t, *chassisList)

t.Cleanup(func() {
err := sbClient.DeleteChassis("kube-ovn-chassis-1")
require.NoError(t, err)
Expand Down

0 comments on commit e1eeabb

Please sign in to comment.