Merge pull request #880 from jingxiaolu/rollback_when_creation_fail

test: add tests for sandbox creation rollback and cleanup
This commit is contained in:
Haomin Tsai 2019-03-09 14:12:22 +08:00 committed by GitHub
commit 7ff18192a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)
}