From 72c4a7b4969acbe5f8fd8dafbeb4481d9d856586 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Tue, 27 Apr 2021 22:40:39 +0000 Subject: [PATCH] Fix issues running libnetwork tests. libnetwork does different stuff depending on if you are running the tests in a container or not... without telling it we are in a container a bunch of the tests actually fail. Signed-off-by: Brian Goff --- Jenkinsfile | 8 +++---- hack/make/cross | 2 +- hack/test/unit | 4 ++-- libnetwork/bitseq/sequence_test.go | 14 +++++++------ libnetwork/cmd/proxy/network_proxy_test.go | 7 +++++++ libnetwork/drivers/remote/driver_test.go | 21 +++++++++++++++---- libnetwork/ipam/allocator_test.go | 12 ++++++----- libnetwork/ipams/remote/remote_test.go | 21 +++++++++++++++---- libnetwork/libnetwork_test.go | 21 +++++++------------ libnetwork/libnetwork_unix_test.go | 5 +++++ libnetwork/libnetwork_windows_test.go | 3 +++ ...vconf_test.go => resolvconf_linux_test.go} | 0 libnetwork/testutils/net.go | 9 ++++---- 13 files changed, 83 insertions(+), 44 deletions(-) create mode 100644 libnetwork/libnetwork_unix_test.go create mode 100644 libnetwork/libnetwork_windows_test.go rename libnetwork/resolvconf/{resolvconf_test.go => resolvconf_linux_test.go} (100%) diff --git a/Jenkinsfile b/Jenkinsfile index e8da834759..ca5680553b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -190,7 +190,7 @@ pipeline { -e VALIDATE_REPO=${GIT_URL} \ -e VALIDATE_BRANCH=${CHANGE_TARGET} \ docker:${GIT_COMMIT} \ - hack/test/unit" + hack/test/unit ''' } post { @@ -587,7 +587,7 @@ pipeline { -e VALIDATE_REPO=${GIT_URL} \ -e VALIDATE_BRANCH=${CHANGE_TARGET} \ docker:${GIT_COMMIT} \ - hack/test/unit" + hack/test/unit ''' } post { @@ -786,7 +786,7 @@ pipeline { -e VALIDATE_REPO=${GIT_URL} \ -e VALIDATE_BRANCH=${CHANGE_TARGET} \ docker:${GIT_COMMIT} \ - hack/test/unit" + hack/test/unit ''' } post { @@ -982,7 +982,7 @@ pipeline { -e VALIDATE_REPO=${GIT_URL} \ -e VALIDATE_BRANCH=${CHANGE_TARGET} \ docker:${GIT_COMMIT} \ - hack/test/unit" + hack/test/unit ''' } post { diff --git a/hack/make/cross b/hack/make/cross index ab9f0b7445..32f88b42c9 100644 --- a/hack/make/cross +++ b/hack/make/cross @@ -30,7 +30,7 @@ for platform in ${DOCKER_CROSSPLATFORMS}; do echo "Cross building: ${DEST}" mkdir -p "${DEST}" ABS_DEST="$(cd "${DEST}" && pwd -P)" - source "${MAKEDIR}/binary-daemon" + source "${MAKEDIR}/binary" source "${MAKEDIR}/cross-platform-dependent" ) diff --git a/hack/test/unit b/hack/test/unit index 10c2ab9ff5..fffbe31157 100755 --- a/hack/test/unit +++ b/hack/test/unit @@ -10,7 +10,7 @@ # # TESTDIRS='./pkg/term' hack/test/unit # -set -eu -o pipefail +set -eux -o pipefail BUILDFLAGS=(-tags 'netgo seccomp libdm_no_deferred_remove') TESTFLAGS+=" -test.timeout=${TIMEOUT:-5m}" @@ -30,4 +30,4 @@ gotestsum --format=standard-quiet --jsonfile=bundles/go-test-report.json --junit -coverprofile=bundles/profile.out \ -covermode=atomic \ ${TESTFLAGS} \ - ${pkg_list} + ${pkg_list} \ No newline at end of file diff --git a/libnetwork/bitseq/sequence_test.go b/libnetwork/bitseq/sequence_test.go index af551a541f..b791a608b1 100644 --- a/libnetwork/bitseq/sequence_test.go +++ b/libnetwork/bitseq/sequence_test.go @@ -4,17 +4,19 @@ import ( "fmt" "io/ioutil" "math/rand" + "os" + "path/filepath" "testing" "time" - "github.com/docker/libkv/store" - "github.com/docker/libkv/store/boltdb" "github.com/docker/docker/libnetwork/datastore" _ "github.com/docker/docker/libnetwork/testutils" + "github.com/docker/libkv/store" + "github.com/docker/libkv/store/boltdb" ) -const ( - defaultPrefix = "/tmp/libnetwork/test/bitseq" +var ( + defaultPrefix = filepath.Join(os.TempDir(), "libnetwork", "test", "bitseq") ) func init() { @@ -32,7 +34,7 @@ func randomLocalStore() (datastore.DataStore, error) { return datastore.NewDataStore(datastore.LocalScope, &datastore.ScopeCfg{ Client: datastore.ScopeClientCfg{ Provider: "boltdb", - Address: defaultPrefix + tmp.Name(), + Address: filepath.Join(defaultPrefix, filepath.Base(tmp.Name())), Config: &store.Config{ Bucket: "libnetwork", ConnectionTimeout: 3 * time.Second, @@ -937,7 +939,7 @@ func TestAllocateRandomDeallocate(t *testing.T) { numBlocks := uint32(8) numBits := int(numBlocks * blockLen) - hnd, err := NewHandle("bitseq-test/data/", ds, "test1", uint64(numBits)) + hnd, err := NewHandle(filepath.Join("bitseq", "test", "data"), ds, "test1", uint64(numBits)) if err != nil { t.Fatal(err) } diff --git a/libnetwork/cmd/proxy/network_proxy_test.go b/libnetwork/cmd/proxy/network_proxy_test.go index 9d70216b01..ec2ab77ccd 100644 --- a/libnetwork/cmd/proxy/network_proxy_test.go +++ b/libnetwork/cmd/proxy/network_proxy_test.go @@ -6,11 +6,14 @@ import ( "io" "io/ioutil" "net" + "runtime" "strings" "testing" "time" "github.com/ishidawataru/sctp" + "gotest.tools/v3/skip" + // this takes care of the incontainer flag _ "github.com/docker/docker/libnetwork/testutils" ) @@ -282,6 +285,8 @@ func TestUDPWriteError(t *testing.T) { } func TestSCTP4Proxy(t *testing.T) { + skip.If(t, runtime.GOOS == "windows", "sctp is not supported on windows") + backend := NewEchoServer(t, "sctp", "127.0.0.1:0", EchoServerOptions{}) defer backend.Close() backend.Run() @@ -295,6 +300,8 @@ func TestSCTP4Proxy(t *testing.T) { func TestSCTP6Proxy(t *testing.T) { t.Skip("Need to start CI docker with --ipv6") + skip.If(t, runtime.GOOS == "windows", "sctp is not supported on windows") + backend := NewEchoServer(t, "sctp", "[::1]:0", EchoServerOptions{}) defer backend.Close() backend.Run() diff --git a/libnetwork/drivers/remote/driver_test.go b/libnetwork/drivers/remote/driver_test.go index 22a1f595f0..dc098f98e9 100644 --- a/libnetwork/drivers/remote/driver_test.go +++ b/libnetwork/drivers/remote/driver_test.go @@ -11,14 +11,16 @@ import ( "net/http" "net/http/httptest" "os" + "path/filepath" + "runtime" "testing" - "github.com/docker/docker/pkg/plugins" "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/discoverapi" "github.com/docker/docker/libnetwork/driverapi" _ "github.com/docker/docker/libnetwork/testutils" "github.com/docker/docker/libnetwork/types" + "github.com/docker/docker/pkg/plugins" ) func decodeToMap(r *http.Request) (res map[string]interface{}, err error) { @@ -41,16 +43,27 @@ func handle(t *testing.T, mux *http.ServeMux, method string, h func(map[string]i } func setupPlugin(t *testing.T, name string, mux *http.ServeMux) func() { - if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil { + specPath := "/etc/docker/plugins" + if runtime.GOOS == "windows" { + specPath = filepath.Join(os.Getenv("programdata"), "docker", "plugins") + } + + if err := os.MkdirAll(specPath, 0755); err != nil { t.Fatal(err) } + defer func() { + if t.Failed() { + os.RemoveAll(specPath) + } + }() + server := httptest.NewServer(mux) if server == nil { t.Fatal("Failed to start an HTTP Server") } - if err := ioutil.WriteFile(fmt.Sprintf("/etc/docker/plugins/%s.spec", name), []byte(server.URL), 0644); err != nil { + if err := ioutil.WriteFile(filepath.Join(specPath, name+".spec"), []byte(server.URL), 0644); err != nil { t.Fatal(err) } @@ -60,7 +73,7 @@ func setupPlugin(t *testing.T, name string, mux *http.ServeMux) func() { }) return func() { - if err := os.RemoveAll("/etc/docker/plugins"); err != nil { + if err := os.RemoveAll(specPath); err != nil { t.Fatal(err) } server.Close() diff --git a/libnetwork/ipam/allocator_test.go b/libnetwork/ipam/allocator_test.go index 58d9dee4ac..d590f29ec6 100644 --- a/libnetwork/ipam/allocator_test.go +++ b/libnetwork/ipam/allocator_test.go @@ -7,24 +7,26 @@ import ( "io/ioutil" "math/rand" "net" + "os" + "path/filepath" "strconv" "sync" "testing" "time" - "github.com/docker/libkv/store" - "github.com/docker/libkv/store/boltdb" "github.com/docker/docker/libnetwork/bitseq" "github.com/docker/docker/libnetwork/datastore" "github.com/docker/docker/libnetwork/ipamapi" _ "github.com/docker/docker/libnetwork/testutils" "github.com/docker/docker/libnetwork/types" + "github.com/docker/libkv/store" + "github.com/docker/libkv/store/boltdb" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) -const ( - defaultPrefix = "/tmp/libnetwork/test/ipam" +var ( + defaultPrefix = filepath.Join(os.TempDir(), "libnetwork", "test", "ipam") ) func init() { @@ -46,7 +48,7 @@ func randomLocalStore(needStore bool) (datastore.DataStore, error) { return datastore.NewDataStore(datastore.LocalScope, &datastore.ScopeCfg{ Client: datastore.ScopeClientCfg{ Provider: "boltdb", - Address: defaultPrefix + tmp.Name(), + Address: filepath.Join(defaultPrefix, filepath.Base(tmp.Name())), Config: &store.Config{ Bucket: "libnetwork", ConnectionTimeout: 3 * time.Second, diff --git a/libnetwork/ipams/remote/remote_test.go b/libnetwork/ipams/remote/remote_test.go index f6c244687e..d99c7030b3 100644 --- a/libnetwork/ipams/remote/remote_test.go +++ b/libnetwork/ipams/remote/remote_test.go @@ -9,11 +9,13 @@ import ( "net/http" "net/http/httptest" "os" + "path/filepath" + "runtime" "testing" - "github.com/docker/docker/pkg/plugins" "github.com/docker/docker/libnetwork/ipamapi" _ "github.com/docker/docker/libnetwork/testutils" + "github.com/docker/docker/pkg/plugins" ) func decodeToMap(r *http.Request) (res map[string]interface{}, err error) { @@ -36,16 +38,27 @@ func handle(t *testing.T, mux *http.ServeMux, method string, h func(map[string]i } func setupPlugin(t *testing.T, name string, mux *http.ServeMux) func() { - if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil { + specPath := "/etc/docker/plugins" + if runtime.GOOS == "windows" { + specPath = filepath.Join(os.Getenv("programdata"), "docker", "plugins") + } + + if err := os.MkdirAll(specPath, 0755); err != nil { t.Fatal(err) } + defer func() { + if t.Failed() { + os.RemoveAll(specPath) + } + }() + server := httptest.NewServer(mux) if server == nil { t.Fatal("Failed to start an HTTP Server") } - if err := ioutil.WriteFile(fmt.Sprintf("/etc/docker/plugins/%s.spec", name), []byte(server.URL), 0644); err != nil { + if err := ioutil.WriteFile(filepath.Join(specPath, name+".spec"), []byte(server.URL), 0644); err != nil { t.Fatal(err) } @@ -55,7 +68,7 @@ func setupPlugin(t *testing.T, name string, mux *http.ServeMux) func() { }) return func() { - if err := os.RemoveAll("/etc/docker/plugins"); err != nil { + if err := os.RemoveAll(specPath); err != nil { t.Fatal(err) } server.Close() diff --git a/libnetwork/libnetwork_test.go b/libnetwork/libnetwork_test.go index 77e8f1ed8d..04a04aed5e 100644 --- a/libnetwork/libnetwork_test.go +++ b/libnetwork/libnetwork_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "os" + "path/filepath" "sync" "testing" @@ -1314,10 +1315,6 @@ func TestEndpointUpdateParent(t *testing.T) { } func TestInvalidRemoteDriver(t *testing.T) { - if !testutils.IsRunningInContainer() { - t.Skip("Skipping test when not running inside a Container") - } - mux := http.NewServeMux() server := httptest.NewServer(mux) if server == nil { @@ -1334,16 +1331,16 @@ func TestInvalidRemoteDriver(t *testing.T) { fmt.Fprintln(w, `{"Implements": ["InvalidDriver"]}`) }) - if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil { + if err := os.MkdirAll(specPath, 0755); err != nil { t.Fatal(err) } defer func() { - if err := os.RemoveAll("/etc/docker/plugins"); err != nil { + if err := os.RemoveAll(specPath); err != nil { t.Fatal(err) } }() - if err := ioutil.WriteFile("/etc/docker/plugins/invalid-network-driver.spec", []byte(server.URL), 0644); err != nil { + if err := ioutil.WriteFile(filepath.Join(specPath, "invalid-network-driver.spec"), []byte(server.URL), 0644); err != nil { t.Fatal(err) } @@ -1365,10 +1362,6 @@ func TestInvalidRemoteDriver(t *testing.T) { } func TestValidRemoteDriver(t *testing.T) { - if !testutils.IsRunningInContainer() { - t.Skip("Skipping test when not running inside a Container") - } - mux := http.NewServeMux() server := httptest.NewServer(mux) if server == nil { @@ -1397,16 +1390,16 @@ func TestValidRemoteDriver(t *testing.T) { fmt.Fprintf(w, "null") }) - if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil { + if err := os.MkdirAll(specPath, 0755); err != nil { t.Fatal(err) } defer func() { - if err := os.RemoveAll("/etc/docker/plugins"); err != nil { + if err := os.RemoveAll(specPath); err != nil { t.Fatal(err) } }() - if err := ioutil.WriteFile("/etc/docker/plugins/valid-network-driver.spec", []byte(server.URL), 0644); err != nil { + if err := ioutil.WriteFile(filepath.Join(specPath, "valid-network-driver.spec"), []byte(server.URL), 0644); err != nil { t.Fatal(err) } diff --git a/libnetwork/libnetwork_unix_test.go b/libnetwork/libnetwork_unix_test.go new file mode 100644 index 0000000000..9acb79add9 --- /dev/null +++ b/libnetwork/libnetwork_unix_test.go @@ -0,0 +1,5 @@ +// +build !windows + +package libnetwork_test + +var specPath = "/etc/docker/plugins" \ No newline at end of file diff --git a/libnetwork/libnetwork_windows_test.go b/libnetwork/libnetwork_windows_test.go new file mode 100644 index 0000000000..3545028179 --- /dev/null +++ b/libnetwork/libnetwork_windows_test.go @@ -0,0 +1,3 @@ +package libnetwork_test + +var specPath = filepath.Join(os.Getenv("programdata"), "docker", "plugins") diff --git a/libnetwork/resolvconf/resolvconf_test.go b/libnetwork/resolvconf/resolvconf_linux_test.go similarity index 100% rename from libnetwork/resolvconf/resolvconf_test.go rename to libnetwork/resolvconf/resolvconf_linux_test.go diff --git a/libnetwork/testutils/net.go b/libnetwork/testutils/net.go index d86f3a9951..65572ce2eb 100644 --- a/libnetwork/testutils/net.go +++ b/libnetwork/testutils/net.go @@ -1,10 +1,11 @@ package testutils -import "flag" - -var runningInContainer = flag.Bool("incontainer", false, "Indicates if the test is running in a container") +import ( + "os" +) // IsRunningInContainer returns whether the test is running inside a container. func IsRunningInContainer() bool { - return (*runningInContainer) + _, err := os.Stat("/.dockerenv") + return err == nil }