From 1e30673adc7d1c211bc247e7fbdf2e28fbd75fe9 Mon Sep 17 00:00:00 2001 From: l00397676 Date: Wed, 14 Nov 2018 20:18:46 +0800 Subject: [PATCH] test: add tests for sandbox creation rollback and cleanup Adding unit tests for rollback ops when sandbox creation failling. Fixes: #1257 Signed-off-by: l00397676 --- virtcontainers/hyperstart_agent_test.go | 20 ++++++++++ virtcontainers/kata_agent_test.go | 19 ++++++++++ virtcontainers/sandbox_test.go | 49 +++++++++++++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/virtcontainers/hyperstart_agent_test.go b/virtcontainers/hyperstart_agent_test.go index 69d75e3b73..6d8aaae30b 100644 --- a/virtcontainers/hyperstart_agent_test.go +++ b/virtcontainers/hyperstart_agent_test.go @@ -10,6 +10,8 @@ import ( "fmt" "io/ioutil" "net" + "os" + "path" "reflect" "testing" @@ -276,3 +278,21 @@ func TestHyperCopyFile(t *testing.T) { err := h.copyFile("", "") assert.Nil(err) } + +func TestHyperCleanupSandbox(t *testing.T) { + assert := assert.New(t) + + s := Sandbox{ + id: "testFoo", + } + dir := path.Join(defaultSharedDir, s.id) + err := os.MkdirAll(dir, 0777) + assert.Nil(err) + + h := &hyper{} + h.cleanup(s.id) + + if _, err = os.Stat(dir); os.IsExist(err) { + t.Fatalf("%s still exists\n", dir) + } +} diff --git a/virtcontainers/kata_agent_test.go b/virtcontainers/kata_agent_test.go index 5a55cbd7e9..bdc869a0e3 100644 --- a/virtcontainers/kata_agent_test.go +++ b/virtcontainers/kata_agent_test.go @@ -12,6 +12,7 @@ import ( "io/ioutil" "net" "os" + "path" "path/filepath" "reflect" "strings" @@ -926,3 +927,21 @@ func TestKataCopyFile(t *testing.T) { err = k.copyFile(src.Name(), dst.Name()) assert.NoError(err) } + +func TestKataCleanupSandbox(t *testing.T) { + assert := assert.New(t) + + s := Sandbox{ + id: "testFoo", + } + dir := path.Join(kataHostSharedDir, s.id) + err := os.MkdirAll(dir, 0777) + assert.Nil(err) + + k := &kataAgent{} + k.cleanup(s.id) + + if _, err = os.Stat(dir); os.IsExist(err) { + t.Fatalf("%s still exists\n", dir) + } +} diff --git a/virtcontainers/sandbox_test.go b/virtcontainers/sandbox_test.go index c60d123173..1c644256d9 100644 --- a/virtcontainers/sandbox_test.go +++ b/virtcontainers/sandbox_test.go @@ -12,6 +12,7 @@ import ( "io/ioutil" "os" "os/exec" + "path" "path/filepath" "reflect" "sync" @@ -1525,3 +1526,51 @@ func TestSandboxStopStopped(t *testing.T) { assert.Nil(t, err) } + +func checkDirNotExist(path string) error { + if _, err := os.Stat(path); os.IsExist(err) { + return fmt.Errorf("%s is still exists", path) + } + return nil +} + +func checkSandboxRemains() error { + var err error + if err = checkDirNotExist(sandboxDirConfig); err != nil { + return fmt.Errorf("%s still exists", sandboxDirConfig) + } + if err = checkDirNotExist(sandboxDirState); err != nil { + return fmt.Errorf("%s still exists", sandboxDirState) + } + if err = checkDirNotExist(path.Join(kataHostSharedDir, testSandboxID)); err != nil { + return fmt.Errorf("%s still exists", path.Join(kataHostSharedDir, testSandboxID)) + } + if _, err = globalSandboxList.lookupSandbox(testSandboxID); err == nil { + return fmt.Errorf("globalSandboxList for %s stil exists", testSandboxID) + } + + return nil +} + +func TestSandboxCreationFromConfigRollbackFromCreateSandbox(t *testing.T) { + cleanUp() + assert := assert.New(t) + ctx := context.Background() + hConf := newHypervisorConfig(nil, nil) + sConf := SandboxConfig{ + ID: testSandboxID, + HypervisorType: QemuHypervisor, + HypervisorConfig: hConf, + AgentType: KataContainersAgent, + NetworkConfig: NetworkConfig{}, + Volumes: nil, + Containers: nil, + } + _, err := createSandboxFromConfig(ctx, sConf, nil) + // Fail at createSandbox: QEMU path does not exist, it is expected. Then rollback is called + assert.Error(err) + + // check dirs + err = checkSandboxRemains() + assert.NoError(err) +}