Skip to content

Commit

Permalink
Fix issues running libnetwork tests.
Browse files Browse the repository at this point in the history
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 <cpuguy83@gmail.com>
  • Loading branch information
cpuguy83 committed Jun 1, 2021
1 parent 4da0dc9 commit 72c4a7b
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 44 deletions.
8 changes: 4 additions & 4 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion hack/make/cross
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
4 changes: 2 additions & 2 deletions hack/test/unit
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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}
14 changes: 8 additions & 6 deletions libnetwork/bitseq/sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions libnetwork/cmd/proxy/network_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
21 changes: 17 additions & 4 deletions libnetwork/drivers/remote/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}

Expand All @@ -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()
Expand Down
12 changes: 7 additions & 5 deletions libnetwork/ipam/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
Expand Down
21 changes: 17 additions & 4 deletions libnetwork/ipams/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}

Expand All @@ -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()
Expand Down
21 changes: 7 additions & 14 deletions libnetwork/libnetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"sync"
"testing"

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
5 changes: 5 additions & 0 deletions libnetwork/libnetwork_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !windows

package libnetwork_test

var specPath = "/etc/docker/plugins"
3 changes: 3 additions & 0 deletions libnetwork/libnetwork_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package libnetwork_test

var specPath = filepath.Join(os.Getenv("programdata"), "docker", "plugins")
File renamed without changes.
9 changes: 5 additions & 4 deletions libnetwork/testutils/net.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 72c4a7b

Please sign in to comment.