208 lines
8.3 KiB
Diff
208 lines
8.3 KiB
Diff
From cdb0e2b6431b4212b809ab1edf954d6b3a702a20 Mon Sep 17 00:00:00 2001
|
|
From: =?UTF-8?q?=E6=AD=A6=E7=A7=AF=E8=B6=85?= <wujichao1@huawei.com>
|
|
Date: Mon, 30 Dec 2024 17:28:23 +0800
|
|
Subject: [PATCH 176/198] add registry ut test
|
|
|
|
---
|
|
test/image/oci/registry/CMakeLists.txt | 1 +
|
|
test/image/oci/registry/registry_ut.cc | 123 +++++++++++++++++++++++++
|
|
2 files changed, 124 insertions(+)
|
|
|
|
diff --git a/test/image/oci/registry/CMakeLists.txt b/test/image/oci/registry/CMakeLists.txt
|
|
index 6166c2d0..d78bb7d3 100644
|
|
--- a/test/image/oci/registry/CMakeLists.txt
|
|
+++ b/test/image/oci/registry/CMakeLists.txt
|
|
@@ -71,5 +71,6 @@ target_include_directories(${EXE} PUBLIC
|
|
)
|
|
|
|
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} -lcrypto -lyajl -lz libhttpclient)
|
|
+set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,map_new -Wl,--wrap,util_common_calloc_s -Wl,--wrap,pthread_mutex_init -Wl,--wrap,pthread_cond_init")
|
|
add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
|
|
set_tests_properties(${EXE} PROPERTIES TIMEOUT 120)
|
|
diff --git a/test/image/oci/registry/registry_ut.cc b/test/image/oci/registry/registry_ut.cc
|
|
index 1503ee3b..4eacdb11 100644
|
|
--- a/test/image/oci/registry/registry_ut.cc
|
|
+++ b/test/image/oci/registry/registry_ut.cc
|
|
@@ -27,6 +27,7 @@
|
|
#include <gtest/gtest.h>
|
|
#include <gmock/gmock.h>
|
|
#include <curl/curl.h>
|
|
+#include <pthread.h>
|
|
|
|
#include "utils.h"
|
|
#include "utils_array.h"
|
|
@@ -45,6 +46,8 @@
|
|
#include "auths.h"
|
|
#include "oci_image_mock.h"
|
|
#include "isulad_config_mock.h"
|
|
+#include "map.h"
|
|
+#include "mock.h"
|
|
|
|
using ::testing::Args;
|
|
using ::testing::ByRef;
|
|
@@ -56,6 +59,55 @@ using ::testing::NotNull;
|
|
using ::testing::AtLeast;
|
|
using ::testing::Invoke;
|
|
|
|
+static int g_pthread_mutex_init_count = 0;
|
|
+static int g_pthread_mutex_init_match = 1;
|
|
+
|
|
+extern "C" {
|
|
+ DECLARE_WRAPPER_V(map_new, map_t *, (map_type_t kvtype, map_cmp_func comparator, map_kvfree_func kvfree));
|
|
+ DEFINE_WRAPPER_V(map_new, map_t *, (map_type_t kvtype, map_cmp_func comparator, map_kvfree_func kvfree), (kvtype, comparator, kvfree));
|
|
+ DECLARE_WRAPPER_V(pthread_mutex_init, int, (pthread_mutex_t *__mutex,const pthread_mutexattr_t *__mutexattr));
|
|
+ DEFINE_WRAPPER_V(pthread_mutex_init, int, (pthread_mutex_t *__mutex,const pthread_mutexattr_t *__mutexattr), (__mutex, __mutexattr));
|
|
+ DECLARE_WRAPPER_V(pthread_cond_init, int, (pthread_cond_t *__restrict __cond,const pthread_condattr_t *__restrict __cond_attr));
|
|
+ DEFINE_WRAPPER_V(pthread_cond_init, int, (pthread_cond_t *__restrict __cond,const pthread_condattr_t *__restrict __cond_attr), (__cond, __cond_attr));
|
|
+ DECLARE_WRAPPER_V(util_common_calloc_s, void *, (size_t size));
|
|
+ DEFINE_WRAPPER_V(util_common_calloc_s, void *, (size_t size), (size));
|
|
+}
|
|
+
|
|
+/*
|
|
+*Repeatedly calling the function executes the wrapper function and original function in the following order:
|
|
+*wrapper function; original function, wrapper function; original function, original function, wrapper function;...
|
|
+*Similar to regular queues (1 means wrapper, 0 means original): 1; 0 1; 0 0 1; 0 0 0 1; ...
|
|
+*It's used to MOCK a function that repeat permutation.
|
|
+*If you want a regular queue, the variables needs to be assigned back to the initial value.
|
|
+*/
|
|
+// extern int pthread_mutex_init (pthread_mutex_t *__mutex,const pthread_mutexattr_t *__mutexattr)
|
|
+static int failed_pthread_mutex_init(pthread_mutex_t *__mutex,const pthread_mutexattr_t *__mutexattr)
|
|
+{
|
|
+ g_pthread_mutex_init_count++;
|
|
+ if (g_pthread_mutex_init_count == g_pthread_mutex_init_match) {
|
|
+ g_pthread_mutex_init_match++;
|
|
+ g_pthread_mutex_init_count = 0;
|
|
+ return -1;
|
|
+ } else {
|
|
+ return __real_pthread_mutex_init(__mutex, __mutexattr);
|
|
+ }
|
|
+}
|
|
+
|
|
+void *util_common_calloc_s_fail(size_t size)
|
|
+{
|
|
+ return nullptr;
|
|
+}
|
|
+
|
|
+static int failed_pthread_cond_init(pthread_cond_t *__restrict __cond,const pthread_condattr_t *__restrict __cond_attr)
|
|
+{
|
|
+ return -1;
|
|
+}
|
|
+
|
|
+static map_t *map_new_return_null(map_type_t kvtype, map_cmp_func comparator, map_kvfree_func kvfree)
|
|
+{
|
|
+ return nullptr;
|
|
+}
|
|
+
|
|
std::string get_dir()
|
|
{
|
|
char abs_path[PATH_MAX] { 0x00 };
|
|
@@ -655,6 +707,25 @@ TEST_F(RegistryUnitTest, test_pull_v1_image)
|
|
ASSERT_EQ(util_mkdir_p(mirror_dir.c_str(), 0700), 0);
|
|
ASSERT_EQ(create_certs(mirror_dir), 0);
|
|
ASSERT_EQ(init_log(), 0);
|
|
+
|
|
+ // test utile common calloc fail
|
|
+ MOCK_SET_V(util_common_calloc_s, util_common_calloc_s_fail);
|
|
+ ASSERT_EQ(registry_init((char *)auths_dir.c_str(), (char *)certs_dir.c_str()), -1);
|
|
+ MOCK_CLEAR(util_common_calloc_s);
|
|
+ // test pthread mutex init fail
|
|
+ MOCK_SET_V(pthread_mutex_init, failed_pthread_mutex_init);
|
|
+ g_pthread_mutex_init_count = 0;
|
|
+ g_pthread_mutex_init_match = 1;
|
|
+ ASSERT_EQ(registry_init((char *)auths_dir.c_str(), (char *)certs_dir.c_str()), -1);
|
|
+ ASSERT_EQ(registry_init((char *)auths_dir.c_str(), (char *)certs_dir.c_str()), -1);
|
|
+ MOCK_CLEAR(pthread_mutex_init);
|
|
+ MOCK_SET_V(pthread_cond_init, failed_pthread_cond_init);
|
|
+ ASSERT_EQ(registry_init((char *)auths_dir.c_str(), (char *)certs_dir.c_str()), -1);
|
|
+ MOCK_CLEAR(pthread_cond_init);
|
|
+ MOCK_SET_V(map_new, map_new_return_null);
|
|
+ ASSERT_EQ(registry_init((char *)auths_dir.c_str(), (char *)certs_dir.c_str()), -1);
|
|
+ MOCK_CLEAR(map_new);
|
|
+
|
|
ASSERT_EQ(registry_init((char *)auths_dir.c_str(), (char *)certs_dir.c_str()), 0);
|
|
|
|
EXPECT_CALL(m_http_mock, HttpRequest(::testing::_, ::testing::_, ::testing::_, ::testing::_))
|
|
@@ -665,6 +736,30 @@ TEST_F(RegistryUnitTest, test_pull_v1_image)
|
|
ASSERT_EQ(registry_pull(&options), 0);
|
|
|
|
ASSERT_EQ(registry_pull(&options), 0);
|
|
+
|
|
+ // test empty options
|
|
+ ASSERT_EQ(registry_pull(nullptr), -1);
|
|
+
|
|
+ // test utile common calloc fail
|
|
+ MOCK_SET_V(util_common_calloc_s, util_common_calloc_s_fail);
|
|
+ ASSERT_EQ(registry_pull(&options), -1);
|
|
+ MOCK_CLEAR(util_common_calloc_s);
|
|
+
|
|
+ options.dest_image_name = nullptr;
|
|
+ ASSERT_EQ(registry_pull(&options), -1);
|
|
+ options.dest_image_name = (char *)"quay.io/coreos/etcd:v3.3.17-arm64";
|
|
+
|
|
+ options.image_name = nullptr;
|
|
+ ASSERT_EQ(registry_pull(&options), -1);
|
|
+ options.image_name = (char *)"quay.io/coreos/etcd:v3.3.17-arm64";
|
|
+
|
|
+ // test pthread mutex init fail
|
|
+ MOCK_SET_V(pthread_mutex_init, failed_pthread_mutex_init);
|
|
+ g_pthread_mutex_init_count = 0;
|
|
+ g_pthread_mutex_init_match = 1;
|
|
+ ASSERT_EQ(registry_pull(&options), -1);
|
|
+ ASSERT_EQ(registry_pull(&options), -1);
|
|
+ MOCK_CLEAR(pthread_mutex_init);
|
|
}
|
|
|
|
TEST_F(RegistryUnitTest, test_login)
|
|
@@ -690,6 +785,21 @@ TEST_F(RegistryUnitTest, test_login)
|
|
options.auth.username = (char *)"test3";
|
|
options.auth.password = (char *)"test3";
|
|
ASSERT_EQ(registry_login(&options), 0);
|
|
+
|
|
+ // test empty options
|
|
+ ASSERT_EQ(registry_login(nullptr), -1);
|
|
+
|
|
+ // test utile common calloc fail
|
|
+ MOCK_SET_V(util_common_calloc_s, util_common_calloc_s_fail);
|
|
+ ASSERT_EQ(registry_login(&options), -1);
|
|
+ MOCK_CLEAR(util_common_calloc_s);
|
|
+
|
|
+ // test pthread mutex init fail
|
|
+ MOCK_SET_V(pthread_mutex_init, failed_pthread_mutex_init);
|
|
+ g_pthread_mutex_init_count = 0;
|
|
+ g_pthread_mutex_init_match = 1;
|
|
+ ASSERT_EQ(registry_login(&options), -1);
|
|
+ MOCK_CLEAR(pthread_mutex_init);
|
|
}
|
|
|
|
TEST_F(RegistryUnitTest, test_logout)
|
|
@@ -699,6 +809,9 @@ TEST_F(RegistryUnitTest, test_logout)
|
|
|
|
ASSERT_EQ(registry_logout((char *)"test2.com"), 0);
|
|
|
|
+ // test empty host
|
|
+ ASSERT_EQ(registry_logout(nullptr), -1);
|
|
+
|
|
auth_data = util_read_text_file(auths_file.c_str());
|
|
ASSERT_NE(strstr(auth_data, "hub-mirror.c.163.com"), nullptr);
|
|
free(auth_data);
|
|
@@ -837,6 +950,16 @@ TEST_F(RegistryUnitTest, test_search_image)
|
|
ASSERT_EQ(result->results[0]->is_automated, false);
|
|
ASSERT_EQ(result->results[0]->is_official, true);
|
|
|
|
+ // test Invalid NULL param
|
|
+ options->search_name = nullptr;
|
|
+ ASSERT_EQ(registry_search(options, &result), -1);
|
|
+ options->search_name = util_strdup_s("index.docker.io/busybox");
|
|
+
|
|
+ // test utile common calloc fail
|
|
+ MOCK_SET_V(util_common_calloc_s, util_common_calloc_s_fail);
|
|
+ ASSERT_EQ(registry_search(options, &result), -1);
|
|
+ MOCK_CLEAR(util_common_calloc_s);
|
|
+
|
|
free_imagetool_search_result(result);
|
|
|
|
// test not found
|
|
--
|
|
2.34.1
|
|
|