This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
forked from opencontainers/runtime-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
validation: add a new test for NSNewNSWithoutPath
This test is to check for NSNewNSWithoutPath, i.e. "If path is not specified, the runtime MUST create a new container namespace of the given type" See also opencontainers#572
- Loading branch information
Dongsu Park
committed
Apr 12, 2018
1 parent
8b973a6
commit 05c200f
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/mndrix/tap-go" | ||
rspec "github.com/opencontainers/runtime-spec/specs-go" | ||
"github.com/opencontainers/runtime-tools/specerror" | ||
"github.com/opencontainers/runtime-tools/validation/util" | ||
) | ||
|
||
func testNamespaceNoPath(t *tap.T) error { | ||
hostNsPath := fmt.Sprintf("/proc/%d/ns", os.Getpid()) | ||
hostNsInodes := map[string]string{} | ||
for _, nsName := range util.ProcNamespaces { | ||
nsInode, err := os.Readlink(filepath.Join(hostNsPath, nsName)) | ||
if err != nil { | ||
return err | ||
} | ||
hostNsInodes[nsName] = nsInode | ||
} | ||
|
||
g := util.GetDefaultGenerator() | ||
|
||
// As the namespaces, cgroups and user, are not set by GetDefaultGenerator(), | ||
// others are set by default. We just set them explicitly to avoid confusion. | ||
g.AddOrReplaceLinuxNamespace("cgroup", "") | ||
g.AddOrReplaceLinuxNamespace("ipc", "") | ||
g.AddOrReplaceLinuxNamespace("mount", "") | ||
g.AddOrReplaceLinuxNamespace("network", "") | ||
g.AddOrReplaceLinuxNamespace("pid", "") | ||
g.AddOrReplaceLinuxNamespace("user", "") | ||
g.AddOrReplaceLinuxNamespace("uts", "") | ||
|
||
err := util.RuntimeOutsideValidate(g, func(config *rspec.Spec, state *rspec.State) error { | ||
containerNsPath := fmt.Sprintf("/proc/%d/ns", state.Pid) | ||
|
||
for _, nsName := range util.ProcNamespaces { | ||
nsInode, err := os.Readlink(filepath.Join(containerNsPath, nsName)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t.Ok(hostNsInodes[nsName] != nsInode, fmt.Sprintf("create namespace %s without path", nsName)) | ||
if hostNsInodes[nsName] == nsInode { | ||
specErr := specerror.NewError(specerror.NSNewNSWithoutPath, | ||
fmt.Errorf("both namespaces for %s have the same inode %s", nsName, nsInode), | ||
rspec.Version) | ||
diagnostic := map[string]interface{}{ | ||
"expected": fmt.Sprintf("!= %s", hostNsInodes[nsName]), | ||
"actual": nsInode, | ||
"namespace type": nsName, | ||
"level": specErr.(*specerror.Error).Err.Level, | ||
"reference": specErr.(*specerror.Error).Err.Reference, | ||
} | ||
t.YAML(diagnostic) | ||
|
||
continue | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
return err | ||
} | ||
|
||
func main() { | ||
t := tap.New() | ||
t.Header(0) | ||
|
||
if "linux" != runtime.GOOS { | ||
t.Skip(1, fmt.Sprintf("linux-specific namespace test")) | ||
} | ||
|
||
err := testNamespaceNoPath(t) | ||
if err != nil { | ||
util.Fatal(err) | ||
} | ||
|
||
t.AutoPlan() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package util | ||
|
||
// NOTE: this list of namespaces to be found under /proc/*/ns/ is not the | ||
// same as generate.Namespaces, because of naming mismatches like | ||
// "mnt" vs "mount" or "net" vs "network". | ||
var ProcNamespaces = []string{ | ||
"cgroup", | ||
"ipc", | ||
"mnt", | ||
"net", | ||
"pid", | ||
"user", | ||
"uts", | ||
} |