From d042d5c0da083098763567ca0fa549e7caa13515 Mon Sep 17 00:00:00 2001 From: Julio Montes Date: Tue, 7 Jan 2020 18:51:36 +0000 Subject: [PATCH] virtcontainers: fix unit tests fix unit test that may need a cgroup path or root to create a new cgroup Signed-off-by: Julio Montes --- virtcontainers/api_test.go | 3 ++- virtcontainers/sandbox_test.go | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/virtcontainers/api_test.go b/virtcontainers/api_test.go index 42cff7328..70ecfb928 100644 --- a/virtcontainers/api_test.go +++ b/virtcontainers/api_test.go @@ -43,7 +43,8 @@ var containerAnnotations = map[string]string{ func newEmptySpec() *specs.Spec { return &specs.Spec{ Linux: &specs.Linux{ - Resources: &specs.LinuxResources{}, + Resources: &specs.LinuxResources{}, + CgroupsPath: defaultCgroupPath, }, Process: &specs.Process{ Capabilities: &specs.LinuxCapabilities{}, diff --git a/virtcontainers/sandbox_test.go b/virtcontainers/sandbox_test.go index 054db6331..f47ad857a 100644 --- a/virtcontainers/sandbox_test.go +++ b/virtcontainers/sandbox_test.go @@ -1454,19 +1454,22 @@ func TestSandbox_SetupSandboxCgroup(t *testing.T) { successfulContainer.Annotations[annotations.ContainerTypeKey] = string(PodSandbox) tests := []struct { - name string - s *Sandbox - wantErr bool + name string + s *Sandbox + wantErr bool + needRoot bool }{ { "New sandbox", &Sandbox{}, true, + false, }, { "New sandbox, new config", &Sandbox{config: &SandboxConfig{}}, true, + false, }, { "sandbox, container no sandbox type", @@ -1475,6 +1478,7 @@ func TestSandbox_SetupSandboxCgroup(t *testing.T) { {}, }}}, true, + false, }, { "sandbox, container sandbox type", @@ -1483,6 +1487,7 @@ func TestSandbox_SetupSandboxCgroup(t *testing.T) { sandboxContainer, }}}, true, + false, }, { "sandbox, empty linux json", @@ -1491,6 +1496,7 @@ func TestSandbox_SetupSandboxCgroup(t *testing.T) { emptyJSONLinux, }}}, false, + true, }, { "sandbox, successful config", @@ -1499,9 +1505,14 @@ func TestSandbox_SetupSandboxCgroup(t *testing.T) { successfulContainer, }}}, false, + true, }, } for _, tt := range tests { + if tt.needRoot && os.Getuid() != 0 { + t.Skip(tt.name + "needs root") + } + t.Run(tt.name, func(t *testing.T) { if err := tt.s.setupSandboxCgroup(); (err != nil) != tt.wantErr { t.Errorf("Sandbox.SetupSandboxCgroupOnly() error = %v, wantErr %v", err, tt.wantErr)