Debian Codegraph @

storage/maria/libmarias3/tests/longlist.c

raw
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 * Copyright 2019 MariaDB Corporation Ab. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19
20 #include <yatl/lite.h>
21 #include <libmarias3/marias3.h>
22 #include <pthread.h>
23
24 /* Tests listing 1500 items
25 * This test creates 1500 items using 10 threads, lists them to check
26 * we can get them all due to the 1000 item pagination limit and then
27 * deletes them using 10 threads.
28 */
29
30 struct thread_info
31 {
32 pthread_t thread_id;
33 int thread_num;
34 int start_count;
35 char *s3bucket;
36 char *s3key;
37 char *s3secret;
38 char *s3region;
39 char *s3host;
40 char *s3port;
41 bool usehttp;
42 bool noverify;
43 };
44
45 const char *test_string = "Another one bites the dust";
46
47 static void *put_thread(void *arg)
48 {
49 int i;
50 struct thread_info *tinfo = arg;
51 ms3_st *ms3 = ms3_init(tinfo->s3key, tinfo->s3secret, tinfo->s3region,
52 tinfo->s3host);
53
54 if (tinfo->noverify)
55 {
56 ms3_set_option(ms3, MS3_OPT_DISABLE_SSL_VERIFY, NULL);
57 }
58
59 if (tinfo->usehttp)
60 {
61 ms3_set_option(ms3, MS3_OPT_USE_HTTP, NULL);
62 }
63
64 if (tinfo->s3port)
65 {
66 int port = atoi(tinfo->s3port);
67 ms3_set_option(ms3, MS3_OPT_PORT_NUMBER, &port);
68 }
69
70 for (i = tinfo->start_count; i < tinfo->start_count + 150; i++)
71 {
72 uint8_t res;
73 char fname[64];
74 snprintf(fname, 64, "listtest/list-%d.dat", i);
75 res = ms3_put(ms3, tinfo->s3bucket, fname, (const uint8_t *)test_string,
76 strlen(test_string));
77 ASSERT_EQ(res, 0);
78 }
79
80 ms3_deinit(ms3);
81
82 return NULL;
83 }
84
85 static void *delete_thread(void *arg)
86 {
87 int i;
88 struct thread_info *tinfo = arg;
89 ms3_st *ms3 = ms3_init(tinfo->s3key, tinfo->s3secret, tinfo->s3region,
90 tinfo->s3host);
91
92 if (tinfo->noverify)
93 {
94 ms3_set_option(ms3, MS3_OPT_DISABLE_SSL_VERIFY, NULL);
95 }
96
97 if (tinfo->usehttp)
98 {
99 ms3_set_option(ms3, MS3_OPT_USE_HTTP, NULL);
100 }
101
102 if (tinfo->s3port)
103 {
104 int port = atoi(tinfo->s3port);
105 ms3_set_option(ms3, MS3_OPT_PORT_NUMBER, &port);
106 }
107
108 for (i = tinfo->start_count; i < tinfo->start_count + 150; i++)
109 {
110 uint8_t res;
111 char fname[64];
112 snprintf(fname, 64, "listtest/list-%d.dat", i);
113 res = ms3_delete(ms3, tinfo->s3bucket, fname);
114 ASSERT_EQ(res, 0);
115 }
116
117 ms3_deinit(ms3);
118 return NULL;
119 }
120
121
122 int main(int argc, char *argv[])
123 {
124
125
126 int tnum;
127 char *s3key = getenv("S3KEY");
128 char *s3secret = getenv("S3SECRET");
129 char *s3region = getenv("S3REGION");
130 char *s3bucket = getenv("S3BUCKET");
131 char *s3host = getenv("S3HOST");
132 char *s3noverify = getenv("S3NOVERIFY");
133 char *s3usehttp = getenv("S3USEHTTP");
134 char *s3port = getenv("S3PORT");
135
136 bool noverify = false;
137 bool usehttp = false;
138 struct thread_info *tinfo;
139 int start_count;
140 uint8_t res;
141 uint8_t list_version;
142 pthread_attr_t attr;
143 ms3_st *ms3;
144 int res_count;
145 ms3_list_st *list = NULL, *list_it = NULL;
146
147 if (s3noverify && !strcmp(s3noverify, "1"))
148 {
149 noverify = true;
150 }
151
152 if (s3usehttp && !strcmp(s3usehttp, "1"))
153 {
154 usehttp = true;
155 }
156
157 SKIP_IF_(!s3key, "Environemnt variable S3KEY missing");
158 SKIP_IF_(!s3secret, "Environemnt variable S3SECRET missing");
159 SKIP_IF_(!s3region, "Environemnt variable S3REGION missing");
160 SKIP_IF_(!s3bucket, "Environemnt variable S3BUCKET missing");
161 (void) argc;
162 (void) argv;
163 ms3_library_init();
164
165 // ms3_debug(true);
166
167 tinfo = calloc(10, sizeof(struct thread_info));
168
169 start_count = 1000;
170
171 pthread_attr_init(&attr);
172
173 // Write 1500 files using 10 threads
174 printf("Writing 1500 items\n");
175
176 for (tnum = 0; tnum < 10; tnum++)
177 {
178 tinfo[tnum].thread_num = tnum + 1;
179 tinfo[tnum].start_count = start_count;
180 start_count += 150;
181 tinfo[tnum].s3key = s3key;
182 tinfo[tnum].s3secret = s3secret;
183 tinfo[tnum].s3region = s3region;
184 tinfo[tnum].s3host = s3host;
185 tinfo[tnum].s3bucket = s3bucket;
186 tinfo[tnum].s3port = s3port;
187 tinfo[tnum].noverify = noverify;
188 tinfo[tnum].usehttp = usehttp;
189 pthread_create(&tinfo[tnum].thread_id, &attr,
190 &put_thread, &tinfo[tnum]);
191 }
192
193 for (tnum = 0; tnum < 10; tnum++)
194 {
195 pthread_join(tinfo[tnum].thread_id, NULL);
196 }
197
198 free(tinfo);
199
200 ms3 = ms3_init(s3key, s3secret, s3region, s3host);
201
202 if (noverify)
203 {
204 ms3_set_option(ms3, MS3_OPT_DISABLE_SSL_VERIFY, NULL);
205 }
206
207 if (usehttp)
208 {
209 ms3_set_option(ms3, MS3_OPT_USE_HTTP, NULL);
210 }
211
212 if (s3port)
213 {
214 int port = atoi(s3port);
215 ms3_set_option(ms3, MS3_OPT_PORT_NUMBER, &port);
216 }
217
218 res = ms3_list(ms3, s3bucket, "listtest/", &list);
219 ASSERT_EQ(res, 0);
220 list_it = list;
221 res_count = 0;
222
223 while (list_it)
224 {
225 res_count++;
226 list_it = list_it->next;
227 }
228
229 printf("Found %d items\n", res_count);
230 ASSERT_EQ(res_count, 1500);
231
232 // Reattempt with list version 1
233 list_version = 1;
234 list = NULL;
235 ms3_set_option(ms3, MS3_OPT_FORCE_LIST_VERSION, &list_version);
236 res = ms3_list(ms3, s3bucket, "listtest/", &list);
237 ASSERT_EQ(res, 0);
238 list_it = list;
239 res_count = 0;
240
241 while (list_it)
242 {
243 res_count++;
244 list_it = list_it->next;
245 }
246
247 printf("V1 Found %d items\n", res_count);
248 ASSERT_EQ(res_count, 1500);
249
250 ms3_deinit(ms3);
251
252 tinfo = calloc(10, sizeof(struct thread_info));
253 start_count = 1000;
254
255 // Destroy 1500 files using 10 threads
256 printf("Deleting 1500 items");
257
258 for (tnum = 0; tnum < 10; tnum++)
259 {
260 tinfo[tnum].thread_num = tnum + 1;
261 tinfo[tnum].start_count = start_count;
262 start_count += 150;
263 tinfo[tnum].s3key = s3key;
264 tinfo[tnum].s3secret = s3secret;
265 tinfo[tnum].s3region = s3region;
266 tinfo[tnum].s3host = s3host;
267 tinfo[tnum].s3bucket = s3bucket;
268 tinfo[tnum].noverify = noverify;
269 tinfo[tnum].usehttp = usehttp;
270 tinfo[tnum].s3port = s3port;
271 pthread_create(&tinfo[tnum].thread_id, &attr,
272 &delete_thread, &tinfo[tnum]);
273 }
274
275 for (tnum = 0; tnum < 10; tnum++)
276 {
277 pthread_join(tinfo[tnum].thread_id, NULL);
278 }
279
280 free(tinfo);
281 ms3_library_deinit();
282 return 0;
283 }