Skip to content

Commit

Permalink
ut: fix use of closed network connection (#4698)
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <[email protected]>
  • Loading branch information
zhangzujian authored Nov 5, 2024
1 parent d71f0a2 commit 63d7158
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
8 changes: 4 additions & 4 deletions pkg/daemon/ovs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,23 +735,23 @@ func (c *Controller) checkNodeGwNicInNs(nodeExtIP, ip, gw string, gwNS ns.NetNS)
return ns.WithNetNSPath(gwNS.Path(), func(_ ns.NetNS) error {
err = waitNetworkReady(util.NodeGwNic, ip, gw, true, true, 3, nil)
if err == nil {
if output, err := exec.Command("sh", "-c", "bfdd-control status").CombinedOutput(); err != nil {
if output, err := exec.Command("bfdd-control", "status").CombinedOutput(); err != nil {
err := fmt.Errorf("failed to get bfdd status, %w, %s", err, output)
klog.Error(err)
return err
}
for _, eip := range ovnEips {
if eip.Status.Ready {
// #nosec G204
cmd := exec.Command("sh", "-c", fmt.Sprintf("bfdd-control status remote %s local %s", eip.Spec.V4Ip, nodeExtIP))
cmd := exec.Command("bfdd-control", "status", "remote", eip.Spec.V4Ip, "local", nodeExtIP)
var outb bytes.Buffer
cmd.Stdout = &outb
if err := cmd.Run(); err == nil {
out := outb.String()
klog.V(3).Info(out)
if strings.Contains(out, "No session") {
// not exist
cmd = exec.Command("sh", "-c", fmt.Sprintf("bfdd-control allow %s", eip.Spec.V4Ip)) // #nosec G204
cmd = exec.Command("bfdd-control", "allow", eip.Spec.V4Ip) // #nosec G204
if err := cmd.Run(); err != nil {
err := fmt.Errorf("failed to add lrp %s ip %s into bfd listening list, %w", eip.Name, eip.Status.V4Ip, err)
klog.Error(err)
Expand Down Expand Up @@ -852,7 +852,7 @@ func configureNodeGwNic(portName, ip, gw string, macAddr net.HardwareAddr, mtu i
if err != nil {
return fmt.Errorf("failed to configure gateway: %w", err)
}
cmd := exec.Command("sh", "-c", "bfdd-beacon --listen=0.0.0.0")
cmd := exec.Command("bfdd-beacon", "--listen=0.0.0.0")
if err := cmd.Run(); err != nil {
err := fmt.Errorf("failed to get start bfd listen, %w", err)
klog.Error(err)
Expand Down
5 changes: 1 addition & 4 deletions pkg/ovs/ovn-nb-logical_router_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,8 @@ func (c *OVNNbClient) LogicalRouterPortOp(lrpName string, mutationsFunc ...func(
}

mutations := make([]model.Mutation, 0, len(mutationsFunc))

for _, f := range mutationsFunc {
mutation := f(lrp)

if mutation != nil {
if mutation := f(lrp); mutation != nil {
mutations = append(mutations, *mutation)
}
}
Expand Down
19 changes: 11 additions & 8 deletions pkg/ovs/ovn-nb-suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"math/rand/v2"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -48,21 +47,21 @@ func (suite *OvnClientTestSuite) SetupSuite() {
emptyNbDBModel, err := emptyNbDatabaseModel()
require.NoError(suite.T(), err)

server1, nbSock1 := newOVSDBServer(suite.T(), emptyNbDBModel, nbClientSchema)
fakeNBServer, nbSock1 := newOVSDBServer(suite.T(), "fake-nb", emptyNbDBModel, nbClientSchema)
nbEndpoint1 := fmt.Sprintf("unix:%s", nbSock1)
require.FileExists(suite.T(), nbSock1)
faiedOvnNBClient, err := newOvnNbClient(suite.T(), nbEndpoint1, 10)
require.NoError(suite.T(), err)
suite.faiedOvnNBClient = faiedOvnNBClient
// close the server to simulate the failed case
server1.Close()
fakeNBServer.Close()
require.NoFileExists(suite.T(), nbSock1)

// setup ovn nb client
nbClientDBModel, err := ovnnb.FullDatabaseModel()
require.NoError(suite.T(), err)

_, nbSock := newOVSDBServer(suite.T(), nbClientDBModel, nbClientSchema)
_, nbSock := newOVSDBServer(suite.T(), "nb", nbClientDBModel, nbClientSchema)
nbEndpoint := fmt.Sprintf("unix:%s", nbSock)
require.FileExists(suite.T(), nbSock)

Expand All @@ -75,7 +74,7 @@ func (suite *OvnClientTestSuite) SetupSuite() {
sbClientDBModel, err := ovnsb.FullDatabaseModel()
require.NoError(suite.T(), err)

_, sbSock := newOVSDBServer(suite.T(), sbClientDBModel, sbClientSchema)
_, sbSock := newOVSDBServer(suite.T(), "sb", sbClientDBModel, sbClientSchema)
sbEndpoint := fmt.Sprintf("unix:%s", sbSock)
require.FileExists(suite.T(), sbSock)

Expand Down Expand Up @@ -1231,7 +1230,7 @@ func Test_scratch(t *testing.T) {
require.NoError(t, err)
}

func newOVSDBServer(t *testing.T, dbModel model.ClientDBModel, schema ovsdb.DatabaseSchema) (*server.OvsdbServer, string) {
func newOVSDBServer(t *testing.T, name string, dbModel model.ClientDBModel, schema ovsdb.DatabaseSchema) (*server.OvsdbServer, string) {
serverDBModel, err := serverdb.FullDatabaseModel()
require.NoError(t, err)
serverSchema := serverdb.Schema()
Expand All @@ -1250,7 +1249,7 @@ func newOVSDBServer(t *testing.T, dbModel model.ClientDBModel, schema ovsdb.Data
server, err := server.NewOvsdbServer(db, dbMod, svrMod)
require.NoError(t, err)

tmpfile := fmt.Sprintf("/tmp/ovsdb-%d.sock", rand.IntN(10000))
tmpfile := fmt.Sprintf("/tmp/ovsdb-%s.sock", name)
t.Cleanup(func() {
os.Remove(tmpfile)
})
Expand All @@ -1259,7 +1258,11 @@ func newOVSDBServer(t *testing.T, dbModel model.ClientDBModel, schema ovsdb.Data
t.Error(err)
}
}()
t.Cleanup(server.Close)
t.Cleanup(func() {
if server.Ready() {
server.Close()
}
})
require.Eventually(t, func() bool {
return server.Ready()
}, 1*time.Second, 10*time.Millisecond)
Expand Down
10 changes: 5 additions & 5 deletions pkg/ovs/ovn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"testing"
"time"

"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnsb"

"github.com/ovn-org/libovsdb/ovsdb"
"github.com/stretchr/testify/require"

"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnsb"
)

func TestNewLegacyClient(t *testing.T) {
Expand Down Expand Up @@ -59,7 +59,7 @@ func (suite *OvnClientTestSuite) testNewOvnNbClient() {
clientDBModel, err := ovnnb.FullDatabaseModel()
require.NoError(suite.T(), err)

_, sock := newOVSDBServer(suite.T(), clientDBModel, clientSchema)
_, sock := newOVSDBServer(suite.T(), "test-nb-client", clientDBModel, clientSchema)
endpoint := fmt.Sprintf("unix:%s", sock)
require.FileExists(suite.T(), sock)

Expand Down Expand Up @@ -88,7 +88,7 @@ func (suite *OvnClientTestSuite) testNewOvnSbClient() {
clientDBModel, err := ovnsb.FullDatabaseModel()
require.NoError(suite.T(), err)

_, sock := newOVSDBServer(suite.T(), clientDBModel, clientSchema)
_, sock := newOVSDBServer(suite.T(), "test-sb-client", clientDBModel, clientSchema)
endpoint := fmt.Sprintf("unix:%s", sock)
require.FileExists(suite.T(), sock)

Expand Down

0 comments on commit 63d7158

Please sign in to comment.