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 <lujingxiao@huawei.com>
This commit is contained in:
l00397676 2018-11-14 20:18:46 +08:00
parent 886d859fbe
commit 1e30673adc
3 changed files with 88 additions and 0 deletions

View File

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

View File

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

View File

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