Do not assume there is no duplicated elements in arrays.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2019-03-28 01:38:04 -07:00
parent 9bd49c98c6
commit 4b4182cf59
3 changed files with 34 additions and 18 deletions

View File

@ -34,6 +34,7 @@ func TestPodHostname(t *testing.T) {
for name, test := range map[string]struct { for name, test := range map[string]struct {
opts []PodSandboxOpts opts []PodSandboxOpts
expectedHostname string expectedHostname string
expectErr bool
}{ }{
"regular pod with custom hostname": { "regular pod with custom hostname": {
opts: []PodSandboxOpts{ opts: []PodSandboxOpts{
@ -47,12 +48,12 @@ func TestPodHostname(t *testing.T) {
}, },
expectedHostname: hostname, expectedHostname: hostname,
}, },
"host network pod with custom hostname": { "host network pod with custom hostname should fail": {
opts: []PodSandboxOpts{ opts: []PodSandboxOpts{
WithHostNetwork, WithHostNetwork,
WithPodHostname("test-hostname"), WithPodHostname("test-hostname"),
}, },
expectedHostname: "test-hostname", expectErr: true,
}, },
} { } {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
@ -64,11 +65,21 @@ func TestPodHostname(t *testing.T) {
t.Log("Create a sandbox with hostname") t.Log("Create a sandbox with hostname")
sbConfig := PodSandboxConfig("sandbox", "hostname", opts...) sbConfig := PodSandboxConfig("sandbox", "hostname", opts...)
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler) sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
require.NoError(t, err) if err != nil {
defer func() { if !test.expectErr {
assert.NoError(t, runtimeService.StopPodSandbox(sb)) t.Fatalf("Unexpected RunPodSandbox error: %v", err)
assert.NoError(t, runtimeService.RemovePodSandbox(sb)) }
}() return
} else {
// Make sure the sandbox is cleaned up.
defer func() {
assert.NoError(t, runtimeService.StopPodSandbox(sb))
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
}()
if test.expectErr {
t.Fatalf("Expected RunPodSandbox to return error")
}
}
const ( const (
testImage = "busybox" testImage = "busybox"

View File

@ -64,15 +64,19 @@ func WithAdditionalGIDs(userstr string) oci.SpecOpts {
} }
func mergeGids(gids1, gids2 []uint32) []uint32 { func mergeGids(gids1, gids2 []uint32) []uint32 {
gidsMap := make(map[uint32]struct{})
for _, gid1 := range gids1 { for _, gid1 := range gids1 {
for i, gid2 := range gids2 { gidsMap[gid1] = struct{}{}
if gid1 == gid2 {
gids2 = append(gids2[:i], gids2[i+1:]...)
break
}
}
} }
return append(gids1, gids2...) for _, gid2 := range gids2 {
gidsMap[gid2] = struct{}{}
}
var gids []uint32
for gid := range gidsMap {
gids = append(gids, gid)
}
sort.Slice(gids, func(i, j int) bool { return gids[i] < gids[j] })
return gids
} }
// WithoutRunMount removes the `/run` inside the spec // WithoutRunMount removes the `/run` inside the spec
@ -668,12 +672,13 @@ func WithoutNamespace(t runtimespec.LinuxNamespaceType) oci.SpecOpts {
if s.Linux == nil { if s.Linux == nil {
return nil return nil
} }
var namespaces []runtimespec.LinuxNamespace
for i, ns := range s.Linux.Namespaces { for i, ns := range s.Linux.Namespaces {
if ns.Type == t { if ns.Type != t {
s.Linux.Namespaces = append(s.Linux.Namespaces[:i], s.Linux.Namespaces[i+1:]...) namespaces = append(namespaces, s.Linux.Namespaces[i])
return nil
} }
} }
s.Linux.Namespaces = namespaces
return nil return nil
} }
} }

View File

@ -28,7 +28,7 @@ import (
func TestMergeGids(t *testing.T) { func TestMergeGids(t *testing.T) {
gids1 := []uint32{3, 2, 1} gids1 := []uint32{3, 2, 1}
gids2 := []uint32{2, 3, 4} gids2 := []uint32{2, 3, 4}
assert.Equal(t, []uint32{3, 2, 1, 4}, mergeGids(gids1, gids2)) assert.Equal(t, []uint32{1, 2, 3, 4}, mergeGids(gids1, gids2))
} }
func TestOrderedMounts(t *testing.T) { func TestOrderedMounts(t *testing.T) {