From 6cf9f48c2339f85fa233c4e557da08884f666704 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Tue, 22 Nov 2022 16:52:51 +0800
Subject: [PATCH 46/54] fix storage layer and driver ut failed in container

Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
 .../oci/storage/layers/storage_driver_ut.cc   | 61 ++++++++++++++++---
 .../oci/storage/layers/storage_layers_ut.cc   | 54 +++++++++++++++-
 2 files changed, 106 insertions(+), 9 deletions(-)

diff --git a/test/image/oci/storage/layers/storage_driver_ut.cc b/test/image/oci/storage/layers/storage_driver_ut.cc
index 735526f1..650368d8 100644
--- a/test/image/oci/storage/layers/storage_driver_ut.cc
+++ b/test/image/oci/storage/layers/storage_driver_ut.cc
@@ -58,6 +58,28 @@ std::string GetDirectory()
     return static_cast<std::string>(abs_path) + "../../../../../../test/image/oci/storage/layers";
 }
 
+bool check_support_overlay(std::string root_dir)
+{
+    if (!util_support_overlay()) {
+        std::cout << "Cannot support overlay, skip storage driver ut test." << std::endl;
+        return false;
+    }
+
+    char *backing_fs = util_get_fs_name(root_dir.c_str());
+    if (backing_fs == NULL) {
+        std::cout << "Failed to get fs name for " << root_dir << ", skip storage driver ut test." << std::endl;
+        return false;
+    }
+
+    if (strcmp(backing_fs, "aufs") == 0 || strcmp(backing_fs, "zfs") == 0 || strcmp(backing_fs, "overlayfs") == 0 ||
+        strcmp(backing_fs, "ecryptfs") == 0) {
+        std::cout << "Backing fs cannot support overlay, skip storage driver ut test." << std::endl;
+        return false;
+    }
+
+    return true;
+}
+
 bool dirExists(const char *path)
 {
     DIR *dp = nullptr;
@@ -99,11 +121,16 @@ protected:
     void SetUp() override
     {
         MockDriverQuota_SetMock(&m_driver_quota_mock);
-        std::string isulad_dir { "/var/lib/isulad/" };
+        std::string isulad_dir { "/tmp/isulad/" };
+        mkdir(isulad_dir.c_str(), 0755);
         std::string root_dir = isulad_dir + "data";
         std::string run_dir = isulad_dir + "data/run";
         std::string data_dir = GetDirectory() + "/data";
-        struct storage_module_init_options *opts;
+
+        support_overlay = check_support_overlay(root_dir);
+        if (!support_overlay) {
+            return;
+        }
 
         ASSERT_STRNE(util_clean_path(data_dir.c_str(), data_path, sizeof(data_path)), nullptr);
         std::string cp_command = "cp -r " + std::string(data_path) + " " + isulad_dir;
@@ -117,15 +144,16 @@ protected:
                             + root_dir + "/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/work ";
         ASSERT_EQ(system(mkdir.c_str()), 0);
 
-        opts = (struct storage_module_init_options *)util_common_calloc_s(sizeof(struct storage_module_init_options));
+        struct storage_module_init_options *opts = (struct storage_module_init_options *)util_common_calloc_s(sizeof(struct storage_module_init_options));
         opts->storage_root = strdup(root_dir.c_str());
         opts->storage_run_root = strdup(run_dir.c_str());
         opts->driver_name = strdup("overlay");
-        opts->driver_opts = (char **)util_common_calloc_s(4 * sizeof(char *));
+        opts->driver_opts = (char **)util_common_calloc_s(5 * sizeof(char *));
         opts->driver_opts[0] = strdup("overlay2.basesize=128M");
         opts->driver_opts[1] = strdup("overlay2.override_kernel_check=true");
         opts->driver_opts[2] = strdup("overlay2.skip_mount_home=false");
         opts->driver_opts[3] = strdup("overlay2.mountopt=rw");
+        opts->driver_opts[4] = strdup("overlay2.skip_mount_home=true");
         opts->driver_opts_len = 4;
 
         EXPECT_CALL(m_driver_quota_mock, QuotaCtl(_, _, _, _)).WillRepeatedly(Invoke(invokeQuotaCtl));
@@ -141,18 +169,25 @@ protected:
     void TearDown() override
     {
         MockDriverQuota_SetMock(nullptr);
-        ASSERT_EQ(graphdriver_cleanup(), 0);
-        std::string rm_command = "rm -rf /var/lib/isulad/data";
+        if (support_overlay) {
+            ASSERT_EQ(graphdriver_cleanup(), 0);
+        }
+        std::string rm_command = "rm -rf /tmp/isulad/";
         ASSERT_EQ(system(rm_command.c_str()), 0);
     }
 
     NiceMock<MockDriverQuota> m_driver_quota_mock;
     char data_path[PATH_MAX] = { 0x00 };
+    bool support_overlay;
 };
 
 
 TEST_F(StorageDriverUnitTest, test_graphdriver_layer_exists)
 {
+    if (!support_overlay) {
+        return;
+    }
+
     std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
     std::string incorrectId { "eb29745b8228e1e97c01b1d5c2554a319c00a94d8dd5746a3904222ad65a13f8" };
     ASSERT_TRUE(graphdriver_layer_exists(id.c_str()));
@@ -161,6 +196,10 @@ TEST_F(StorageDriverUnitTest, test_graphdriver_layer_exists)
 
 TEST_F(StorageDriverUnitTest, test_graphdriver_create_rw)
 {
+    if (!support_overlay) {
+        return;
+    }
+
     std::string id { "eb29745b8228e1e97c01b1d5c2554a319c00a94d8dd5746a3904222ad65a13f8" };
     struct driver_create_opts *create_opts;
 
@@ -186,8 +225,12 @@ TEST_F(StorageDriverUnitTest, test_graphdriver_create_rw)
 
 TEST_F(StorageDriverUnitTest, test_graphdriver_mount_layer)
 {
+    if (!support_overlay) {
+        return;
+    }
+
     std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
-    std::string merged_dir = "/var/lib/isulad/data/overlay/" + id + "/merged";
+    std::string merged_dir = "/tmp/isulad/data/overlay/" + id + "/merged";
     struct driver_mount_opts *mount_opts = nullptr;
     char* mount_dir = nullptr;
 
@@ -219,6 +262,10 @@ TEST_F(StorageDriverUnitTest, test_graphdriver_mount_layer)
 
 TEST_F(StorageDriverUnitTest, test_graphdriver_try_repair_lowers)
 {
+    if (!support_overlay) {
+        return;
+    }
+
     std::string id { "1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569" };
     ASSERT_EQ(graphdriver_try_repair_lowers(id.c_str(), nullptr), 0);
 }
diff --git a/test/image/oci/storage/layers/storage_layers_ut.cc b/test/image/oci/storage/layers/storage_layers_ut.cc
index 87dfb4a1..fca37e83 100644
--- a/test/image/oci/storage/layers/storage_layers_ut.cc
+++ b/test/image/oci/storage/layers/storage_layers_ut.cc
@@ -59,6 +59,28 @@ std::string GetDirectory()
     return static_cast<std::string>(abs_path) + "../../../../../../test/image/oci/storage/layers";
 }
 
+bool check_support_overlay(std::string root_dir)
+{
+    if (!util_support_overlay()) {
+        std::cout << "Cannot support overlay, skip storage driver ut test." << std::endl;
+        return false;
+    }
+
+    char *backing_fs = util_get_fs_name(root_dir.c_str());
+    if (backing_fs == NULL) {
+        std::cout << "Failed to get fs name for " << root_dir << ", skip storage driver ut test." << std::endl;
+        return false;
+    }
+
+    if (strcmp(backing_fs, "aufs") == 0 || strcmp(backing_fs, "zfs") == 0 || strcmp(backing_fs, "overlayfs") == 0 ||
+        strcmp(backing_fs, "ecryptfs") == 0) {
+        std::cout << "Backing fs cannot support overlay, skip storage driver ut test." << std::endl;
+        return false;
+    }
+
+    return true;
+}
+
 bool dirExists(const char *path)
 {
     DIR *dp = nullptr;
@@ -159,6 +181,11 @@ protected:
         std::string run_dir = isulad_dir + "data/run";
         std::string data_dir = GetDirectory() + "/data";
 
+        support_overlay = check_support_overlay(root_dir);
+        if (!support_overlay) {
+            return;
+        }
+
         ASSERT_STRNE(util_clean_path(data_dir.c_str(), data_path, sizeof(data_path)), nullptr);
         std::string cp_command = "cp -r " + std::string(data_path) + " " + isulad_dir;
         ASSERT_EQ(system(cp_command.c_str()), 0);
@@ -186,8 +213,10 @@ protected:
     {
         MockDriverQuota_SetMock(nullptr);
 
-        layer_store_exit();
-        layer_store_cleanup();
+        if (support_overlay) {
+            layer_store_exit();
+            layer_store_cleanup();
+        }
 
         std::string rm_command = "rm -rf /tmp/isulad/";
         ASSERT_EQ(system(rm_command.c_str()), 0);
@@ -197,10 +226,15 @@ protected:
     char real_path[PATH_MAX] = { 0x00 };
     char real_run_path[PATH_MAX] = { 0x00 };
     char data_path[PATH_MAX] = { 0x00 };
+    bool support_overlay;
 };
 
 TEST_F(StorageLayersUnitTest, test_layers_load)
 {
+    if (!support_overlay) {
+        return;
+    }
+
     struct layer_list *layer_list = (struct layer_list *)util_common_calloc_s(sizeof(struct layer_list));
     ASSERT_NE(layer_list, nullptr);
 
@@ -246,6 +280,10 @@ TEST_F(StorageLayersUnitTest, test_layers_load)
 
 TEST_F(StorageLayersUnitTest, test_layer_store_exists)
 {
+    if (!support_overlay) {
+        return;
+    }
+
     std::string id { "7db8f44a0a8e12ea4283e3180e98880007efbd5de2e7c98b67de9cdd4dfffb0b" };
     std::string incorrectId { "50551ff67da98ab8540d7132" };
 
@@ -255,6 +293,10 @@ TEST_F(StorageLayersUnitTest, test_layer_store_exists)
 
 TEST_F(StorageLayersUnitTest, test_layer_store_create)
 {
+    if (!support_overlay) {
+        return;
+    }
+
     struct layer_opts *layer_opt = (struct layer_opts *)util_common_calloc_s(sizeof(struct layer_opts));
     layer_opt->parent = strdup("9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63");
     layer_opt->writable = true;
@@ -278,6 +320,10 @@ TEST_F(StorageLayersUnitTest, test_layer_store_create)
 
 TEST_F(StorageLayersUnitTest, test_layer_store_by_compress_digest)
 {
+    if (!support_overlay) {
+        return;
+    }
+
     std::string compress { "sha256:0e03bdcc26d7a9a57ef3b6f1bf1a210cff6239bff7c8cac72435984032851689" };
     std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
     struct layer_list *layer_list = (struct layer_list *)util_common_calloc_s(sizeof(struct layer_list));
@@ -294,6 +340,10 @@ TEST_F(StorageLayersUnitTest, test_layer_store_by_compress_digest)
 
 TEST_F(StorageLayersUnitTest, test_layer_store_by_uncompress_digest)
 {
+    if (!support_overlay) {
+        return;
+    }
+
     std::string uncompress { "sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
     std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
     struct layer_list *layer_list = (struct layer_list *)util_common_calloc_s(sizeof(struct layer_list));
-- 
2.25.1

