Line data Source code
1 : /*
2 : test_deployd_cmd.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 <check.h>
30 :
31 : #include "../src/deployd.h"
32 :
33 : //#include "utils.h"
34 :
35 :
36 : // TODO add to make file when ready WIP
37 :
38 : // struct to set return value of stub functions
39 : typedef struct test_stubs_s
40 : {
41 : int url_check;
42 : int verify_http_config;
43 : int reload_http_config;
44 : int letsencrypt;
45 : int delete_http_config;
46 : int write_http_config;
47 : int check_namespace;
48 : char * make_html_release;
49 :
50 : cJSON * read_relase_json;
51 : cJSON * create_thisrelease_json;
52 : char * read_release_json_filepath;
53 : char * sub_path;
54 : char * base_path;
55 : char *tagPATH;
56 : char *tagHREF;
57 : char * check_namespace_filepath;
58 : int write_namespace;
59 : int validate_project_path_slug;
60 :
61 : char * create_releases_json;
62 : char * create_release_json_js;
63 : cJSON * get_release;
64 : int is_clean_tag;
65 : char * check_last_tag;
66 : } test_stubs_t;
67 :
68 :
69 : // stubfunctions:
70 18 : int url_check(void * opaque, char * basePATH,char * baseHREF)
71 : {
72 18 : return(( (test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->url_check);
73 : }
74 20 : int verify_http_config(void * opaque)
75 : {
76 20 : return(( (test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->verify_http_config);
77 : }
78 :
79 18 : int reload_http_config(void * opaque)
80 : {
81 18 : return(( (test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->reload_http_config);
82 : }
83 :
84 0 : int letsencrypt(void * opaque, char * domain,char * email)
85 : {
86 0 : return( ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->letsencrypt);
87 : }
88 :
89 2 : int delete_http_config(void * opaque,char * basePATH)
90 : {
91 2 : ck_assert_str_eq(basePATH, ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->base_path);
92 2 : return( ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->delete_http_config);
93 : }
94 :
95 22 : int write_http_config(void * opaque, int is_https,char * basePATH, char * server_name)
96 : {
97 22 : ck_assert_str_eq(basePATH, ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->base_path);
98 22 : return( ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->write_http_config);
99 : }
100 :
101 34 : int check_namespace(void * opaque,char * filepath)
102 : {
103 34 : ck_assert_str_eq(filepath, ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->base_path);
104 34 : return( ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->check_namespace);
105 : }
106 12 : int write_namespace(void * opaque, char * filepath )
107 : {
108 12 : ck_assert_str_eq(filepath, ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->base_path);
109 12 : return( ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->write_namespace);
110 : }
111 :
112 22 : int validate_project_path_slug(cJSON* env_vars, struct trace_Struct *trace, int suggest)
113 : {
114 22 : return( ((test_stubs_t *)(trace->tests))->validate_project_path_slug );
115 : }
116 : /* no longer used used
117 : char * make_html_release(void * opaque, cJSON * release_tag, const char * filesPATH, const char * filesHREF, int deploy_production )
118 : {
119 : ck_assert_str_eq(filesPATH, ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->tagPATH);
120 : ck_assert_str_eq(filesHREF, ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->tagHREF);
121 : return( strdup(((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->make_html_release));
122 : }
123 : */
124 15 : char * create_releases_json( void * opaque , const cJSON * this_release, cJSON ** releases, int production)
125 : {
126 15 : *releases=cJSON_Parse(((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->create_releases_json);
127 15 : return( strdup(((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->create_releases_json));
128 : }
129 :
130 8 : cJSON * create_thisrelease_json( void * opaque , cJSON * release_info, int production, const char * base_path,const char * sub_path)
131 : {
132 8 : ck_assert_str_eq(sub_path,((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->sub_path);
133 16 : ck_assert_str_eq(base_path, ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->base_path);
134 8 : return( cJSON_Duplicate( ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->create_thisrelease_json, 1));
135 : }
136 :
137 15 : char * create_release_json_js (void * opaque, char * download_url, char * releases_json, int tag, char * last_tag)
138 : {
139 15 : return( strdup(((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->create_release_json_js));
140 : }
141 :
142 :
143 7 : cJSON * read_release_json(void * opaque, const char * dirpath)
144 : {
145 7 : ck_assert_str_eq(dirpath,((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->read_release_json_filepath);
146 7 : return( cJSON_Duplicate(((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->read_relase_json,1));
147 : }
148 :
149 8 : int get_release(void * opaque, cJSON ** release)
150 : {
151 8 : *release=cJSON_Duplicate(((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->get_release,1);
152 8 : if (*release) return(0);
153 1 : else return (1);
154 : }
155 :
156 :
157 2 : char * check_last_tag(void * opaque,const char * basePATH)
158 : {
159 2 : ck_assert_str_eq(basePATH, ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->base_path);
160 2 : if (((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->check_last_tag)
161 1 : return( strdup(((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->check_last_tag));
162 : else return NULL;
163 : }
164 :
165 7 : int is_clean_tag(void * opaque,const char * tag, const char ** list)
166 : {
167 7 : return( ((test_stubs_t *)((data_exchange_t *)opaque)->trace->tests)->is_clean_tag );
168 : }
169 :
170 0 : char* read_file( const char * dirpath, const char * filename)
171 : {
172 0 : return NULL;
173 : }
174 0 : int url_repo_base_path (char ** basepath,char * baseHREF,struct trace_Struct * trace)
175 : {
176 0 : return 0;
177 : }
178 :
179 : // repos.c
180 0 : cJSON * create_repo_dirs(void * opaque, char * repo_PATH , cJSON* repos)
181 : {
182 0 : return NULL;
183 : }
184 0 : int init_repo_dirs(void * opaque, char * basepath, cJSON* endpoints)
185 : {
186 0 : return 0;
187 : }
188 :
189 0 : int update_repo_dirs(void * opaque, char * basepath, cJSON* endpoints)
190 : {
191 0 : return 0;
192 : }
193 :
194 0 : int add_deploy_info_json(void * opaque,cJSON * project_json)
195 : {
196 0 : return 0;
197 : }
198 0 : char * create_repo_json_js (void * opaque, char * download_url, char * repo_json)
199 : {
200 0 : return NULL;
201 : }
202 :
203 0 : int get_repo_list(void * opaque, const char *projectpath, cJSON** repo_info)
204 : {
205 0 : return 0;
206 : }
207 0 : int match_end_points(void * opaque, cJSON* repo_info, cJSON* end_points, cJSON ** matches, cJSON ** skips, cJSON** symlinks)
208 : {
209 0 : return 0;
210 : }
211 :
212 0 : cJSON * read_json(void * opaque, const char * base_path,const char * sub_path,const char * filename)
213 : {
214 0 : return NULL;
215 : }
216 :
217 : // yalm2cjson.c
218 0 : cJSON * yaml_sting_2_cJSON (void * opaque ,const char * yaml)
219 : {
220 0 : return NULL;
221 : }
222 :
223 0 : void repo_print_skips(void * opaque,cJSON * skips)
224 : {
225 0 : return;
226 : }
227 0 : void repo_copy_matches(void * opaque,cJSON * matches,char* basepath)
228 : {
229 0 : return;
230 : }
231 :
232 0 : void repo_create_symlinks(void * opaque,cJSON * symlinks,char* basepath)
233 : {
234 0 : return;
235 : }
236 :
237 0 : int copy_script_sh(void * opaque, const char * source, const char * destination, char * repo_name, char * repo_url, int PGP_repo, int PGP_pack)
238 : {
239 0 : return 0;
240 : }
241 : //
242 0 : char * substitute_repo_script(char * in, char * repo_name, char * repo_url, int PGP_repo, int PGP_pack)
243 : {
244 0 : return in;
245 : }
246 :
247 0 : char * html_body_release(char * buf_in)
248 : {
249 0 : return buf_in;
250 : }
251 :
252 : //helper function for tests
253 2 : int write_test_file(const char * base,const char * sub,const char * file, const char * data)
254 : {
255 : //LCOV_EXCL_START
256 : char filename[1024];
257 : sprintf((char *)filename,"%s%s/%s",base,sub,file);
258 : debug("write test file %s\n",filename);
259 : FILE *f = fopen(filename, "w");
260 : if (f == NULL){ debug("Error opening file : %s\n",strerror(errno));return -1;}
261 : /* print some text */
262 : int retvalue=0;
263 : int errvalue=0;
264 : do{ retvalue=fprintf(f, "%s",data);} while ( retvalue==-1 && (errvalue=errno)== EINTR );
265 : if (retvalue<0) { debug("Error io-command %s: %s\n", filename,strerror(errvalue)); return -1; }
266 : if (fclose(f) <0 ) { debug("Error io-command %s: %s\n", filename,strerror(errno));return -1;}
267 : return retvalue;
268 : //LCOV_EXCL_STOP
269 : }
270 :
271 :
272 :
273 96 : int update_details(void * userp)
274 : {
275 96 : return 0;
276 : }
277 :
278 :
279 : char * user;
280 : char * group;
281 :
282 : uid_t thisuid;
283 : gid_t thisgid;
284 :
285 3 : static void setup(void)
286 : {
287 : struct passwd *pw;
288 : struct group * grp;
289 :
290 3 : thisuid = geteuid();
291 3 : thisgid= getgid();
292 3 : grp = getgrgid(thisgid);
293 3 : pw = getpwuid (thisuid);
294 3 : user=pw->pw_name;
295 3 : group=grp->gr_name;
296 3 : }
297 :
298 :
299 :
300 1 : START_TEST(check_delete_domain)
301 : {
302 : parameter_t parameters;
303 : data_exchange_t data_exchange;
304 1 : void * opaque=&data_exchange;
305 : char * newarg[4];
306 1 : int exitcode=0;
307 1 : struct trace_Struct * trace=NULL;
308 1 : init_dynamic_trace( &trace,"token","url",1);
309 1 : data_exchange.trace=trace;
310 1 : data_exchange.needenvp=0;
311 1 : data_exchange.gid=getgid();
312 1 : data_exchange.uid=geteuid() ;
313 :
314 1 : cJSON * env_json= cJSON_CreateObject();
315 1 : ck_assert_ptr_ne(env_json,NULL);
316 1 : data_exchange.env_json=env_json;
317 :
318 1 : data_exchange.paramlist=(char **)newarg;
319 1 : data_exchange.timeout=1;
320 1 : data_exchange.parameters=¶meters;
321 : test_stubs_t test_stubs;
322 1 : trace->tests=&test_stubs;
323 : // set results for tests
324 :
325 1 : test_stubs.check_namespace=0;
326 1 : test_stubs.validate_project_path_slug=0;
327 : char base_PATH[1024];
328 1 : test_stubs.base_path=base_PATH;
329 :
330 :
331 : char cwd[1024];
332 2 : ck_assert_ptr_ne(getcwd(cwd, sizeof(cwd)),NULL);
333 : char prefix[1024];
334 1 : sprintf(prefix,"%s/cmd_delete",cwd);
335 1 : parameters.prefix=prefix;
336 :
337 : char this_command[256];
338 1 : data_exchange.this_command=this_command;
339 : char temp[1024];
340 1 : sprintf(temp,"%s/projectdir",cwd);
341 :
342 1 : cJSON_AddItemToObject(env_json,"CI_ENVIRONMENT_NAME" , cJSON_CreateString("production" ));
343 :
344 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_PATH_SLUG", cJSON_CreateString("testpath-test-project" ));
345 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_PATH" , cJSON_CreateString("testpath/test-project" ));
346 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_DIR" , cJSON_CreateString(temp ));
347 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_URL" , cJSON_CreateString("https://gitlab.example.com/testpath/test-project"));
348 1 : cJSON_AddItemToObject(env_json,"CI_COMMIT_SHA" , cJSON_CreateString("01234567890"));
349 1 : cJSON_AddItemToObject(env_json,"DEPLOY_DOMAIN" , cJSON_CreateString("www.example.com"));
350 1 : cJSON_AddItemToObject(env_json,"DEPLOY_DOMAIN_APP" , cJSON_CreateString("deploy.example.com"));
351 :
352 :
353 1 : clear_dynamic_trace(trace);
354 :
355 1 : sprintf(base_PATH, "%s/deploy/domain/%s",prefix,cJSON_get_key(env_json, "DEPLOY_DOMAIN"));
356 :
357 1 : _mkdir(base_PATH, "/public");
358 :
359 : // should fail, missing environment var
360 :
361 1 : exitcode= cmd_delete(opaque);
362 2 : ck_assert_int_ne(exitcode,0);
363 1 : clear_dynamic_trace(trace);
364 :
365 : //add missing env
366 1 : cJSON_AddItemToObject(env_json,"CI_ENVIRONMENT_SLUG" , cJSON_CreateString("production" ));
367 :
368 : // should be ok!!
369 1 : exitcode= cmd_delete(opaque);
370 2 : ck_assert_int_eq(exitcode,0);
371 1 : clear_dynamic_trace(trace);
372 :
373 : // simulate name space taken
374 1 : test_stubs.check_namespace=1;
375 1 : test_stubs.validate_project_path_slug=0;
376 1 : exitcode= cmd_delete(opaque);
377 1 : clear_dynamic_trace(trace);
378 2 : ck_assert_int_ne(exitcode,0);
379 :
380 : // simulate wrong projectpath slug, should be ok, in production
381 1 : test_stubs.check_namespace=0;
382 1 : test_stubs.validate_project_path_slug=1;
383 1 : exitcode= cmd_delete(opaque);
384 1 : clear_dynamic_trace(trace);
385 2 : ck_assert_int_eq(exitcode,0);
386 1 : exitcode=0;
387 :
388 1 : sprintf((char *)base_PATH,"%s/deploy/domain/%s.%s.%s", \
389 : parameters.prefix, \
390 : cJSON_get_key(env_json, "CI_ENVIRONMENT_SLUG"), \
391 : cJSON_get_key(env_json, "CI_PROJECT_PATH_SLUG"), \
392 : cJSON_get_key(env_json, "DEPLOY_DOMAIN_APP") );
393 :
394 1 : _mkdir(base_PATH, "/public");
395 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
396 :
397 : // production, without deploy domain
398 1 : test_stubs.check_namespace=0;
399 1 : test_stubs.validate_project_path_slug=0;
400 :
401 1 : exitcode= cmd_delete(opaque);
402 1 : clear_dynamic_trace(trace);
403 2 : ck_assert_int_eq(exitcode,0);
404 1 : exitcode=0;
405 : // need to delete again, cmd_delete will produce deploy_domain!!!!
406 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
407 :
408 : // Production with toooooo long domain name, NOK
409 : char toolong[256];
410 1 : memset(toolong, 'a', 255);
411 1 : cJSON_ReplaceItemInObject(env_json,"CI_ENVIRONMENT_SLUG" , cJSON_CreateString(toolong ));
412 :
413 1 : exitcode= cmd_delete(opaque);
414 1 : clear_dynamic_trace(trace);
415 2 : ck_assert_int_ne(exitcode,0);
416 1 : exitcode=0;
417 :
418 1 : cJSON_ReplaceItemInObject(env_json,"CI_ENVIRONMENT_SLUG" , cJSON_CreateString("production" ));
419 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
420 :
421 : // production, without deploy domain and bad project_path_slug => NOK
422 1 : test_stubs.check_namespace=0;
423 1 : test_stubs.validate_project_path_slug=1;
424 1 : exitcode= cmd_delete(opaque);
425 1 : clear_dynamic_trace(trace);
426 2 : ck_assert_int_ne(exitcode,0);
427 1 : exitcode=0;
428 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
429 : // non production, OK
430 :
431 1 : cJSON_ReplaceItemInObject(env_json,"CI_ENVIRONMENT_NAME" , cJSON_CreateString("review/master" ));
432 1 : cJSON_ReplaceItemInObject(env_json,"CI_ENVIRONMENT_SLUG" , cJSON_CreateString("review-master0989" ));
433 :
434 :
435 : // make sites dir
436 1 : sprintf((char *)base_PATH,"%s/deploy/sites/%s/%s", \
437 : parameters.prefix, \
438 : cJSON_get_key(env_json, "CI_PROJECT_PATH_SLUG") , \
439 : cJSON_get_key(env_json, "CI_ENVIRONMENT_SLUG") );
440 1 : _mkdir(base_PATH, "/public");
441 :
442 1 : test_stubs.check_namespace=0;
443 1 : test_stubs.validate_project_path_slug=0;
444 1 : exitcode= cmd_delete(opaque);
445 1 : debug("\nDelete...\n%s\n",get_dynamic_trace(trace));
446 1 : clear_dynamic_trace(trace);
447 2 : ck_assert_int_eq(exitcode,0);
448 1 : exitcode=0;
449 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
450 :
451 : // non production with missing Deploy domain app should be NOK
452 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN_APP");
453 :
454 1 : test_stubs.check_namespace=0;
455 1 : test_stubs.validate_project_path_slug=0;
456 1 : exitcode= cmd_delete(opaque);
457 1 : clear_dynamic_trace(trace);
458 2 : ck_assert_int_ne(exitcode,0);
459 1 : exitcode=0;
460 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
461 :
462 1 : cJSON_AddItemToObject(env_json,"DEPLOY_DOMAIN_APP" , cJSON_CreateString("deploy.example.com"));
463 : // non production with invalid project slug should be NOK
464 1 : test_stubs.check_namespace=0;
465 1 : test_stubs.validate_project_path_slug=1;
466 1 : exitcode= cmd_delete(opaque);
467 1 : clear_dynamic_trace(trace);
468 2 : ck_assert_int_ne(exitcode,0);
469 1 : exitcode=0;
470 : // cleanup
471 1 : cJSON_Delete(env_json);
472 1 : _rmdir(prefix,"");
473 : }
474 1 : END_TEST
475 :
476 : //int cmd_static(void * opaque)
477 :
478 1 : START_TEST(check_cmd_static){
479 : parameter_t parameters;
480 : data_exchange_t data_exchange;
481 1 : void * opaque=&data_exchange;
482 :
483 1 : int exitcode=0;
484 : char * newarg[11];
485 :
486 1 : struct trace_Struct * trace=NULL;
487 1 : init_dynamic_trace( &trace,"token","url",1);
488 1 : data_exchange.trace=trace;
489 1 : data_exchange.needenvp=0;
490 1 : data_exchange.gid=getgid();
491 1 : data_exchange.uid=geteuid() ;
492 :
493 1 : cJSON * env_json= cJSON_CreateObject();
494 1 : ck_assert_ptr_ne(env_json,NULL);
495 1 : data_exchange.env_json=env_json;
496 :
497 1 : data_exchange.paramlist=(char **)newarg;
498 1 : data_exchange.timeout=1;
499 1 : data_exchange.parameters=¶meters;
500 : test_stubs_t test_stubs;
501 1 : trace->tests=&test_stubs;
502 : // set results for tests
503 : char base_PATH[1024];
504 1 : test_stubs.base_path=base_PATH;
505 :
506 1 : test_stubs.check_namespace=0;
507 1 : test_stubs.validate_project_path_slug=0;
508 1 : test_stubs.url_check=0;
509 1 : test_stubs.write_http_config=0;
510 1 : test_stubs.verify_http_config=0;
511 1 : test_stubs.delete_http_config=0;
512 1 : test_stubs.reload_http_config=0;
513 1 : test_stubs.write_namespace=0;
514 :
515 : char cwd[1024];
516 2 : ck_assert_ptr_ne(getcwd(cwd, sizeof(cwd)),NULL);
517 : char prefix[1024];
518 1 : sprintf(prefix,"%s/cmd_static",cwd);
519 1 : parameters.prefix=prefix;
520 :
521 : char this_command[256];
522 1 : data_exchange.this_command=this_command;
523 : char projectdir[1024];
524 :
525 :
526 1 : sprintf(projectdir,"%s/projectdir_static",cwd);
527 :
528 1 : _mkdir( "/public",projectdir);
529 :
530 : //setdebug();
531 :
532 1 : cJSON_AddItemToObject(env_json,"CI_ENVIRONMENT_NAME" , cJSON_CreateString("production" ));
533 :
534 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_PATH_SLUG", cJSON_CreateString("testpath-test-project" ));
535 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_PATH" , cJSON_CreateString("testpath/test-project" ));
536 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_DIR" , cJSON_CreateString(projectdir ));
537 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_URL" , cJSON_CreateString("https://gitlab.example.com/testpath/test-project"));
538 1 : cJSON_AddItemToObject(env_json,"CI_COMMIT_SHA" , cJSON_CreateString("01234567890"));
539 1 : cJSON_AddItemToObject(env_json,"DEPLOY_DOMAIN" , cJSON_CreateString("www.example.com"));
540 1 : cJSON_AddItemToObject(env_json,"DEPLOY_DOMAIN_APP" , cJSON_CreateString("deploy.example.com"));
541 1 : cJSON * pubarray=cJSON_CreateArray();
542 1 : cJSON * path_obj=cJSON_CreateObject();
543 1 : cJSON_AddStringToObject(path_obj, "/","/public" );
544 1 : cJSON_AddItemToArray(pubarray, path_obj);
545 1 : cJSON_AddItemToObject(env_json,"DEPLOY_PUBLISH_PATH", pubarray);
546 :
547 1 : clear_dynamic_trace(trace);
548 1 : exitcode=0;
549 :
550 1 : sprintf(base_PATH, "%s/deploy/domain/www.example.com",prefix);
551 :
552 : // should fail, missing environment var
553 :
554 1 : exitcode= cmd_static(opaque);
555 2 : ck_assert_int_ne(exitcode,0);
556 1 : clear_dynamic_trace(trace);
557 1 : exitcode=0;
558 :
559 : //add missing env
560 1 : cJSON_AddItemToObject(env_json,"CI_ENVIRONMENT_SLUG" , cJSON_CreateString("production" ));
561 :
562 : // should be ok!!
563 1 : exitcode= cmd_static(opaque);
564 1 : fprintf(stderr,"%s\n",get_dynamic_trace(trace));
565 2 : ck_assert_int_eq(exitcode,0);
566 :
567 : // set DEPLOY path
568 :
569 1 : clear_dynamic_trace(trace);
570 1 : exitcode=0;
571 :
572 : // and cleanup
573 1 : _rmdir(prefix,"");
574 :
575 :
576 : // simulate name space taken
577 1 : test_stubs.check_namespace=1;
578 :
579 1 : exitcode= cmd_static(opaque);
580 1 : clear_dynamic_trace(trace);
581 1 : test_stubs.check_namespace=0;
582 2 : ck_assert_int_ne(exitcode,0);
583 :
584 : // production with invalid deploy_domain dns should be NOK
585 1 : test_stubs.url_check=1;
586 1 : exitcode= cmd_static(opaque);
587 1 : clear_dynamic_trace(trace);
588 2 : ck_assert_int_ne(exitcode,0);
589 1 : exitcode=0;
590 1 : test_stubs.url_check=0;
591 :
592 : // and cleanup
593 1 : _rmdir(prefix,"");
594 :
595 : // production problem writing config
596 1 : test_stubs.write_http_config=1;
597 1 : exitcode= cmd_static(opaque);
598 1 : clear_dynamic_trace(trace);
599 2 : ck_assert_int_ne(exitcode,0);
600 1 : exitcode=0;
601 1 : test_stubs.write_http_config=0;
602 :
603 : // and cleanup
604 1 : _rmdir(prefix,"");
605 :
606 : // production problem verify config
607 1 : test_stubs.verify_http_config=1;
608 1 : exitcode= cmd_static(opaque);
609 1 : clear_dynamic_trace(trace);
610 2 : ck_assert_int_ne(exitcode,0);
611 1 : exitcode=0;
612 1 : test_stubs.verify_http_config=0;
613 :
614 : // and cleanup
615 1 : _rmdir(prefix,"");
616 :
617 : // simulate wrong projectpath slug, should be ok, in production w deploy_domain
618 1 : test_stubs.validate_project_path_slug=1;
619 1 : exitcode= cmd_static(opaque);
620 1 : clear_dynamic_trace(trace);
621 2 : ck_assert_int_eq(exitcode,0);
622 1 : exitcode=0;
623 1 : test_stubs.validate_project_path_slug=0;
624 :
625 : // and cleanup
626 1 : _rmdir(prefix,"");
627 1 : exitcode= cmd_delete(opaque);
628 1 : clear_dynamic_trace(trace);
629 2 : ck_assert_int_eq(exitcode,0);
630 1 : exitcode=0;
631 :
632 :
633 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
634 :
635 :
636 : // ********************************************
637 : // static: production, without deploy domain *
638 : // ********************************************
639 : //OK
640 1 : sprintf((char *)base_PATH,"%s/deploy/domain/%s.%s.%s", \
641 : parameters.prefix, \
642 : cJSON_get_key(env_json, "CI_ENVIRONMENT_SLUG"), \
643 : cJSON_get_key(env_json, "CI_PROJECT_PATH_SLUG"), \
644 : cJSON_get_key(env_json, "DEPLOY_DOMAIN_APP") );
645 :
646 1 : exitcode= cmd_static(opaque);
647 1 : clear_dynamic_trace(trace);
648 2 : ck_assert_int_eq(exitcode,0);
649 1 : exitcode=0;
650 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
651 :
652 : // and cleanup
653 1 : _rmdir(prefix,"");
654 :
655 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
656 :
657 : // Production with toooooo long domain name, NOK
658 : char toolong[256];
659 1 : memset(toolong, 'a', 255);
660 1 : cJSON_ReplaceItemInObject(env_json,"CI_ENVIRONMENT_SLUG" , cJSON_CreateString(toolong ));
661 :
662 1 : exitcode= cmd_static(opaque);
663 1 : clear_dynamic_trace(trace);
664 2 : ck_assert_int_ne(exitcode,0);
665 1 : exitcode=0;
666 :
667 1 : cJSON_ReplaceItemInObject(env_json,"CI_ENVIRONMENT_SLUG" , cJSON_CreateString("production" ));
668 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
669 :
670 : // production, without deploy domain and bad project_path_slug => NOK
671 1 : test_stubs.validate_project_path_slug=1;
672 :
673 1 : exitcode= cmd_static(opaque);
674 1 : clear_dynamic_trace(trace);
675 2 : ck_assert_int_ne(exitcode,0);
676 1 : exitcode=0;
677 1 : test_stubs.validate_project_path_slug=0;
678 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
679 :
680 : // production without deploy domain and with invalid dns should be NOK
681 1 : test_stubs.url_check=1;
682 1 : exitcode= cmd_static(opaque);
683 1 : clear_dynamic_trace(trace);
684 2 : ck_assert_int_ne(exitcode,0);
685 1 : exitcode=0;
686 1 : test_stubs.url_check=0;
687 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
688 :
689 : // **************************
690 : // static: non production *
691 : // **************************
692 1 : cJSON_ReplaceItemInObject(env_json,"CI_ENVIRONMENT_NAME" , cJSON_CreateString("review/master" ));
693 1 : cJSON_ReplaceItemInObject(env_json,"CI_ENVIRONMENT_SLUG" , cJSON_CreateString("review-master0989" ));
694 : // make sites dir
695 1 : sprintf((char *)base_PATH,"%s/deploy/sites/%s/%s", \
696 : parameters.prefix, \
697 : cJSON_get_key(env_json, "CI_PROJECT_PATH_SLUG") , \
698 : cJSON_get_key(env_json, "CI_ENVIRONMENT_SLUG") );
699 :
700 : //ok
701 1 : exitcode= cmd_static(opaque);
702 1 : clear_dynamic_trace(trace);
703 2 : ck_assert_int_eq(exitcode,0);
704 1 : exitcode=0;
705 :
706 : //cleanup
707 1 : exitcode= cmd_delete(opaque);
708 1 : clear_dynamic_trace(trace);
709 2 : ck_assert_int_eq(exitcode,0);
710 1 : exitcode=0;
711 :
712 :
713 : // simulate name space taken
714 1 : test_stubs.check_namespace=1;
715 :
716 1 : exitcode= cmd_static(opaque);
717 1 : clear_dynamic_trace(trace);
718 1 : test_stubs.check_namespace=0;
719 2 : ck_assert_int_ne(exitcode,0);
720 :
721 :
722 : // non production with invalid dns should be NOK
723 1 : test_stubs.url_check=1;
724 1 : exitcode= cmd_static(opaque);
725 1 : clear_dynamic_trace(trace);
726 2 : ck_assert_int_ne(exitcode,0);
727 1 : exitcode=0;
728 1 : test_stubs.url_check=0;
729 :
730 : // and cleanup
731 1 : exitcode= cmd_delete(opaque);
732 1 : clear_dynamic_trace(trace);
733 2 : ck_assert_int_eq(exitcode,0);
734 1 : exitcode=0;
735 :
736 : // non production with missing Deploy domain app should be NOK
737 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN_APP");
738 :
739 1 : exitcode= cmd_static(opaque);
740 1 : clear_dynamic_trace(trace);
741 2 : ck_assert_int_ne(exitcode,0);
742 1 : exitcode=0;
743 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
744 :
745 1 : cJSON_AddItemToObject(env_json,"DEPLOY_DOMAIN_APP" , cJSON_CreateString("deploy.example.com"));
746 :
747 : // non production with invalid project slug should be NOK
748 1 : test_stubs.validate_project_path_slug=1;
749 1 : exitcode= cmd_static(opaque);
750 1 : clear_dynamic_trace(trace);
751 2 : ck_assert_int_ne(exitcode,0);
752 1 : exitcode=0;
753 1 : test_stubs.validate_project_path_slug=0;
754 :
755 : // cleanup
756 1 : _rmdir(projectdir,"");
757 1 : _rmdir(prefix,"");
758 1 : cJSON_Delete(env_json);
759 1 : free_dynamic_trace(&trace);
760 :
761 : }
762 1 : END_TEST
763 :
764 : //int cmd_release(void * opaque)
765 1 : START_TEST(check_cmd_release){
766 : parameter_t parameters;
767 : data_exchange_t data_exchange;
768 1 : void * opaque=&data_exchange;
769 1 : int exitcode=0;
770 : char * newarg[11];
771 1 : struct trace_Struct * trace=NULL;
772 1 : init_dynamic_trace( &trace,"token","url",1);
773 1 : data_exchange.trace=trace;
774 1 : data_exchange.needenvp=0;
775 1 : data_exchange.gid=getgid();
776 1 : data_exchange.uid=geteuid() ;
777 :
778 1 : cJSON * env_json= cJSON_CreateObject();
779 1 : ck_assert_ptr_ne(env_json,NULL);
780 1 : data_exchange.env_json=env_json;
781 :
782 1 : data_exchange.paramlist=(char **)newarg;
783 1 : data_exchange.timeout=1;
784 1 : data_exchange.parameters=¶meters;
785 : test_stubs_t test_stubs;
786 1 : trace->tests=&test_stubs;
787 : // set results for tests
788 1 : test_stubs.check_namespace=0;
789 1 : test_stubs.validate_project_path_slug=0;
790 1 : test_stubs.url_check=0;
791 1 : test_stubs.write_http_config=0;
792 1 : test_stubs.verify_http_config=0;
793 1 : test_stubs.delete_http_config=0;
794 1 : test_stubs.reload_http_config=0;
795 1 : test_stubs.write_namespace=0;
796 :
797 :
798 :
799 : char cwd[1024];
800 2 : ck_assert_ptr_ne(getcwd(cwd, sizeof(cwd)),NULL);
801 : char prefix[1024];
802 1 : sprintf(prefix,"%s/cmd_release",cwd);
803 1 : parameters.prefix=prefix;
804 :
805 : char this_command[256];
806 1 : data_exchange.this_command=this_command;
807 :
808 : char projectdir[1024];
809 1 : sprintf(projectdir,"%s/projectdir_release",cwd);
810 :
811 :
812 1 : _mkdir( "/release",projectdir); // create project dir
813 1 : write_test_file(projectdir, "/release", "testfile.txt", "test_data");
814 : //make_dir(opaque, projectdir); // create project dir
815 :
816 1 : cJSON_AddItemToObject(env_json,"CI_ENVIRONMENT_NAME" , cJSON_CreateString("production" ));
817 :
818 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_PATH_SLUG", cJSON_CreateString("testpath-test-project" ));
819 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_PATH" , cJSON_CreateString("testpath/test-project" ));
820 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_DIR" , cJSON_CreateString(projectdir ));
821 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_URL" , cJSON_CreateString("https://gitlab.example.com/testpath/test-project"));
822 1 : cJSON_AddItemToObject(env_json,"CI_COMMIT_SHA" , cJSON_CreateString("01234567890"));
823 1 : cJSON_AddItemToObject(env_json,"DEPLOY_DOMAIN" , cJSON_CreateString("www.example.com"));
824 1 : cJSON_AddItemToObject(env_json,"DEPLOY_DOMAIN_APP" , cJSON_CreateString("deploy.example.com"));
825 :
826 1 : cJSON_AddItemToObject(env_json,"CI_COMMIT_REF_SLUG" , cJSON_CreateString("1-1-1"));
827 1 : cJSON_AddItemToObject(env_json,"CI_JOB_TOKEN" , cJSON_CreateString("dkfkdfkdjfjdfjdkjfkdf"));
828 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_ID" , cJSON_CreateString("123"));
829 1 : cJSON * pubarray=cJSON_CreateArray();
830 1 : cJSON * path_obj=cJSON_CreateObject();
831 1 : cJSON_AddStringToObject(path_obj, "/","/release" );
832 1 : cJSON_AddItemToArray(pubarray, path_obj);
833 1 : cJSON_AddItemToObject(env_json,"DEPLOY_PUBLISH_PATH", pubarray);
834 :
835 1 : clear_dynamic_trace(trace);
836 1 : exitcode=0;
837 :
838 : char base_PATH[1024];
839 : char sub_PATH[1024];
840 : char read_release_json_filepath[1024];
841 : char tagPATH[1024];
842 : char tagHREF[1024];
843 1 : test_stubs.is_clean_tag=1;
844 1 : test_stubs.tagPATH=tagPATH;
845 1 : test_stubs.tagHREF=tagHREF;
846 1 : test_stubs.base_path=base_PATH;
847 1 : test_stubs.sub_path=sub_PATH;
848 1 : test_stubs.read_release_json_filepath=read_release_json_filepath;
849 :
850 1 : test_stubs.check_namespace=0;
851 1 : test_stubs.validate_project_path_slug=0;
852 :
853 1 : test_stubs.get_release=cJSON_Parse("{ \"name\":\"0.1.4\",\"message\":\"release 0.1.4\"," \
854 : "\"release\":{\"tag_name\":\"0.1.4\",\"description\":\"# New release\r\n\r\n\r\n\"}}");
855 1 : test_stubs.read_relase_json=NULL;
856 2 : test_stubs.create_releases_json=strdup( \
857 : "\"releases\": [{\"tag\": {\"name\": \"0.1.6\",\"link\": \"https://gitlab.gioxa.com/deployctl/test_deploy_release/tags/0.1.6\"}, " \
858 : "\"project\": { \"name\": \"test_deploy_release\", "\
859 : "\"link\": \"https://gitlab.gioxa.com/deployctl/test_deploy_release\"}," \
860 : "\"source\": {\"name\": \"{CI_COMMIT_SHA}\", " \
861 : "\"link\": \"https://gitlab.gioxa.com/deployctl/test_deploy_release/tree/0.1.6\"}, " \
862 : "\"commit\": {\"name\": \"86e245ef\", " \
863 : "\"link\": \"https://gitlab.gioxa.com/deployctl/test_deploy_release/commit/d89a8974b4363b3f65256330ccdd6dec6614df4e\"}, " \
864 : "\"deployed\": {\"by\": \"danny.goossen@gioxa.com\",\"link\": \"https://gitlab.gioxa.com/dgoo2308\"}, " \
865 : "\"tag_info\": { " \
866 : "\"message\": \"test site and api\"," \
867 : "\"notes_html\": \"<p>test site and api ... etc</p>\"}, " \
868 : "\"files\": [{\"name\": \"file2.sh\",\"link\": \"0.1.6/files/file2.sh\"}]}]} ");
869 1 : test_stubs.create_thisrelease_json=cJSON_Parse( \
870 : "{\"tag\": {\"name\": \"0.1.6\",\"link\": \"https://gitlab.gioxa.com/deployctl/test_deploy_release/tags/0.1.6\"}, " \
871 : "\"project\": { \"name\": \"test_deploy_release\", "\
872 : "\"link\": \"https://gitlab.gioxa.com/deployctl/test_deploy_release\"}," \
873 : "\"source\": {\"name\": \"{CI_COMMIT_SHA}\", " \
874 : "\"link\": \"https://gitlab.gioxa.com/deployctl/test_deploy_release/tree/0.1.6\"}, " \
875 : "\"commit\": {\"name\": \"86e245ef\", " \
876 : "\"link\": \"https://gitlab.gioxa.com/deployctl/test_deploy_release/commit/d89a8974b4363b3f65256330ccdd6dec6614df4e\"}, " \
877 : "\"deployed\": {\"by\": \"danny.goossen@gioxa.com\",\"link\": \"https://gitlab.gioxa.com/dgoo2308\"}, " \
878 : "\"tag_info\": { " \
879 : "\"message\": \"test site and api\"," \
880 : "\"notes_html\": \"<p>test site and api ... etc</p>\"}, " \
881 : "\"files\": [{\"name\": \"file2.sh\",\"link\": \"0.1.6/files/file2.sh\"}]}" );
882 :
883 1 : test_stubs.create_release_json_js=strdup("var java\n");
884 1 : test_stubs.make_html_release=strdup("<html>");
885 :
886 1 : sprintf(base_PATH, "%s/deploy/domain/www.example.com",prefix);
887 1 : sprintf(sub_PATH, "/1-1-1/files");
888 1 : sprintf(tagPATH, "%s/public/1-1-1",base_PATH);
889 1 : sprintf(tagHREF, "http://www.example.com/1-1-1");
890 1 : sprintf(read_release_json_filepath, "%s/public/release.json",base_PATH);
891 :
892 : // ------------------------------------------------------------------------
893 : // START Release testing
894 : // should fail, missing environment var
895 : //
896 1 : exitcode= cmd_release(opaque);
897 1 : ck_assert_int_ne(exitcode,0);
898 1 : clear_dynamic_trace(trace);
899 1 : exitcode=0;
900 :
901 : //add missing env
902 1 : cJSON_AddItemToObject(env_json,"CI_ENVIRONMENT_SLUG" , cJSON_CreateString("production" ));
903 :
904 : // should fail, missing var "CI_COMMIT_TAG"
905 :
906 1 : exitcode= cmd_release(opaque);
907 2 : ck_assert_int_ne(exitcode,0);
908 1 : clear_dynamic_trace(trace);
909 1 : exitcode=0;
910 :
911 : //add missing env, should be ok!! make first release
912 :
913 1 : cJSON_AddItemToObject(env_json,"CI_COMMIT_TAG" , cJSON_CreateString("1.1.1" ));
914 :
915 1 : exitcode= cmd_release(opaque);
916 2 : ck_assert_int_eq(exitcode,0);
917 1 : clear_dynamic_trace(trace);
918 1 : exitcode=0;
919 :
920 : // make now clean ta (Beta ... etc )
921 1 : cJSON_ReplaceItemInObject(env_json,"CI_COMMIT_TAG" , cJSON_CreateString("1.1.1-beta" ));
922 1 : test_stubs.check_last_tag=NULL;
923 1 : test_stubs.is_clean_tag=0;
924 :
925 1 : exitcode= cmd_release(opaque);
926 2 : ck_assert_int_eq(exitcode,0);
927 1 : clear_dynamic_trace(trace);
928 1 : exitcode=0;
929 :
930 1 : cJSON_ReplaceItemInObject(env_json,"CI_COMMIT_TAG" , cJSON_CreateString("1.1.1-test" ));
931 2 : test_stubs.check_last_tag=strdup("1.1.1");
932 1 : test_stubs.is_clean_tag=0;
933 1 : exitcode= cmd_release(opaque);
934 1 : ck_assert_int_eq(exitcode,0);
935 1 : clear_dynamic_trace(trace);
936 1 : exitcode=0;
937 1 : test_stubs.is_clean_tag=1;
938 :
939 : // and cleanup
940 1 : _rmdir(prefix,"");
941 :
942 :
943 :
944 1 : _rmdir(projectdir,"");
945 :
946 1 : _mkdir( "/release",projectdir);// create project dir with release and content
947 1 : write_test_file(projectdir, "/release", "testfile", "test_data");
948 : // and cleanup
949 1 : _rmdir(prefix,"");
950 :
951 1 : cJSON_Delete(test_stubs.get_release);
952 1 : test_stubs.get_release=cJSON_Parse("{ \"name\":\"0.1.4\"," \
953 : "\"release\":{\"tag_name\":\"0.1.4\",\"description\":\"# New release\r\n\r\n\r\n\"}}");
954 :
955 : // No message in return from get release should be nok!!, for now OK, no error on that
956 1 : exitcode= cmd_release(opaque);
957 2 : ck_assert_int_eq(exitcode,0);
958 :
959 1 : clear_dynamic_trace(trace);
960 1 : exitcode=0;
961 : // and cleanup
962 1 : _rmdir(prefix,"");
963 :
964 : // problem getting release: should be NOK, for now OK: Discussion!!!!
965 1 : cJSON_Delete(test_stubs.get_release);
966 1 : test_stubs.get_release=NULL; // simulate problem getting release
967 :
968 1 : exitcode= cmd_release(opaque);
969 2 : ck_assert_int_eq(exitcode,0);
970 1 : clear_dynamic_trace(trace);
971 1 : exitcode=0;
972 : // and cleanup
973 1 : _rmdir(prefix,"");
974 :
975 : // back to normal
976 1 : test_stubs.get_release=cJSON_Parse("{ \"name\":\"0.1.4\",\"message\":\"release 0.1.4\"," \
977 : "\"release\":{\"tag_name\":\"0.1.4\",\"description\":\"# New release\r\n\r\n\r\n\"}}");
978 :
979 : // simulate name space taken
980 1 : test_stubs.check_namespace=1;
981 :
982 1 : exitcode= cmd_release(opaque);
983 1 : clear_dynamic_trace(trace);
984 1 : test_stubs.check_namespace=0;
985 2 : ck_assert_int_ne(exitcode,0);
986 :
987 : // production with invalid deploy_domain dns should be NOK
988 1 : test_stubs.url_check=1;
989 1 : exitcode= cmd_release(opaque);
990 1 : clear_dynamic_trace(trace);
991 2 : ck_assert_int_ne(exitcode,0);
992 1 : exitcode=0;
993 1 : test_stubs.url_check=0;
994 :
995 : // and cleanup
996 1 : _rmdir(prefix,"");
997 :
998 : // production problem writing config
999 1 : test_stubs.write_http_config=1;
1000 1 : exitcode= cmd_release(opaque);
1001 1 : clear_dynamic_trace(trace);
1002 2 : ck_assert_int_ne(exitcode,0);
1003 1 : exitcode=0;
1004 1 : test_stubs.write_http_config=0;
1005 :
1006 : // and cleanup
1007 1 : _rmdir(prefix,"");
1008 :
1009 : // production problem verify config
1010 1 : test_stubs.verify_http_config=1;
1011 1 : exitcode= cmd_release(opaque);
1012 1 : clear_dynamic_trace(trace);
1013 2 : ck_assert_int_ne(exitcode,0);
1014 1 : exitcode=0;
1015 1 : test_stubs.verify_http_config=0;
1016 :
1017 : // and cleanup
1018 1 : _rmdir(prefix,"");
1019 :
1020 : // simulate wrong projectpath slug, should be ok, in production w deploy_domain
1021 1 : test_stubs.validate_project_path_slug=1;
1022 1 : exitcode= cmd_release(opaque);
1023 1 : clear_dynamic_trace(trace);
1024 2 : ck_assert_int_eq(exitcode,0);
1025 1 : exitcode=0;
1026 1 : test_stubs.validate_project_path_slug=0;
1027 :
1028 : // and cleanup
1029 1 : _rmdir(prefix,"");
1030 :
1031 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
1032 :
1033 : // ********************************************
1034 : // release: production, without deploy domain *
1035 : // ********************************************
1036 : //OK
1037 1 : debug("***** release: production, without deploy domain ******\n");
1038 1 : sprintf(base_PATH, "%s/deploy/domain/production.testpath-test-project.deploy.example.com",prefix);
1039 1 : sprintf(sub_PATH, "/1-1-1/files");
1040 1 : sprintf(read_release_json_filepath, "%s/public/release.json",base_PATH);
1041 1 : sprintf(tagPATH, "%s/public/1-1-1",base_PATH);
1042 1 : sprintf(tagHREF, "http://production.testpath-test-project.deploy.example.com/1-1-1");
1043 :
1044 :
1045 1 : exitcode= cmd_release(opaque);
1046 1 : clear_dynamic_trace(trace);
1047 2 : ck_assert_int_eq(exitcode,0);
1048 1 : exitcode=0;
1049 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
1050 :
1051 : // cleanup
1052 1 : _rmdir(prefix,"");
1053 :
1054 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
1055 : /* TODO: NOT IMPLEMETED YET, and also for non Production
1056 : // Production with toooooo long domain name, NOK
1057 : char toolong[256];
1058 : memset(toolong, 'a', 255);
1059 : cJSON_ReplaceItemInObject(env_json,"CI_ENVIRONMENT_SLUG" , cJSON_CreateString(toolong ));
1060 :
1061 : exitcode= cmd_static(opaque);
1062 : clear_dynamic_trace(trace);
1063 : ck_assert_int_ne(exitcode,0);
1064 : exitcode=0;
1065 : */
1066 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
1067 :
1068 : // production, without deploy domain and bad project_path_slug => NOK
1069 1 : test_stubs.validate_project_path_slug=1;
1070 :
1071 1 : exitcode= cmd_release(opaque);
1072 1 : clear_dynamic_trace(trace);
1073 2 : ck_assert_int_ne(exitcode,0);
1074 1 : exitcode=0;
1075 1 : test_stubs.validate_project_path_slug=0;
1076 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
1077 :
1078 : // production without deploy domain and with invalid dns should be NOK
1079 1 : test_stubs.url_check=1;
1080 1 : exitcode= cmd_release(opaque);
1081 1 : clear_dynamic_trace(trace);
1082 2 : ck_assert_int_ne(exitcode,0);
1083 1 : exitcode=0;
1084 1 : test_stubs.url_check=0;
1085 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
1086 :
1087 : // ***********************
1088 : // non production, OK
1089 :
1090 1 : sprintf(sub_PATH, "/files");
1091 1 : cJSON_ReplaceItemInObject(env_json,"CI_ENVIRONMENT_NAME" , cJSON_CreateString("review/master" ));
1092 1 : cJSON_ReplaceItemInObject(env_json,"CI_ENVIRONMENT_SLUG" , cJSON_CreateString("review-master0989" ));
1093 :
1094 1 : sprintf(base_PATH, "%s/deploy/sites/testpath-test-project/review-master0989",prefix);
1095 1 : sprintf(tagPATH, "%s/public",base_PATH);
1096 1 : sprintf(tagHREF, "http://review-master0989.testpath-test-project.deploy.example.com");
1097 :
1098 : //ok
1099 : //setdebug();
1100 1 : exitcode= cmd_release(opaque);
1101 1 : clear_dynamic_trace(trace);
1102 2 : ck_assert_int_eq(exitcode,0);
1103 1 : exitcode=0;
1104 :
1105 : //cleanup
1106 1 : _rmdir(prefix,"");
1107 :
1108 :
1109 : // simulate name space taken
1110 1 : test_stubs.check_namespace=1;
1111 :
1112 1 : exitcode= cmd_release(opaque);
1113 1 : clear_dynamic_trace(trace);
1114 1 : test_stubs.check_namespace=0;
1115 2 : ck_assert_int_ne(exitcode,0);
1116 :
1117 :
1118 : // non production with invalid dns should be NOK
1119 1 : test_stubs.url_check=1;
1120 1 : exitcode= cmd_release(opaque);
1121 1 : clear_dynamic_trace(trace);
1122 2 : ck_assert_int_ne(exitcode,0);
1123 1 : exitcode=0;
1124 1 : test_stubs.url_check=0;
1125 :
1126 : // and cleanup
1127 1 : _rmdir(prefix,"");
1128 :
1129 : // non production with missing Deploy domain app should be NOK
1130 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN_APP");
1131 :
1132 1 : exitcode= cmd_release(opaque);
1133 1 : clear_dynamic_trace(trace);
1134 2 : ck_assert_int_ne(exitcode,0);
1135 1 : exitcode=0;
1136 1 : cJSON_DeleteItemFromObject(env_json, "DEPLOY_DOMAIN");
1137 :
1138 1 : cJSON_AddItemToObject(env_json,"DEPLOY_DOMAIN_APP" , cJSON_CreateString("deploy.example.com"));
1139 :
1140 : // non production with invalid project slug should be NOK
1141 1 : test_stubs.validate_project_path_slug=1;
1142 1 : exitcode= cmd_release(opaque);
1143 1 : clear_dynamic_trace(trace);
1144 2 : ck_assert_int_ne(exitcode,0);
1145 1 : exitcode=0;
1146 1 : test_stubs.validate_project_path_slug=0;
1147 :
1148 : // cleanup
1149 1 : _rmdir(projectdir,"");
1150 1 : _rmdir(prefix,"");
1151 1 : cJSON_Delete(env_json);
1152 :
1153 : }
1154 1 : END_TEST
1155 :
1156 4 : Suite * deployd_cmd_suite(void)
1157 : {
1158 : Suite *s;
1159 : TCase *tc_core;
1160 : //TCase *tc_progress;
1161 4 : s = suite_create("test_deployd_cmd");
1162 : /* Core test case */
1163 4 : tc_core = tcase_create("Core");
1164 4 : tcase_add_checked_fixture(tc_core, setup, NULL);
1165 : //tcase_add_unchecked_fixture(tc_core, setup, teardown);
1166 4 : tcase_set_timeout(tc_core,65);
1167 4 : tcase_add_test(tc_core, check_delete_domain);
1168 4 : tcase_add_test(tc_core, check_cmd_static);
1169 4 : tcase_add_test(tc_core, check_cmd_release);
1170 :
1171 4 : suite_add_tcase(s, tc_core);
1172 4 : return s;
1173 : }
1174 :
1175 :
1176 4 : int main(void)
1177 : {
1178 : int number_failed;
1179 : Suite *s;
1180 : SRunner *sr;
1181 :
1182 4 : s = deployd_cmd_suite();
1183 4 : sr = srunner_create(s);
1184 4 : srunner_run_all(sr, CK_VERBOSE | CK_NOFORK);
1185 1 : number_failed = srunner_ntests_failed(sr);
1186 1 : srunner_free(sr);
1187 1 : return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1188 : }
|