Browse code

Copy Inslice() to those parts that use it

Signed-off-by: Chao Wang <wangchao.fnst@cn.fujitsu.com>

Chao Wang authored on 2017/11/10 14:18:48
Showing 7 changed files
... ...
@@ -6,7 +6,6 @@ import (
6 6
 	"fmt"
7 7
 	"strings"
8 8
 
9
-	"github.com/docker/docker/pkg/stringutils"
10 9
 	"github.com/syndtr/gocapability/capability"
11 10
 )
12 11
 
... ...
@@ -69,6 +68,17 @@ func GetAllCapabilities() []string {
69 69
 	return output
70 70
 }
71 71
 
72
+// inSlice tests whether a string is contained in a slice of strings or not.
73
+// Comparison is case insensitive
74
+func inSlice(slice []string, s string) bool {
75
+	for _, ss := range slice {
76
+		if strings.ToLower(s) == strings.ToLower(ss) {
77
+			return true
78
+		}
79
+	}
80
+	return false
81
+}
82
+
72 83
 // TweakCapabilities can tweak capabilities by adding or dropping capabilities
73 84
 // based on the basics capabilities.
74 85
 func TweakCapabilities(basics, adds, drops []string) ([]string, error) {
... ...
@@ -86,17 +96,17 @@ func TweakCapabilities(basics, adds, drops []string) ([]string, error) {
86 86
 			continue
87 87
 		}
88 88
 
89
-		if !stringutils.InSlice(allCaps, "CAP_"+cap) {
89
+		if !inSlice(allCaps, "CAP_"+cap) {
90 90
 			return nil, fmt.Errorf("Unknown capability drop: %q", cap)
91 91
 		}
92 92
 	}
93 93
 
94 94
 	// handle --cap-add=all
95
-	if stringutils.InSlice(adds, "all") {
95
+	if inSlice(adds, "all") {
96 96
 		basics = allCaps
97 97
 	}
98 98
 
99
-	if !stringutils.InSlice(drops, "all") {
99
+	if !inSlice(drops, "all") {
100 100
 		for _, cap := range basics {
101 101
 			// skip `all` already handled above
102 102
 			if strings.ToLower(cap) == "all" {
... ...
@@ -104,7 +114,7 @@ func TweakCapabilities(basics, adds, drops []string) ([]string, error) {
104 104
 			}
105 105
 
106 106
 			// if we don't drop `all`, add back all the non-dropped caps
107
-			if !stringutils.InSlice(drops, cap[4:]) {
107
+			if !inSlice(drops, cap[4:]) {
108 108
 				newCaps = append(newCaps, strings.ToUpper(cap))
109 109
 			}
110 110
 		}
... ...
@@ -118,12 +128,12 @@ func TweakCapabilities(basics, adds, drops []string) ([]string, error) {
118 118
 
119 119
 		cap = "CAP_" + cap
120 120
 
121
-		if !stringutils.InSlice(allCaps, cap) {
121
+		if !inSlice(allCaps, cap) {
122 122
 			return nil, fmt.Errorf("Unknown capability to add: %q", cap)
123 123
 		}
124 124
 
125 125
 		// add cap if not already in the list
126
-		if !stringutils.InSlice(newCaps, cap) {
126
+		if !inSlice(newCaps, cap) {
127 127
 			newCaps = append(newCaps, strings.ToUpper(cap))
128 128
 		}
129 129
 	}
... ...
@@ -18,7 +18,6 @@ import (
18 18
 	"github.com/docker/docker/oci"
19 19
 	"github.com/docker/docker/pkg/idtools"
20 20
 	"github.com/docker/docker/pkg/mount"
21
-	"github.com/docker/docker/pkg/stringutils"
22 21
 	"github.com/docker/docker/volume"
23 22
 	"github.com/opencontainers/runc/libcontainer/apparmor"
24 23
 	"github.com/opencontainers/runc/libcontainer/cgroups"
... ...
@@ -522,6 +521,17 @@ var (
522 522
 	}
523 523
 )
524 524
 
525
+// inSlice tests whether a string is contained in a slice of strings or not.
526
+// Comparison is case sensitive
527
+func inSlice(slice []string, s string) bool {
528
+	for _, ss := range slice {
529
+		if s == ss {
530
+			return true
531
+		}
532
+	}
533
+	return false
534
+}
535
+
525 536
 func setMounts(daemon *Daemon, s *specs.Spec, c *container.Container, mounts []container.Mount) error {
526 537
 	userMounts := make(map[string]struct{})
527 538
 	for _, m := range mounts {
... ...
@@ -632,7 +642,7 @@ func setMounts(daemon *Daemon, s *specs.Spec, c *container.Container, mounts []c
632 632
 				continue
633 633
 			}
634 634
 			if _, ok := userMounts[m.Destination]; !ok {
635
-				if !stringutils.InSlice(m.Options, "ro") {
635
+				if !inSlice(m.Options, "ro") {
636 636
 					s.Mounts[i].Options = append(s.Mounts[i].Options, "ro")
637 637
 				}
638 638
 			}
... ...
@@ -10,8 +10,8 @@ import (
10 10
 	"github.com/docker/docker/api/types/versions/v1p20"
11 11
 	"github.com/docker/docker/client"
12 12
 	"github.com/docker/docker/integration-cli/checker"
13
-	"github.com/docker/docker/pkg/stringutils"
14 13
 	"github.com/go-check/check"
14
+	"github.com/stretchr/testify/assert"
15 15
 )
16 16
 
17 17
 func (s *DockerSuite) TestInspectAPIContainerResponse(c *check.C) {
... ...
@@ -115,8 +115,8 @@ func (s *DockerSuite) TestInspectAPIImageResponse(c *check.C) {
115 115
 	c.Assert(err, checker.IsNil)
116 116
 
117 117
 	c.Assert(imageJSON.RepoTags, checker.HasLen, 2)
118
-	c.Assert(stringutils.InSlice(imageJSON.RepoTags, "busybox:latest"), checker.Equals, true)
119
-	c.Assert(stringutils.InSlice(imageJSON.RepoTags, "busybox:mytag"), checker.Equals, true)
118
+	assert.Contains(c, imageJSON.RepoTags, "busybox:latest")
119
+	assert.Contains(c, imageJSON.RepoTags, "busybox:mytag")
120 120
 }
121 121
 
122 122
 // #17131, #17139, #17173
... ...
@@ -14,9 +14,9 @@ import (
14 14
 	"github.com/docker/docker/integration-cli/checker"
15 15
 	"github.com/docker/docker/integration-cli/cli"
16 16
 	"github.com/docker/docker/integration-cli/cli/build"
17
-	"github.com/docker/docker/pkg/stringutils"
18 17
 	"github.com/go-check/check"
19 18
 	"github.com/opencontainers/go-digest"
19
+	"github.com/stretchr/testify/assert"
20 20
 )
21 21
 
22 22
 var (
... ...
@@ -403,7 +403,7 @@ func (s *DockerRegistrySuite) TestInspectImageWithDigests(c *check.C) {
403 403
 	c.Assert(err, checker.IsNil)
404 404
 	c.Assert(imageJSON, checker.HasLen, 1)
405 405
 	c.Assert(imageJSON[0].RepoDigests, checker.HasLen, 1)
406
-	c.Assert(stringutils.InSlice(imageJSON[0].RepoDigests, imageReference), checker.Equals, true)
406
+	assert.Contains(c, imageJSON[0].RepoDigests, imageReference)
407 407
 }
408 408
 
409 409
 func (s *DockerRegistrySuite) TestPsListContainersFilterAncestorImageByDigest(c *check.C) {
... ...
@@ -41,17 +41,6 @@ func Truncate(s string, maxlen int) string {
41 41
 	return string(r[:maxlen])
42 42
 }
43 43
 
44
-// InSlice tests whether a string is contained in a slice of strings or not.
45
-// Comparison is case insensitive
46
-func InSlice(slice []string, s string) bool {
47
-	for _, ss := range slice {
48
-		if strings.ToLower(s) == strings.ToLower(ss) {
49
-			return true
50
-		}
51
-	}
52
-	return false
53
-}
54
-
55 44
 func quote(word string, buf *bytes.Buffer) {
56 45
 	// Bail out early for "simple" strings
57 46
 	if word != "" && !strings.ContainsAny(word, "\\'\"`${[|&;<>()~*?! \t\n") {
... ...
@@ -77,23 +77,6 @@ func TestTruncate(t *testing.T) {
77 77
 	}
78 78
 }
79 79
 
80
-func TestInSlice(t *testing.T) {
81
-	slice := []string{"t🐳st", "in", "slice"}
82
-
83
-	test := InSlice(slice, "t🐳st")
84
-	if !test {
85
-		t.Fatalf("Expected string t🐳st to be in slice")
86
-	}
87
-	test = InSlice(slice, "SLICE")
88
-	if !test {
89
-		t.Fatalf("Expected string SLICE to be in slice")
90
-	}
91
-	test = InSlice(slice, "notinslice")
92
-	if test {
93
-		t.Fatalf("Expected string notinslice not to be in slice")
94
-	}
95
-}
96
-
97 80
 func TestShellQuoteArgumentsEmpty(t *testing.T) {
98 81
 	actual := ShellQuoteArguments([]string{})
99 82
 	expected := ""
... ...
@@ -8,7 +8,6 @@ import (
8 8
 	"fmt"
9 9
 
10 10
 	"github.com/docker/docker/api/types"
11
-	"github.com/docker/docker/pkg/stringutils"
12 11
 	"github.com/opencontainers/runtime-spec/specs-go"
13 12
 	libseccomp "github.com/seccomp/libseccomp-golang"
14 13
 )
... ...
@@ -39,6 +38,17 @@ var nativeToSeccomp = map[string]types.Arch{
39 39
 	"s390x":       types.ArchS390X,
40 40
 }
41 41
 
42
+// inSlice tests whether a string is contained in a slice of strings or not.
43
+// Comparison is case sensitive
44
+func inSlice(slice []string, s string) bool {
45
+	for _, ss := range slice {
46
+		if s == ss {
47
+			return true
48
+		}
49
+	}
50
+	return false
51
+}
52
+
42 53
 func setupSeccomp(config *types.Seccomp, rs *specs.Spec) (*specs.LinuxSeccomp, error) {
43 54
 	if config == nil {
44 55
 		return nil, nil
... ...
@@ -89,25 +99,25 @@ Loop:
89 89
 	// Loop through all syscall blocks and convert them to libcontainer format after filtering them
90 90
 	for _, call := range config.Syscalls {
91 91
 		if len(call.Excludes.Arches) > 0 {
92
-			if stringutils.InSlice(call.Excludes.Arches, arch) {
92
+			if inSlice(call.Excludes.Arches, arch) {
93 93
 				continue Loop
94 94
 			}
95 95
 		}
96 96
 		if len(call.Excludes.Caps) > 0 {
97 97
 			for _, c := range call.Excludes.Caps {
98
-				if stringutils.InSlice(rs.Process.Capabilities.Effective, c) {
98
+				if inSlice(rs.Process.Capabilities.Effective, c) {
99 99
 					continue Loop
100 100
 				}
101 101
 			}
102 102
 		}
103 103
 		if len(call.Includes.Arches) > 0 {
104
-			if !stringutils.InSlice(call.Includes.Arches, arch) {
104
+			if !inSlice(call.Includes.Arches, arch) {
105 105
 				continue Loop
106 106
 			}
107 107
 		}
108 108
 		if len(call.Includes.Caps) > 0 {
109 109
 			for _, c := range call.Includes.Caps {
110
-				if !stringutils.InSlice(rs.Process.Capabilities.Effective, c) {
110
+				if !inSlice(rs.Process.Capabilities.Effective, c) {
111 111
 					continue Loop
112 112
 				}
113 113
 			}