Browse code

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>

Brian Goff authored on 2021/04/28 07:40:39
Showing 14 changed files
... ...
@@ -190,7 +190,7 @@ pipeline {
190 190
                                   -e VALIDATE_REPO=${GIT_URL} \
191 191
                                   -e VALIDATE_BRANCH=${CHANGE_TARGET} \
192 192
                                   docker:${GIT_COMMIT} \
193
-                                  hack/test/unit"
193
+                                  hack/test/unit
194 194
                                 '''
195 195
                             }
196 196
                             post {
... ...
@@ -587,7 +587,7 @@ pipeline {
587 587
                                   -e VALIDATE_REPO=${GIT_URL} \
588 588
                                   -e VALIDATE_BRANCH=${CHANGE_TARGET} \
589 589
                                   docker:${GIT_COMMIT} \
590
-                                  hack/test/unit"
590
+                                  hack/test/unit
591 591
                                 '''
592 592
                             }
593 593
                             post {
... ...
@@ -786,7 +786,7 @@ pipeline {
786 786
                                   -e VALIDATE_REPO=${GIT_URL} \
787 787
                                   -e VALIDATE_BRANCH=${CHANGE_TARGET} \
788 788
                                   docker:${GIT_COMMIT} \
789
-                                  hack/test/unit"
789
+                                  hack/test/unit
790 790
                                 '''
791 791
                             }
792 792
                             post {
... ...
@@ -982,7 +982,7 @@ pipeline {
982 982
                                   -e VALIDATE_REPO=${GIT_URL} \
983 983
                                   -e VALIDATE_BRANCH=${CHANGE_TARGET} \
984 984
                                   docker:${GIT_COMMIT} \
985
-                                  hack/test/unit"
985
+                                  hack/test/unit
986 986
                                 '''
987 987
                             }
988 988
                             post {
... ...
@@ -30,7 +30,7 @@ for platform in ${DOCKER_CROSSPLATFORMS}; do
30 30
 		echo "Cross building: ${DEST}"
31 31
 		mkdir -p "${DEST}"
32 32
 		ABS_DEST="$(cd "${DEST}" && pwd -P)"
33
-		source "${MAKEDIR}/binary-daemon"
33
+		source "${MAKEDIR}/binary"
34 34
 
35 35
 		source "${MAKEDIR}/cross-platform-dependent"
36 36
 	)
... ...
@@ -10,7 +10,7 @@
10 10
 #
11 11
 #   TESTDIRS='./pkg/term' hack/test/unit
12 12
 #
13
-set -eu -o pipefail
13
+set -eux -o pipefail
14 14
 
15 15
 BUILDFLAGS=(-tags 'netgo seccomp libdm_no_deferred_remove')
16 16
 TESTFLAGS+=" -test.timeout=${TIMEOUT:-5m}"
... ...
@@ -30,4 +30,4 @@ gotestsum --format=standard-quiet --jsonfile=bundles/go-test-report.json --junit
30 30
 	-coverprofile=bundles/profile.out \
31 31
 	-covermode=atomic \
32 32
 	${TESTFLAGS} \
33
-	${pkg_list}
33
+	${pkg_list}
34 34
\ No newline at end of file
... ...
@@ -4,17 +4,19 @@ import (
4 4
 	"fmt"
5 5
 	"io/ioutil"
6 6
 	"math/rand"
7
+	"os"
8
+	"path/filepath"
7 9
 	"testing"
8 10
 	"time"
9 11
 
10
-	"github.com/docker/libkv/store"
11
-	"github.com/docker/libkv/store/boltdb"
12 12
 	"github.com/docker/docker/libnetwork/datastore"
13 13
 	_ "github.com/docker/docker/libnetwork/testutils"
14
+	"github.com/docker/libkv/store"
15
+	"github.com/docker/libkv/store/boltdb"
14 16
 )
15 17
 
16
-const (
17
-	defaultPrefix = "/tmp/libnetwork/test/bitseq"
18
+var (
19
+	defaultPrefix = filepath.Join(os.TempDir(), "libnetwork", "test", "bitseq")
18 20
 )
19 21
 
20 22
 func init() {
... ...
@@ -32,7 +34,7 @@ func randomLocalStore() (datastore.DataStore, error) {
32 32
 	return datastore.NewDataStore(datastore.LocalScope, &datastore.ScopeCfg{
33 33
 		Client: datastore.ScopeClientCfg{
34 34
 			Provider: "boltdb",
35
-			Address:  defaultPrefix + tmp.Name(),
35
+			Address:  filepath.Join(defaultPrefix, filepath.Base(tmp.Name())),
36 36
 			Config: &store.Config{
37 37
 				Bucket:            "libnetwork",
38 38
 				ConnectionTimeout: 3 * time.Second,
... ...
@@ -937,7 +939,7 @@ func TestAllocateRandomDeallocate(t *testing.T) {
937 937
 
938 938
 	numBlocks := uint32(8)
939 939
 	numBits := int(numBlocks * blockLen)
940
-	hnd, err := NewHandle("bitseq-test/data/", ds, "test1", uint64(numBits))
940
+	hnd, err := NewHandle(filepath.Join("bitseq", "test", "data"), ds, "test1", uint64(numBits))
941 941
 	if err != nil {
942 942
 		t.Fatal(err)
943 943
 	}
... ...
@@ -6,11 +6,14 @@ import (
6 6
 	"io"
7 7
 	"io/ioutil"
8 8
 	"net"
9
+	"runtime"
9 10
 	"strings"
10 11
 	"testing"
11 12
 	"time"
12 13
 
13 14
 	"github.com/ishidawataru/sctp"
15
+	"gotest.tools/v3/skip"
16
+
14 17
 	// this takes care of the incontainer flag
15 18
 	_ "github.com/docker/docker/libnetwork/testutils"
16 19
 )
... ...
@@ -282,6 +285,8 @@ func TestUDPWriteError(t *testing.T) {
282 282
 }
283 283
 
284 284
 func TestSCTP4Proxy(t *testing.T) {
285
+	skip.If(t, runtime.GOOS == "windows", "sctp is not supported on windows")
286
+
285 287
 	backend := NewEchoServer(t, "sctp", "127.0.0.1:0", EchoServerOptions{})
286 288
 	defer backend.Close()
287 289
 	backend.Run()
... ...
@@ -295,6 +300,8 @@ func TestSCTP4Proxy(t *testing.T) {
295 295
 
296 296
 func TestSCTP6Proxy(t *testing.T) {
297 297
 	t.Skip("Need to start CI docker with --ipv6")
298
+	skip.If(t, runtime.GOOS == "windows", "sctp is not supported on windows")
299
+
298 300
 	backend := NewEchoServer(t, "sctp", "[::1]:0", EchoServerOptions{})
299 301
 	defer backend.Close()
300 302
 	backend.Run()
... ...
@@ -11,14 +11,16 @@ import (
11 11
 	"net/http"
12 12
 	"net/http/httptest"
13 13
 	"os"
14
+	"path/filepath"
15
+	"runtime"
14 16
 	"testing"
15 17
 
16
-	"github.com/docker/docker/pkg/plugins"
17 18
 	"github.com/docker/docker/libnetwork/datastore"
18 19
 	"github.com/docker/docker/libnetwork/discoverapi"
19 20
 	"github.com/docker/docker/libnetwork/driverapi"
20 21
 	_ "github.com/docker/docker/libnetwork/testutils"
21 22
 	"github.com/docker/docker/libnetwork/types"
23
+	"github.com/docker/docker/pkg/plugins"
22 24
 )
23 25
 
24 26
 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
41 41
 }
42 42
 
43 43
 func setupPlugin(t *testing.T, name string, mux *http.ServeMux) func() {
44
-	if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil {
44
+	specPath := "/etc/docker/plugins"
45
+	if runtime.GOOS == "windows" {
46
+		specPath = filepath.Join(os.Getenv("programdata"), "docker", "plugins")
47
+	}
48
+
49
+	if err := os.MkdirAll(specPath, 0755); err != nil {
45 50
 		t.Fatal(err)
46 51
 	}
47 52
 
53
+	defer func() {
54
+		if t.Failed() {
55
+			os.RemoveAll(specPath)
56
+		}
57
+	}()
58
+
48 59
 	server := httptest.NewServer(mux)
49 60
 	if server == nil {
50 61
 		t.Fatal("Failed to start an HTTP Server")
51 62
 	}
52 63
 
53
-	if err := ioutil.WriteFile(fmt.Sprintf("/etc/docker/plugins/%s.spec", name), []byte(server.URL), 0644); err != nil {
64
+	if err := ioutil.WriteFile(filepath.Join(specPath, name+".spec"), []byte(server.URL), 0644); err != nil {
54 65
 		t.Fatal(err)
55 66
 	}
56 67
 
... ...
@@ -60,7 +73,7 @@ func setupPlugin(t *testing.T, name string, mux *http.ServeMux) func() {
60 60
 	})
61 61
 
62 62
 	return func() {
63
-		if err := os.RemoveAll("/etc/docker/plugins"); err != nil {
63
+		if err := os.RemoveAll(specPath); err != nil {
64 64
 			t.Fatal(err)
65 65
 		}
66 66
 		server.Close()
... ...
@@ -7,24 +7,26 @@ import (
7 7
 	"io/ioutil"
8 8
 	"math/rand"
9 9
 	"net"
10
+	"os"
11
+	"path/filepath"
10 12
 	"strconv"
11 13
 	"sync"
12 14
 	"testing"
13 15
 	"time"
14 16
 
15
-	"github.com/docker/libkv/store"
16
-	"github.com/docker/libkv/store/boltdb"
17 17
 	"github.com/docker/docker/libnetwork/bitseq"
18 18
 	"github.com/docker/docker/libnetwork/datastore"
19 19
 	"github.com/docker/docker/libnetwork/ipamapi"
20 20
 	_ "github.com/docker/docker/libnetwork/testutils"
21 21
 	"github.com/docker/docker/libnetwork/types"
22
+	"github.com/docker/libkv/store"
23
+	"github.com/docker/libkv/store/boltdb"
22 24
 	"gotest.tools/v3/assert"
23 25
 	is "gotest.tools/v3/assert/cmp"
24 26
 )
25 27
 
26
-const (
27
-	defaultPrefix = "/tmp/libnetwork/test/ipam"
28
+var (
29
+	defaultPrefix = filepath.Join(os.TempDir(), "libnetwork", "test", "ipam")
28 30
 )
29 31
 
30 32
 func init() {
... ...
@@ -46,7 +48,7 @@ func randomLocalStore(needStore bool) (datastore.DataStore, error) {
46 46
 	return datastore.NewDataStore(datastore.LocalScope, &datastore.ScopeCfg{
47 47
 		Client: datastore.ScopeClientCfg{
48 48
 			Provider: "boltdb",
49
-			Address:  defaultPrefix + tmp.Name(),
49
+			Address:  filepath.Join(defaultPrefix, filepath.Base(tmp.Name())),
50 50
 			Config: &store.Config{
51 51
 				Bucket:            "libnetwork",
52 52
 				ConnectionTimeout: 3 * time.Second,
... ...
@@ -9,11 +9,13 @@ import (
9 9
 	"net/http"
10 10
 	"net/http/httptest"
11 11
 	"os"
12
+	"path/filepath"
13
+	"runtime"
12 14
 	"testing"
13 15
 
14
-	"github.com/docker/docker/pkg/plugins"
15 16
 	"github.com/docker/docker/libnetwork/ipamapi"
16 17
 	_ "github.com/docker/docker/libnetwork/testutils"
18
+	"github.com/docker/docker/pkg/plugins"
17 19
 )
18 20
 
19 21
 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
36 36
 }
37 37
 
38 38
 func setupPlugin(t *testing.T, name string, mux *http.ServeMux) func() {
39
-	if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil {
39
+	specPath := "/etc/docker/plugins"
40
+	if runtime.GOOS == "windows" {
41
+		specPath = filepath.Join(os.Getenv("programdata"), "docker", "plugins")
42
+	}
43
+
44
+	if err := os.MkdirAll(specPath, 0755); err != nil {
40 45
 		t.Fatal(err)
41 46
 	}
42 47
 
48
+	defer func() {
49
+		if t.Failed() {
50
+			os.RemoveAll(specPath)
51
+		}
52
+	}()
53
+
43 54
 	server := httptest.NewServer(mux)
44 55
 	if server == nil {
45 56
 		t.Fatal("Failed to start an HTTP Server")
46 57
 	}
47 58
 
48
-	if err := ioutil.WriteFile(fmt.Sprintf("/etc/docker/plugins/%s.spec", name), []byte(server.URL), 0644); err != nil {
59
+	if err := ioutil.WriteFile(filepath.Join(specPath, name+".spec"), []byte(server.URL), 0644); err != nil {
49 60
 		t.Fatal(err)
50 61
 	}
51 62
 
... ...
@@ -55,7 +68,7 @@ func setupPlugin(t *testing.T, name string, mux *http.ServeMux) func() {
55 55
 	})
56 56
 
57 57
 	return func() {
58
-		if err := os.RemoveAll("/etc/docker/plugins"); err != nil {
58
+		if err := os.RemoveAll(specPath); err != nil {
59 59
 			t.Fatal(err)
60 60
 		}
61 61
 		server.Close()
... ...
@@ -7,6 +7,7 @@ import (
7 7
 	"net/http"
8 8
 	"net/http/httptest"
9 9
 	"os"
10
+	"path/filepath"
10 11
 	"sync"
11 12
 	"testing"
12 13
 
... ...
@@ -1314,10 +1315,6 @@ func TestEndpointUpdateParent(t *testing.T) {
1314 1314
 }
1315 1315
 
1316 1316
 func TestInvalidRemoteDriver(t *testing.T) {
1317
-	if !testutils.IsRunningInContainer() {
1318
-		t.Skip("Skipping test when not running inside a Container")
1319
-	}
1320
-
1321 1317
 	mux := http.NewServeMux()
1322 1318
 	server := httptest.NewServer(mux)
1323 1319
 	if server == nil {
... ...
@@ -1334,16 +1331,16 @@ func TestInvalidRemoteDriver(t *testing.T) {
1334 1334
 		fmt.Fprintln(w, `{"Implements": ["InvalidDriver"]}`)
1335 1335
 	})
1336 1336
 
1337
-	if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil {
1337
+	if err := os.MkdirAll(specPath, 0755); err != nil {
1338 1338
 		t.Fatal(err)
1339 1339
 	}
1340 1340
 	defer func() {
1341
-		if err := os.RemoveAll("/etc/docker/plugins"); err != nil {
1341
+		if err := os.RemoveAll(specPath); err != nil {
1342 1342
 			t.Fatal(err)
1343 1343
 		}
1344 1344
 	}()
1345 1345
 
1346
-	if err := ioutil.WriteFile("/etc/docker/plugins/invalid-network-driver.spec", []byte(server.URL), 0644); err != nil {
1346
+	if err := ioutil.WriteFile(filepath.Join(specPath, "invalid-network-driver.spec"), []byte(server.URL), 0644); err != nil {
1347 1347
 		t.Fatal(err)
1348 1348
 	}
1349 1349
 
... ...
@@ -1365,10 +1362,6 @@ func TestInvalidRemoteDriver(t *testing.T) {
1365 1365
 }
1366 1366
 
1367 1367
 func TestValidRemoteDriver(t *testing.T) {
1368
-	if !testutils.IsRunningInContainer() {
1369
-		t.Skip("Skipping test when not running inside a Container")
1370
-	}
1371
-
1372 1368
 	mux := http.NewServeMux()
1373 1369
 	server := httptest.NewServer(mux)
1374 1370
 	if server == nil {
... ...
@@ -1397,16 +1390,16 @@ func TestValidRemoteDriver(t *testing.T) {
1397 1397
 		fmt.Fprintf(w, "null")
1398 1398
 	})
1399 1399
 
1400
-	if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil {
1400
+	if err := os.MkdirAll(specPath, 0755); err != nil {
1401 1401
 		t.Fatal(err)
1402 1402
 	}
1403 1403
 	defer func() {
1404
-		if err := os.RemoveAll("/etc/docker/plugins"); err != nil {
1404
+		if err := os.RemoveAll(specPath); err != nil {
1405 1405
 			t.Fatal(err)
1406 1406
 		}
1407 1407
 	}()
1408 1408
 
1409
-	if err := ioutil.WriteFile("/etc/docker/plugins/valid-network-driver.spec", []byte(server.URL), 0644); err != nil {
1409
+	if err := ioutil.WriteFile(filepath.Join(specPath, "valid-network-driver.spec"), []byte(server.URL), 0644); err != nil {
1410 1410
 		t.Fatal(err)
1411 1411
 	}
1412 1412
 
1413 1413
new file mode 100644
... ...
@@ -0,0 +1,5 @@
0
+// +build !windows
1
+
2
+package libnetwork_test
3
+
4
+var specPath = "/etc/docker/plugins"
0 5
\ No newline at end of file
1 6
new file mode 100644
... ...
@@ -0,0 +1,3 @@
0
+package libnetwork_test
1
+
2
+var specPath = filepath.Join(os.Getenv("programdata"), "docker", "plugins")
0 3
new file mode 100644
... ...
@@ -0,0 +1,308 @@
0
+package resolvconf
1
+
2
+import (
3
+	"bytes"
4
+	"io/ioutil"
5
+	"os"
6
+	"testing"
7
+
8
+	"github.com/docker/docker/pkg/ioutils"
9
+	_ "github.com/docker/docker/libnetwork/testutils"
10
+	"github.com/docker/docker/libnetwork/types"
11
+)
12
+
13
+func TestGet(t *testing.T) {
14
+	resolvConfUtils, err := Get()
15
+	if err != nil {
16
+		t.Fatal(err)
17
+	}
18
+	resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf")
19
+	if err != nil {
20
+		t.Fatal(err)
21
+	}
22
+	if string(resolvConfUtils.Content) != string(resolvConfSystem) {
23
+		t.Fatalf("/etc/resolv.conf and GetResolvConf have different content.")
24
+	}
25
+	hashSystem, err := ioutils.HashData(bytes.NewReader(resolvConfSystem))
26
+	if err != nil {
27
+		t.Fatal(err)
28
+	}
29
+	if resolvConfUtils.Hash != hashSystem {
30
+		t.Fatalf("/etc/resolv.conf and GetResolvConf have different hashes.")
31
+	}
32
+}
33
+
34
+func TestGetNameservers(t *testing.T) {
35
+	for resolv, result := range map[string][]string{`
36
+nameserver 1.2.3.4
37
+nameserver 40.3.200.10
38
+search example.com`: {"1.2.3.4", "40.3.200.10"},
39
+		`search example.com`: {},
40
+		`nameserver 1.2.3.4
41
+search example.com
42
+nameserver 4.30.20.100`: {"1.2.3.4", "4.30.20.100"},
43
+		``:                        {},
44
+		`  nameserver 1.2.3.4   `: {"1.2.3.4"},
45
+		`search example.com
46
+nameserver 1.2.3.4
47
+#nameserver 4.3.2.1`: {"1.2.3.4"},
48
+		`search example.com
49
+nameserver 1.2.3.4 # not 4.3.2.1`: {"1.2.3.4"},
50
+	} {
51
+		test := GetNameservers([]byte(resolv), types.IP)
52
+		if !strSlicesEqual(test, result) {
53
+			t.Fatalf("Wrong nameserver string {%s} should be %v. Input: %s", test, result, resolv)
54
+		}
55
+	}
56
+}
57
+
58
+func TestGetNameserversAsCIDR(t *testing.T) {
59
+	for resolv, result := range map[string][]string{`
60
+nameserver 1.2.3.4
61
+nameserver 40.3.200.10
62
+search example.com`: {"1.2.3.4/32", "40.3.200.10/32"},
63
+		`search example.com`: {},
64
+		`nameserver 1.2.3.4
65
+search example.com
66
+nameserver 4.30.20.100`: {"1.2.3.4/32", "4.30.20.100/32"},
67
+		``:                        {},
68
+		`  nameserver 1.2.3.4   `: {"1.2.3.4/32"},
69
+		`search example.com
70
+nameserver 1.2.3.4
71
+#nameserver 4.3.2.1`: {"1.2.3.4/32"},
72
+		`search example.com
73
+nameserver 1.2.3.4 # not 4.3.2.1`: {"1.2.3.4/32"},
74
+	} {
75
+		test := GetNameserversAsCIDR([]byte(resolv))
76
+		if !strSlicesEqual(test, result) {
77
+			t.Fatalf("Wrong nameserver string {%s} should be %v. Input: %s", test, result, resolv)
78
+		}
79
+	}
80
+}
81
+
82
+func TestGetSearchDomains(t *testing.T) {
83
+	for resolv, result := range map[string][]string{
84
+		`search example.com`:           {"example.com"},
85
+		`search example.com # ignored`: {"example.com"},
86
+		`	  search	 example.com	  `: {"example.com"},
87
+		`	  search	 example.com	  # ignored`: {"example.com"},
88
+		`search foo.example.com example.com`: {"foo.example.com", "example.com"},
89
+		`	   search	   foo.example.com	 example.com	`: {"foo.example.com", "example.com"},
90
+		`	   search	   foo.example.com	 example.com	# ignored`: {"foo.example.com", "example.com"},
91
+		``:          {},
92
+		`# ignored`: {},
93
+		`nameserver 1.2.3.4
94
+search foo.example.com example.com`: {"foo.example.com", "example.com"},
95
+		`nameserver 1.2.3.4
96
+search dup1.example.com dup2.example.com
97
+search foo.example.com example.com`: {"foo.example.com", "example.com"},
98
+		`nameserver 1.2.3.4
99
+search foo.example.com example.com
100
+nameserver 4.30.20.100`: {"foo.example.com", "example.com"},
101
+	} {
102
+		test := GetSearchDomains([]byte(resolv))
103
+		if !strSlicesEqual(test, result) {
104
+			t.Fatalf("Wrong search domain string {%s} should be %v. Input: %s", test, result, resolv)
105
+		}
106
+	}
107
+}
108
+
109
+func TestGetOptions(t *testing.T) {
110
+	for resolv, result := range map[string][]string{
111
+		`options opt1`:           {"opt1"},
112
+		`options opt1 # ignored`: {"opt1"},
113
+		`	  options	 opt1	  `: {"opt1"},
114
+		`	  options	 opt1	  # ignored`: {"opt1"},
115
+		`options opt1 opt2 opt3`:           {"opt1", "opt2", "opt3"},
116
+		`options opt1 opt2 opt3 # ignored`: {"opt1", "opt2", "opt3"},
117
+		`	   options	 opt1	 opt2	 opt3	`: {"opt1", "opt2", "opt3"},
118
+		`	   options	 opt1	 opt2	 opt3	# ignored`: {"opt1", "opt2", "opt3"},
119
+		``:                   {},
120
+		`# ignored`:          {},
121
+		`nameserver 1.2.3.4`: {},
122
+		`nameserver 1.2.3.4
123
+options opt1 opt2 opt3`: {"opt1", "opt2", "opt3"},
124
+		`nameserver 1.2.3.4
125
+options opt1 opt2
126
+options opt3 opt4`: {"opt3", "opt4"},
127
+	} {
128
+		test := GetOptions([]byte(resolv))
129
+		if !strSlicesEqual(test, result) {
130
+			t.Fatalf("Wrong options string {%s} should be %v. Input: %s", test, result, resolv)
131
+		}
132
+	}
133
+}
134
+
135
+func strSlicesEqual(a, b []string) bool {
136
+	if len(a) != len(b) {
137
+		return false
138
+	}
139
+
140
+	for i, v := range a {
141
+		if v != b[i] {
142
+			return false
143
+		}
144
+	}
145
+
146
+	return true
147
+}
148
+
149
+func TestBuild(t *testing.T) {
150
+	file, err := ioutil.TempFile("", "")
151
+	if err != nil {
152
+		t.Fatal(err)
153
+	}
154
+	defer os.Remove(file.Name())
155
+
156
+	_, err = Build(file.Name(), []string{"ns1", "ns2", "ns3"}, []string{"search1"}, []string{"opt1"})
157
+	if err != nil {
158
+		t.Fatal(err)
159
+	}
160
+
161
+	content, err := ioutil.ReadFile(file.Name())
162
+	if err != nil {
163
+		t.Fatal(err)
164
+	}
165
+
166
+	if expected := "search search1\nnameserver ns1\nnameserver ns2\nnameserver ns3\noptions opt1\n"; !bytes.Contains(content, []byte(expected)) {
167
+		t.Fatalf("Expected to find '%s' got '%s'", expected, content)
168
+	}
169
+}
170
+
171
+func TestBuildWithZeroLengthDomainSearch(t *testing.T) {
172
+	file, err := ioutil.TempFile("", "")
173
+	if err != nil {
174
+		t.Fatal(err)
175
+	}
176
+	defer os.Remove(file.Name())
177
+
178
+	_, err = Build(file.Name(), []string{"ns1", "ns2", "ns3"}, []string{"."}, []string{"opt1"})
179
+	if err != nil {
180
+		t.Fatal(err)
181
+	}
182
+
183
+	content, err := ioutil.ReadFile(file.Name())
184
+	if err != nil {
185
+		t.Fatal(err)
186
+	}
187
+
188
+	if expected := "nameserver ns1\nnameserver ns2\nnameserver ns3\noptions opt1\n"; !bytes.Contains(content, []byte(expected)) {
189
+		t.Fatalf("Expected to find '%s' got '%s'", expected, content)
190
+	}
191
+	if notExpected := "search ."; bytes.Contains(content, []byte(notExpected)) {
192
+		t.Fatalf("Expected to not find '%s' got '%s'", notExpected, content)
193
+	}
194
+}
195
+
196
+func TestBuildWithNoOptions(t *testing.T) {
197
+	file, err := ioutil.TempFile("", "")
198
+	if err != nil {
199
+		t.Fatal(err)
200
+	}
201
+	defer os.Remove(file.Name())
202
+
203
+	_, err = Build(file.Name(), []string{"ns1", "ns2", "ns3"}, []string{"search1"}, []string{})
204
+	if err != nil {
205
+		t.Fatal(err)
206
+	}
207
+
208
+	content, err := ioutil.ReadFile(file.Name())
209
+	if err != nil {
210
+		t.Fatal(err)
211
+	}
212
+
213
+	if expected := "search search1\nnameserver ns1\nnameserver ns2\nnameserver ns3\n"; !bytes.Contains(content, []byte(expected)) {
214
+		t.Fatalf("Expected to find '%s' got '%s'", expected, content)
215
+	}
216
+	if notExpected := "search ."; bytes.Contains(content, []byte(notExpected)) {
217
+		t.Fatalf("Expected to not find '%s' got '%s'", notExpected, content)
218
+	}
219
+}
220
+
221
+func TestFilterResolvDns(t *testing.T) {
222
+	ns0 := "nameserver 10.16.60.14\nnameserver 10.16.60.21\n"
223
+
224
+	if result, _ := FilterResolvDNS([]byte(ns0), false); result != nil {
225
+		if ns0 != string(result.Content) {
226
+			t.Fatalf("Failed No Localhost: expected \n<%s> got \n<%s>", ns0, string(result.Content))
227
+		}
228
+	}
229
+
230
+	ns1 := "nameserver 10.16.60.14\nnameserver 10.16.60.21\nnameserver 127.0.0.1\n"
231
+	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
232
+		if ns0 != string(result.Content) {
233
+			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result.Content))
234
+		}
235
+	}
236
+
237
+	ns1 = "nameserver 10.16.60.14\nnameserver 127.0.0.1\nnameserver 10.16.60.21\n"
238
+	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
239
+		if ns0 != string(result.Content) {
240
+			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result.Content))
241
+		}
242
+	}
243
+
244
+	ns1 = "nameserver 127.0.1.1\nnameserver 10.16.60.14\nnameserver 10.16.60.21\n"
245
+	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
246
+		if ns0 != string(result.Content) {
247
+			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result.Content))
248
+		}
249
+	}
250
+
251
+	ns1 = "nameserver ::1\nnameserver 10.16.60.14\nnameserver 127.0.2.1\nnameserver 10.16.60.21\n"
252
+	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
253
+		if ns0 != string(result.Content) {
254
+			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result.Content))
255
+		}
256
+	}
257
+
258
+	ns1 = "nameserver 10.16.60.14\nnameserver ::1\nnameserver 10.16.60.21\nnameserver ::1"
259
+	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
260
+		if ns0 != string(result.Content) {
261
+			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result.Content))
262
+		}
263
+	}
264
+
265
+	// with IPv6 disabled (false param), the IPv6 nameserver should be removed
266
+	ns1 = "nameserver 10.16.60.14\nnameserver 2002:dead:beef::1\nnameserver 10.16.60.21\nnameserver ::1"
267
+	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
268
+		if ns0 != string(result.Content) {
269
+			t.Fatalf("Failed Localhost+IPv6 off: expected \n<%s> got \n<%s>", ns0, string(result.Content))
270
+		}
271
+	}
272
+
273
+	// with IPv6 disabled (false param), the IPv6 link-local nameserver with zone ID should be removed
274
+	ns1 = "nameserver 10.16.60.14\nnameserver FE80::BB1%1\nnameserver FE80::BB1%eth0\nnameserver 10.16.60.21\n"
275
+	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
276
+		if ns0 != string(result.Content) {
277
+			t.Fatalf("Failed Localhost+IPv6 off: expected \n<%s> got \n<%s>", ns0, string(result.Content))
278
+		}
279
+	}
280
+
281
+	// with IPv6 enabled, the IPv6 nameserver should be preserved
282
+	ns0 = "nameserver 10.16.60.14\nnameserver 2002:dead:beef::1\nnameserver 10.16.60.21\n"
283
+	ns1 = "nameserver 10.16.60.14\nnameserver 2002:dead:beef::1\nnameserver 10.16.60.21\nnameserver ::1"
284
+	if result, _ := FilterResolvDNS([]byte(ns1), true); result != nil {
285
+		if ns0 != string(result.Content) {
286
+			t.Fatalf("Failed Localhost+IPv6 on: expected \n<%s> got \n<%s>", ns0, string(result.Content))
287
+		}
288
+	}
289
+
290
+	// with IPv6 enabled, and no non-localhost servers, Google defaults (both IPv4+IPv6) should be added
291
+	ns0 = "\nnameserver 8.8.8.8\nnameserver 8.8.4.4\nnameserver 2001:4860:4860::8888\nnameserver 2001:4860:4860::8844"
292
+	ns1 = "nameserver 127.0.0.1\nnameserver ::1\nnameserver 127.0.2.1"
293
+	if result, _ := FilterResolvDNS([]byte(ns1), true); result != nil {
294
+		if ns0 != string(result.Content) {
295
+			t.Fatalf("Failed no Localhost+IPv6 enabled: expected \n<%s> got \n<%s>", ns0, string(result.Content))
296
+		}
297
+	}
298
+
299
+	// with IPv6 disabled, and no non-localhost servers, Google defaults (only IPv4) should be added
300
+	ns0 = "\nnameserver 8.8.8.8\nnameserver 8.8.4.4"
301
+	ns1 = "nameserver 127.0.0.1\nnameserver ::1\nnameserver 127.0.2.1"
302
+	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
303
+		if ns0 != string(result.Content) {
304
+			t.Fatalf("Failed no Localhost+IPv6 enabled: expected \n<%s> got \n<%s>", ns0, string(result.Content))
305
+		}
306
+	}
307
+}
0 308
deleted file mode 100644
... ...
@@ -1,308 +0,0 @@
1
-package resolvconf
2
-
3
-import (
4
-	"bytes"
5
-	"io/ioutil"
6
-	"os"
7
-	"testing"
8
-
9
-	"github.com/docker/docker/pkg/ioutils"
10
-	_ "github.com/docker/docker/libnetwork/testutils"
11
-	"github.com/docker/docker/libnetwork/types"
12
-)
13
-
14
-func TestGet(t *testing.T) {
15
-	resolvConfUtils, err := Get()
16
-	if err != nil {
17
-		t.Fatal(err)
18
-	}
19
-	resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf")
20
-	if err != nil {
21
-		t.Fatal(err)
22
-	}
23
-	if string(resolvConfUtils.Content) != string(resolvConfSystem) {
24
-		t.Fatalf("/etc/resolv.conf and GetResolvConf have different content.")
25
-	}
26
-	hashSystem, err := ioutils.HashData(bytes.NewReader(resolvConfSystem))
27
-	if err != nil {
28
-		t.Fatal(err)
29
-	}
30
-	if resolvConfUtils.Hash != hashSystem {
31
-		t.Fatalf("/etc/resolv.conf and GetResolvConf have different hashes.")
32
-	}
33
-}
34
-
35
-func TestGetNameservers(t *testing.T) {
36
-	for resolv, result := range map[string][]string{`
37
-nameserver 1.2.3.4
38
-nameserver 40.3.200.10
39
-search example.com`: {"1.2.3.4", "40.3.200.10"},
40
-		`search example.com`: {},
41
-		`nameserver 1.2.3.4
42
-search example.com
43
-nameserver 4.30.20.100`: {"1.2.3.4", "4.30.20.100"},
44
-		``:                        {},
45
-		`  nameserver 1.2.3.4   `: {"1.2.3.4"},
46
-		`search example.com
47
-nameserver 1.2.3.4
48
-#nameserver 4.3.2.1`: {"1.2.3.4"},
49
-		`search example.com
50
-nameserver 1.2.3.4 # not 4.3.2.1`: {"1.2.3.4"},
51
-	} {
52
-		test := GetNameservers([]byte(resolv), types.IP)
53
-		if !strSlicesEqual(test, result) {
54
-			t.Fatalf("Wrong nameserver string {%s} should be %v. Input: %s", test, result, resolv)
55
-		}
56
-	}
57
-}
58
-
59
-func TestGetNameserversAsCIDR(t *testing.T) {
60
-	for resolv, result := range map[string][]string{`
61
-nameserver 1.2.3.4
62
-nameserver 40.3.200.10
63
-search example.com`: {"1.2.3.4/32", "40.3.200.10/32"},
64
-		`search example.com`: {},
65
-		`nameserver 1.2.3.4
66
-search example.com
67
-nameserver 4.30.20.100`: {"1.2.3.4/32", "4.30.20.100/32"},
68
-		``:                        {},
69
-		`  nameserver 1.2.3.4   `: {"1.2.3.4/32"},
70
-		`search example.com
71
-nameserver 1.2.3.4
72
-#nameserver 4.3.2.1`: {"1.2.3.4/32"},
73
-		`search example.com
74
-nameserver 1.2.3.4 # not 4.3.2.1`: {"1.2.3.4/32"},
75
-	} {
76
-		test := GetNameserversAsCIDR([]byte(resolv))
77
-		if !strSlicesEqual(test, result) {
78
-			t.Fatalf("Wrong nameserver string {%s} should be %v. Input: %s", test, result, resolv)
79
-		}
80
-	}
81
-}
82
-
83
-func TestGetSearchDomains(t *testing.T) {
84
-	for resolv, result := range map[string][]string{
85
-		`search example.com`:           {"example.com"},
86
-		`search example.com # ignored`: {"example.com"},
87
-		`	  search	 example.com	  `: {"example.com"},
88
-		`	  search	 example.com	  # ignored`: {"example.com"},
89
-		`search foo.example.com example.com`: {"foo.example.com", "example.com"},
90
-		`	   search	   foo.example.com	 example.com	`: {"foo.example.com", "example.com"},
91
-		`	   search	   foo.example.com	 example.com	# ignored`: {"foo.example.com", "example.com"},
92
-		``:          {},
93
-		`# ignored`: {},
94
-		`nameserver 1.2.3.4
95
-search foo.example.com example.com`: {"foo.example.com", "example.com"},
96
-		`nameserver 1.2.3.4
97
-search dup1.example.com dup2.example.com
98
-search foo.example.com example.com`: {"foo.example.com", "example.com"},
99
-		`nameserver 1.2.3.4
100
-search foo.example.com example.com
101
-nameserver 4.30.20.100`: {"foo.example.com", "example.com"},
102
-	} {
103
-		test := GetSearchDomains([]byte(resolv))
104
-		if !strSlicesEqual(test, result) {
105
-			t.Fatalf("Wrong search domain string {%s} should be %v. Input: %s", test, result, resolv)
106
-		}
107
-	}
108
-}
109
-
110
-func TestGetOptions(t *testing.T) {
111
-	for resolv, result := range map[string][]string{
112
-		`options opt1`:           {"opt1"},
113
-		`options opt1 # ignored`: {"opt1"},
114
-		`	  options	 opt1	  `: {"opt1"},
115
-		`	  options	 opt1	  # ignored`: {"opt1"},
116
-		`options opt1 opt2 opt3`:           {"opt1", "opt2", "opt3"},
117
-		`options opt1 opt2 opt3 # ignored`: {"opt1", "opt2", "opt3"},
118
-		`	   options	 opt1	 opt2	 opt3	`: {"opt1", "opt2", "opt3"},
119
-		`	   options	 opt1	 opt2	 opt3	# ignored`: {"opt1", "opt2", "opt3"},
120
-		``:                   {},
121
-		`# ignored`:          {},
122
-		`nameserver 1.2.3.4`: {},
123
-		`nameserver 1.2.3.4
124
-options opt1 opt2 opt3`: {"opt1", "opt2", "opt3"},
125
-		`nameserver 1.2.3.4
126
-options opt1 opt2
127
-options opt3 opt4`: {"opt3", "opt4"},
128
-	} {
129
-		test := GetOptions([]byte(resolv))
130
-		if !strSlicesEqual(test, result) {
131
-			t.Fatalf("Wrong options string {%s} should be %v. Input: %s", test, result, resolv)
132
-		}
133
-	}
134
-}
135
-
136
-func strSlicesEqual(a, b []string) bool {
137
-	if len(a) != len(b) {
138
-		return false
139
-	}
140
-
141
-	for i, v := range a {
142
-		if v != b[i] {
143
-			return false
144
-		}
145
-	}
146
-
147
-	return true
148
-}
149
-
150
-func TestBuild(t *testing.T) {
151
-	file, err := ioutil.TempFile("", "")
152
-	if err != nil {
153
-		t.Fatal(err)
154
-	}
155
-	defer os.Remove(file.Name())
156
-
157
-	_, err = Build(file.Name(), []string{"ns1", "ns2", "ns3"}, []string{"search1"}, []string{"opt1"})
158
-	if err != nil {
159
-		t.Fatal(err)
160
-	}
161
-
162
-	content, err := ioutil.ReadFile(file.Name())
163
-	if err != nil {
164
-		t.Fatal(err)
165
-	}
166
-
167
-	if expected := "search search1\nnameserver ns1\nnameserver ns2\nnameserver ns3\noptions opt1\n"; !bytes.Contains(content, []byte(expected)) {
168
-		t.Fatalf("Expected to find '%s' got '%s'", expected, content)
169
-	}
170
-}
171
-
172
-func TestBuildWithZeroLengthDomainSearch(t *testing.T) {
173
-	file, err := ioutil.TempFile("", "")
174
-	if err != nil {
175
-		t.Fatal(err)
176
-	}
177
-	defer os.Remove(file.Name())
178
-
179
-	_, err = Build(file.Name(), []string{"ns1", "ns2", "ns3"}, []string{"."}, []string{"opt1"})
180
-	if err != nil {
181
-		t.Fatal(err)
182
-	}
183
-
184
-	content, err := ioutil.ReadFile(file.Name())
185
-	if err != nil {
186
-		t.Fatal(err)
187
-	}
188
-
189
-	if expected := "nameserver ns1\nnameserver ns2\nnameserver ns3\noptions opt1\n"; !bytes.Contains(content, []byte(expected)) {
190
-		t.Fatalf("Expected to find '%s' got '%s'", expected, content)
191
-	}
192
-	if notExpected := "search ."; bytes.Contains(content, []byte(notExpected)) {
193
-		t.Fatalf("Expected to not find '%s' got '%s'", notExpected, content)
194
-	}
195
-}
196
-
197
-func TestBuildWithNoOptions(t *testing.T) {
198
-	file, err := ioutil.TempFile("", "")
199
-	if err != nil {
200
-		t.Fatal(err)
201
-	}
202
-	defer os.Remove(file.Name())
203
-
204
-	_, err = Build(file.Name(), []string{"ns1", "ns2", "ns3"}, []string{"search1"}, []string{})
205
-	if err != nil {
206
-		t.Fatal(err)
207
-	}
208
-
209
-	content, err := ioutil.ReadFile(file.Name())
210
-	if err != nil {
211
-		t.Fatal(err)
212
-	}
213
-
214
-	if expected := "search search1\nnameserver ns1\nnameserver ns2\nnameserver ns3\n"; !bytes.Contains(content, []byte(expected)) {
215
-		t.Fatalf("Expected to find '%s' got '%s'", expected, content)
216
-	}
217
-	if notExpected := "search ."; bytes.Contains(content, []byte(notExpected)) {
218
-		t.Fatalf("Expected to not find '%s' got '%s'", notExpected, content)
219
-	}
220
-}
221
-
222
-func TestFilterResolvDns(t *testing.T) {
223
-	ns0 := "nameserver 10.16.60.14\nnameserver 10.16.60.21\n"
224
-
225
-	if result, _ := FilterResolvDNS([]byte(ns0), false); result != nil {
226
-		if ns0 != string(result.Content) {
227
-			t.Fatalf("Failed No Localhost: expected \n<%s> got \n<%s>", ns0, string(result.Content))
228
-		}
229
-	}
230
-
231
-	ns1 := "nameserver 10.16.60.14\nnameserver 10.16.60.21\nnameserver 127.0.0.1\n"
232
-	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
233
-		if ns0 != string(result.Content) {
234
-			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result.Content))
235
-		}
236
-	}
237
-
238
-	ns1 = "nameserver 10.16.60.14\nnameserver 127.0.0.1\nnameserver 10.16.60.21\n"
239
-	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
240
-		if ns0 != string(result.Content) {
241
-			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result.Content))
242
-		}
243
-	}
244
-
245
-	ns1 = "nameserver 127.0.1.1\nnameserver 10.16.60.14\nnameserver 10.16.60.21\n"
246
-	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
247
-		if ns0 != string(result.Content) {
248
-			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result.Content))
249
-		}
250
-	}
251
-
252
-	ns1 = "nameserver ::1\nnameserver 10.16.60.14\nnameserver 127.0.2.1\nnameserver 10.16.60.21\n"
253
-	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
254
-		if ns0 != string(result.Content) {
255
-			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result.Content))
256
-		}
257
-	}
258
-
259
-	ns1 = "nameserver 10.16.60.14\nnameserver ::1\nnameserver 10.16.60.21\nnameserver ::1"
260
-	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
261
-		if ns0 != string(result.Content) {
262
-			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result.Content))
263
-		}
264
-	}
265
-
266
-	// with IPv6 disabled (false param), the IPv6 nameserver should be removed
267
-	ns1 = "nameserver 10.16.60.14\nnameserver 2002:dead:beef::1\nnameserver 10.16.60.21\nnameserver ::1"
268
-	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
269
-		if ns0 != string(result.Content) {
270
-			t.Fatalf("Failed Localhost+IPv6 off: expected \n<%s> got \n<%s>", ns0, string(result.Content))
271
-		}
272
-	}
273
-
274
-	// with IPv6 disabled (false param), the IPv6 link-local nameserver with zone ID should be removed
275
-	ns1 = "nameserver 10.16.60.14\nnameserver FE80::BB1%1\nnameserver FE80::BB1%eth0\nnameserver 10.16.60.21\n"
276
-	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
277
-		if ns0 != string(result.Content) {
278
-			t.Fatalf("Failed Localhost+IPv6 off: expected \n<%s> got \n<%s>", ns0, string(result.Content))
279
-		}
280
-	}
281
-
282
-	// with IPv6 enabled, the IPv6 nameserver should be preserved
283
-	ns0 = "nameserver 10.16.60.14\nnameserver 2002:dead:beef::1\nnameserver 10.16.60.21\n"
284
-	ns1 = "nameserver 10.16.60.14\nnameserver 2002:dead:beef::1\nnameserver 10.16.60.21\nnameserver ::1"
285
-	if result, _ := FilterResolvDNS([]byte(ns1), true); result != nil {
286
-		if ns0 != string(result.Content) {
287
-			t.Fatalf("Failed Localhost+IPv6 on: expected \n<%s> got \n<%s>", ns0, string(result.Content))
288
-		}
289
-	}
290
-
291
-	// with IPv6 enabled, and no non-localhost servers, Google defaults (both IPv4+IPv6) should be added
292
-	ns0 = "\nnameserver 8.8.8.8\nnameserver 8.8.4.4\nnameserver 2001:4860:4860::8888\nnameserver 2001:4860:4860::8844"
293
-	ns1 = "nameserver 127.0.0.1\nnameserver ::1\nnameserver 127.0.2.1"
294
-	if result, _ := FilterResolvDNS([]byte(ns1), true); result != nil {
295
-		if ns0 != string(result.Content) {
296
-			t.Fatalf("Failed no Localhost+IPv6 enabled: expected \n<%s> got \n<%s>", ns0, string(result.Content))
297
-		}
298
-	}
299
-
300
-	// with IPv6 disabled, and no non-localhost servers, Google defaults (only IPv4) should be added
301
-	ns0 = "\nnameserver 8.8.8.8\nnameserver 8.8.4.4"
302
-	ns1 = "nameserver 127.0.0.1\nnameserver ::1\nnameserver 127.0.2.1"
303
-	if result, _ := FilterResolvDNS([]byte(ns1), false); result != nil {
304
-		if ns0 != string(result.Content) {
305
-			t.Fatalf("Failed no Localhost+IPv6 enabled: expected \n<%s> got \n<%s>", ns0, string(result.Content))
306
-		}
307
-	}
308
-}
... ...
@@ -1,10 +1,11 @@
1 1
 package testutils
2 2
 
3
-import "flag"
4
-
5
-var runningInContainer = flag.Bool("incontainer", false, "Indicates if the test is running in a container")
3
+import (
4
+	"os"
5
+)
6 6
 
7 7
 // IsRunningInContainer returns whether the test is running inside a container.
8 8
 func IsRunningInContainer() bool {
9
-	return (*runningInContainer)
9
+	_, err := os.Stat("/.dockerenv")
10
+	return err == nil
10 11
 }