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 <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2021-04-27 22:40:39 +00:00
parent 4da0dc974c
commit 72c4a7b496
13 changed files with 83 additions and 44 deletions

8
Jenkinsfile vendored
View File

@ -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 {

View File

@ -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"
)

View File

@ -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}

View File

@ -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)
}

View File

@ -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()

View File

@ -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()

View File

@ -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,

View File

@ -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()

View File

@ -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)
}

View File

@ -0,0 +1,5 @@
// +build !windows
package libnetwork_test
var specPath = "/etc/docker/plugins"

View File

@ -0,0 +1,3 @@
package libnetwork_test
var specPath = filepath.Join(os.Getenv("programdata"), "docker", "plugins")

View File

@ -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
}