From 0c1f841e13e4cb70ff07e18ddfb53e8927edbe95 Mon Sep 17 00:00:00 2001
From: daisicheng <daisicheng@huawei.com>
Date: Tue, 1 Nov 2022 17:11:19 +0800
Subject: [PATCH] improve some error coverage of unit tests in import_test and
 store_test

---
 cmd/cli/import_test.go |  9 ++++-----
 store/store_test.go    | 29 ++++++++++++++++++++++++++++-
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/cmd/cli/import_test.go b/cmd/cli/import_test.go
index 057bf17..82983b0 100644
--- a/cmd/cli/import_test.go
+++ b/cmd/cli/import_test.go
@@ -85,14 +85,13 @@ func TestRunImport(t *testing.T) {
 	ctx := context.Background()
 	mockImport := newMockDaemon()
 	cli := newMockClient(&mockGrpcClient{importFunc: mockImport.importImage})
-	fileEmpty := "empty.tar"
 	fileNormal := "test.tar"
 	exceededFile := fs.NewFile(t, t.Name())
 	err := ioutil.WriteFile(exceededFile.Path(), []byte("This is exceeded test file"), constant.DefaultSharedFileMode)
 	assert.NilError(t, err)
 	err = os.Truncate(exceededFile.Path(), ExceededImportFileSize)
 	assert.NilError(t, err)
-	ctxDir := fs.NewDir(t, "import", fs.WithFile(fileEmpty, ""), fs.WithFile(fileNormal, "test"))
+	ctxDir := fs.NewDir(t, "import", fs.WithFile(fileNormal, "test"))
 	defer ctxDir.Remove()
 	defer exceededFile.Remove()
 
@@ -104,10 +103,10 @@ func TestRunImport(t *testing.T) {
 	}
 	var testcases = []testcase{
 		{
-			name:      "TC1 - abnormal case with empty file",
-			source:    filepath.Join(ctxDir.Path(), fileEmpty),
+			name:      "TC1 - abnormal case with relative path",
+			source:    filepath.Join("./", fileNormal),
 			wantErr:   true,
-			errString: "empty",
+			errString: "no such file or directory",
 		},
 		{
 			name:      "TC2 - abnormal case with exceeded limit file",
diff --git a/store/store_test.go b/store/store_test.go
index 77a9353..360e4dd 100644
--- a/store/store_test.go
+++ b/store/store_test.go
@@ -67,12 +67,39 @@ func TestCleanContainers(t *testing.T) {
 
 	s, err := GetStore()
 	assert.NilError(t, err)
-	s.CreateContainer("", []string{""}, "", "", "", nil)
+	s.CreateContainer("", []string{""}, "", "TC1", "", nil)
 	s.CleanContainers()
 	containers, _ := s.Containers()
 	if len(containers) > 0 {
 		t.Errorf("Failed to clean containers")
 	}
+	s.CreateContainer("", []string{""}, "", "TC2", "", nil)
+	err = os.RemoveAll(filepath.Join(storeOpts.DataRoot, "overlay-layers"))
+	assert.NilError(t, err)
+	s.CleanContainers()
+
+	defer func() {
+		unix.Unmount(filepath.Join(storeOpts.DataRoot, "overlay"), 0)
+		unix.Unmount(filepath.Join(storeOpts.RunRoot, "overlay"), 0)
+		os.RemoveAll(dataDir)
+		os.RemoveAll(runDir)
+	}()
+}
+
+func TestCleanContainer(t *testing.T) {
+	dataDir := "/tmp/lib"
+	runDir := "/tmp/run"
+	storeOpts.DataRoot = filepath.Join(dataDir, "containers/storage")
+	storeOpts.RunRoot = filepath.Join(runDir, "containers/storage")
+
+	s, err := GetStore()
+	assert.NilError(t, err)
+	s.CreateContainer("", []string{""}, "", "TC1", "", nil)
+	err = s.CleanContainer("TC1")
+	assert.NilError(t, err)
+	err = s.CleanContainer("TC2")
+	assert.ErrorContains(t, err, "not a container")
+
 	defer func() {
 		unix.Unmount(filepath.Join(storeOpts.DataRoot, "overlay"), 0)
 		unix.Unmount(filepath.Join(storeOpts.RunRoot, "overlay"), 0)
-- 
2.33.0

