1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
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
|