From bd2d7a377c38b6fc075edd49e27ac3dc0a93ad27 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 4 Mar 2014 08:55:12 -0800 Subject: [PATCH] Add find tests and remove panic in DEBUG Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- libcontainer/types.go | 21 ++++++++++++--------- libcontainer/types_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 libcontainer/types_test.go diff --git a/libcontainer/types.go b/libcontainer/types.go index 8c28530..94fe876 100644 --- a/libcontainer/types.go +++ b/libcontainer/types.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "github.com/syndtr/gocapability/capability" - "os" ) var ( @@ -74,16 +73,18 @@ func GetNamespace(key string) *Namespace { return ns } } - if os.Getenv("DEBUG") != "" { - panic("Unreachable: Namespace not found") - } return nil } // Contains returns true if the specified Namespace is // in the slice func (n Namespaces) Contains(ns string) bool { - return GetNamespace(ns) != nil + for _, nsp := range n { + if nsp.Key == ns { + return true + } + } + return false } type ( @@ -121,14 +122,16 @@ func GetCapability(key string) *Capability { return capp } } - if os.Getenv("DEBUG") != "" { - panic("Unreachable: Capability not found") - } return nil } // Contains returns true if the specified Capability is // in the slice func (c Capabilities) Contains(capp string) bool { - return GetCapability(capp) != nil + for _, cap := range c { + if cap.Key == capp { + return true + } + } + return false } diff --git a/libcontainer/types_test.go b/libcontainer/types_test.go new file mode 100644 index 0000000..52b85a4 --- /dev/null +++ b/libcontainer/types_test.go @@ -0,0 +1,35 @@ +package libcontainer + +import ( + "testing" +) + +func TestNamespacesContains(t *testing.T) { + ns := Namespaces{ + GetNamespace("NEWPID"), + GetNamespace("NEWNS"), + GetNamespace("NEWUTS"), + } + + if ns.Contains("NEWNET") { + t.Fatal("namespaces should not contain NEWNET") + } + + if !ns.Contains("NEWPID") { + t.Fatal("namespaces should contain NEWPID but does not") + } +} + +func TestCapabilitiesContains(t *testing.T) { + caps := Capabilities{ + GetCapability("MKNOD"), + GetCapability("SETPCAP"), + } + + if caps.Contains("SYS_ADMIN") { + t.Fatal("capabilities should not contain SYS_ADMIN") + } + if !caps.Contains("MKNOD") { + t.Fatal("capabilities should container MKNOD but does not") + } +}