mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-16 08:26:16 +00:00
Merge pull request #880 from jingxiaolu/rollback_when_creation_fail
test: add tests for sandbox creation rollback and cleanup
This commit is contained in:
commit
7ff18192a4
@ -10,6 +10,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -276,3 +278,21 @@ func TestHyperCopyFile(t *testing.T) {
|
|||||||
err := h.copyFile("", "")
|
err := h.copyFile("", "")
|
||||||
assert.Nil(err)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
@ -926,3 +927,21 @@ func TestKataCopyFile(t *testing.T) {
|
|||||||
err = k.copyFile(src.Name(), dst.Name())
|
err = k.copyFile(src.Name(), dst.Name())
|
||||||
assert.NoError(err)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
@ -1525,3 +1526,51 @@ func TestSandboxStopStopped(t *testing.T) {
|
|||||||
|
|
||||||
assert.Nil(t, err)
|
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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user