From a0f49a91e434cd60d59c35b7e4438b469559f68e Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Thu, 11 Apr 2019 23:21:27 -0700 Subject: [PATCH 1/2] ut: fix UT failure due to non-root We should allow UT to run without root. Signed-off-by: Peng Tao --- virtcontainers/container_test.go | 4 ++++ virtcontainers/hyperstart_agent_test.go | 6 ++++++ virtcontainers/kata_agent_test.go | 6 ++++++ 3 files changed, 16 insertions(+) diff --git a/virtcontainers/container_test.go b/virtcontainers/container_test.go index 8dfdafc38..c164b72be 100644 --- a/virtcontainers/container_test.go +++ b/virtcontainers/container_test.go @@ -137,6 +137,10 @@ func TestContainerRemoveDrive(t *testing.T) { } func testSetupFakeRootfs(t *testing.T) (testRawFile, loopDev, mntDir string, err error) { + if os.Geteuid() != 0 { + t.Skip(testDisabledAsNonRoot) + } + tmpDir, err := ioutil.TempDir("", "") if err != nil { t.Fatal(err) diff --git a/virtcontainers/hyperstart_agent_test.go b/virtcontainers/hyperstart_agent_test.go index 6d8aaae30..b65c19086 100644 --- a/virtcontainers/hyperstart_agent_test.go +++ b/virtcontainers/hyperstart_agent_test.go @@ -282,6 +282,12 @@ func TestHyperCopyFile(t *testing.T) { func TestHyperCleanupSandbox(t *testing.T) { assert := assert.New(t) + defaultSharedDirSaved := defaultSharedDir + defaultSharedDir, _ = ioutil.TempDir("", "hyper-cleanup") + defer func() { + defaultSharedDir = defaultSharedDirSaved + }() + s := Sandbox{ id: "testFoo", } diff --git a/virtcontainers/kata_agent_test.go b/virtcontainers/kata_agent_test.go index 378c4fc65..caad5d782 100644 --- a/virtcontainers/kata_agent_test.go +++ b/virtcontainers/kata_agent_test.go @@ -935,6 +935,12 @@ func TestKataCopyFile(t *testing.T) { func TestKataCleanupSandbox(t *testing.T) { assert := assert.New(t) + kataHostSharedDirSaved := kataHostSharedDir + kataHostSharedDir, _ = ioutil.TempDir("", "kata-cleanup") + defer func() { + kataHostSharedDir = kataHostSharedDirSaved + }() + s := Sandbox{ id: "testFoo", } From 9040f6a8cd5a0f2b668176f383f434d0f862af24 Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Thu, 11 Apr 2019 23:22:16 -0700 Subject: [PATCH 2/2] ut: fix UT failure due to incorrect cleanup cleanup() actually removes all things under testdir and causes UT failures. === RUN TestCreateMockSandbox ERRO[0000] Create new sandbox failed error="QEMU path (/tmp/vc-tmp-007004370/hypervisor) does not exist" sandbox=7f49d00d-1995-4156-8c79-5f5ab24ce138 sandboxid=7f49d00d-1995-4156-8c79-5f5ab24ce138 source=virtcontainers subsystem=sandbox exit status 1 Fixes: #1525 Signed-off-by: Peng Tao --- virtcontainers/virtcontainers_test.go | 45 +++++++-------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/virtcontainers/virtcontainers_test.go b/virtcontainers/virtcontainers_test.go index 77f498299..47cb88194 100644 --- a/virtcontainers/virtcontainers_test.go +++ b/virtcontainers/virtcontainers_test.go @@ -53,12 +53,18 @@ func cleanUp() { os.MkdirAll(dir, store.DirMode) } + setup() +} + +func setup() { os.Mkdir(filepath.Join(testDir, testBundle), store.DirMode) - _, err := os.Create(filepath.Join(testDir, testImage)) - if err != nil { - fmt.Println("Could not recreate test image:", err) - os.Exit(1) + for _, filename := range []string{testQemuKernelPath, testQemuInitrdPath, testQemuImagePath, testQemuPath} { + _, err := os.Create(filename) + if err != nil { + fmt.Printf("Could not recreate %s:%v", filename, err) + os.Exit(1) + } } } @@ -95,36 +101,7 @@ func TestMain(m *testing.M) { testQemuImagePath = filepath.Join(testDir, testImage) testQemuPath = filepath.Join(testDir, testHypervisor) - fmt.Printf("INFO: Creating virtcontainers test kernel %s\n", testQemuKernelPath) - _, err = os.Create(testQemuKernelPath) - if err != nil { - fmt.Println("Could not create test kernel:", err) - os.RemoveAll(testDir) - os.Exit(1) - } - - fmt.Printf("INFO: Creating virtcontainers test image %s\n", testQemuImagePath) - _, err = os.Create(testQemuImagePath) - if err != nil { - fmt.Println("Could not create test image:", err) - os.RemoveAll(testDir) - os.Exit(1) - } - - fmt.Printf("INFO: Creating virtcontainers test hypervisor %s\n", testQemuPath) - _, err = os.Create(testQemuPath) - if err != nil { - fmt.Println("Could not create test hypervisor:", err) - os.RemoveAll(testDir) - os.Exit(1) - } - - err = os.Mkdir(filepath.Join(testDir, testBundle), store.DirMode) - if err != nil { - fmt.Println("Could not create test bundle directory:", err) - os.RemoveAll(testDir) - os.Exit(1) - } + setup() // allow the tests to run without affecting the host system. store.ConfigStoragePath = filepath.Join(testDir, store.StoragePathSuffix, "config")