Merge pull request #4452 from crosbymichael/small-fixes-to-libcontainer

Add find tests and remove panic in DEBUG
This commit is contained in:
Michael Crosby 2014-03-04 14:37:41 -05:00
commit 4f641377e0
2 changed files with 47 additions and 9 deletions

View file

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/syndtr/gocapability/capability" "github.com/syndtr/gocapability/capability"
"os"
) )
var ( var (
@ -74,16 +73,18 @@ func GetNamespace(key string) *Namespace {
return ns return ns
} }
} }
if os.Getenv("DEBUG") != "" {
panic("Unreachable: Namespace not found")
}
return nil return nil
} }
// Contains returns true if the specified Namespace is // Contains returns true if the specified Namespace is
// in the slice // in the slice
func (n Namespaces) Contains(ns string) bool { func (n Namespaces) Contains(ns string) bool {
return GetNamespace(ns) != nil for _, nsp := range n {
if nsp.Key == ns {
return true
}
}
return false
} }
type ( type (
@ -121,14 +122,16 @@ func GetCapability(key string) *Capability {
return capp return capp
} }
} }
if os.Getenv("DEBUG") != "" {
panic("Unreachable: Capability not found")
}
return nil return nil
} }
// Contains returns true if the specified Capability is // Contains returns true if the specified Capability is
// in the slice // in the slice
func (c Capabilities) Contains(capp string) bool { func (c Capabilities) Contains(capp string) bool {
return GetCapability(capp) != nil for _, cap := range c {
if cap.Key == capp {
return true
}
}
return false
} }

View file

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