From 7e4921d01576f180c3624195a0edff4b4f6807f8 Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Tue, 18 Oct 2022 10:46:14 +0800
Subject: [PATCH 21/43] Add ut for utils_mount_spec

Signed-off-by: zhongtao <taozh97@163.com>
---
 test/cutils/CMakeLists.txt                    |  1 +
 test/cutils/utils_mount_spec/CMakeLists.txt   | 16 +++++
 .../utils_mount_spec/utils_mount_spec_ut.cc   | 64 +++++++++++++++++++
 3 files changed, 81 insertions(+)
 create mode 100644 test/cutils/utils_mount_spec/CMakeLists.txt
 create mode 100644 test/cutils/utils_mount_spec/utils_mount_spec_ut.cc

diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 7f454f75..f159aacb 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -29,3 +29,4 @@ add_subdirectory(utils_error)
 add_subdirectory(utils_fs)
 add_subdirectory(utils_filters)
 add_subdirectory(utils_timestamp)
+add_subdirectory(utils_mount_spec)
diff --git a/test/cutils/utils_mount_spec/CMakeLists.txt b/test/cutils/utils_mount_spec/CMakeLists.txt
new file mode 100644
index 00000000..24fb5add
--- /dev/null
+++ b/test/cutils/utils_mount_spec/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_mount_spec_ut)
+
+add_executable(${EXE}
+    utils_mount_spec_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+    ${GTEST_INCLUDE_DIR}
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+    )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_mount_spec/utils_mount_spec_ut.cc b/test/cutils/utils_mount_spec/utils_mount_spec_ut.cc
new file mode 100644
index 00000000..0f60d397
--- /dev/null
+++ b/test/cutils/utils_mount_spec/utils_mount_spec_ut.cc
@@ -0,0 +1,64 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *     http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhongtao
+ * Create: 2022-10-18
+ * Description: utils mount spec unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "utils_mount_spec.h"
+
+TEST(utils_mount_spec, test_util_valid_mount_spec)
+{
+    char *base_valid = (char *)"type=bind,source=/home,target=/vol3,readonly=true,bind-selinux-opts=z,bind-propagation=rprivate";
+    char *oci_valid = (char *)"type=tmpfs,dst=/tmpfs,tmpfs-size=1m,tmpfs-mode=1700";
+    char *invalid1 = (char *)"type=volume,src=vol,dst=/vol,ro=true,red=false";
+    char *invalid2 = (char *)"type,src,dst";
+    char *errmsg = NULL;
+
+    ASSERT_EQ(util_valid_mount_spec(base_valid, &errmsg), true);
+    ASSERT_EQ(util_valid_mount_spec(oci_valid, &errmsg), true);
+
+    ASSERT_EQ(util_valid_mount_spec(invalid1, &errmsg), false);
+    ASSERT_EQ(util_valid_mount_spec(invalid2, &errmsg), false);
+    ASSERT_EQ(util_valid_mount_spec(nullptr, &errmsg), false);
+    ASSERT_EQ(util_valid_mount_spec(base_valid, nullptr), false);
+}
+
+TEST(utils_mount_spec, test_util_parse_mount_spec)
+{
+    char *base_valid = (char *)"type=bind,source=/home,target=/vol3,readonly=true,bind-selinux-opts=z,bind-propagation=rprivate";
+    char *oci_valid = (char *)"type=tmpfs,dst=/tmpfs,tmpfs-size=1m,tmpfs-mode=1700";
+    char *invalid1 = (char *)"type=volume,src=vol,dst=/vol,ro=true,red=false";
+    char *invalid2 = (char *)"type,src,dst";
+    mount_spec *m = NULL;
+    char *errmsg = NULL;
+
+    ASSERT_EQ(util_parse_mount_spec(base_valid, &m, &errmsg), 0);
+    ASSERT_STREQ(m->type, "bind");
+    ASSERT_STREQ(m->source, "/home");
+    ASSERT_STREQ(m->target, "/vol3");
+    ASSERT_EQ(m->readonly, true);
+    ASSERT_STREQ(m->bind_options->propagation, "rprivate");
+    ASSERT_STREQ(m->bind_options->selinux_opts, "z");
+
+    ASSERT_EQ(util_parse_mount_spec(oci_valid, &m, &errmsg), 0);
+    ASSERT_STREQ(m->type, "tmpfs");
+    ASSERT_STREQ(m->target, "/tmpfs");
+    ASSERT_EQ(m->tmpfs_options->size_bytes, 1048576);
+    ASSERT_EQ(m->tmpfs_options->mode, 960);
+
+    ASSERT_NE(util_parse_mount_spec(invalid1, &m, &errmsg), 0);
+    ASSERT_NE(util_parse_mount_spec(invalid2, &m, &errmsg), 0);
+    ASSERT_NE(util_parse_mount_spec(nullptr, &m, &errmsg), 0);
+    ASSERT_NE(util_parse_mount_spec(base_valid, nullptr, &errmsg), 0);
+    ASSERT_NE(util_parse_mount_spec(base_valid, &m, nullptr), 0);
+}
-- 
2.25.1

