From e17d4ea9e2e6ec5555429cbc0363748e33170dea Mon Sep 17 00:00:00 2001
From: WangFengTu <wangfengtu@huawei.com>
Date: Mon, 23 Nov 2020 16:52:56 +0800
Subject: [PATCH 02/17] change default tmp directory from /var/tmp to
 /var/lib/isulad/tmp

Signed-off-by: WangFengTu <wangfengtu@huawei.com>
---
 src/daemon/modules/image/oci/oci_image.c      |  21 +++-
 src/daemon/modules/image/oci/oci_load.c       |  20 ++--
 .../modules/image/oci/registry/registry.c     |  20 ++--
 .../modules/image/oci/storage/storage.c       |  34 ------
 .../modules/image/oci/storage/storage.h       |   2 -
 src/daemon/modules/image/oci/utils_images.c   | 102 ++++++++++++++----
 src/daemon/modules/image/oci/utils_images.h   |   3 +-
 test/image/oci/registry/CMakeLists.txt        |   2 +-
 test/image/oci/registry/registry_ut.cc        |  22 +++-
 test/image/oci/storage/images/CMakeLists.txt  |   2 +
 .../oci/storage/images/storage_images_ut.cc   |   1 +
 test/image/oci/storage/rootfs/CMakeLists.txt  |   4 +-
 .../oci/storage/rootfs/storage_rootfs_ut.cc   |   1 +
 test/mocks/isulad_config_mock.cc              |  16 +++
 test/mocks/isulad_config_mock.h               |   2 +
 15 files changed, 160 insertions(+), 92 deletions(-)

diff --git a/src/daemon/modules/image/oci/oci_image.c b/src/daemon/modules/image/oci/oci_image.c
index f0ba19cb..f4fa1e88 100644
--- a/src/daemon/modules/image/oci/oci_image.c
+++ b/src/daemon/modules/image/oci/oci_image.c
@@ -150,26 +150,34 @@ out:
     return ret;
 }
 
-static void cleanup_image_tmpdir()
+static int recreate_image_tmpdir()
 {
     char *image_tmp_path = NULL;
+    int ret = 0;
 
-    image_tmp_path = get_image_tmp_path();
+    image_tmp_path = oci_get_isulad_tmpdir();
     if (image_tmp_path == NULL) {
         ERROR("failed to get image tmp path");
-        return;
+        ret = -1;
+        goto out;
     }
 
     if (util_recursive_rmdir(image_tmp_path, 0)) {
         ERROR("failed to remove directory %s", image_tmp_path);
+        ret = -1;
+        goto out;
     }
 
     if (util_mkdir_p(image_tmp_path, TEMP_DIRECTORY_MODE)) {
         ERROR("failed to create directory %s", image_tmp_path);
+        ret = -1;
+        goto out;
     }
+
+out:
     free(image_tmp_path);
 
-    return;
+    return ret;
 }
 
 int oci_init(const isulad_daemon_configs *args)
@@ -181,7 +189,10 @@ int oci_init(const isulad_daemon_configs *args)
         return ret;
     }
 
