Line data Source code
1 : /*
2 : test_release_html.c
3 : Created by Danny Goossen, Gioxa Ltd on 26/3/17.
4 :
5 : MIT License
6 :
7 : Copyright (c) 2017 deployctl, Gioxa Ltd.
8 :
9 : Permission is hereby granted, free of charge, to any person obtaining a copy
10 : of this software and associated documentation files (the "Software"), to deal
11 : in the Software without restriction, including without limitation the rights
12 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 : copies of the Software, and to permit persons to whom the Software is
14 : furnished to do so, subject to the following conditions:
15 :
16 : The above copyright notice and this permission notice shall be included in all
17 : copies or substantial portions of the Software.
18 :
19 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 : SOFTWARE.
26 :
27 : */
28 :
29 : #include <stdio.h>
30 : //LCOV_EXCL_START
31 : #include <check.h>
32 : //LCOV_EXCL_STOP
33 : #include "../src/deployd.h"
34 : #include "utils.h"
35 :
36 :
37 : /*
38 : {
39 : "name":"0.1.4",
40 : "message":"release 0.1.4",
41 : "commit":{
42 : "id":"13f1259c76ac5dd93ae4e9f7929174a47d6f6d9c",
43 : "message":"Update.gitlab-ci.yml",
44 : "parent_ids":["e8ceddaf1dcbda60be6908d88bd97f8e1be1b6cf"],
45 : "authored_date":"2017-02-28T18:56:43.000+07:00",
46 : "author_name":"Danny","author_email":"danny.goossen@gioxa.com",
47 : "committed_date":"2017-02-28T18:56:43.000+07:00",
48 : "committer_name":"Danny","committer_email":"danny.goossen@gioxa.com"
49 : },
50 : "release":{"tag_name":"0.1.4","description":"# New release\r\n\r\n\r\n"}
51 : }
52 : */
53 1 : cJSON * create_test_grelease(void)
54 : {
55 1 : cJSON * g=cJSON_CreateObject();
56 1 : cJSON_AddItemToObject(g,"name" , cJSON_CreateString("0.1.4"));
57 1 : cJSON_AddItemToObject(g,"message" , cJSON_CreateString("releasee 0.1.4"));
58 1 : cJSON * commit=cJSON_CreateObject();
59 1 : cJSON_AddItemToObject(commit,"id" , cJSON_CreateString("13f1259c76ac5dd93ae4e9f7929174a47d6f6d9c"));
60 1 : cJSON_AddItemToObject(commit,"message" , cJSON_CreateString(""));
61 1 : cJSON_AddItemToObject(commit,"parent_ids" , cJSON_CreateString("e8ceddaf1dcbda60be6908d88bd97f8e1be1b6cf"));
62 1 : cJSON_AddItemToObject(commit,"authored_date" , cJSON_CreateString("2017-02-28T18:56:43.000+07:00"));
63 1 : cJSON_AddItemToObject(commit,"author_name" , cJSON_CreateString("Danny"));
64 1 : cJSON_AddItemToObject(commit,"author_email" , cJSON_CreateString("danny.goossen@gioxa.com"));
65 :
66 1 : cJSON_AddItemToObject(commit,"committed_date" , cJSON_CreateString("2017-02-28T18:56:43.000+07:00"));
67 1 : cJSON_AddItemToObject(commit,"committer_name" , cJSON_CreateString(""));
68 1 : cJSON_AddItemToObject(commit,"committer_email" , cJSON_CreateString("danny.goossen@gioxa.com"));
69 1 : cJSON * release=cJSON_CreateObject();
70 1 : cJSON_AddItemToObject(release,"tag_name" , cJSON_CreateString("0.1.4"));
71 1 : cJSON_AddItemToObject(release,"description" , cJSON_CreateString("# New release\r\n\r\n\r\n"));
72 1 : cJSON_AddItemToObject(g,"release" , release);
73 1 : return g;
74 : }
75 :
76 : // check_ci_extract_server_version
77 :
78 1 : START_TEST(check_ci_extract_server_version)
79 : {
80 1 : debug("Start Test check_ci_extract_server_version\n");
81 : int ma,mi,rv;
82 1 : ck_assert_int_eq(0,ci_extract_server_version("10.0.0", &ma, &mi, &rv));
83 2 : ck_assert_int_eq(10,ma);
84 2 : ck_assert_int_eq(0,mi);
85 2 : ck_assert_int_eq(0,rv);
86 : }
87 1 : END_TEST
88 :
89 :
90 1 : START_TEST(check_slugit)
91 : {
92 :
93 1 : debug("Start Test slugit\n");
94 :
95 1 : char * out=NULL;
96 : // test normal
97 1 : out=slug_it("1%3*5",0);
98 1 : ck_assert_str_eq(out,"1-3-5");
99 1 : free(out);
100 : // test fix len input
101 1 : out=slug_it("1%3*5",3);
102 2 : ck_assert_str_eq(out,"1-3");
103 1 : free(out);
104 : //test consequtive karakters
105 1 : out=slug_it("%#2-4%%%%$#7+",0);
106 2 : ck_assert_str_eq(out,"2-4-7");
107 1 : free(out);
108 : // test all non valid
109 1 : out=slug_it("%$#$%^&*@!",0);
110 2 : ck_assert_ptr_eq(out,NULL);
111 : // test empty input
112 1 : out=slug_it(NULL,0);
113 2 : ck_assert_ptr_eq(out,0);
114 : }
115 1 : END_TEST
116 :
117 : //tcase_add_test(tc_core, check_upper_string);
118 1 : START_TEST(check_upper_string)
119 : {
120 1 : char test[]="lower";
121 1 : upper_string(test);
122 1 : ck_assert_str_eq(test, "LOWER");
123 1 : bzero(test, 5);
124 1 : upper_string(test);
125 2 : ck_assert_str_eq(test, "");
126 1 : upper_string(NULL);
127 : }
128 1 : END_TEST
129 :
130 1 : START_TEST(check_is_clean_tag)
131 : {
132 : // returns 1 on clean, 0 on not clean
133 : //clean
134 1 : ck_assert_int_eq(is_clean_tag(NULL,"ok", (const char *[]) {"BETA","TEST",NULL}),1);
135 :
136 : // not clean
137 2 : ck_assert_int_eq(is_clean_tag(NULL,"beta-1", (const char *[]) {"BETA","TEST",NULL}),0);
138 2 : ck_assert_int_eq(is_clean_tag(NULL,"1-BETA", (const char *[]) {"BETA","TEST",NULL}),0);
139 2 : ck_assert_int_eq(is_clean_tag(NULL,"testz3245", (const char *[]) {"BETA","TEST",NULL}),0);
140 2 : ck_assert_int_eq(is_clean_tag(NULL,"x2345TEST", (const char *[]) {"BETA","TEST",NULL}),0);
141 :
142 : // no list or list poits to NULL, clean
143 2 : ck_assert_int_eq(is_clean_tag(NULL,"x2345TEST", NULL),1);
144 2 : ck_assert_int_eq(is_clean_tag(NULL,"x2345TEST", (const char *[]) {NULL}),1);
145 :
146 : // empty tag is not clean
147 2 : ck_assert_int_eq(is_clean_tag(NULL,NULL, (const char *[]) {"BETA","TEST",NULL}),0);
148 : }
149 1 : END_TEST
150 :
151 1 : START_TEST(check_validate_project_path_slug)
152 : {
153 1 : struct trace_Struct * trace=NULL;
154 1 : init_dynamic_trace( &trace,"token","url",1);
155 :
156 : // OK situation
157 1 : cJSON *environment = cJSON_CreateObject();
158 : data_exchange_t data_exchange;
159 1 : data_exchange.env_json=environment;
160 1 : ck_assert_ptr_ne(environment,NULL);
161 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_PATH" , cJSON_CreateString("deployctl/test_deploy_release" ));
162 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_PATH_SLUG" , cJSON_CreateString("deployctl-test-deploy-release" ));
163 1 : int result=validate_project_path_slug(environment,trace,1);
164 2 : ck_assert_int_eq( result,0);
165 2 : ck_assert_int_eq(strlen(cJSON_get_key(trace->params, "trace")), 0);
166 1 : cJSON_Delete(environment);
167 :
168 : // wrong SLUG
169 :
170 1 : clear_dynamic_trace( trace);
171 1 : environment = cJSON_CreateObject();
172 1 : data_exchange.env_json=environment;
173 2 : ck_assert_ptr_ne(environment,NULL);
174 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_PATH" , cJSON_CreateString("deployctl/test_deploy_release" ));
175 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_PATH_SLUG" , cJSON_CreateString("deployctl_release" ));
176 1 : result=validate_project_path_slug(environment,trace,1);
177 2 : ck_assert_int_ne( result,0);
178 2 : ck_assert_int_gt(strlen(cJSON_get_key(trace->params, "trace")), 0);
179 1 : cJSON_Delete(environment);
180 :
181 : // results in empty slug
182 1 : clear_dynamic_trace( trace);
183 1 : environment = cJSON_CreateObject();
184 1 : data_exchange.env_json=environment;
185 2 : ck_assert_ptr_ne(environment,NULL);
186 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_PATH" , cJSON_CreateString("*******" ));
187 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_PATH_SLUG" , cJSON_CreateString("hhhhhhh" ));
188 1 : result=validate_project_path_slug(environment,trace,1);
189 2 : ck_assert_int_ne( result,0);
190 2 : ck_assert_int_gt(strlen(cJSON_get_key(trace->params, "trace")), 0);
191 1 : cJSON_Delete(environment);
192 :
193 : // empty projectpath to simulate problems
194 1 : clear_dynamic_trace( trace);
195 1 : environment = cJSON_CreateObject();
196 1 : data_exchange.env_json=environment;
197 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_PATH_SLUG" , cJSON_CreateString("hallo" ));
198 1 : result=validate_project_path_slug(environment,trace,1);
199 2 : ck_assert_int_ne( result,0);
200 2 : ck_assert_int_gt(strlen(cJSON_get_key(trace->params, "trace")), 0);
201 1 : cJSON_Delete(environment);
202 :
203 : // empty projectpathslug to simulate problems
204 1 : clear_dynamic_trace( trace);
205 1 : environment = cJSON_CreateObject();
206 1 : data_exchange.env_json=environment;
207 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_PATH" , cJSON_CreateString("hallo" ));
208 1 : result=validate_project_path_slug(environment,trace,1);
209 2 : ck_assert_int_ne( result,0);
210 2 : ck_assert_int_gt(strlen(cJSON_get_key(trace->params, "trace")), 0);
211 1 : cJSON_Delete(environment);
212 1 : free_dynamic_trace( &trace);
213 : }
214 1 : END_TEST
215 :
216 : // TODO change !!
217 1 : START_TEST(check_read_release)
218 : {
219 : char basePATH[1024];
220 1 : sprintf(basePATH,"./test_check_read_release");
221 :
222 :
223 : //clean
224 1 : _rmdir(basePATH,"");
225 1 : _mkdir("public",basePATH);
226 :
227 1 : cJSON * g=cJSON_CreateObject();
228 1 : cJSON_AddItemToObject(g,"name" , cJSON_CreateString("0.1.4"));
229 1 : char * release_print=cJSON_Print(g);
230 1 : debug("going to write release.json\n");
231 1 : write_test_file(basePATH,"/public", "release.json", release_print);
232 1 : free(release_print);
233 1 : cJSON_Delete(g);
234 :
235 : char testPATH[1024];
236 : cJSON * release;
237 : // ok, normal
238 1 : sprintf(testPATH,"%s/public/release.json",basePATH);
239 1 : release=read_release_json(NULL,testPATH);
240 1 : ck_assert_ptr_ne(release,NULL);
241 1 : cJSON_Delete(release);
242 :
243 : // file not exists
244 1 : sprintf(testPATH,"%s/public/notexist.json",basePATH);
245 1 : release=read_release_json(NULL,testPATH);
246 2 : ck_assert_ptr_eq(release,NULL);
247 :
248 : // not a jason file
249 1 : write_test_file(basePATH,"/public", "release.json","# hallo I'm not a json file");
250 1 : sprintf(testPATH,"%s/public/release.json",basePATH);
251 1 : release=read_release_json(NULL,testPATH);
252 2 : ck_assert_ptr_eq(release,NULL);
253 1 : _rmdir(basePATH,"");
254 : }
255 1 : END_TEST
256 :
257 :
258 1 : START_TEST(check_create_this_release_json)
259 : {
260 : // all releases file
261 : // todo check files and links !!!!
262 1 : const char * projectdir="create_this_release_json_project_dir";
263 :
264 1 : _rmdir(projectdir,"");
265 1 : _mkdir(projectdir,"");
266 : //setdebug();
267 :
268 1 : debug("START TEST create_release\n");
269 : // setdebug();
270 : char filesPATH[1024];
271 : char basePATH[1024];
272 : char full_files_path[1024];
273 1 : sprintf(filesPATH,"/1-1-1/files");
274 1 : sprintf(basePATH,"test_check_create_this_release_json");
275 1 : sprintf(full_files_path,"/public%s",filesPATH);
276 :
277 : //clean
278 1 : _rmdir(basePATH,"");
279 1 : _mkdir(basePATH, full_files_path);
280 1 : write_test_file(basePATH, full_files_path, "file1.txt", "fileone");
281 : // this release info for gitlab api
282 1 : cJSON * grelease=create_test_grelease();
283 1 : ck_assert_ptr_ne(grelease,NULL);
284 :
285 1 : cJSON * environment = cJSON_CreateObject();
286 : data_exchange_t data_exchange;
287 1 : data_exchange.env_json=environment;
288 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_PATH" , cJSON_CreateString("test/test" ));
289 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_PATH_SLUG" , cJSON_CreateString("test-test"));
290 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_URL" , cJSON_CreateString("https://test.com"));
291 1 : cJSON_AddItemToObject(environment,"CI_COMMIT_REF_NAME" , cJSON_CreateString("0.1.0"));
292 1 : cJSON_AddItemToObject(environment,"CI_ENVIRONMENT_NAME" , cJSON_CreateString("production"));
293 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_NAME" , cJSON_CreateString("test"));
294 1 : cJSON_AddItemToObject(environment,"CI_COMMIT_SHA" , cJSON_CreateString("abcdabcd"));
295 1 : cJSON_AddItemToObject(environment,"GITLAB_USER_EMAIL" , cJSON_CreateString("test@test.com"));
296 1 : cJSON_AddItemToObject(environment,"GITLAB_USER_ID" , cJSON_CreateString("1"));
297 1 : cJSON_AddItemToObject(environment,"CI_COMMIT_TAG" , cJSON_CreateString("0.1.0"));
298 1 : cJSON_AddItemToObject(environment,"CI_PIPELINE_ID" , cJSON_CreateString("1"));
299 1 : cJSON_AddItemToObject(environment,"CI_JOB_ID" , cJSON_CreateString("1"));
300 :
301 :
302 1 : cJSON * therelease= NULL;
303 1 : cJSON * release_tag=NULL;
304 1 : therelease=create_thisrelease_json( &data_exchange, grelease, 1, basePATH,filesPATH);
305 :
306 2 : ck_assert_ptr_ne( therelease, NULL);
307 1 : cJSON_Delete(therelease);
308 :
309 2 : ck_assert_ptr_ne(grelease,NULL);
310 2 : ck_assert_ptr_ne(environment,NULL);
311 :
312 :
313 : //missing release message
314 1 : char * grelease_str=cJSON_PrintBuffered(grelease, 1, 1);
315 1 : debug("original\n---------\n%s\n",grelease_str);
316 1 : free(grelease_str);
317 :
318 1 : cJSON_DeleteItemFromObject(grelease,"message");
319 :
320 1 : grelease_str=cJSON_PrintBuffered(grelease, 1, 1);
321 1 : debug("after delete message\n---------\n%s\n",grelease_str);
322 1 : free(grelease_str);
323 :
324 1 : therelease=create_thisrelease_json( &data_exchange, grelease, 1, basePATH,filesPATH);
325 2 : ck_assert_ptr_ne(therelease,NULL);
326 1 : cJSON_Delete(therelease);
327 :
328 : // missing release description
329 1 : release_tag =cJSON_GetObjectItem(grelease,"release");
330 1 : cJSON_DeleteItemFromObject(release_tag,"description");
331 1 : therelease=create_thisrelease_json( &data_exchange, grelease, 1, basePATH,filesPATH);
332 2 : ck_assert_ptr_ne(therelease,NULL);
333 1 : cJSON_Delete(therelease);
334 :
335 : // missing release
336 1 : cJSON_DeleteItemFromObject(grelease,"release");
337 1 : therelease=create_thisrelease_json( &data_exchange, grelease, 1, basePATH,filesPATH);
338 2 : ck_assert_ptr_ne(therelease,NULL);
339 1 : cJSON_Delete(therelease);
340 :
341 : // not a production release
342 1 : therelease=create_thisrelease_json( &data_exchange, grelease, 1, basePATH,filesPATH);
343 2 : ck_assert_ptr_ne(therelease,NULL);
344 1 : cJSON_Delete(therelease);
345 :
346 : // todo check above !!
347 :
348 : // not a production release and no environment set
349 1 : cJSON_DeleteItemFromObject(environment,"CI_ENVIRONMENT_NAME");
350 1 : therelease=create_thisrelease_json( &data_exchange, grelease, 1, basePATH,filesPATH);
351 2 : ck_assert_ptr_ne(therelease,NULL);
352 1 : cJSON_Delete(therelease);
353 :
354 : // no commit tag
355 1 : cJSON_DeleteItemFromObject(environment,"CI_COMMIT_TAG");
356 1 : therelease=create_thisrelease_json( &data_exchange, grelease, 1, basePATH,filesPATH);
357 2 : ck_assert_ptr_ne(therelease,NULL);
358 1 : cJSON_Delete(therelease);
359 :
360 :
361 : // now add GIT_VERSION, same as first, should return w #
362 1 : cJSON_AddItemToObject(environment,"CI_GIT_VERSION" , cJSON_CreateString("abcdabcd"));
363 1 : therelease=create_thisrelease_json( &data_exchange, grelease, 1, basePATH,filesPATH);
364 2 : ck_assert_ptr_ne(therelease,NULL);
365 :
366 1 : debug("\nJason Release \n>%s<\n",cJSON_Print(therelease));
367 2 : ck_assert_str_eq(cJSON_get_key(cJSON_GetObjectItem(therelease, "tag"), "name"),"#abcdabcd" );
368 1 : cJSON_Delete(therelease);
369 :
370 : // now add GIT_VERSION, not same as first, should return without #
371 1 : cJSON_ReplaceItemInObject(environment,"CI_GIT_VERSION" , cJSON_CreateString("not_same"));
372 1 : therelease=create_thisrelease_json( &data_exchange, grelease, 1, basePATH,filesPATH);
373 2 : ck_assert_ptr_ne(therelease,NULL);
374 :
375 1 : debug("\nJason Release \n>%s<\n",cJSON_Print(therelease));
376 2 : ck_assert_str_eq(cJSON_get_key(cJSON_GetObjectItem(therelease, "tag"), "name"),"not_same" );
377 1 : cJSON_Delete(therelease);
378 :
379 :
380 : // now add project path and a release file
381 1 : cJSON_DeleteItemFromObject(environment,"CI_GIT_VERSION" );
382 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_DIR" , cJSON_CreateString(projectdir));
383 :
384 1 : write_test_file(projectdir,"",".GIT_VERSION" ,"12345678");
385 :
386 1 : therelease=create_thisrelease_json( &data_exchange, grelease, 1, basePATH,filesPATH);
387 2 : ck_assert_ptr_ne(therelease,NULL);
388 :
389 1 : debug("\nJason Release \n>%s<\n",cJSON_Print(therelease));
390 2 : ck_assert_str_eq(cJSON_get_key(cJSON_GetObjectItem(therelease, "tag"), "name"),"12345678" );
391 1 : cJSON_Delete(therelease);
392 :
393 1 : write_test_file(projectdir,"",".GIT_VERSION" ,"abcdabcd");
394 :
395 1 : therelease=create_thisrelease_json( &data_exchange, grelease, 1, basePATH,filesPATH);
396 2 : ck_assert_ptr_ne(therelease,NULL);
397 :
398 1 : debug("\nJason Release \n>%s<\n",cJSON_Print(therelease));
399 2 : ck_assert_str_eq(cJSON_get_key(cJSON_GetObjectItem(therelease, "tag"), "name"),"#abcdabcd" );
400 1 : cJSON_Delete(therelease);
401 :
402 1 : cJSON_Delete(grelease);
403 1 : cJSON_Delete(environment);
404 1 : _rmdir(projectdir,"");
405 1 : _rmdir(basePATH,"");
406 : }
407 1 : END_TEST
408 :
409 1 : START_TEST(check_html_body_release)
410 : {
411 1 : char *html= html_body_release("# h1\nhallo\n");
412 1 : ck_assert_ptr_ne(html,NULL);
413 2 : if (html) free(html);
414 : }
415 1 : END_TEST
416 :
417 1 : START_TEST(check_JSON_dir_list)
418 : {
419 : char filesPATH[1024];
420 : char basePATH[1024];
421 : char linkPATH[1024];
422 1 : sprintf(filesPATH,"/1-1-1/files");
423 1 : sprintf(basePATH,"./test_check_JSON_dir_list");
424 1 : _rmdir(basePATH,"");
425 : char full_filesPATH[1024];
426 1 : sprintf(full_filesPATH,"/public%s",filesPATH);
427 1 : _mkdir(full_filesPATH,basePATH);
428 :
429 1 : write_test_file(basePATH, full_filesPATH, "file1.txt", "file1");
430 1 : write_test_file(basePATH, full_filesPATH, "link1.url", "/link123\t 53%");
431 1 : cJSON * dirlist2=NULL;
432 1 : dirlist2 = JSON_dir_list(basePATH,filesPATH );
433 1 : ck_assert_ptr_ne(dirlist2,NULL);
434 1 : debug("dirlist2 Pointer %p\n",dirlist2);
435 1 : cJSON_IsArray(dirlist2);
436 2 : ck_assert_int_ne(cJSON_IsArray(dirlist2),0);
437 2 : ck_assert_int_eq( cJSON_GetArraySize(dirlist2),1);
438 :
439 1 : cJSON * item=cJSON_GetArrayItem(dirlist2,0);
440 2 : ck_assert_ptr_ne(item,NULL);
441 2 : ck_assert_str_eq(cJSON_get_key(item,"name"),"file1.txt");
442 1 : sprintf(linkPATH,"%s/%s",filesPATH,cJSON_get_key(item,"name"));
443 2 : ck_assert_str_eq(cJSON_get_key(item,"link"),linkPATH);
444 1 : cJSON * size_obj= cJSON_GetObjectItem(item, "size");
445 2 : ck_assert_ptr_ne(size_obj, NULL);
446 2 : ck_assert_int_eq ((int)size_obj->valuedouble,5);
447 : // delete results
448 1 : cJSON_Delete(dirlist2);
449 :
450 : // error conditions: non existing dirlist
451 1 : dirlist2=JSON_dir_list("/mynonexistingdir","" );
452 2 : ck_assert_ptr_eq(dirlist2,NULL);
453 :
454 : // error NULL parameters
455 1 : dirlist2=JSON_dir_list(NULL,NULL);
456 2 : ck_assert_ptr_eq(dirlist2,NULL);
457 1 : _rmdir(basePATH,"");
458 : }
459 1 : END_TEST
460 :
461 1 : START_TEST(check_JSON_link_list)
462 : {
463 1 : cJSON * dirlist2=NULL;
464 : char filesPATH[1024];
465 : char basePATH[1024];
466 1 : sprintf(filesPATH,"/1-1-1/files");
467 1 : sprintf(basePATH,"./test_check_JSON_link_list");
468 1 : _rmdir(basePATH,"");
469 : char full_filesPATH[1024];
470 1 : sprintf(full_filesPATH,"/public%s",filesPATH);
471 1 : _mkdir(full_filesPATH,basePATH);
472 :
473 1 : const char * data[]={"link123,53%","http://link123","https://link123","/link123\n",NULL};
474 1 : const char * links[]={"/1-1-1/files/link123","http://link123","https://link123","/1-1-1/files/link123",NULL};
475 1 : const char * metrics[]={"53%","","","",NULL};
476 :
477 1 : write_test_file(basePATH, full_filesPATH, "file1.txt","nothing");
478 1 : write_test_file(basePATH, full_filesPATH, "link_0.url", data[0]);
479 1 : write_test_file(basePATH, full_filesPATH, "link_1.url", data[1]);
480 1 : write_test_file(basePATH, full_filesPATH, "link_2.url", data[2]);
481 1 : write_test_file(basePATH, full_filesPATH, "link_3.url", data[3]);
482 :
483 : // Do the work
484 1 : dirlist2 = JSON_link_list(basePATH,filesPATH );
485 :
486 1 : ck_assert_ptr_ne(dirlist2,NULL);
487 1 : debug("dirlist2 Pointer %p\n",dirlist2);
488 2 : ck_assert_int_ne(cJSON_IsArray(dirlist2),0);
489 2 : ck_assert_int_eq( cJSON_GetArraySize(dirlist2),4);
490 :
491 1 : fprintf(stderr,"start json dir list test\n");
492 : // loop results
493 1 : cJSON * pos=NULL;
494 1 : const char * name=NULL;
495 1 : const char * link=NULL;
496 1 : const char * metric=NULL;
497 1 : int link_no=0;
498 6 : cJSON_ArrayForEach(pos,dirlist2)
499 : {
500 4 : name = cJSON_get_key(pos,"name");
501 4 : metric=cJSON_get_key(pos,"metric");
502 4 : link= cJSON_get_key(pos,"link");
503 4 : ck_assert_ptr_nonnull(name);
504 8 : ck_assert_ptr_nonnull(link);
505 8 : ck_assert_int_eq(strlen(name), 6);
506 4 : fprintf(stderr,"name: >%s<\n",name);
507 4 : link_no=name[5]-'0';
508 4 : fprintf(stderr,"Links:%d, %s >%s< == >%s< , metric %s\n",link_no,name,link,links[link_no], metric);
509 8 : ck_assert_str_eq(link,links[link_no]);
510 :
511 8 : if (metrics[link_no]!=NULL && strlen(metrics[link_no])>0)
512 1 : ck_assert_str_eq(metric,metrics[link_no]);
513 : else
514 3 : ck_assert_ptr_eq(metric,NULL);
515 : }
516 :
517 : // delete results
518 1 : cJSON_Delete(dirlist2);
519 :
520 :
521 : // error conditions: non existing dirlist
522 1 : dirlist2=JSON_dir_list("/mynonexistingdir","" );
523 1 : ck_assert_ptr_eq(dirlist2,NULL);
524 :
525 : // error NULL parameters
526 1 : dirlist2=JSON_dir_list(NULL,NULL);
527 2 : ck_assert_ptr_eq(dirlist2,NULL);
528 1 : _rmdir(basePATH,"");
529 : }
530 1 : END_TEST
531 :
532 :
533 :
534 : //tcase_add_test(tc_core, check_check_if_git_version_file);
535 1 : START_TEST( check_check_if_git_version_file)
536 : {
537 1 : char * test=NULL;
538 1 : const char * projectdir="./test_check_git_file_project_dir";
539 1 : const char * filename=".GIT_VERSION";
540 :
541 1 : _rmdir(projectdir,"");
542 : // dir does not exist, should return NULL
543 1 : test= check_if_git_version_file(projectdir);
544 1 : ck_assert_ptr_eq(test, NULL);
545 :
546 : // have dir but no file, should return 0
547 1 : _mkdir(projectdir,"");
548 1 : test= check_if_git_version_file(projectdir);
549 2 : ck_assert_ptr_eq(test, NULL);
550 :
551 : // a good value is length > 2 and < 30
552 2 : ck_assert_int_eq(write_test_file(projectdir,"",filename,"agoodvalue"),strlen("agoodvalue"));
553 1 : debug("check if value\n");
554 1 : test= check_if_git_version_file(projectdir);
555 2 : ck_assert_ptr_ne(test, NULL);
556 2 : ck_assert_str_eq(test, "agoodvalue");
557 1 : free(test);
558 1 : test=NULL;
559 :
560 : // should not return have \n or \a
561 2 : ck_assert_int_eq(write_test_file(projectdir,"",filename,"agoodvalue\n"),strlen("agoodvalue\n"));
562 1 : test= check_if_git_version_file(projectdir);
563 2 : ck_assert_str_eq(test, "agoodvalue");
564 1 : free(test);
565 1 : test=NULL;
566 :
567 2 : ck_assert_int_eq(write_test_file(projectdir,"",filename,"agoodvalue\r"),strlen("agoodvalue\r"));
568 1 : test= check_if_git_version_file(projectdir);
569 2 : ck_assert_str_eq(test, "agoodvalue");
570 1 : free(test);
571 1 : test=NULL;
572 :
573 : // a too short value should return NULL
574 2 : ck_assert_int_eq(write_test_file(projectdir,"",filename,"1"),strlen("1"));
575 1 : test= check_if_git_version_file(projectdir);
576 2 : ck_assert_ptr_eq(test, NULL);
577 :
578 2 : ck_assert_int_eq(write_test_file(projectdir,"",filename,"awaytooooooooooooooooooooooloooooooonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnngvalueshoulreturnNULL"), \
579 : strlen("awaytooooooooooooooooooooooloooooooonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnngvalueshoulreturnNULL"));
580 1 : test= check_if_git_version_file(projectdir);
581 2 : ck_assert_ptr_eq(test, NULL);
582 :
583 :
584 1 : _rmdir(projectdir,"");
585 : // strip \n
586 :
587 : }
588 1 : END_TEST
589 :
590 : //tcase_add_test(tc_core, check_check_last_tag);
591 1 : START_TEST( check_check_last_tag)
592 : {
593 :
594 1 : const char * basePATH="test_check_last_tag";
595 : // clean start
596 1 : _rmdir(basePATH,"");
597 :
598 1 : char * test=NULL;
599 :
600 : // no correct dir should return NULL
601 1 : test=check_last_tag(NULL, basePATH);
602 1 : ck_assert_ptr_eq(test, NULL);
603 :
604 1 : _mkdir( "public",basePATH);
605 : // have dir, no file should return NULL
606 1 : test=check_last_tag(NULL, basePATH);
607 2 : ck_assert_ptr_eq(test, NULL);
608 :
609 : // a good value is length > 2 and < 30
610 2 : ck_assert_int_eq(write_test_file(basePATH, "/public","latest.tag","agoodvalue"),strlen("agoodvalue"));
611 1 : debug("check if value\n");
612 1 : test= check_last_tag(NULL, basePATH);
613 2 : ck_assert_ptr_ne(test, NULL);
614 2 : ck_assert_str_eq(test, "agoodvalue");
615 1 : free(test);
616 1 : test=NULL;
617 :
618 : // result should not return \n
619 2 : ck_assert_int_eq(write_test_file(basePATH, "/public","latest.tag","agoodvalue\n"),strlen("agoodvalue\n"));
620 1 : test= check_last_tag(NULL, basePATH);
621 2 : ck_assert_ptr_ne(test, NULL);
622 2 : ck_assert_str_eq(test, "agoodvalue");
623 1 : free(test);
624 1 : test=NULL;
625 :
626 :
627 : // result should not return have \r
628 2 : ck_assert_int_eq(write_test_file(basePATH, "/public","latest.tag","agoodvalue\r"),strlen("agoodvalue\r"));
629 1 : test= check_last_tag(NULL, basePATH);
630 2 : ck_assert_ptr_ne(test, NULL);
631 2 : ck_assert_str_eq(test, "agoodvalue");
632 1 : free(test);
633 1 : test=NULL;
634 :
635 :
636 : // too short value should return NULL
637 2 : ck_assert_int_eq(write_test_file(basePATH, "/public","latest.tag","1"),strlen("1"));
638 1 : test= check_last_tag(NULL, basePATH);
639 2 : ck_assert_ptr_eq(test, NULL);
640 :
641 : // toooo long value should return NULL
642 2 : ck_assert_int_eq(write_test_file(basePATH, "/public","latest.tag","awaytooooooooooooooooooooooloooooooonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnngvalueshoulreturnNULL"), \
643 : strlen("awaytooooooooooooooooooooooloooooooonnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnngvalueshoulreturnNULL"));
644 1 : test= check_last_tag(NULL, basePATH);
645 2 : ck_assert_ptr_eq(test, NULL);
646 :
647 1 : _rmdir(basePATH,"");
648 :
649 : }
650 1 : END_TEST
651 :
652 : // merges existing rleases with release, remove dup
653 1 : START_TEST(check_create_releases_json)
654 : {
655 :
656 1 : debug("check_create_releases_json\n");
657 1 : cJSON * environment = cJSON_CreateObject();
658 :
659 : data_exchange_t data_exchange;
660 1 : data_exchange.env_json=environment;
661 1 : cJSON_AddItemToObject(environment,"CI_COMMIT_REF_NAME" , cJSON_CreateString("0.1.0"));
662 :
663 : // minimum requirement: { "tag" : { "name": "0.0.1"}}
664 1 : cJSON * this_release = cJSON_CreateObject();
665 1 : cJSON * tag = cJSON_CreateObject();
666 1 : cJSON_AddItemToObject(tag,"name" , cJSON_CreateString("0.1.0"));
667 1 : cJSON_AddItemToObject(this_release,"tag" , tag);
668 :
669 : //so start empty
670 1 : char * test=NULL;
671 1 : cJSON * releases=NULL;
672 1 : debug("do the crea releases_json\n");
673 1 : test=create_releases_json(&data_exchange, this_release, &releases, 1);
674 1 : ck_assert_ptr_ne(test, NULL);
675 2 : ck_assert_ptr_ne(releases, NULL);
676 1 : debug("\nresutl\n>%s<\n",test);
677 2 : if (test) free(test);
678 :
679 : // try to make dup
680 1 : test=create_releases_json(&data_exchange, this_release, &releases, 1);
681 1 : ck_assert_ptr_ne(test, NULL);
682 2 : ck_assert_ptr_ne(releases, NULL);
683 : // size of array should be 1, no dups!!!!
684 1 : cJSON * arr=cJSON_GetObjectItem(releases, "releases");
685 2 : ck_assert_int_eq(1, cJSON_GetArraySize(arr));
686 1 : debug("\nresutl\n>%s<\n",test);
687 2 : if (test) free(test);
688 1 : test=NULL;
689 :
690 1 : cJSON_DeleteItemFromObject(environment,"CI_COMMIT_REF_NAME" );
691 :
692 : // no commit and here, delete releases
693 :
694 1 : test=create_releases_json(&data_exchange, this_release, &releases, 1);
695 1 : ck_assert_ptr_ne(test, NULL);
696 2 : ck_assert_ptr_ne(releases, NULL);
697 :
698 1 : cJSON_Delete(releases);
699 1 : cJSON_Delete(this_release);
700 1 : cJSON_Delete(environment);
701 : }
702 1 : END_TEST
703 :
704 : //tcase_add_test(tc_core, check_create_release_json_js);
705 1 : START_TEST( check_create_release_json_js)
706 : {
707 1 : char *test=NULL;
708 :
709 1 : debug("\nSTART TEST create_release_json_js\n");
710 :
711 1 : cJSON * environment = cJSON_CreateObject();
712 :
713 : data_exchange_t data_exchange;
714 1 : data_exchange.env_json=environment;
715 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_PATH" , cJSON_CreateString("test/test" ));
716 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_PATH_SLUG" , cJSON_CreateString("test-test"));
717 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_URL" , cJSON_CreateString("https://test.com"));
718 1 : cJSON_AddItemToObject(environment,"CI_COMMIT_REF_NAME" , cJSON_CreateString("0.1.0"));
719 1 : cJSON_AddItemToObject(environment,"CI_ENVIRONMENT_NAME" , cJSON_CreateString("production"));
720 1 : cJSON_AddItemToObject(environment,"CI_PROJECT_NAME" , cJSON_CreateString("test"));
721 1 : cJSON_AddItemToObject(environment,"CI_COMMIT_SHA" , cJSON_CreateString("abcdabcd"));
722 1 : cJSON_AddItemToObject(environment,"GITLAB_USER_EMAIL" , cJSON_CreateString("test@test.com"));
723 1 : cJSON_AddItemToObject(environment,"GITLAB_USER_ID" , cJSON_CreateString("1"));
724 1 : cJSON_AddItemToObject(environment,"CI_COMMIT_TAG" , cJSON_CreateString("0.1.0"));
725 1 : cJSON_AddItemToObject(environment,"CI_PIPELINE_ID" , cJSON_CreateString("1"));
726 1 : cJSON_AddItemToObject(environment,"CI_JOB_ID" , cJSON_CreateString("1"));
727 :
728 : char test1[1024];
729 1 : sprintf(test1, "var target = \"Releases\";\nvar deployctl_version = \"%s\";\n" \
730 : "var project_info = {\"name\" : \"test\",\n\"path\" : \"test/test\",\n\"link\" : \"https://test.com\"};\n",PACKAGE_VERSION);
731 1 : test=create_release_json_js(&data_exchange, NULL, NULL, 0, NULL);
732 1 : ck_assert_ptr_ne( test, NULL);
733 2 : ck_assert_str_eq(test, test1);
734 1 : debug("\nall NULL\n>%s<\n",test);
735 1 : free(test);
736 :
737 1 : sprintf(test1, "var target = \"Releases\";\nvar download_url = \"Download\";\nvar deployctl_version = \"%s\";\n" \
738 : "var project_info = {\"name\" : \"test\",\n\"path\" : \"test/test\",\n\"link\" : \"https://test.com\"};\n",PACKAGE_VERSION);
739 1 : test=create_release_json_js(&data_exchange, "Download", NULL, 0, NULL);
740 2 : ck_assert_ptr_ne( test, NULL);
741 1 : debug("\nset download\n>%s<\n",test);
742 2 : ck_assert_str_eq(test, test1);
743 1 : free(test);
744 :
745 : // no tag means it's releases, so need last tag if present
746 1 : test=create_release_json_js(&data_exchange, NULL, NULL, 0, "0.1.0");
747 2 : ck_assert_ptr_ne( test, NULL);
748 1 : debug("\nset tag, no tag\n>%s<\n",test);
749 1 : sprintf(test1, "var target = \"Releases\";\nvar last_tag = \"0.1.0\";\nvar deployctl_version = \"%s\";\n" \
750 : "var project_info = {\"name\" : \"test\",\n\"path\" : \"test/test\",\n\"link\" : \"https://test.com\"};\n",PACKAGE_VERSION);
751 2 : ck_assert_str_eq(test, test1);
752 :
753 1 : free(test);
754 :
755 : // tag => should not display last_tag
756 1 : test=create_release_json_js(&data_exchange, NULL, NULL, 1, "0.1.0");
757 2 : ck_assert_ptr_ne( test, NULL);
758 1 : debug("\nset tag and tag\n>%s<\n",test);
759 1 : sprintf(test1, "var target = \"Release\";\nvar deployctl_version = \"%s\";\n" \
760 : "var project_info = {\"name\" : \"test\",\n\"path\" : \"test/test\",\n\"link\" : \"https://test.com\"};\n",PACKAGE_VERSION);
761 2 : ck_assert_str_eq(test, test1);
762 :
763 1 : free(test);
764 :
765 1 : test=create_release_json_js(&data_exchange, NULL, "Release", 1, NULL);
766 2 : ck_assert_ptr_ne( test, NULL);
767 1 : debug("\nset release\n>%s<\n",test);
768 1 : sprintf(test1, "var target = \"Release\";\nvar deployctl_version = \"%s\";\n" \
769 : "var project_info = {\"name\" : \"test\",\n\"path\" : \"test/test\",\n\"link\" : \"https://test.com\"};\nvar ReleaseData = Release;\n",PACKAGE_VERSION);
770 2 : ck_assert_str_eq(test, test1);
771 :
772 1 : free(test);
773 :
774 1 : cJSON_ReplaceItemInObject(environment,"CI_ENVIRONMENT_NAME" , cJSON_CreateString("review/master" ));
775 1 : test=create_release_json_js(&data_exchange, NULL, "NULL", 1, NULL);
776 2 : ck_assert_ptr_ne( test, NULL);
777 1 : debug("\nset release\n>%s<\n",test);
778 1 : sprintf(test1, "var target = \"review/master\";\nvar deployctl_version = \"%s\";\n" \
779 : "var project_info = {\"name\" : \"test\",\n\"path\" : \"test/test\",\n\"link\" : \"https://test.com\"};\nvar ReleaseData = NULL;\n",PACKAGE_VERSION);
780 2 : ck_assert_str_eq(test, test1);
781 1 : free(test);
782 :
783 1 : cJSON_Delete(environment);
784 : }
785 1 : END_TEST
786 :
787 15 : Suite * release_html_suite(void)
788 : {
789 : Suite *s;
790 : TCase *tc_core;
791 : //TCase *tc_progress;
792 15 : s = suite_create("test_release_html");
793 : /* Core test case */
794 15 : tc_core = tcase_create("Core");
795 : //tcase_add_checked_fixture(tc_core, NULL,NULL);
796 15 : tcase_add_unchecked_fixture(tc_core,NULL,NULL);
797 15 : tcase_set_timeout(tc_core,5);
798 :
799 15 : tcase_add_test(tc_core, check_ci_extract_server_version);
800 15 : tcase_add_test(tc_core, check_create_releases_json);
801 :
802 15 : tcase_add_test(tc_core, check_slugit);
803 15 : tcase_add_test(tc_core, check_validate_project_path_slug);
804 15 : tcase_add_test(tc_core, check_read_release);
805 15 : tcase_add_test(tc_core, check_html_body_release);
806 15 : tcase_add_test(tc_core, check_JSON_dir_list);
807 15 : tcase_add_test(tc_core, check_JSON_link_list);
808 15 : tcase_add_test(tc_core, check_create_this_release_json);
809 :
810 15 : tcase_add_test(tc_core, check_upper_string);
811 15 : tcase_add_test(tc_core, check_is_clean_tag);
812 :
813 15 : tcase_add_test(tc_core, check_create_release_json_js);
814 :
815 15 : tcase_add_test(tc_core, check_check_if_git_version_file);
816 15 : tcase_add_test(tc_core, check_check_last_tag);
817 :
818 15 : suite_add_tcase(s, tc_core);
819 15 : return s;
820 : }
821 :
822 15 : int main(void)
823 : {
824 : int number_failed;
825 : Suite *s;
826 : SRunner *sr;
827 15 : s = release_html_suite();
828 15 : sr = srunner_create(s);
829 15 : srunner_run_all(sr, CK_VERBOSE );
830 1 : number_failed = srunner_ntests_failed(sr);
831 1 : srunner_free(sr);
832 1 : return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
833 : }
|