summaryrefslogtreecommitdiff
path: root/0175-add-layer-storage-ut-test.patch
blob: 264fb097224c033950adfe23eda87500c6b99bbe (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
From 96ce67b474de6d6cff1a87cd652ff00dafda7d6e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=AD=A6=E7=A7=AF=E8=B6=85?= <wujichao1@huawei.com>
Date: Tue, 24 Dec 2024 19:39:26 +0800
Subject: [PATCH 11/11] add layer storage ut test

---
 test/image/oci/storage/layers/CMakeLists.txt  |   1 +
 .../oci/storage/layers/storage_layers_ut.cc   | 166 +++++++++++++++++-
 2 files changed, 165 insertions(+), 2 deletions(-)

diff --git a/test/image/oci/storage/layers/CMakeLists.txt b/test/image/oci/storage/layers/CMakeLists.txt
index e1c76453..c4384e8f 100644
--- a/test/image/oci/storage/layers/CMakeLists.txt
+++ b/test/image/oci/storage/layers/CMakeLists.txt
@@ -148,5 +148,6 @@ target_link_libraries(${LAYER_EXE}
     ${LIBTAR_LIBRARY}
     -lwebsockets -lcrypto -lyajl -larchive ${SELINUX_LIBRARY} -ldevmapper -lz -lcap)
 
+set_target_properties(${LAYER_EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,map_new -Wl,--wrap,map_insert -Wl,--wrap,map_search -Wl,--wrap,util_common_calloc_s -Wl,--wrap,util_smart_calloc_s") 
 add_test(NAME ${LAYER_EXE} COMMAND ${LAYER_EXE} --gtest_output=xml:${LAYER_EXE}-Results.xml)
 set_tests_properties(${LAYER_EXE} PROPERTIES TIMEOUT 120)
diff --git a/test/image/oci/storage/layers/storage_layers_ut.cc b/test/image/oci/storage/layers/storage_layers_ut.cc
index 73611fdc..a03f4ce8 100644
--- a/test/image/oci/storage/layers/storage_layers_ut.cc
+++ b/test/image/oci/storage/layers/storage_layers_ut.cc
@@ -29,6 +29,8 @@
 #include "storage.h"
 #include "layer.h"
 #include "driver_quota_mock.h"
+#include "map.h"
+#include "mock.h"
 
 using ::testing::Args;
 using ::testing::ByRef;
@@ -41,6 +43,95 @@ using ::testing::AtLeast;
 using ::testing::Invoke;
 using ::testing::_;
 
+static int g_map_search_count = 0;
+static int g_map_search_match = 1;
+static int g_map_new_count = 0;
+static int g_map_new_match = 1;
+static int g_map_insert_count = 0;
+static int g_map_insert_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(map_insert, bool, (map_t *map, void *key, void *value));
+    DEFINE_WRAPPER_V(map_insert, bool, (map_t *map, void *key, void *value), (map, key, value));
+    DECLARE_WRAPPER_V(map_search, void *, (const map_t *map, void *key));
+    DEFINE_WRAPPER_V(map_search, void *, (const map_t *map, void *key), (map, key));
+
+    DECLARE_WRAPPER_V(util_smart_calloc_s, void *, (size_t size, size_t len));
+    DEFINE_WRAPPER_V(util_smart_calloc_s, void *, (size_t size, size_t len), (size, len));
+    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.
+*/
+static map_t *map_new_return_null(map_type_t kvtype, map_cmp_func comparator, map_kvfree_func kvfree)
+{
+    g_map_new_count++;
+    if (g_map_new_count == g_map_new_match) {
+        g_map_new_match++;
+        g_map_new_count = 0;
+        return nullptr;
+    } else {
+        return __real_map_new(kvtype, comparator, kvfree);
+    }
+}
+
+/*
+* 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.
+*/
+static bool map_insert_return_false(map_t *map, void *key, void *value)
+{
+    g_map_insert_count++;
+    if (g_map_insert_count == g_map_insert_match) {
+        g_map_insert_match++;
+        g_map_insert_count = 0;
+        return false;
+    } else {
+        return __real_map_insert(map, key, value);
+    }
+}
+
+/*
+* 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.
+*/
+void *map_search_fail(const map_t *map, void *key)
+{
+    g_map_search_count++;
+    if (g_map_search_count == g_map_search_match) {
+        g_map_search_match++;
+        g_map_search_count = 0;
+        return nullptr;
+    } else {
+        return __real_map_search(map, key);
+    }
+
+}
+
+void *util_common_calloc_s_fail(size_t size)
+{
+    return nullptr;
+}
+
+void *util_smart_calloc_s_fail(size_t size, size_t len)
+{
+    return nullptr;
+}
+
 std::string GetDirectory()
 {
     char abs_path[PATH_MAX] { 0x00 };
@@ -178,6 +269,7 @@ protected:
         std::string isulad_dir = "/tmp/isulad/";
         mkdir(isulad_dir.c_str(), 0755);
         std::string root_dir = isulad_dir + "data";
+        mkdir(root_dir.c_str(), 0755);
         std::string run_dir = isulad_dir + "data/run";
         std::string data_dir = GetDirectory() + "/data";
 
@@ -194,12 +286,40 @@ protected:
         opts.storage_root = strdup(real_path);
         ASSERT_STRNE(util_clean_path(run_dir.c_str(), real_run_path, sizeof(real_run_path)), nullptr);
         opts.storage_run_root = strdup(real_run_path);
-        opts.driver_name = strdup("overlay");
         opts.driver_opts = static_cast<char **>(util_smart_calloc_s(sizeof(char *), 1));
         opts.driver_opts[0] = strdup("overlay2.skip_mount_home=true");
         opts.driver_opts_len = 1;
-
+#ifdef ENABLE_REMOTE_LAYER_STORE
+        opts.enable_remote_layer = true;
+#endif
         EXPECT_CALL(m_driver_quota_mock, QuotaCtl(_, _, _, _)).WillRepeatedly(Invoke(invokeQuotaCtl));
+
+        opts.driver_name = NULL;
+        ASSERT_EQ(layer_store_init(&opts), -1);
+
+        char over_path_max_driver_name[5000] { 0x00 }; // PATH_MAX = 4096
+        std::memset(over_path_max_driver_name, 'a', 4999);
+        over_path_max_driver_name[4999]= '\0';
+        opts.driver_name = over_path_max_driver_name;
+        ASSERT_EQ(layer_store_init(&opts), -1);
+
+        opts.driver_name = strdup("overlay");
+        MOCK_SET_V(map_new, map_new_return_null);
+        g_map_new_count = 0;
+        g_map_new_match = 1;
+        ASSERT_EQ(layer_store_init(&opts), -1);
+        ASSERT_EQ(layer_store_init(&opts), -1);
+        ASSERT_EQ(layer_store_init(&opts), -1);
+        ASSERT_EQ(layer_store_init(&opts), -1);
+        MOCK_CLEAR(map_new);
+
+        MOCK_SET_V(map_insert, map_insert_return_false);
+        g_map_insert_count = 0;
+        g_map_insert_match = 1;
+        ASSERT_EQ(layer_store_init(&opts), -1);
+        ASSERT_EQ(layer_store_init(&opts), -1);
+        MOCK_CLEAR(map_insert);
+
         ASSERT_EQ(layer_store_init(&opts), 0);
 
         free(opts.storage_root);
@@ -238,6 +358,13 @@ TEST_F(StorageLayersUnitTest, test_layers_load)
     struct layer_list *layer_list = (struct layer_list *)util_common_calloc_s(sizeof(struct layer_list));
     ASSERT_NE(layer_list, nullptr);
 
+    ASSERT_EQ(layer_store_list(NULL), -1);
+    MOCK_SET_V(util_smart_calloc_s, util_smart_calloc_s_fail);
+    ASSERT_EQ(layer_store_list(layer_list), -1);
+    MOCK_CLEAR(util_smart_calloc_s);
+    MOCK_SET_V(util_common_calloc_s, util_common_calloc_s_fail);
+    ASSERT_EQ(layer_store_list(layer_list), -1);
+    MOCK_CLEAR(util_common_calloc_s);
     ASSERT_EQ(layer_store_list(layer_list), 0);
     ASSERT_EQ(layer_list->layers_len, 2);
 
@@ -315,6 +442,18 @@ TEST_F(StorageLayersUnitTest, test_layer_store_by_compress_digest)
     std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
     struct layer_list *layer_list = (struct layer_list *)util_common_calloc_s(sizeof(struct layer_list));
 
+    MOCK_SET_V(util_smart_calloc_s, util_smart_calloc_s_fail);
+    ASSERT_EQ(layer_store_by_compress_digest(compress.c_str(), layer_list), -1);
+    MOCK_CLEAR(util_smart_calloc_s);
+    MOCK_SET_V(util_common_calloc_s, util_common_calloc_s_fail);
+    ASSERT_EQ(layer_store_by_compress_digest(compress.c_str(), layer_list), -1);
+    MOCK_CLEAR(util_common_calloc_s);
+    MOCK_SET_V(map_search, map_search_fail);
+    g_map_search_count = 0;
+    g_map_search_match = 1;
+    ASSERT_EQ(layer_store_by_compress_digest(compress.c_str(), layer_list), -1);
+    MOCK_CLEAR(map_search);
+
     ASSERT_EQ(layer_store_by_compress_digest(compress.c_str(), layer_list), 0);
     ASSERT_EQ(layer_list->layers_len, 1);
 
@@ -324,3 +463,26 @@ TEST_F(StorageLayersUnitTest, test_layer_store_by_compress_digest)
 
     free_layer_list(layer_list);
 }
+
+#ifdef ENABLE_REMOTE_LAYER_STORE
+TEST_F(StorageLayersUnitTest, test_remote_layer_common)
+{
+    ASSERT_EQ(remote_layer_remove_memory_stores_with_lock(NULL), -1);
+    char arr[] = "random_id";
+    const char *random_id = arr;
+    MOCK_SET_V(map_search, map_search_fail);
+    g_map_search_count = 0;
+    g_map_search_match = 1;
+    ASSERT_EQ(remote_layer_remove_memory_stores_with_lock(random_id), 0);
+    MOCK_CLEAR(map_search);
+
+    ASSERT_EQ(remote_load_one_layer(NULL), -1);
+    MOCK_SET_V(map_search, map_search_fail);
+    g_map_search_count = 0;
+    g_map_search_match = 1;
+    ASSERT_EQ(remote_load_one_layer(random_id), -1);
+    MOCK_CLEAR(map_search);
+
+    ASSERT_EQ(remote_load_one_layer(random_id), -1);
+}
+#endif
-- 
2.23.0