From 4661c239dc6394aba960ba73144f2a7e3859537f Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 16 Dec 2014 15:32:54 -0800 Subject: [PATCH] Add type for namespaces for better UI This adds `type Namespaces []Namespace` so that methods can be added to this slice so that it is easier for consumers to work with the values. Signed-off-by: Michael Crosby --- config.go | 35 ++++++++++++++++++++++++++++++++++- integration/exec_test.go | 20 +++----------------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/config.go b/config.go index 94c2bd98..33e55aa8 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,39 @@ type Namespace struct { Path string `json:"path,omitempty"` } +type Namespaces []Namespace + +func (n Namespaces) Exists(name string) bool { + return n.index(name) != -1 +} + +func (n Namespaces) Remove(name string) bool { + i := n.index(name) + if i == -1 { + return false + } + n = append(n[:i], n[i+1:]...) + return true +} + +func (n Namespaces) Add(name, path string) { + i := n.index(name) + if i == -1 { + n = append(n, Namespace{Name: name, Path: path}) + return + } + n[i].Path = path +} + +func (n Namespaces) index(name string) int { + for i, ns := range n { + if ns.Name == name { + return i + } + } + return -1 +} + // Config defines configuration options for executing a process inside a contained environment. type Config struct { // Mount specific options. @@ -45,7 +78,7 @@ type Config struct { // Namespaces specifies the container's namespaces that it should setup when cloning the init process // If a namespace is not provided that namespace is shared from the container's parent process - Namespaces []Namespace `json:"namespaces,omitempty"` + Namespaces Namespaces `json:"namespaces,omitempty"` // Capabilities specify the capabilities to keep when executing the process inside the container // All capbilities not specified will be dropped from the processes capability mask diff --git a/integration/exec_test.go b/integration/exec_test.go index cf749efb..bb2d6c11 100644 --- a/integration/exec_test.go +++ b/integration/exec_test.go @@ -4,8 +4,6 @@ import ( "os" "strings" "testing" - - "github.com/docker/libcontainer" ) func TestExecPS(t *testing.T) { @@ -88,8 +86,7 @@ func TestIPCHost(t *testing.T) { } config := newTemplateConfig(rootfs) - i := getNamespaceIndex(config, "NEWIPC") - config.Namespaces = append(config.Namespaces[:i], config.Namespaces[i+1:]...) + config.Namespaces.Remove("NEWIPC") buffers, exitCode, err := runContainer(config, "", "readlink", "/proc/self/ns/ipc") if err != nil { t.Fatal(err) @@ -121,8 +118,7 @@ func TestIPCJoinPath(t *testing.T) { } config := newTemplateConfig(rootfs) - i := getNamespaceIndex(config, "NEWIPC") - config.Namespaces[i].Path = "/proc/1/ns/ipc" + config.Namespaces.Add("NEWIPC", "/proc/1/ns/ipc") buffers, exitCode, err := runContainer(config, "", "readlink", "/proc/self/ns/ipc") if err != nil { @@ -150,8 +146,7 @@ func TestIPCBadPath(t *testing.T) { defer remove(rootfs) config := newTemplateConfig(rootfs) - i := getNamespaceIndex(config, "NEWIPC") - config.Namespaces[i].Path = "/proc/1/ns/ipcc" + config.Namespaces.Add("NEWIPC", "/proc/1/ns/ipcc") _, _, err = runContainer(config, "", "true") if err == nil { @@ -179,12 +174,3 @@ func TestRlimit(t *testing.T) { t.Fatalf("expected rlimit to be 1024, got %s", limit) } } - -func getNamespaceIndex(config *libcontainer.Config, name string) int { - for i, v := range config.Namespaces { - if v.Name == name { - return i - } - } - return -1 -}