mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-13 13:46:46 +00:00
Merge pull request #4141 from dgibson/cleanup-tmp
Fix Go unit tests to clean up /tmp after themselves
This commit is contained in:
commit
5b6e45ed6c
@ -50,7 +50,6 @@ func TestCreateSandboxSuccess(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
||||||
// defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -99,7 +98,6 @@ func TestCreateSandboxFail(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -137,7 +135,6 @@ func TestCreateSandboxConfigFail(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
tmpdir, bundlePath, _ := ktu.SetupOCIConfigFile(t)
|
tmpdir, bundlePath, _ := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -187,7 +184,6 @@ func TestCreateContainerSuccess(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -227,7 +223,6 @@ func TestCreateContainerFail(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -278,7 +273,6 @@ func TestCreateContainerConfigFail(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
package containerdshim
|
package containerdshim
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
taskAPI "github.com/containerd/containerd/runtime/v2/task"
|
taskAPI "github.com/containerd/containerd/runtime/v2/task"
|
||||||
@ -25,8 +24,8 @@ func TestDeleteContainerSuccessAndFail(t *testing.T) {
|
|||||||
MockID: testSandboxID,
|
MockID: testSandboxID,
|
||||||
}
|
}
|
||||||
|
|
||||||
rootPath, bundlePath, _ := ktu.SetupOCIConfigFile(t)
|
_, bundlePath, _ := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(rootPath)
|
|
||||||
_, err := compatoci.ParseConfigJSON(bundlePath)
|
_, err := compatoci.ParseConfigJSON(bundlePath)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
|
@ -41,8 +41,7 @@ func TestServiceCreate(t *testing.T) {
|
|||||||
|
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
tmpdir, bundleDir, _ := ktu.SetupOCIConfigFile(t)
|
_, bundleDir, _ := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
@ -346,11 +346,10 @@ func IsInGitHubActions() bool {
|
|||||||
func SetupOCIConfigFile(t *testing.T) (rootPath string, bundlePath, ociConfigFile string) {
|
func SetupOCIConfigFile(t *testing.T) (rootPath string, bundlePath, ociConfigFile string) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
tmpdir, err := os.MkdirTemp("", "katatest-")
|
tmpdir := t.TempDir()
|
||||||
assert.NoError(err)
|
|
||||||
|
|
||||||
bundlePath = filepath.Join(tmpdir, "bundle")
|
bundlePath = filepath.Join(tmpdir, "bundle")
|
||||||
err = os.MkdirAll(bundlePath, testDirMode)
|
err := os.MkdirAll(bundlePath, testDirMode)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
ociConfigFile = filepath.Join(bundlePath, "config.json")
|
ociConfigFile = filepath.Join(bundlePath, "config.json")
|
||||||
|
@ -212,7 +212,6 @@ func TestCreateSandboxConfigFail(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
tmpdir, bundlePath, _ := ktu.SetupOCIConfigFile(t)
|
tmpdir, bundlePath, _ := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -246,7 +245,6 @@ func TestCreateSandboxFail(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
tmpdir, bundlePath, _ := ktu.SetupOCIConfigFile(t)
|
tmpdir, bundlePath, _ := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -269,7 +267,6 @@ func TestCreateSandboxAnnotations(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
tmpdir, bundlePath, _ := ktu.SetupOCIConfigFile(t)
|
tmpdir, bundlePath, _ := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -342,8 +339,7 @@ func TestCheckForFips(t *testing.T) {
|
|||||||
func TestCreateContainerContainerConfigFail(t *testing.T) {
|
func TestCreateContainerContainerConfigFail(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
_, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
spec, err := compatoci.ParseConfigJSON(bundlePath)
|
spec, err := compatoci.ParseConfigJSON(bundlePath)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -370,8 +366,7 @@ func TestCreateContainerContainerConfigFail(t *testing.T) {
|
|||||||
func TestCreateContainerFail(t *testing.T) {
|
func TestCreateContainerFail(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
_, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
spec, err := compatoci.ParseConfigJSON(bundlePath)
|
spec, err := compatoci.ParseConfigJSON(bundlePath)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -405,8 +400,7 @@ func TestCreateContainer(t *testing.T) {
|
|||||||
mockSandbox.CreateContainerFunc = nil
|
mockSandbox.CreateContainerFunc = nil
|
||||||
}()
|
}()
|
||||||
|
|
||||||
tmpdir, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
_, bundlePath, ociConfigFile := ktu.SetupOCIConfigFile(t)
|
||||||
defer os.RemoveAll(tmpdir)
|
|
||||||
|
|
||||||
spec, err := compatoci.ParseConfigJSON(bundlePath)
|
spec, err := compatoci.ParseConfigJSON(bundlePath)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
@ -22,6 +22,7 @@ var testContainerIDHook = "test-container-id"
|
|||||||
var testControllerIDHook = "test-controller-id"
|
var testControllerIDHook = "test-controller-id"
|
||||||
var testBinHookPath = "mockhook/hook"
|
var testBinHookPath = "mockhook/hook"
|
||||||
var testBundlePath = "/test/bundle"
|
var testBundlePath = "/test/bundle"
|
||||||
|
var mockHookLogFile = "/tmp/mock_hook.log"
|
||||||
|
|
||||||
func getMockHookBinPath() string {
|
func getMockHookBinPath() string {
|
||||||
return testBinHookPath
|
return testBinHookPath
|
||||||
@ -49,12 +50,17 @@ func createWrongHook() specs.Hook {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cleanMockHookLogFile() {
|
||||||
|
_ = os.Remove(mockHookLogFile)
|
||||||
|
}
|
||||||
|
|
||||||
func TestRunHook(t *testing.T) {
|
func TestRunHook(t *testing.T) {
|
||||||
if tc.NotValid(ktu.NeedRoot()) {
|
if tc.NotValid(ktu.NeedRoot()) {
|
||||||
t.Skip(ktu.TestDisabledNeedRoot)
|
t.Skip(ktu.TestDisabledNeedRoot)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
t.Cleanup(cleanMockHookLogFile)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
spec := specs.Spec{}
|
spec := specs.Spec{}
|
||||||
@ -87,6 +93,7 @@ func TestPreStartHooks(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
t.Cleanup(cleanMockHookLogFile)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
@ -129,6 +136,7 @@ func TestPostStartHooks(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
t.Cleanup(cleanMockHookLogFile)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
@ -173,6 +181,7 @@ func TestPostStopHooks(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
t.Cleanup(cleanMockHookLogFile)
|
||||||
|
|
||||||
// Hooks field is nil
|
// Hooks field is nil
|
||||||
spec := specs.Spec{}
|
spec := specs.Spec{}
|
||||||
|
@ -13,14 +13,12 @@ import (
|
|||||||
|
|
||||||
vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers"
|
vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/factory/direct"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/factory/direct"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/fs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTemplateFactory(t *testing.T) {
|
func TestTemplateFactory(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
testDir := fs.MockStorageRootPath()
|
testDir := t.TempDir()
|
||||||
defer fs.MockStorageDestroy()
|
|
||||||
|
|
||||||
hyperConfig := vc.HypervisorConfig{
|
hyperConfig := vc.HypervisorConfig{
|
||||||
KernelPath: testDir,
|
KernelPath: testDir,
|
||||||
|
@ -12,14 +12,12 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers"
|
vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/fs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTemplateFactory(t *testing.T) {
|
func TestTemplateFactory(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
testDir := fs.MockStorageRootPath()
|
testDir := t.TempDir()
|
||||||
defer fs.MockStorageDestroy()
|
|
||||||
|
|
||||||
hyperConfig := vc.HypervisorConfig{
|
hyperConfig := vc.HypervisorConfig{
|
||||||
KernelPath: testDir,
|
KernelPath: testDir,
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
|
|
||||||
vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers"
|
vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/factory/base"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/factory/base"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/fs"
|
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/mock"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/mock"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -39,10 +38,10 @@ func TestNewFactory(t *testing.T) {
|
|||||||
_, err = NewFactory(ctx, config, false)
|
_, err = NewFactory(ctx, config, false)
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
|
|
||||||
defer fs.MockStorageDestroy()
|
testDir := t.TempDir()
|
||||||
config.VMConfig.HypervisorConfig = vc.HypervisorConfig{
|
config.VMConfig.HypervisorConfig = vc.HypervisorConfig{
|
||||||
KernelPath: fs.MockStorageRootPath(),
|
KernelPath: testDir,
|
||||||
ImagePath: fs.MockStorageRootPath(),
|
ImagePath: testDir,
|
||||||
}
|
}
|
||||||
|
|
||||||
// direct
|
// direct
|
||||||
@ -69,7 +68,7 @@ func TestNewFactory(t *testing.T) {
|
|||||||
defer hybridVSockTTRPCMock.Stop()
|
defer hybridVSockTTRPCMock.Stop()
|
||||||
|
|
||||||
config.Template = true
|
config.Template = true
|
||||||
config.TemplatePath = fs.MockStorageRootPath()
|
config.TemplatePath = testDir
|
||||||
f, err = NewFactory(ctx, config, false)
|
f, err = NewFactory(ctx, config, false)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
f.CloseFactory(ctx)
|
f.CloseFactory(ctx)
|
||||||
@ -134,8 +133,7 @@ func TestCheckVMConfig(t *testing.T) {
|
|||||||
err = checkVMConfig(config1, config2)
|
err = checkVMConfig(config1, config2)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
|
|
||||||
testDir := fs.MockStorageRootPath()
|
testDir := t.TempDir()
|
||||||
defer fs.MockStorageDestroy()
|
|
||||||
|
|
||||||
config1.HypervisorConfig = vc.HypervisorConfig{
|
config1.HypervisorConfig = vc.HypervisorConfig{
|
||||||
KernelPath: testDir,
|
KernelPath: testDir,
|
||||||
@ -155,8 +153,7 @@ func TestCheckVMConfig(t *testing.T) {
|
|||||||
func TestFactoryGetVM(t *testing.T) {
|
func TestFactoryGetVM(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
testDir := fs.MockStorageRootPath()
|
testDir := t.TempDir()
|
||||||
defer fs.MockStorageDestroy()
|
|
||||||
|
|
||||||
hyperConfig := vc.HypervisorConfig{
|
hyperConfig := vc.HypervisorConfig{
|
||||||
KernelPath: testDir,
|
KernelPath: testDir,
|
||||||
@ -321,8 +318,7 @@ func TestDeepCompare(t *testing.T) {
|
|||||||
config.VMConfig = vc.VMConfig{
|
config.VMConfig = vc.VMConfig{
|
||||||
HypervisorType: vc.MockHypervisor,
|
HypervisorType: vc.MockHypervisor,
|
||||||
}
|
}
|
||||||
testDir := fs.MockStorageRootPath()
|
testDir := t.TempDir()
|
||||||
defer fs.MockStorageDestroy()
|
|
||||||
|
|
||||||
config.VMConfig.HypervisorConfig = vc.HypervisorConfig{
|
config.VMConfig.HypervisorConfig = vc.HypervisorConfig{
|
||||||
KernelPath: testDir,
|
KernelPath: testDir,
|
||||||
|
@ -16,7 +16,6 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers"
|
vc "github.com/kata-containers/kata-containers/src/runtime/virtcontainers"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/fs"
|
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/mock"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/mock"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -32,8 +31,7 @@ func TestTemplateFactory(t *testing.T) {
|
|||||||
|
|
||||||
templateWaitForAgent = 1 * time.Microsecond
|
templateWaitForAgent = 1 * time.Microsecond
|
||||||
|
|
||||||
testDir := fs.MockStorageRootPath()
|
testDir := t.TempDir()
|
||||||
defer fs.MockStorageDestroy()
|
|
||||||
|
|
||||||
hyperConfig := vc.HypervisorConfig{
|
hyperConfig := vc.HypervisorConfig{
|
||||||
KernelPath: testDir,
|
KernelPath: testDir,
|
||||||
|
@ -27,6 +27,7 @@ import (
|
|||||||
hv "github.com/kata-containers/kata-containers/src/runtime/pkg/hypervisors"
|
hv "github.com/kata-containers/kata-containers/src/runtime/pkg/hypervisors"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils/katatrace"
|
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils/katatrace"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config"
|
||||||
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/fs"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/firecracker/client"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/firecracker/client"
|
||||||
models "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/firecracker/client/models"
|
models "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/firecracker/client/models"
|
||||||
ops "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/firecracker/client/operations"
|
ops "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/firecracker/client/operations"
|
||||||
@ -84,8 +85,6 @@ const (
|
|||||||
fcMetricsFifo = "metrics.fifo"
|
fcMetricsFifo = "metrics.fifo"
|
||||||
|
|
||||||
defaultFcConfig = "fcConfig.json"
|
defaultFcConfig = "fcConfig.json"
|
||||||
// storagePathSuffix mirrors persist/fs/fs.go:storagePathSuffix
|
|
||||||
storagePathSuffix = "vc"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Specify the minimum version of firecracker supported
|
// Specify the minimum version of firecracker supported
|
||||||
@ -244,7 +243,7 @@ func (fc *firecracker) setPaths(hypervisorConfig *HypervisorConfig) {
|
|||||||
// <cgroups_base>/<exec_file_name>/<id>/
|
// <cgroups_base>/<exec_file_name>/<id>/
|
||||||
hypervisorName := filepath.Base(hypervisorConfig.HypervisorPath)
|
hypervisorName := filepath.Base(hypervisorConfig.HypervisorPath)
|
||||||
//fs.RunStoragePath cannot be used as we need exec perms
|
//fs.RunStoragePath cannot be used as we need exec perms
|
||||||
fc.chrootBaseDir = filepath.Join("/run", storagePathSuffix)
|
fc.chrootBaseDir = filepath.Join("/run", fs.StoragePathSuffix)
|
||||||
|
|
||||||
fc.vmPath = filepath.Join(fc.chrootBaseDir, hypervisorName, fc.id)
|
fc.vmPath = filepath.Join(fc.chrootBaseDir, hypervisorName, fc.id)
|
||||||
fc.jailerRoot = filepath.Join(fc.vmPath, "root") // auto created by jailer
|
fc.jailerRoot = filepath.Join(fc.vmPath, "root") // auto created by jailer
|
||||||
|
@ -191,8 +191,7 @@ func TestKataAgentSendReq(t *testing.T) {
|
|||||||
func TestHandleEphemeralStorage(t *testing.T) {
|
func TestHandleEphemeralStorage(t *testing.T) {
|
||||||
k := kataAgent{}
|
k := kataAgent{}
|
||||||
var ociMounts []specs.Mount
|
var ociMounts []specs.Mount
|
||||||
mountSource := "/tmp/mountPoint"
|
mountSource := t.TempDir()
|
||||||
os.Mkdir(mountSource, 0755)
|
|
||||||
|
|
||||||
mount := specs.Mount{
|
mount := specs.Mount{
|
||||||
Type: KataEphemeralDevType,
|
Type: KataEphemeralDevType,
|
||||||
@ -212,8 +211,7 @@ func TestHandleEphemeralStorage(t *testing.T) {
|
|||||||
func TestHandleLocalStorage(t *testing.T) {
|
func TestHandleLocalStorage(t *testing.T) {
|
||||||
k := kataAgent{}
|
k := kataAgent{}
|
||||||
var ociMounts []specs.Mount
|
var ociMounts []specs.Mount
|
||||||
mountSource := "/tmp/mountPoint"
|
mountSource := t.TempDir()
|
||||||
os.Mkdir(mountSource, 0755)
|
|
||||||
|
|
||||||
mount := specs.Mount{
|
mount := specs.Mount{
|
||||||
Type: KataLocalDevType,
|
Type: KataLocalDevType,
|
||||||
@ -688,8 +686,7 @@ func TestHandleShm(t *testing.T) {
|
|||||||
// In case the type of mount is ephemeral, the container mount is not
|
// In case the type of mount is ephemeral, the container mount is not
|
||||||
// shared with the sandbox shm.
|
// shared with the sandbox shm.
|
||||||
ociMounts[0].Type = KataEphemeralDevType
|
ociMounts[0].Type = KataEphemeralDevType
|
||||||
mountSource := "/tmp/mountPoint"
|
mountSource := t.TempDir()
|
||||||
os.Mkdir(mountSource, 0755)
|
|
||||||
ociMounts[0].Source = mountSource
|
ociMounts[0].Source = mountSource
|
||||||
k.handleShm(ociMounts, sandbox)
|
k.handleShm(ociMounts, sandbox)
|
||||||
|
|
||||||
|
@ -381,6 +381,12 @@ func TestBindMountFailingMount(t *testing.T) {
|
|||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cleanupFooMount() {
|
||||||
|
dest := filepath.Join(testDir, "fooDirDest")
|
||||||
|
|
||||||
|
syscall.Unmount(dest, 0)
|
||||||
|
}
|
||||||
|
|
||||||
func TestBindMountSuccessful(t *testing.T) {
|
func TestBindMountSuccessful(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
if tc.NotValid(ktu.NeedRoot()) {
|
if tc.NotValid(ktu.NeedRoot()) {
|
||||||
@ -389,9 +395,7 @@ func TestBindMountSuccessful(t *testing.T) {
|
|||||||
|
|
||||||
source := filepath.Join(testDir, "fooDirSrc")
|
source := filepath.Join(testDir, "fooDirSrc")
|
||||||
dest := filepath.Join(testDir, "fooDirDest")
|
dest := filepath.Join(testDir, "fooDirDest")
|
||||||
syscall.Unmount(dest, 0)
|
t.Cleanup(cleanupFooMount)
|
||||||
os.Remove(source)
|
|
||||||
os.Remove(dest)
|
|
||||||
|
|
||||||
err := os.MkdirAll(source, mountPerm)
|
err := os.MkdirAll(source, mountPerm)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -401,8 +405,6 @@ func TestBindMountSuccessful(t *testing.T) {
|
|||||||
|
|
||||||
err = bindMount(context.Background(), source, dest, false, "private")
|
err = bindMount(context.Background(), source, dest, false, "private")
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
syscall.Unmount(dest, 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBindMountReadonlySuccessful(t *testing.T) {
|
func TestBindMountReadonlySuccessful(t *testing.T) {
|
||||||
@ -413,9 +415,7 @@ func TestBindMountReadonlySuccessful(t *testing.T) {
|
|||||||
|
|
||||||
source := filepath.Join(testDir, "fooDirSrc")
|
source := filepath.Join(testDir, "fooDirSrc")
|
||||||
dest := filepath.Join(testDir, "fooDirDest")
|
dest := filepath.Join(testDir, "fooDirDest")
|
||||||
syscall.Unmount(dest, 0)
|
t.Cleanup(cleanupFooMount)
|
||||||
os.Remove(source)
|
|
||||||
os.Remove(dest)
|
|
||||||
|
|
||||||
err := os.MkdirAll(source, mountPerm)
|
err := os.MkdirAll(source, mountPerm)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -426,8 +426,6 @@ func TestBindMountReadonlySuccessful(t *testing.T) {
|
|||||||
err = bindMount(context.Background(), source, dest, true, "private")
|
err = bindMount(context.Background(), source, dest, true, "private")
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
defer syscall.Unmount(dest, 0)
|
|
||||||
|
|
||||||
// should not be able to create file in read-only mount
|
// should not be able to create file in read-only mount
|
||||||
destFile := filepath.Join(dest, "foo")
|
destFile := filepath.Join(dest, "foo")
|
||||||
_, err = os.OpenFile(destFile, os.O_CREATE, mountPerm)
|
_, err = os.OpenFile(destFile, os.O_CREATE, mountPerm)
|
||||||
@ -442,9 +440,7 @@ func TestBindMountInvalidPgtypes(t *testing.T) {
|
|||||||
|
|
||||||
source := filepath.Join(testDir, "fooDirSrc")
|
source := filepath.Join(testDir, "fooDirSrc")
|
||||||
dest := filepath.Join(testDir, "fooDirDest")
|
dest := filepath.Join(testDir, "fooDirDest")
|
||||||
syscall.Unmount(dest, 0)
|
t.Cleanup(cleanupFooMount)
|
||||||
os.Remove(source)
|
|
||||||
os.Remove(dest)
|
|
||||||
|
|
||||||
err := os.MkdirAll(source, mountPerm)
|
err := os.MkdirAll(source, mountPerm)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
@ -29,11 +29,11 @@ const dirMode = os.FileMode(0700) | os.ModeDir
|
|||||||
// fileMode is the permission bits used for creating a file
|
// fileMode is the permission bits used for creating a file
|
||||||
const fileMode = os.FileMode(0600)
|
const fileMode = os.FileMode(0600)
|
||||||
|
|
||||||
// storagePathSuffix is the suffix used for all storage paths
|
// StoragePathSuffix is the suffix used for all storage paths
|
||||||
//
|
//
|
||||||
// Note: this very brief path represents "virtcontainers". It is as
|
// Note: this very brief path represents "virtcontainers". It is as
|
||||||
// terse as possible to minimise path length.
|
// terse as possible to minimise path length.
|
||||||
const storagePathSuffix = "vc"
|
const StoragePathSuffix = "vc"
|
||||||
|
|
||||||
// sandboxPathSuffix is the suffix used for sandbox storage
|
// sandboxPathSuffix is the suffix used for sandbox storage
|
||||||
const sandboxPathSuffix = "sbs"
|
const sandboxPathSuffix = "sbs"
|
||||||
@ -64,7 +64,7 @@ func Init() (persistapi.PersistDriver, error) {
|
|||||||
return &FS{
|
return &FS{
|
||||||
sandboxState: &persistapi.SandboxState{},
|
sandboxState: &persistapi.SandboxState{},
|
||||||
containerState: make(map[string]persistapi.ContainerState),
|
containerState: make(map[string]persistapi.ContainerState),
|
||||||
storageRootPath: filepath.Join("/run", storagePathSuffix),
|
storageRootPath: filepath.Join("/run", StoragePathSuffix),
|
||||||
driverName: "fs",
|
driverName: "fs",
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,8 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getFsDriver() (*FS, error) {
|
func getFsDriver(t *testing.T) (*FS, error) {
|
||||||
driver, err := MockFSInit()
|
driver, err := MockFSInit(t.TempDir())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to init fs driver")
|
return nil, fmt.Errorf("failed to init fs driver")
|
||||||
}
|
}
|
||||||
@ -27,16 +27,8 @@ func getFsDriver() (*FS, error) {
|
|||||||
return fs.FS, nil
|
return fs.FS, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initTestDir() func() {
|
|
||||||
return func() {
|
|
||||||
os.RemoveAll(MockStorageRootPath())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFsLockShared(t *testing.T) {
|
func TestFsLockShared(t *testing.T) {
|
||||||
defer initTestDir()()
|
fs, err := getFsDriver(t)
|
||||||
|
|
||||||
fs, err := getFsDriver()
|
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotNil(t, fs)
|
assert.NotNil(t, fs)
|
||||||
|
|
||||||
@ -61,9 +53,7 @@ func TestFsLockShared(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFsLockExclusive(t *testing.T) {
|
func TestFsLockExclusive(t *testing.T) {
|
||||||
defer initTestDir()()
|
fs, err := getFsDriver(t)
|
||||||
|
|
||||||
fs, err := getFsDriver()
|
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotNil(t, fs)
|
assert.NotNil(t, fs)
|
||||||
|
|
||||||
@ -89,9 +79,7 @@ func TestFsLockExclusive(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFsDriver(t *testing.T) {
|
func TestFsDriver(t *testing.T) {
|
||||||
defer initTestDir()()
|
fs, err := getFsDriver(t)
|
||||||
|
|
||||||
fs, err := getFsDriver()
|
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotNil(t, fs)
|
assert.NotNil(t, fs)
|
||||||
|
|
||||||
@ -162,12 +150,10 @@ func TestFsDriver(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGlobalReadWrite(t *testing.T) {
|
func TestGlobalReadWrite(t *testing.T) {
|
||||||
defer initTestDir()()
|
|
||||||
|
|
||||||
relPath := "test/123/aaa.json"
|
relPath := "test/123/aaa.json"
|
||||||
data := "hello this is testing global read write"
|
data := "hello this is testing global read write"
|
||||||
|
|
||||||
fs, err := getFsDriver()
|
fs, err := getFsDriver(t)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotNil(t, fs)
|
assert.NotNil(t, fs)
|
||||||
|
|
||||||
|
@ -7,19 +7,27 @@ package fs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
|
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var mockRootPath = ""
|
||||||
|
|
||||||
type MockFS struct {
|
type MockFS struct {
|
||||||
// inherit from FS. Overwrite if needed.
|
// inherit from FS. Overwrite if needed.
|
||||||
*FS
|
*FS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EnableMockTesting(rootPath string) {
|
||||||
|
mockRootPath = rootPath
|
||||||
|
}
|
||||||
|
|
||||||
func MockStorageRootPath() string {
|
func MockStorageRootPath() string {
|
||||||
return filepath.Join(os.TempDir(), "vc", "mockfs")
|
if mockRootPath == "" {
|
||||||
|
panic("Using uninitialized mock storage root path")
|
||||||
|
}
|
||||||
|
return mockRootPath
|
||||||
}
|
}
|
||||||
|
|
||||||
func MockRunStoragePath() string {
|
func MockRunStoragePath() string {
|
||||||
@ -30,11 +38,7 @@ func MockRunVMStoragePath() string {
|
|||||||
return filepath.Join(MockStorageRootPath(), vmPathSuffix)
|
return filepath.Join(MockStorageRootPath(), vmPathSuffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
func MockStorageDestroy() {
|
func MockFSInit(rootPath string) (persistapi.PersistDriver, error) {
|
||||||
os.RemoveAll(MockStorageRootPath())
|
|
||||||
}
|
|
||||||
|
|
||||||
func MockFSInit() (persistapi.PersistDriver, error) {
|
|
||||||
driver, err := Init()
|
driver, err := Init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Could not create Mock FS driver: %v", err)
|
return nil, fmt.Errorf("Could not create Mock FS driver: %v", err)
|
||||||
@ -45,8 +49,15 @@ func MockFSInit() (persistapi.PersistDriver, error) {
|
|||||||
return nil, fmt.Errorf("Could not create Mock FS driver")
|
return nil, fmt.Errorf("Could not create Mock FS driver")
|
||||||
}
|
}
|
||||||
|
|
||||||
fsDriver.storageRootPath = MockStorageRootPath()
|
fsDriver.storageRootPath = rootPath
|
||||||
fsDriver.driverName = "mockfs"
|
fsDriver.driverName = "mockfs"
|
||||||
|
|
||||||
return &MockFS{fsDriver}, nil
|
return &MockFS{fsDriver}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MockAutoInit() (persistapi.PersistDriver, error) {
|
||||||
|
if mockRootPath != "" {
|
||||||
|
return MockFSInit(MockStorageRootPath())
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
34
src/runtime/virtcontainers/persist/fs/mockfs_test.go
Normal file
34
src/runtime/virtcontainers/persist/fs/mockfs_test.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// Copyright Red Hat.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
//
|
||||||
|
|
||||||
|
package fs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMockAutoInit(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
orgMockRootPath := mockRootPath
|
||||||
|
defer func() {
|
||||||
|
mockRootPath = orgMockRootPath
|
||||||
|
}()
|
||||||
|
|
||||||
|
mockRootPath = ""
|
||||||
|
|
||||||
|
fsd, err := MockAutoInit()
|
||||||
|
assert.Nil(fsd)
|
||||||
|
assert.NoError(err)
|
||||||
|
|
||||||
|
// Testing mock driver
|
||||||
|
mockRootPath = t.TempDir()
|
||||||
|
fsd, err = MockAutoInit()
|
||||||
|
assert.NoError(err)
|
||||||
|
expectedFS, err := MockFSInit(MockStorageRootPath())
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Equal(expectedFS, fsd)
|
||||||
|
}
|
@ -28,13 +28,8 @@ var (
|
|||||||
RootFSName: fs.Init,
|
RootFSName: fs.Init,
|
||||||
RootlessFSName: fs.RootlessInit,
|
RootlessFSName: fs.RootlessInit,
|
||||||
}
|
}
|
||||||
mockTesting = false
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func EnableMockTesting() {
|
|
||||||
mockTesting = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDriver returns new PersistDriver according to driver name
|
// GetDriver returns new PersistDriver according to driver name
|
||||||
func GetDriverByName(name string) (persistapi.PersistDriver, error) {
|
func GetDriverByName(name string) (persistapi.PersistDriver, error) {
|
||||||
if expErr != nil {
|
if expErr != nil {
|
||||||
@ -56,8 +51,9 @@ func GetDriver() (persistapi.PersistDriver, error) {
|
|||||||
return nil, expErr
|
return nil, expErr
|
||||||
}
|
}
|
||||||
|
|
||||||
if mockTesting {
|
mock, err := fs.MockAutoInit()
|
||||||
return fs.MockFSInit()
|
if mock != nil || err != nil {
|
||||||
|
return mock, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if rootless.IsRootless() {
|
if rootless.IsRootless() {
|
||||||
|
@ -27,12 +27,6 @@ func TestGetDriverByName(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetDriver(t *testing.T) {
|
func TestGetDriver(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
orgMockTesting := mockTesting
|
|
||||||
defer func() {
|
|
||||||
mockTesting = orgMockTesting
|
|
||||||
}()
|
|
||||||
|
|
||||||
mockTesting = false
|
|
||||||
|
|
||||||
fsd, err := GetDriver()
|
fsd, err := GetDriver()
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
@ -46,12 +40,4 @@ func TestGetDriver(t *testing.T) {
|
|||||||
|
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
assert.Equal(expectedFS, fsd)
|
assert.Equal(expectedFS, fsd)
|
||||||
|
|
||||||
// Testing mock driver
|
|
||||||
mockTesting = true
|
|
||||||
fsd, err = GetDriver()
|
|
||||||
assert.NoError(err)
|
|
||||||
expectedFS, err = fs.MockFSInit()
|
|
||||||
assert.NoError(err)
|
|
||||||
assert.Equal(expectedFS, fsd)
|
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist"
|
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/fs"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/fs"
|
||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -58,8 +57,6 @@ var testHyperstartTtySocket = ""
|
|||||||
// cleanUp Removes any stale sandbox/container state that can affect
|
// cleanUp Removes any stale sandbox/container state that can affect
|
||||||
// the next test to run.
|
// the next test to run.
|
||||||
func cleanUp() {
|
func cleanUp() {
|
||||||
os.RemoveAll(fs.MockRunStoragePath())
|
|
||||||
os.RemoveAll(fs.MockRunVMStoragePath())
|
|
||||||
syscall.Unmount(GetSharePath(testSandboxID), syscall.MNT_DETACH|UmountNoFollow)
|
syscall.Unmount(GetSharePath(testSandboxID), syscall.MNT_DETACH|UmountNoFollow)
|
||||||
os.RemoveAll(testDir)
|
os.RemoveAll(testDir)
|
||||||
os.MkdirAll(testDir, DirMode)
|
os.MkdirAll(testDir, DirMode)
|
||||||
@ -108,8 +105,6 @@ func setupClh() {
|
|||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
persist.EnableMockTesting()
|
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
logger := logrus.NewEntry(logrus.New())
|
logger := logrus.NewEntry(logrus.New())
|
||||||
@ -126,6 +121,8 @@ func TestMain(m *testing.M) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fs.EnableMockTesting(filepath.Join(testDir, "mockfs"))
|
||||||
|
|
||||||
fmt.Printf("INFO: Creating virtcontainers test directory %s\n", testDir)
|
fmt.Printf("INFO: Creating virtcontainers test directory %s\n", testDir)
|
||||||
err = os.MkdirAll(testDir, DirMode)
|
err = os.MkdirAll(testDir, DirMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user