diff --git a/CI/Dockerfile b/CI/Dockerfile index 7fffdd2f672fdd7fc81cbe11e54813365435ae33..16179842816650b4e948525513350f19d0d0cbb9 100644 --- a/CI/Dockerfile +++ b/CI/Dockerfile @@ -154,6 +154,7 @@ RUN export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH && \ cd ~ && \ git clone https://gitee.com/src-openeuler/grpc.git && \ cd grpc && \ + git checkout -b openEuler-20.03-LTS-tag openEuler-20.03-LTS-tag && \ tar -xzvf grpc-1.22.0.tar.gz && \ cd grpc-1.22.0 && \ make -j $(nproc) && \ @@ -200,8 +201,8 @@ RUN export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH && \ cd ~ && \ git clone https://gitee.com/src-openeuler/http-parser.git && \ cd http-parser && \ - tar -xzvf http-parser-2.9.2.tar.gz && \ - cd http-parser-2.9.2 && \ + tar -xzvf http-parser-2.9.4.tar.gz && \ + cd http-parser-2.9.4 && \ make -j CFLAGS="-Wno-error" && \ make CFLAGS="-Wno-error" install && \ ldconfig @@ -212,9 +213,8 @@ RUN export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH && \ cd ~ && \ git clone https://gitee.com/src-openeuler/libwebsockets.git && \ cd libwebsockets && \ - tar -xzvf libwebsockets-2.4.2.tar.gz && \ - cd libwebsockets-2.4.2 && \ - patch -p1 -F1 -s < ../libwebsockets-fix-coredump.patch && \ + tar -xzvf libwebsockets-4.0.1.tar.gz && \ + cd libwebsockets-4.0.1 && \ mkdir build && \ cd build && \ cmake -DLWS_WITH_SSL=0 -DLWS_MAX_SMP=32 -DCMAKE_BUILD_TYPE=Debug ../ && \ diff --git a/test/image/oci/storage/CMakeLists.txt b/test/image/oci/storage/CMakeLists.txt index 08b4548cd7c3b3af1cccf44101f0029134b56767..feb81b14547e646163909f1a2b4a0e0a8eff8538 100644 --- a/test/image/oci/storage/CMakeLists.txt +++ b/test/image/oci/storage/CMakeLists.txt @@ -2,4 +2,4 @@ project(iSulad_UT) add_subdirectory(images) add_subdirectory(rootfs) -# add_subdirectory(layers) +add_subdirectory(layers) diff --git a/test/image/oci/storage/layers/CMakeLists.txt b/test/image/oci/storage/layers/CMakeLists.txt index 4905fcdeab07f540a37e6ab4dc56fd286c404fe8..4972548fcdf88672217a463a0f8feb35a206ad12 100644 --- a/test/image/oci/storage/layers/CMakeLists.txt +++ b/test/image/oci/storage/layers/CMakeLists.txt @@ -1,5 +1,64 @@ project(iSulad_UT) +SET(DRIVER_EXE storage_driver_ut) + +add_executable(${DRIVER_EXE} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_regex.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_verify.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_array.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_string.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_convert.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_file.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_fs.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/util_atomic.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_base64.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_timestamp.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/path.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/map/map.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/map/rb_tree.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/tar/util_archive.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/sha256/sha256.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/libisulad.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/layer_store/graphdriver/driver.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/layer_store/graphdriver/devmapper/metadata_store.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/layer_store/graphdriver/quota/project_quota.c + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks/driver_quota_mock.cc + storage_driver_ut.cpp) + +target_include_directories(${DRIVER_EXE} PUBLIC + ${GTEST_INCLUDE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../include + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/tar + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/map + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/sha256 + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/console + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/layer_store/graphdriver + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/layer_store/graphdriver/devmapper/ + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/layer_store/graphdriver/overlay2 + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/daemon/image/oci/storage/layer_store/graphdriver/quota + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks + ) + +target_link_libraries(${DRIVER_EXE} + ${GTEST_BOTH_LIBRARIES} + ${GMOCK_LIBRARY} + ${GMOCK_MAIN_LIBRARY} + ${CMAKE_THREAD_LIBS_INIT} + ${ISULA_LIBUTILS_LIBRARY} + ${LIBTAR_LIBRARY} + -lwebsockets -lcrypto -lyajl -larchive -ldevmapper -lz) + +#[[ SET(EXE storage_layers_ut) aux_source_directory(${CMAKE_BINARY_DIR}/json json_files) @@ -70,3 +129,4 @@ target_link_libraries(${EXE} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lwebsockets -lcrypto -lyajl -larchive -ldevmapper -lz) +]] \ No newline at end of file diff --git a/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/bin/busybox b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/bin/busybox new file mode 100755 index 0000000000000000000000000000000000000000..a78a07e1056bd44ebf5e90f32a84280929e10a95 Binary files /dev/null and b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/bin/busybox differ diff --git a/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/etc/group b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/etc/group new file mode 100644 index 0000000000000000000000000000000000000000..76346b35f2da0d79e5ab4e79459eb1ecff3cd7f0 --- /dev/null +++ b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/etc/group @@ -0,0 +1,26 @@ +root:x:0: +daemon:x:1: +bin:x:2: +sys:x:3: +adm:x:4: +tty:x:5: +disk:x:6: +lp:x:7: +mail:x:8: +kmem:x:9: +wheel:x:10:root +cdrom:x:11: +dialout:x:18: +floppy:x:19: +video:x:28: +audio:x:29: +tape:x:32: +www-data:x:33: +operator:x:37: +utmp:x:43: +plugdev:x:46: +staff:x:50: +lock:x:54: +netdev:x:82: +users:x:100: +nogroup:x:65534: diff --git a/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/etc/localtime b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/etc/localtime new file mode 100644 index 0000000000000000000000000000000000000000..c3b97f1a199421d6d9625b280316d99b85a4a4e8 Binary files /dev/null and b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/etc/localtime differ diff --git a/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/etc/passwd b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/etc/passwd new file mode 100644 index 0000000000000000000000000000000000000000..d8281d2585362b698a26b340074ecc4c9adc5151 --- /dev/null +++ b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/etc/passwd @@ -0,0 +1,9 @@ +root:x:0:0:root:/root:/bin/sh +daemon:x:1:1:daemon:/usr/sbin:/bin/false +bin:x:2:2:bin:/bin:/bin/false +sys:x:3:3:sys:/dev:/bin/false +sync:x:4:100:sync:/bin:/bin/sync +mail:x:8:8:mail:/var/spool/mail:/bin/false +www-data:x:33:33:www-data:/var/www:/bin/false +operator:x:37:37:Operator:/var:/bin/false +nobody:x:65534:65534:nobody:/home:/bin/false diff --git a/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/etc/shadow b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/etc/shadow new file mode 100644 index 0000000000000000000000000000000000000000..0391bdc4aa1ab7570c5aca8affb2374168870791 --- /dev/null +++ b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff/etc/shadow @@ -0,0 +1,9 @@ +root:*::::::: +daemon:*::::::: +bin:*::::::: +sys:*::::::: +sync:*::::::: +mail:*::::::: +www-data:*::::::: +operator:*::::::: +nobody:*::::::: diff --git a/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/link b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/link new file mode 100644 index 0000000000000000000000000000000000000000..767cc8597d681bbbadbea94c8198dbdefa5c626e --- /dev/null +++ b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/link @@ -0,0 +1 @@ +d462e429837b2644fce88885d7 \ No newline at end of file diff --git a/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/lower b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/lower new file mode 100644 index 0000000000000000000000000000000000000000..cea229ff9afaa8d2d57fa1417abcd48d42ee8dd5 --- /dev/null +++ b/test/image/oci/storage/layers/data/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/lower @@ -0,0 +1 @@ +l/c3343428f60aab9c86d24293f6 diff --git a/test/image/oci/storage/layers/data/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/diff/hello b/test/image/oci/storage/layers/data/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/diff/hello new file mode 100755 index 0000000000000000000000000000000000000000..5052f25bc5c6f3f56e3795d8bee963a28b06e5b3 Binary files /dev/null and b/test/image/oci/storage/layers/data/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/diff/hello differ diff --git a/test/image/oci/storage/layers/data/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/link b/test/image/oci/storage/layers/data/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/link new file mode 100644 index 0000000000000000000000000000000000000000..27aaf612b926bdb294d0cd12dfdc488205451ef8 --- /dev/null +++ b/test/image/oci/storage/layers/data/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/link @@ -0,0 +1 @@ +c3343428f60aab9c86d24293f6 \ No newline at end of file diff --git a/test/image/oci/storage/layers/data/overlay/l/c3343428f60aab9c86d24293f6 b/test/image/oci/storage/layers/data/overlay/l/c3343428f60aab9c86d24293f6 new file mode 120000 index 0000000000000000000000000000000000000000..380c847e370c3442b7555db0774bf78479610940 --- /dev/null +++ b/test/image/oci/storage/layers/data/overlay/l/c3343428f60aab9c86d24293f6 @@ -0,0 +1 @@ +../9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/diff \ No newline at end of file diff --git a/test/image/oci/storage/layers/data/overlay/l/d462e429837b2644fce88885d7 b/test/image/oci/storage/layers/data/overlay/l/d462e429837b2644fce88885d7 new file mode 120000 index 0000000000000000000000000000000000000000..05b766a606d3fb2e82fef66232655dce8dd5982d --- /dev/null +++ b/test/image/oci/storage/layers/data/overlay/l/d462e429837b2644fce88885d7 @@ -0,0 +1 @@ +../1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/diff \ No newline at end of file diff --git a/test/image/oci/storage/layers/storage_driver_ut.cpp b/test/image/oci/storage/layers/storage_driver_ut.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e4bbec94acee8dba84f933b744b35b7fa678dd6 --- /dev/null +++ b/test/image/oci/storage/layers/storage_driver_ut.cpp @@ -0,0 +1,232 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. 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: zhangxiaoyu + * Create: 2020-06-24 + * Description: provide oci storage driver unit test + ******************************************************************************/ +#include "driver.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "path.h" +#include "utils_array.h" +#include "driver_overlay2.h" +#include "driver_quota_mock.h" + +using ::testing::Args; +using ::testing::ByRef; +using ::testing::SetArgPointee; +using ::testing::DoAll; +using ::testing::NiceMock; +using ::testing::Return; +using ::testing::NotNull; +using ::testing::AtLeast; +using ::testing::Invoke; +using ::testing::_; +using ::testing::FLAGS_gmock_catch_leaked_mocks; + +std::string GetDirectory() +{ + char abs_path[PATH_MAX]; + int ret = readlink("/proc/self/exe", abs_path, sizeof(abs_path)); + if (ret < 0 || (size_t)ret >= sizeof(abs_path)) { + return ""; + } + + for (int i { ret }; i >= 0; --i) { + if (abs_path[i] == '/') { + abs_path[i + 1] = '\0'; + break; + } + } + + return static_cast(abs_path); +} + +bool dirExists(const char *path) +{ + DIR *dp = NULL; + if ((dp = opendir(path)) == NULL) { + return false; + } + + closedir(dp); + return true; +} + +int invokeGetPageSize() +{ + std::string abs_mount = "rw," + "lowerdir=/var/lib/isulad/data/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/empty," + "upperdir=/var/lib/isulad/data/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/diff," + "workdir=/var/lib/isulad/data/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/work"; + return abs_mount.length() - 1; +} + +int invokeIOCtl(int fd, int cmd) +{ + return 0; +} + +int invokeQuotaCtl(int cmd, const char* special, int id, caddr_t addr) +{ +#define XFS_QUOTA_PDQ_ACCT (1<<4) // project quota accounting +#define XFS_QUOTA_PDQ_ENFD (1<<5) // project quota limits enforcement + + fs_quota_stat_t* fs_quota_stat_info = (fs_quota_stat_t *)addr; + fs_quota_stat_info->qs_flags = XFS_QUOTA_PDQ_ACCT | XFS_QUOTA_PDQ_ENFD; + + return 0; +} + +class StorageDriverUnitTest : public testing::Test { +protected: + void SetUp() override + { + MockDriverQuota_SetMock(&m_driver_quota_mock); + std::string isulad_dir { "/var/lib/isulad/" }; + 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; + + ASSERT_STRNE(cleanpath(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); + + std::string mkdir = "mkdir -p " + root_dir + "/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/merged " + + root_dir + "/overlay/1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569/work && " + "mkdir -p " + root_dir + "/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/empty " + + root_dir + "/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/merged " + + root_dir + "/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/work "; + ASSERT_EQ(system(mkdir.c_str()), 0); + + opts = (struct storage_module_init_options *)malloc(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 **)malloc(4 * 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_len = 4; + + EXPECT_CALL(m_driver_quota_mock, QuotaCtl(_, _, _, _)).WillRepeatedly(Invoke(invokeQuotaCtl)); + ASSERT_EQ(graphdriver_init(opts), 0); + + free(opts->storage_root); + free(opts->storage_run_root); + free(opts->driver_name); + util_free_array_by_len(opts->driver_opts, opts->driver_opts_len); + free(opts); + } + + void TearDown() override + { + MockDriverQuota_SetMock(nullptr); + ASSERT_EQ(graphdriver_cleanup(), 0); + std::string rm_command = "rm -rf /var/lib/isulad/data"; + ASSERT_EQ(system(rm_command.c_str()), 0); + } + + NiceMock m_driver_quota_mock; + char data_path[PATH_MAX] = { 0x00 }; +}; + + +TEST_F(StorageDriverUnitTest, test_graphdriver_layer_exists) +{ + std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" }; + std::string incorrectId { "eb29745b8228e1e97c01b1d5c2554a319c00a94d8dd5746a3904222ad65a13f8" }; + ASSERT_TRUE(graphdriver_layer_exists(id.c_str())); + ASSERT_FALSE(graphdriver_layer_exists(incorrectId.c_str())); +} + +TEST_F(StorageDriverUnitTest, test_graphdriver_create_rw) +{ + std::string id { "eb29745b8228e1e97c01b1d5c2554a319c00a94d8dd5746a3904222ad65a13f8" }; + struct driver_create_opts *create_opts; + + create_opts = (struct driver_create_opts *)malloc(sizeof(struct driver_create_opts)); + ASSERT_NE(create_opts, nullptr); + + create_opts->mount_label = strdup("mount_label"); + create_opts->storage_opt = (json_map_string_string *)malloc(sizeof(json_map_string_string)); + ASSERT_NE(create_opts->storage_opt, nullptr); + create_opts->storage_opt->keys = (char **)malloc(sizeof(char *)); + create_opts->storage_opt->values = (char **)malloc(sizeof(char *)); + create_opts->storage_opt->keys[0] = strdup("size"); + create_opts->storage_opt->values[0] = strdup("128M"); + create_opts->storage_opt->len = 1; + + EXPECT_CALL(m_driver_quota_mock, IOCtl(_, _)).WillRepeatedly(Invoke(invokeIOCtl)); + ASSERT_EQ(graphdriver_create_rw(id.c_str(), nullptr, create_opts), 0); + ASSERT_TRUE(graphdriver_layer_exists(id.c_str())); + + ASSERT_EQ(graphdriver_rm_layer(id.c_str()), 0); + ASSERT_FALSE(graphdriver_layer_exists(id.c_str())); + free_driver_create_opts(create_opts); +} + +TEST_F(StorageDriverUnitTest, test_graphdriver_mount_layer) +{ + std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" }; + std::string merged_dir = "/var/lib/isulad/data/overlay/" + id + "/merged"; + struct driver_mount_opts *mount_opts = nullptr; + char* mount_dir = nullptr; + + EXPECT_CALL(m_driver_quota_mock, GetPageSize()).WillRepeatedly(Invoke(invokeGetPageSize)); + FLAGS_gmock_catch_leaked_mocks = false; // the exit in the child without deleting the mock object + mount_dir = graphdriver_mount_layer(id.c_str(), mount_opts); + ASSERT_STREQ(mount_dir, merged_dir.c_str()); + FLAGS_gmock_catch_leaked_mocks = true; + + ASSERT_EQ(graphdriver_umount_layer(id.c_str()), 0); + free(mount_dir); + mount_dir = nullptr; + + mount_opts = (struct driver_mount_opts *)malloc(sizeof(struct driver_mount_opts)); + ASSERT_NE(mount_opts, nullptr); + mount_opts->mount_label = strdup("mount_label"); + mount_opts->options = (char **)malloc(1 * sizeof(char *)); + mount_opts->options[0] = strdup("ro"); + mount_opts->options_len = 1; + + FLAGS_gmock_catch_leaked_mocks = false; + mount_dir = graphdriver_mount_layer(id.c_str(), mount_opts); + ASSERT_STREQ(mount_dir, merged_dir.c_str()); + FLAGS_gmock_catch_leaked_mocks = true; + + ASSERT_EQ(graphdriver_umount_layer(id.c_str()), 0); + free_driver_mount_opts(mount_opts); + free(mount_dir); +} + +TEST_F(StorageDriverUnitTest, test_graphdriver_try_repair_lowers) +{ + std::string id { "1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569" }; + ASSERT_EQ(graphdriver_try_repair_lowers(id.c_str(), nullptr), 0); +} + +TEST(StorageOverlay2QuotaOptionsTest, test_overlay2_is_quota_options) +{ + std::vector options { "overlay2.size", "overlay2.basesize" }; + for(auto option : options) { + ASSERT_TRUE(overlay2_is_quota_options(nullptr, option.c_str())); + } +} \ No newline at end of file diff --git a/test/mocks/driver_quota_mock.cc b/test/mocks/driver_quota_mock.cc new file mode 100644 index 0000000000000000000000000000000000000000..e3c32601f2f81077b07e2b87d5700c8ce13c9cb1 --- /dev/null +++ b/test/mocks/driver_quota_mock.cc @@ -0,0 +1,49 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2020. 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: zhangxiaoyu + * Create: 2020-06-20 + * Description: provide driver quota mock + ******************************************************************************/ + +#include "driver_quota_mock.h" + +namespace { +MockDriverQuota *g_driver_quota_mock = NULL; +} + +void MockDriverQuota_SetMock(MockDriverQuota* mock) +{ + g_driver_quota_mock = mock; +} + +int getpagesize() +{ + if (g_driver_quota_mock != nullptr) { + return g_driver_quota_mock->GetPageSize(); + } + return 0; +} + +int ioctl(int fd, unsigned long int cmd, ...) +{ + if (g_driver_quota_mock != nullptr) { + return g_driver_quota_mock->IOCtl(fd, cmd); + } + return 0; +} + +int quotactl(int cmd, const char* special, int id, caddr_t addr) +{ + if (g_driver_quota_mock != nullptr) { + return g_driver_quota_mock->QuotaCtl(cmd, special, id, addr); + } + return 0; +} \ No newline at end of file diff --git a/test/mocks/driver_quota_mock.h b/test/mocks/driver_quota_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..4d8b0a12c8d7815170900c5534c9e6e8a05cddd1 --- /dev/null +++ b/test/mocks/driver_quota_mock.h @@ -0,0 +1,33 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2020. 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: zhangxiaoyu + * Create: 2020-06-20 + * Description: provide driver quota mock + ******************************************************************************/ + +#ifndef DRIVER_QUOTA_MOCK_H_ +#define DRIVER_QUOTA_MOCK_H_ + +#include +#include "project_quota.h" + +class MockDriverQuota { +public: + virtual ~MockDriverQuota() = default; + MOCK_METHOD0(GetPageSize, int()); + MOCK_METHOD2(IOCtl, int(int, unsigned long int)); + MOCK_METHOD4(QuotaCtl, int(int, const char*, int, caddr_t)); + +}; + +void MockDriverQuota_SetMock(MockDriverQuota* mock); + +#endif // DRIVER_QUOTA_MOCK_H_