Line data Source code
1 : /*
2 : test_gitlab_api.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 :
30 :
31 : #include <check.h>
32 : #include "../src/deployd.h"
33 : #include "test_web_api.h"
34 :
35 : webserver_t web;
36 :
37 1 : START_TEST(check_gitlab_api)
38 : {
39 1 : u_int16_t port=0;
40 : pid_t child_pid;
41 1 : setdebug();
42 1 : int result=0;
43 1 : const char * project_path="path/test";
44 1 : const char * project_id="1";
45 1 : const char * tag="111";
46 1 : const char * job_token="abcdefg";
47 1 : cJSON *test_json_api=cJSON_CreateObject();
48 1 : cJSON_AddItemToObject(test_json_api,"test_gitlab_api", cJSON_CreateString("succes"));
49 :
50 :
51 :
52 : // what content we want the api to serve if he got the request matching request
53 1 : char * temp=cJSON_Print(test_json_api);
54 1 : sprintf(web.reply_content,"%s",temp);
55 1 : free(temp);
56 :
57 :
58 : // what response headers we want to sent back, in case request matches
59 1 : sprintf(web.reply_header,"%s","HTTP/1.1 200 OK\r\nServer: testweb/0.0.1\r\nContent-Length:%d\r\n\r\n%s");
60 :
61 :
62 : // what reply on fail
63 1 : const char * m404_mesg="{\"message\":\"404 NOT FOUND\"}";
64 1 : sprintf(web.reply_fail,"HTTP/1.1 404 NOT FOUND\r\nServer: testweb/0.0.1\r\nContent-Length:%lu\r\n\r\n%s",strlen(m404_mesg),m404_mesg);
65 :
66 : // what we expect the api to get as request as condition not to reply fail
67 1 : sprintf(web.request,"GET /api/v4/projects/%s/repository/tags/%s HTTP/1.1\r\n" \
68 : "Host: localhost:%%d\r\n" \
69 : "Accept: */*\r\n" \
70 : "JOB_TOKEN: %s\r\n" \
71 : "\r\n" ,project_id,tag,job_token);
72 :
73 : // start test server
74 1 : result=webserver(&child_pid,&port,&web,0);
75 1 : ck_assert_int_eq(result, 0);
76 2 : ck_assert_int_ne(port, 0);
77 1 : debug("result create webserver server: %d, port=%d\n",result,port);
78 :
79 : // prepare environment for command:
80 :
81 : data_exchange_t data_exchange;
82 : parameter_t parameters;
83 1 : void * opaque=&data_exchange;
84 :
85 1 : int exitcode=0;
86 : char * newarg[4];
87 1 : struct trace_Struct * trace=NULL;
88 1 : init_dynamic_trace( &trace,"token","url",1);
89 1 : data_exchange.trace=trace;
90 :
91 1 : data_exchange.needenvp=0;
92 1 : data_exchange.gid=getgid();
93 1 : data_exchange.uid=geteuid() ;
94 :
95 1 : cJSON * env_json= cJSON_CreateObject();
96 : //ck_assert_ptr_ne(env_json,NULL);
97 1 : data_exchange.env_json=env_json;
98 1 : data_exchange.paramlist=(char **)newarg;
99 1 : data_exchange.parameters=¶meters;
100 1 : data_exchange.parameters->current_user=1;
101 :
102 : char project_url[1024];
103 1 : sprintf(project_url, "http://localhost:%d/%s",port,project_path);
104 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_URL" , cJSON_CreateString(project_url));
105 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_PATH" , cJSON_CreateString(project_path));
106 1 : cJSON_AddItemToObject(env_json,"CI_PROJECT_ID", cJSON_CreateString(project_id));
107 1 : cJSON_AddItemToObject(env_json,"CI_COMMIT_TAG", cJSON_CreateString(tag));
108 1 : cJSON_AddItemToObject(env_json,"CI_JOB_TOKEN", cJSON_CreateString(job_token));
109 :
110 1 : cJSON * gitlab_release=NULL;
111 1 : int result_getrelease=0;
112 :
113 : // -----------------------------------------------------------------------------
114 : // Test one, all conditions ok, should return 0 and have a valid gitlab_release
115 : // -----------------------------------------------------------------------------
116 1 : result_getrelease=get_release(opaque, &gitlab_release);
117 2 : ck_assert_int_eq(result_getrelease, 0);
118 2 : ck_assert_ptr_ne(gitlab_release, NULL);
119 1 : cJSON_Delete(gitlab_release);
120 1 : gitlab_release=NULL;
121 1 : ck_assert_ptr_eq(gitlab_release, NULL);
122 :
123 : // stop
124 1 : stop_test_web(child_pid);
125 :
126 : // -----------------------------------------------------------------------------
127 : // change response, to non jason, should return 1
128 : // -----------------------------------------------------------------------------
129 :
130 1 : sprintf(web.reply_content,"");
131 :
132 : // start test server
133 1 : result=webserver(&child_pid,&port,&web,0);
134 2 : ck_assert_int_eq(result, 0);
135 2 : ck_assert_int_ne(port, 0);
136 1 : debug("result create webserver server pid %d, result %d, port=%d\n",child_pid,result,port);
137 :
138 : // set correct port for request
139 1 : sprintf(project_url, "http://localhost:%d/%s",port,project_path);
140 1 : cJSON_ReplaceItemInObject(env_json,"CI_PROJECT_URL" , cJSON_CreateString(project_url));
141 :
142 1 : result_getrelease=get_release(opaque, &gitlab_release);
143 2 : ck_assert_int_ne(result_getrelease, 0);
144 2 : ck_assert_ptr_eq(gitlab_release, NULL);
145 :
146 : // -----------------------------------------------------------------------------
147 : // Simulate wrong request => should be ok if we receive json message
148 : // -----------------------------------------------------------------------------
149 :
150 1 : cJSON_ReplaceItemInObject(env_json, "CI_COMMIT_TAG", cJSON_CreateString("wrong"));
151 :
152 1 : result_getrelease=get_release(opaque, &gitlab_release);
153 2 : ck_assert_int_eq(result_getrelease, 0);
154 2 : ck_assert_ptr_eq(gitlab_release, NULL);
155 : //cJSON_Delete(gitlab_release);
156 1 : gitlab_release=NULL;
157 :
158 : // -----------------------------------------------------------------------------
159 : //simulate connection to non existing server, stop webserver!!
160 : // -----------------------------------------------------------------------------
161 :
162 1 : stop_test_web(child_pid);
163 :
164 1 : result_getrelease=get_release(opaque, &gitlab_release);
165 2 : ck_assert_int_ne(result_getrelease, 0);
166 2 : ck_assert_ptr_eq(gitlab_release, NULL);
167 1 : free_dynamic_trace(&trace);
168 :
169 : }
170 1 : END_TEST
171 :
172 2 : Suite * gitlab_suite(void)
173 : {
174 : Suite *s;
175 : TCase *tc_core;
176 : //TCase *tc_progress;
177 2 : s = suite_create("test_error");
178 : /* Core test case */
179 2 : tc_core = tcase_create("Core");
180 : //tcase_add_checked_fixture(tc_core, setup, teardown);
181 2 : tcase_add_unchecked_fixture(tc_core, NULL, NULL);
182 2 : tcase_set_timeout(tc_core,15);
183 2 : tcase_add_test(tc_core, check_gitlab_api);
184 2 : suite_add_tcase(s, tc_core);
185 2 : return s;
186 : }
187 :
188 :
189 2 : int main(void)
190 : {
191 : int number_failed;
192 : Suite *s;
193 : SRunner *sr;
194 :
195 2 : s = gitlab_suite();
196 2 : sr = srunner_create(s);
197 2 : srunner_run_all(sr, CK_NORMAL);
198 1 : number_failed = srunner_ntests_failed(sr);
199 1 : srunner_free(sr);
200 1 : return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
201 : }
|