From 48aa2f4eef3ee0c49eff18dc0b96d8d88c964a07 Mon Sep 17 00:00:00 2001 From: Alina Sudakov Date: Wed, 14 Jun 2023 17:02:42 +0300 Subject: [PATCH] Fix race conditions in DHCP test The test named "correctly handles multiple DELs for the same container" in the ipam/dhcp package experiences race conditions when multiple goroutines concurrently access and modify the Args struct (of type CmdArgs). To address these issues, a copy of the CmdArgs struct is now created in each function to eliminate data races. Also, the test-linux.sh and test-windows.sh scripts have been updated to include the '-race' flag, enabling race detection during testing. This change helps prevent future race conditions by activating the Go race detector. Signed-off-by: Alina Sudakov --- plugins/ipam/dhcp/dhcp_test.go | 12 ++++++++++-- test_linux.sh | 4 ++-- test_windows.sh | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/plugins/ipam/dhcp/dhcp_test.go b/plugins/ipam/dhcp/dhcp_test.go index 6695fe17..9c77a744 100644 --- a/plugins/ipam/dhcp/dhcp_test.go +++ b/plugins/ipam/dhcp/dhcp_test.go @@ -332,9 +332,17 @@ var _ = Describe("DHCP Operations", func() { started.Done() started.Wait() - err = originalNS.Do(func(ns.NetNS) error { + err := originalNS.Do(func(ns.NetNS) error { return testutils.CmdDelWithArgs(args, func() error { - return cmdDel(args) + copiedArgs := &skel.CmdArgs{ + ContainerID: args.ContainerID, + Netns: args.Netns, + IfName: args.IfName, + StdinData: args.StdinData, + Path: args.Path, + Args: args.Args, + } + return cmdDel(copiedArgs) }) }) Expect(err).NotTo(HaveOccurred()) diff --git a/test_linux.sh b/test_linux.sh index e2003f7a..ec9fe85d 100755 --- a/test_linux.sh +++ b/test_linux.sh @@ -15,7 +15,7 @@ source ./build_linux.sh echo "Running tests" function testrun { - sudo -E bash -c "umask 0; PATH=${GOPATH}/bin:$(pwd)/bin:${PATH} go test $@" + sudo -E bash -c "umask 0; PATH=${GOPATH}/bin:$(pwd)/bin:${PATH} go test -race $@" } COVERALLS=${COVERALLS:-""} @@ -31,7 +31,7 @@ PKG=${PKG:-$(go list ./... | xargs echo)} i=0 for t in ${PKG}; do if [ -n "${COVERALLS}" ]; then - COVERFLAGS="-covermode set -coverprofile ${i}.coverprofile" + COVERFLAGS="-covermode atomic -coverprofile ${i}.coverprofile" fi echo "${t}" testrun "${COVERFLAGS:-""} ${t}" diff --git a/test_windows.sh b/test_windows.sh index a8dfa63a..b74fb570 100755 --- a/test_windows.sh +++ b/test_windows.sh @@ -17,4 +17,4 @@ for d in $PLUGINS; do done echo "testing packages $PKGS" -go test -v $PKGS -ginkgo.randomizeAllSpecs -ginkgo.failOnPending -ginkgo.progress +go test -race -v $PKGS -ginkgo.randomizeAllSpecs -ginkgo.failOnPending -ginkgo.progress