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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
From 7321614a1097d4527f5968d1e3c7c1a06f6a9cd4 Mon Sep 17 00:00:00 2001
From: wujichao <wujichao1@huawei.com>
Date: Thu, 9 Jan 2025 16:15:25 +0800
Subject: [PATCH 178/198] add storage driver ut
---
.../graphdriver/overlay2/driver_overlay2.c | 18 +-
test/image/oci/storage/layers/CMakeLists.txt | 2 +
.../oci/storage/layers/storage_driver_ut.cc | 174 ++++++++++++++++--
3 files changed, 169 insertions(+), 25 deletions(-)
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c
index cc24909a..2b2e1eb3 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/overlay2/driver_overlay2.c
@@ -26,6 +26,8 @@
#include <stdio.h>
#include <strings.h>
+#include <isula_libutils/utils_macro.h>
+
#include "isula_libutils/log.h"
#ifdef ENABLE_USERNS_REMAP
#include "isulad_config.h"
@@ -828,7 +830,7 @@ out:
return ret;
}
-static int set_layer_quota(const char *dir, const json_map_string_string *opts, const struct graphdriver *driver)
+STATIC int set_layer_quota(const char *dir, const json_map_string_string *opts, const struct graphdriver *driver)
{
int ret = 0;
size_t i = 0;
@@ -875,7 +877,7 @@ out:
}
#ifdef ENABLE_REMOTE_LAYER_STORE
-static int do_create_remote_ro(const char *id, const char *parent, const struct graphdriver *driver,
+STATIC int do_create_remote_ro(const char *id, const char *parent, const struct graphdriver *driver,
const struct driver_create_opts *create_opts)
{
int ret = -1;
@@ -1021,7 +1023,7 @@ out:
return ret;
}
-static int append_default_quota_opts(struct driver_create_opts *ori_opts, uint64_t quota)
+STATIC int append_default_quota_opts(struct driver_create_opts *ori_opts, uint64_t quota)
{
int ret = 0;
int nret = 0;
@@ -1362,7 +1364,7 @@ out:
return ret;
}
-static char *get_mount_opt_data_with_custom_option(size_t cur_size, const char *cur_opts,
+STATIC char *get_mount_opt_data_with_custom_option(size_t cur_size, const char *cur_opts,
const struct driver_mount_opts *mount_opts)
{
int nret = 0;
@@ -1405,7 +1407,7 @@ out:
return mount_data;
}
-static char *get_mount_opt_data_with_driver_option(size_t cur_size, const char *cur_opts, const char *mount_opts)
+STATIC char *get_mount_opt_data_with_driver_option(size_t cur_size, const char *cur_opts, const char *mount_opts)
{
int nret = 0;
char *mount_data = NULL;
@@ -1523,7 +1525,7 @@ out:
return mount_data;
}
-static char *get_rel_mount_opt_data(const char *id, const char *rel_lower_dir, const struct graphdriver *driver,
+STATIC char *get_rel_mount_opt_data(const char *id, const char *rel_lower_dir, const struct graphdriver *driver,
const struct driver_mount_opts *mount_opts)
{
int nret = 0;
@@ -1663,7 +1665,7 @@ out:
return ret;
}
-static int rel_mount(const char *driver_home, const char *id, const char *mount_data)
+STATIC int rel_mount(const char *driver_home, const char *id, const char *mount_data)
{
int ret = 0;
char *mount_target = NULL;
@@ -2151,7 +2153,7 @@ out:
return ret;
}
-static int check_lower_valid(const char *driver_home, const char *lower)
+STATIC int check_lower_valid(const char *driver_home, const char *lower)
{
int ret = 0;
char *abs_path = NULL;
diff --git a/test/image/oci/storage/layers/CMakeLists.txt b/test/image/oci/storage/layers/CMakeLists.txt
index c4384e8f..eae67833 100644
--- a/test/image/oci/storage/layers/CMakeLists.txt
+++ b/test/image/oci/storage/layers/CMakeLists.txt
@@ -5,6 +5,8 @@ add_subdirectory(devmapper)
# storage_driver_ut
SET(DRIVER_EXE storage_driver_ut)
+add_definitions(-DUNIT_TEST=ON)
+
add_executable(${DRIVER_EXE}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/utils/cutils/utils.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/utils/cutils/utils_regex.c
diff --git a/test/image/oci/storage/layers/storage_driver_ut.cc b/test/image/oci/storage/layers/storage_driver_ut.cc
index 943fa073..79b29b71 100644
--- a/test/image/oci/storage/layers/storage_driver_ut.cc
+++ b/test/image/oci/storage/layers/storage_driver_ut.cc
@@ -40,6 +40,20 @@ using ::testing::Invoke;
using ::testing::_;
using ::testing::FLAGS_gmock_catch_leaked_mocks;
+extern "C" {
+ int set_layer_quota(const char *dir, const json_map_string_string *opts, const struct graphdriver *driver);
+ int do_create_remote_ro(const char *id, const char *parent, const struct graphdriver *driver,
+ const struct driver_create_opts *create_opts);
+ int append_default_quota_opts(struct driver_create_opts *ori_opts, uint64_t quota);
+ char *get_mount_opt_data_with_custom_option(size_t cur_size, const char *cur_opts,
+ const struct driver_mount_opts *mount_opts);
+ char *get_mount_opt_data_with_driver_option(size_t cur_size, const char *cur_opts, const char *mount_opts);
+ char *get_rel_mount_opt_data(const char *id, const char *rel_lower_dir, const struct graphdriver *driver,
+ const struct driver_mount_opts *mount_opts);
+ int rel_mount(const char *driver_home, const char *id, const char *mount_data);
+ int check_lower_valid(const char *driver_home, const char *lower);
+}
+
std::string GetDirectory()
{
char abs_path[PATH_MAX] { 0x00 };
@@ -121,7 +135,7 @@ protected:
void SetUp() override
{
MockDriverQuota_SetMock(&m_driver_quota_mock);
- std::string isulad_dir { "/tmp/isulad/" };
+ std::string isulad_dir = "/opt/isulad_storege_driver_ut/";
mkdir(isulad_dir.c_str(), 0755);
std::string root_dir = isulad_dir + "data";
std::string run_dir = isulad_dir + "data/run";
@@ -146,15 +160,15 @@ protected:
struct storage_module_init_options *opts = (struct storage_module_init_options *)util_common_calloc_s(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->storage_root = util_strdup_s(root_dir.c_str());
+ opts->storage_run_root = util_strdup_s(run_dir.c_str());
+ opts->driver_name = util_strdup_s("overlay");
opts->driver_opts = (char **)util_common_calloc_s(5 * 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[4] = strdup("overlay2.skip_mount_home=true");
+ opts->driver_opts[0] = util_strdup_s("overlay2.basesize=128M");
+ opts->driver_opts[1] = util_strdup_s("overlay2.override_kernel_check=true");
+ opts->driver_opts[2] = util_strdup_s("overlay2.skip_mount_home=false");
+ opts->driver_opts[3] = util_strdup_s("overlay2.mountopt=rw");
+ opts->driver_opts[4] = util_strdup_s("overlay2.skip_mount_home=true");
opts->driver_opts_len = 4;
EXPECT_CALL(m_driver_quota_mock, QuotaCtl(_, _, _, _)).WillRepeatedly(Invoke(invokeQuotaCtl));
@@ -173,7 +187,7 @@ protected:
if (support_overlay) {
ASSERT_EQ(graphdriver_cleanup(), 0);
}
- std::string rm_command = "rm -rf /tmp/isulad/";
+ std::string rm_command = "rm -rf /opt/isulad_storege_driver_ut/";
ASSERT_EQ(system(rm_command.c_str()), 0);
}
@@ -195,6 +209,134 @@ TEST_F(StorageDriverUnitTest, test_graphdriver_layer_exists)
ASSERT_FALSE(graphdriver_layer_exists(incorrectId.c_str()));
}
+TEST_F(StorageDriverUnitTest, test_set_layer_quota)
+{
+ if (!support_overlay) {
+ return;
+ }
+
+ struct driver_create_opts *create_opts = (struct driver_create_opts *)util_common_calloc_s(sizeof(struct driver_create_opts));
+ ASSERT_NE(create_opts, nullptr);
+ create_opts->storage_opt = (json_map_string_string *)util_common_calloc_s(sizeof(json_map_string_string));
+ ASSERT_NE(create_opts->storage_opt, nullptr);
+ create_opts->storage_opt->keys = (char **)util_common_calloc_s(sizeof(char *));
+ create_opts->storage_opt->values = (char **)util_common_calloc_s(sizeof(char *));
+ create_opts->storage_opt->keys[0] = util_strdup_s("size");
+ create_opts->storage_opt->values[0] = util_strdup_s("");
+ create_opts->storage_opt->len = 1;
+ ASSERT_EQ(set_layer_quota("/opt/isulad_storege_driver_ut/", create_opts->storage_opt, nullptr), -1);
+ create_opts->storage_opt->keys[0] = util_strdup_s("notsize");
+ ASSERT_EQ(set_layer_quota("/opt/isulad_storege_driver_ut/", create_opts->storage_opt, nullptr), -1);
+ free_driver_create_opts(create_opts);
+}
+
+#ifdef ENABLE_REMOTE_LAYER_STORE
+TEST_F(StorageDriverUnitTest, test_do_create_remote_ro)
+{
+ if (!support_overlay) {
+ return;
+ }
+
+ struct graphdriver *graph_driver = (struct graphdriver *)util_common_calloc_s(sizeof(struct graphdriver));
+ ASSERT_NE(graph_driver, nullptr);
+ graph_driver->home = nullptr;
+ ASSERT_EQ(do_create_remote_ro(nullptr, nullptr, graph_driver, nullptr), -1);
+ graph_driver->home = "driver_home";
+ ASSERT_EQ(do_create_remote_ro(nullptr, nullptr, graph_driver, nullptr), -1);
+ free(graph_driver);
+}
+#endif
+
+TEST_F(StorageDriverUnitTest, test_append_default_quota_opts)
+{
+ if (!support_overlay) {
+ return;
+ }
+
+ struct driver_create_opts *create_opts = (struct driver_create_opts *)util_common_calloc_s(sizeof(struct driver_create_opts));
+ ASSERT_NE(create_opts, nullptr);
+ create_opts->storage_opt = (json_map_string_string *)util_common_calloc_s(sizeof(json_map_string_string));
+ ASSERT_NE(create_opts->storage_opt, nullptr);
+ create_opts->storage_opt->keys = (char **)util_common_calloc_s(sizeof(char *));
+ create_opts->storage_opt->values = (char **)util_common_calloc_s(sizeof(char *));
+ create_opts->storage_opt->keys[0] = util_strdup_s("size");
+ create_opts->storage_opt->values[0] = util_strdup_s("128M");
+ create_opts->storage_opt->len = 1;
+ ASSERT_EQ(append_default_quota_opts(nullptr, 0), 0);
+ ASSERT_EQ(append_default_quota_opts(create_opts, 134217728), 0); // 134217728 = 128*1024*1024
+ free_driver_create_opts(create_opts);
+}
+
+TEST_F(StorageDriverUnitTest, test_get_mount_opt_data_with_custom_option)
+{
+ if (!support_overlay) {
+ return;
+ }
+
+ struct driver_mount_opts * mount_opts = (struct driver_mount_opts *)util_common_calloc_s(sizeof(struct driver_mount_opts));
+ ASSERT_NE(mount_opts, nullptr);
+ mount_opts->options = (char **)util_common_calloc_s(1 * sizeof(char *));
+ mount_opts->options[0] = util_strdup_s("ro");
+ mount_opts->options_len = 1;
+ size_t cur_size = 0;
+ const char *cur_opts = "cur_opts";
+ ASSERT_EQ(get_mount_opt_data_with_custom_option(cur_size, cur_opts, mount_opts), nullptr);
+ free_driver_mount_opts(mount_opts);
+}
+
+TEST_F(StorageDriverUnitTest, test_get_mount_opt_data_with_driver_option)
+{
+ if (!support_overlay) {
+ return;
+ }
+ struct driver_mount_opts * mount_opts = (struct driver_mount_opts *)util_common_calloc_s(sizeof(struct driver_mount_opts));
+ ASSERT_NE(mount_opts, nullptr);
+ mount_opts->options = (char **)util_common_calloc_s(1 * sizeof(char *));
+ mount_opts->options[0] = util_strdup_s("ro");
+ size_t cur_size = 0;
+ const char *cur_opts = "cur_opts";
+ ASSERT_EQ(get_mount_opt_data_with_driver_option(cur_size, cur_opts, mount_opts->options[0]), nullptr);
+ free_driver_mount_opts(mount_opts);
+}
+
+TEST_F(StorageDriverUnitTest, test_get_rel_mount_opt_data)
+{
+ if (!support_overlay) {
+ return;
+ }
+ std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
+ struct driver_mount_opts * mount_opts = (struct driver_mount_opts *)util_common_calloc_s(sizeof(struct driver_mount_opts));
+ ASSERT_NE(mount_opts, nullptr);
+ mount_opts->options = (char **)util_common_calloc_s(1 * sizeof(char *));
+ mount_opts->options[0] = util_strdup_s("ro");
+ mount_opts->options_len = 1;
+ const char *rel_lower_dir = "rel_lower_dir";
+ std::string res { "ro,lowerdir=rel_lower_dir,upperdir=9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/diff,workdir=9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/work" };
+ ASSERT_EQ(get_rel_mount_opt_data(id.c_str(), rel_lower_dir, nullptr, mount_opts), res);
+ free_driver_mount_opts(mount_opts);
+}
+
+TEST_F(StorageDriverUnitTest, test_rel_mount)
+{
+ if (!support_overlay) {
+ return;
+ }
+ const char *mount_data = "mount_data";
+ const char *driver_home = nullptr;
+ std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
+ ASSERT_EQ(rel_mount(driver_home, id.c_str(), mount_data),-1);
+}
+
+TEST_F(StorageDriverUnitTest, test_check_lower_valid)
+{
+ if (!support_overlay) {
+ return;
+ }
+ const char *lower = "lower";
+ const char *driver_home = nullptr;
+ ASSERT_EQ(check_lower_valid(driver_home, lower), -1);
+}
+
TEST_F(StorageDriverUnitTest, test_graphdriver_create_rw)
{
if (!support_overlay) {
@@ -202,17 +344,15 @@ TEST_F(StorageDriverUnitTest, test_graphdriver_create_rw)
}
std::string id { "eb29745b8228e1e97c01b1d5c2554a319c00a94d8dd5746a3904222ad65a13f8" };
- struct driver_create_opts *create_opts;
-
- create_opts = (struct driver_create_opts *)util_common_calloc_s(sizeof(struct driver_create_opts));
+ struct driver_create_opts *create_opts = (struct driver_create_opts *)util_common_calloc_s(sizeof(struct driver_create_opts));
ASSERT_NE(create_opts, nullptr);
create_opts->storage_opt = (json_map_string_string *)util_common_calloc_s(sizeof(json_map_string_string));
ASSERT_NE(create_opts->storage_opt, nullptr);
create_opts->storage_opt->keys = (char **)util_common_calloc_s(sizeof(char *));
create_opts->storage_opt->values = (char **)util_common_calloc_s(sizeof(char *));
- create_opts->storage_opt->keys[0] = strdup("size");
- create_opts->storage_opt->values[0] = strdup("128M");
+ create_opts->storage_opt->keys[0] = util_strdup_s("size");
+ create_opts->storage_opt->values[0] = util_strdup_s("128M");
create_opts->storage_opt->len = 1;
EXPECT_CALL(m_driver_quota_mock, IOCtl(_, _)).WillRepeatedly(Invoke(invokeIOCtl));
@@ -231,7 +371,7 @@ TEST_F(StorageDriverUnitTest, test_graphdriver_mount_layer)
}
std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
- std::string merged_dir = "/tmp/isulad/data/overlay/" + id + "/merged";
+ std::string merged_dir = "/opt/isulad_storege_driver_ut/data/overlay/" + id + "/merged";
struct driver_mount_opts *mount_opts = nullptr;
char* mount_dir = nullptr;
@@ -248,7 +388,7 @@ TEST_F(StorageDriverUnitTest, test_graphdriver_mount_layer)
mount_opts = (struct driver_mount_opts *)util_common_calloc_s(sizeof(struct driver_mount_opts));
ASSERT_NE(mount_opts, nullptr);
mount_opts->options = (char **)util_common_calloc_s(1 * sizeof(char *));
- mount_opts->options[0] = strdup("ro");
+ mount_opts->options[0] = util_strdup_s("ro");
mount_opts->options_len = 1;
FLAGS_gmock_catch_leaked_mocks = false;
--
2.34.1
|