-    cleanup_image_tmpdir();
+    ret = recreate_image_tmpdir();
+    if (ret != 0) {
+        goto out;
+    }
 
     ret = registry_init(NULL, NULL);
     if (ret != 0) {
diff --git a/src/daemon/modules/image/oci/oci_load.c b/src/daemon/modules/image/oci/oci_load.c
index 073ad55b..97cff34b 100644
--- a/src/daemon/modules/image/oci/oci_load.c
+++ b/src/daemon/modules/image/oci/oci_load.c
@@ -1011,25 +1011,23 @@ static char *oci_load_path_create()
 {
     int ret = 0;
     int nret = 0;
-    char *oci_load_work_dir = NULL;
+    char *image_tmp_path = NULL;
     char tmp_dir[PATH_MAX] = { 0 };
 
-    oci_load_work_dir = storage_oci_load_work_dir();
-    if (oci_load_work_dir == NULL) {
-        ERROR("Failed to get oci load work dir");
-        isulad_try_set_error_message("Failed to get oci load work dir");
-        ret = -1;
+    ret = makesure_isulad_tmpdir_perm_right();
+    if (ret != 0) {
+        ERROR("failed to make sure permission of image tmp work dir");
         goto out;
     }
 
-    if (util_mkdir_p(oci_load_work_dir, TEMP_DIRECTORY_MODE) != 0) {
-        ERROR("Unable to create oci image load tmp work dir:%s", oci_load_work_dir);
-        isulad_try_set_error_message("Unable to create oci image load tmp work dir:%s", oci_load_work_dir);
+    image_tmp_path = oci_get_isulad_tmpdir();
+    if (image_tmp_path == NULL) {
+        ERROR("failed to get image tmp work dir");
         ret = -1;
         goto out;
     }
 
-    nret = snprintf(tmp_dir, PATH_MAX, "%s/oci-image-load-XXXXXX", oci_load_work_dir);
+    nret = snprintf(tmp_dir, PATH_MAX, "%s/oci-image-load-XXXXXX", image_tmp_path);
     if (nret < 0 || (size_t)nret >= sizeof(tmp_dir)) {
         ERROR("Path is too long");
         ret = -1;
@@ -1044,7 +1042,7 @@ static char *oci_load_path_create()
     }
 
 out:
-    free(oci_load_work_dir);
+    free(image_tmp_path);
     return ret == 0 ? util_strdup_s(tmp_dir) : NULL;
 }
 
diff --git a/src/daemon/modules/image/oci/registry/registry.c b/src/daemon/modules/image/oci/registry/registry.c
index 2656b9b2..a94d10b1 100644
--- a/src/daemon/modules/image/oci/registry/registry.c
+++ b/src/daemon/modules/image/oci/registry/registry.c
@@ -1718,7 +1718,13 @@ static int prepare_pull_desc(pull_descriptor *desc, registry_pull_options *optio
 
     update_host(desc);
 
-    image_tmp_path = get_image_tmp_path();
+    ret = makesure_isulad_tmpdir_perm_right();
+    if (ret != 0) {
+        ERROR("failed to make sure permission of image tmp work dir");
+        goto out;
+    }
+
+    image_tmp_path = oci_get_isulad_tmpdir();
     if (image_tmp_path == NULL) {
         ERROR("failed to get image tmp work dir");
         ret = -1;
@@ -1859,18 +1865,6 @@ static void cached_layers_kvfree(void *key, void *value)
 int registry_init(char *auths_dir, char *certs_dir)
 {
     int ret = 0;
-    char *image_tmp_path = NULL;
-
-    image_tmp_path = get_image_tmp_path();
-    if (image_tmp_path == NULL) {
-        ERROR("failed to get image tmp path");
-        return -1;
-    }
-
-    if (util_mkdir_p(image_tmp_path, TEMP_DIRECTORY_MODE)) {
-        ERROR("failed to create directory %s", image_tmp_path);
-    }
-    free(image_tmp_path);
 
     auths_set_dir(auths_dir);
     certs_set_dir(certs_dir);
diff --git a/src/daemon/modules/image/oci/storage/storage.c b/src/daemon/modules/image/oci/storage/storage.c
index f15531be..0d837079 100644
--- a/src/daemon/modules/image/oci/storage/storage.c
+++ b/src/daemon/modules/image/oci/storage/storage.c
@@ -1715,20 +1715,6 @@ out:
 int storage_module_init(struct storage_module_init_options *opts)
 {
     int ret = 0;
-    char *oci_load_work_dir = NULL;
-
-    oci_load_work_dir = storage_oci_load_work_dir();
-    if (oci_load_work_dir == NULL) {
-        ERROR("Get oci load work dir failed");
-        ret = -1;
-        goto out;
-    }
-
-    ret = util_recursive_rmdir(oci_load_work_dir, 0);
-    if (ret != 0) {
-        ERROR("failed to remove dir %s", oci_load_work_dir);
-        goto out;
-    }
 
     if (check_module_init_opt(opts) != 0) {
         ret = -1;
@@ -1783,25 +1769,5 @@ int storage_module_init(struct storage_module_init_options *opts)
     }
 
 out:
-    free(oci_load_work_dir);
     return ret;
 }
-
-
-char *storage_oci_load_work_dir()
-{
-    char *isulad_tmp = NULL;
-    char *oci_load_work_dir = NULL;
-
-    isulad_tmp = oci_get_isulad_tmpdir();
-    if (isulad_tmp == NULL) {
-        ERROR("Failed to get isulad tmp dir");
-        goto out;
-    }
-
-    oci_load_work_dir = util_path_join(isulad_tmp, "isulad-oci-load");
-
-out:
-    free(isulad_tmp);
-    return oci_load_work_dir;
-}
\ No newline at end of file
diff --git a/src/daemon/modules/image/oci/storage/storage.h b/src/daemon/modules/image/oci/storage/storage.h
index b030a3a5..f2141924 100644
--- a/src/daemon/modules/image/oci/storage/storage.h
+++ b/src/daemon/modules/image/oci/storage/storage.h
@@ -179,8 +179,6 @@ int storage_rootfs_umount(const char *container_id, bool force);
 
 container_inspect_graph_driver *storage_get_metadata_by_container_id(const char *id);
 
-char *storage_oci_load_work_dir();
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/daemon/modules/image/oci/utils_images.c b/src/daemon/modules/image/oci/utils_images.c
index de0eb324..7eddd25c 100644
--- a/src/daemon/modules/image/oci/utils_images.c
+++ b/src/daemon/modules/image/oci/utils_images.c
@@ -27,6 +27,7 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
+#include <sys/stat.h>
 
 #include "isula_libutils/log.h"
 #include "utils.h"
@@ -36,10 +37,10 @@
 #include "utils_file.h"
 #include "utils_string.h"
 #include "utils_verify.h"
+#include "isulad_config.h"
 
 // nanos of 2038-01-19T03:14:07, the max valid linux time
 #define MAX_NANOS 2147483647000000000
-#define ISULAD_DEFAULT_TMP_DIR "/var/tmp"
 
 char *get_last_part(char **parts)
 {
@@ -488,36 +489,101 @@ bool oci_valid_time(char *time)
     return true;
 }
 
+static int makesure_path_is_dir(char *path)
+{
+    struct stat st = {0};
+
+    if (lstat(path, &st) != 0) {
+        if (errno == ENOENT) {
+            return util_mkdir_p(path, TEMP_DIRECTORY_MODE);
+        }
+        ERROR("lstat %s failed: %s", path, strerror(errno));
+        return -1;
+    }
+
+    if (!S_ISDIR(st.st_mode)) {
+        if (util_recursive_rmdir(path, 0)) {
+            ERROR("failed to remove directory %s", path);
+            return -1;
+        }
+    }
+
+    if (util_mkdir_p(path, TEMP_DIRECTORY_MODE) != 0) {
+        ERROR("make dir:%s failed", path);
+        return -1;
+    }
+
+    return 0;
+}
 
 char *oci_get_isulad_tmpdir()
 {
-    char *isula_tmp = NULL;
+    char *isulad_tmpdir = NULL;
+    char *isulad_root_dir = NULL;
+    char *env_dir = NULL;
+    int ret = 0;
 
-    isula_tmp = getenv("ISULAD_TMPDIR");
-    if (util_valid_str(isula_tmp) && !util_dir_exists(isula_tmp)) {
-        if (util_mkdir_p(isula_tmp, TEMP_DIRECTORY_MODE) != 0) {
-            ERROR("make dir:%s failed", isula_tmp);
-            return NULL;
-        }
+    isulad_root_dir = conf_get_isulad_rootdir();
+    if (isulad_root_dir == NULL) {
+        ERROR("get isulad root dir failed");
+        return NULL;
     }
 
-    return util_valid_str(isula_tmp) ? util_strdup_s(isula_tmp) : util_strdup_s(ISULAD_DEFAULT_TMP_DIR);
+    env_dir = getenv("ISULAD_TMPDIR");
+    if (util_valid_str(env_dir)) {
+        isulad_tmpdir = util_path_join(env_dir, "isulad_tmpdir");
+    } else {
+        isulad_tmpdir = util_path_join(isulad_root_dir, "isulad_tmpdir");
+    }
+    if (isulad_tmpdir == NULL) {
+        ERROR("join temporary directory failed");
+        ret = -1;
+        goto out;
+    }
+
+out:
+    free(isulad_root_dir);
+    if (ret != 0) {
+        free(isulad_tmpdir);
+        isulad_tmpdir = NULL;
+    }
+
+    return isulad_tmpdir;
 }
 
-char *get_image_tmp_path()
+int makesure_isulad_tmpdir_perm_right()
 {
-    char *isulad_tmp = NULL;
-    char *isula_image = NULL;
+    struct stat st = {0};
+    char *isulad_tmpdir = NULL;
+    int ret = 0;
+
+    isulad_tmpdir = oci_get_isulad_tmpdir();
+    if (isulad_tmpdir == NULL) {
+        return -1;
+    }
 
-    isulad_tmp = oci_get_isulad_tmpdir();
-    if (isulad_tmp == NULL) {
-        ERROR("Failed to get isulad tmp dir");
+    ret = makesure_path_is_dir(isulad_tmpdir);
+    if (ret != 0) {
+        goto out;
+    }
+
+    if (lstat(isulad_tmpdir, &st) != 0) {
+        ERROR("lstat %s failed: %s", isulad_tmpdir, strerror(errno));
+        ret = -1;
         goto out;
     }
 
-    isula_image = util_path_join(isulad_tmp, "isula-image");
+    // chown to root
+    ret = lchown(isulad_tmpdir, 0, 0);
+    if (ret == 0 || (ret == EPERM && st.st_uid == 0 && st.st_gid == 0)) {
+        ret = 0;
+        goto out;
+    } else {
+        ERROR("lchown %s failed: %s", isulad_tmpdir, strerror(errno));
+    }
 
 out:
-    free(isulad_tmp);
-    return isula_image;
+    free(isulad_tmpdir);
+
+    return ret;
 }
diff --git a/src/daemon/modules/image/oci/utils_images.h b/src/daemon/modules/image/oci/utils_images.h
index 5dedd569..cebcc796 100644
--- a/src/daemon/modules/image/oci/utils_images.h
+++ b/src/daemon/modules/image/oci/utils_images.h
@@ -55,8 +55,7 @@ int add_rootfs_and_history(const layer_blob *layers, size_t layers_len, const re
 bool oci_valid_time(char *time);
 
 char *oci_get_isulad_tmpdir();
-
-char *get_image_tmp_path();
+int makesure_isulad_tmpdir_perm_right();
 
 #ifdef __cplusplus
 }
diff --git a/test/image/oci/registry/CMakeLists.txt b/test/image/oci/registry/CMakeLists.txt
index 9e34103c..36e7cab8 100644
--- a/test/image/oci/registry/CMakeLists.txt
+++ b/test/image/oci/registry/CMakeLists.txt
@@ -24,7 +24,6 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/utils/buffer/buffer.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/utils/cutils/utils_aes.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/daemon/modules/image/oci/storage/image_store/image_type.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/daemon/config/isulad_config.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/daemon/modules/image/oci/registry_type.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/daemon/common/sysinfo.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/daemon/modules/image/oci/storage/image_store/image_store.c
@@ -37,6 +36,7 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../mocks/storage_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../mocks/oci_image_mock.cc
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../mocks/http_mock.cc
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../mocks/isulad_config_mock.cc
     registry_ut.cc)
 
 target_include_directories(${EXE} PUBLIC
diff --git a/test/image/oci/registry/registry_ut.cc b/test/image/oci/registry/registry_ut.cc
index f656a49d..25ddf694 100644
--- a/test/image/oci/registry/registry_ut.cc
+++ b/test/image/oci/registry/registry_ut.cc
@@ -43,6 +43,7 @@
 #include "aes.h"
 #include "auths.h"
 #include "oci_image_mock.h"
+#include "isulad_config_mock.h"
 
 using ::testing::Args;
 using ::testing::ByRef;
@@ -79,17 +80,21 @@ protected:
         MockHttp_SetMock(&m_http_mock);
         MockStorage_SetMock(&m_storage_mock);
         MockOciImage_SetMock(&m_oci_image_mock);
+        MockIsuladConf_SetMock(&m_isulad_conf_mock);
     }
 
     void TearDown() override
     {
         MockHttp_SetMock(nullptr);
         MockStorage_SetMock(nullptr);
+        MockOciImage_SetMock(nullptr);
+        MockIsuladConf_SetMock(nullptr);
     }
 
     NiceMock<MockHttp> m_http_mock;
     NiceMock<MockStorage> m_storage_mock;
     NiceMock<MockOciImage> m_oci_image_mock;
+    NiceMock<MockIsuladConf> m_isulad_conf_mock;
 };
 
 int invokeHttpRequestV1(const char *url, struct http_get_options *options, long *response_code, int recursive_len)
@@ -497,7 +502,12 @@ static int init_log()
     return 0;
 }
 
-void mockCommonAll(MockStorage *mock, MockOciImage *oci_image_mock)
+static char *invokeConfGetISuladRootDir()
+{
+    return util_strdup_s(get_dir().c_str());
+}
+
+void mockCommonAll(MockStorage *mock, MockOciImage *oci_image_mock, MockIsuladConf *isulad_conf_mock)
 {
     EXPECT_CALL(*mock, StorageImgCreate(::testing::_, ::testing::_, ::testing::_, ::testing::_))
     .WillRepeatedly(Invoke(invokeStorageImgCreate));
@@ -531,6 +541,8 @@ void mockCommonAll(MockStorage *mock, MockOciImage *oci_image_mock)
     .WillRepeatedly(Invoke(invokeFreeLayer));
     EXPECT_CALL(*oci_image_mock, OciValidTime(::testing::_))
     .WillRepeatedly(Invoke(invokeOciValidTime));
+    EXPECT_CALL(*isulad_conf_mock, ConfGetISuladRootDir())
+    .WillRepeatedly(Invoke(invokeConfGetISuladRootDir));
     return;
 }
 
@@ -588,7 +600,7 @@ TEST_F(RegistryUnitTest, test_pull_v1_image)
 
     EXPECT_CALL(m_http_mock, HttpRequest(::testing::_, ::testing::_, ::testing::_, ::testing::_))
     .WillRepeatedly(Invoke(invokeHttpRequestV1));
-    mockCommonAll(&m_storage_mock, &m_oci_image_mock);
+    mockCommonAll(&m_storage_mock, &m_oci_image_mock, &m_isulad_conf_mock);
     ASSERT_EQ(registry_pull(&options), 0);
 
     ASSERT_EQ(registry_pull(&options), 0);
@@ -648,7 +660,7 @@ TEST_F(RegistryUnitTest, test_pull_v2_image)
 
     EXPECT_CALL(m_http_mock, HttpRequest(::testing::_, ::testing::_, ::testing::_, ::testing::_))
     .WillRepeatedly(Invoke(invokeHttpRequestV2));
-    mockCommonAll(&m_storage_mock, &m_oci_image_mock);
+    mockCommonAll(&m_storage_mock, &m_oci_image_mock, &m_isulad_conf_mock);
 
     // test retry success
     ASSERT_EQ(registry_pull(&options), 0);
@@ -682,7 +694,7 @@ TEST_F(RegistryUnitTest, test_pull_oci_image)
     options->insecure_registry = false;
     EXPECT_CALL(m_http_mock, HttpRequest(::testing::_, ::testing::_, ::testing::_, ::testing::_))
     .WillRepeatedly(Invoke(invokeHttpRequestOCI));
-    mockCommonAll(&m_storage_mock, &m_oci_image_mock);
+    mockCommonAll(&m_storage_mock, &m_oci_image_mock, &m_isulad_conf_mock);
     ASSERT_EQ(registry_pull(options), 0);
 
     free_registry_pull_options(options);
@@ -700,7 +712,7 @@ TEST_F(RegistryUnitTest, test_pull_already_exist)
 
     EXPECT_CALL(m_http_mock, HttpRequest(::testing::_, ::testing::_, ::testing::_, ::testing::_))
     .WillRepeatedly(Invoke(invokeHttpRequestV2));
-    mockCommonAll(&m_storage_mock, &m_oci_image_mock);
+    mockCommonAll(&m_storage_mock, &m_oci_image_mock, &m_isulad_conf_mock);
     EXPECT_CALL(m_storage_mock, StorageLayerGet(::testing::_))
     .WillRepeatedly(Invoke(invokeStorageLayerGet1));
     ASSERT_EQ(registry_pull(&options), 0);
diff --git a/test/image/oci/storage/images/CMakeLists.txt b/test/image/oci/storage/images/CMakeLists.txt
index 82ad4680..b00c5a0e 100644
--- a/test/image/oci/storage/images/CMakeLists.txt
+++ b/test/image/oci/storage/images/CMakeLists.txt
@@ -22,6 +22,7 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/modules/image/oci/registry_type.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/modules/image/oci/storage/image_store/image_store.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks/storage_mock.cc
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks/isulad_config_mock.cc
     storage_images_ut.cc)
 
 target_include_directories(${EXE} PUBLIC
@@ -34,6 +35,7 @@ target_include_directories(${EXE} PUBLIC
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/utils/cutils/map
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/utils/sha256
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/utils/http
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/config
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/modules/image/oci
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/modules/image/oci/storage
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/modules/image/oci/storage/image_store
diff --git a/test/image/oci/storage/images/storage_images_ut.cc b/test/image/oci/storage/images/storage_images_ut.cc
index 4115e911..234ab742 100644
--- a/test/image/oci/storage/images/storage_images_ut.cc
+++ b/test/image/oci/storage/images/storage_images_ut.cc
@@ -33,6 +33,7 @@
 #include "isula_libutils/imagetool_images_list.h"
 #include "isula_libutils/imagetool_image.h"
 #include "storage_mock.h"
+#include "isulad_config_mock.h"
 
 using ::testing::Args;
 using ::testing::ByRef;
diff --git a/test/image/oci/storage/rootfs/CMakeLists.txt b/test/image/oci/storage/rootfs/CMakeLists.txt
index 3ba56bb9..8da8196b 100644
--- a/test/image/oci/storage/rootfs/CMakeLists.txt
+++ b/test/image/oci/storage/rootfs/CMakeLists.txt
@@ -21,6 +21,7 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/modules/image/oci/storage/rootfs_store/rootfs.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks/storage_mock.cc
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks/isulad_config_mock.cc
     storage_rootfs_ut.cc)
 
 target_include_directories(${EXE} PUBLIC
@@ -33,7 +34,8 @@ target_include_directories(${EXE} PUBLIC
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/utils/sha256
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json/schema/src
     ${CMAKE_BINARY_DIR}/conf
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../../test/mocks
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../test/mocks
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/config
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/modules/image/oci
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/modules/image/oci/storage
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/modules/image/oci/storage/rootfs_store
diff --git a/test/image/oci/storage/rootfs/storage_rootfs_ut.cc b/test/image/oci/storage/rootfs/storage_rootfs_ut.cc
index a4864dad..9cf91fa9 100644
--- a/test/image/oci/storage/rootfs/storage_rootfs_ut.cc
+++ b/test/image/oci/storage/rootfs/storage_rootfs_ut.cc
@@ -30,6 +30,7 @@
 #include "utils.h"
 #include "path.h"
 #include "storage.h"
+#include "isulad_config_mock.h"
 
 std::string META_DATA_CONTENT = "metadata test";
 
diff --git a/test/mocks/isulad_config_mock.cc b/test/mocks/isulad_config_mock.cc
index 9d993157..88a74a41 100644
--- a/test/mocks/isulad_config_mock.cc
+++ b/test/mocks/isulad_config_mock.cc
@@ -136,3 +136,19 @@ char *conf_get_isulad_storage_driver_backing_fs()
     }
     return nullptr;
 }
+
+char *conf_get_isulad_rootdir()
+{
+    if (g_isulad_conf_mock != nullptr) {
+        return g_isulad_conf_mock->ConfGetISuladRootDir();
+    }
+    return nullptr;
+}
+
+bool conf_get_use_decrypted_key_flag()
+{
+    if (g_isulad_conf_mock != nullptr) {
+        return g_isulad_conf_mock->ConfGetUseDecryptedKeyFlag();
+    }
+    return true;
+}
diff --git a/test/mocks/isulad_config_mock.h b/test/mocks/isulad_config_mock.h
index fdf27cdb..03af3cc9 100644
--- a/test/mocks/isulad_config_mock.h
+++ b/test/mocks/isulad_config_mock.h
@@ -34,6 +34,8 @@ public:
     MOCK_METHOD1(GetSystemCpuUsage, int(uint64_t *val));
     MOCK_METHOD0(ConfGetIsuladStorageDriverBackingFs, char *());
     MOCK_METHOD0(GetMonitordPath, char *(void));
+    MOCK_METHOD0(ConfGetISuladRootDir, char *(void));
+    MOCK_METHOD0(ConfGetUseDecryptedKeyFlag, bool (void));
 };
 
 void MockIsuladConf_SetMock(MockIsuladConf *mock);
-- 
2.25.1

