Line data Source code
1 : /*
2 : test_repo.c
3 : Created by Danny Goossen, Gioxa Ltd on 12/6/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 : #include "../src/deployd.h"
31 :
32 : // struct to set return value of stub functions
33 : typedef struct test_stubs_s
34 : {
35 :
36 : int get_rpm_fileinfo;
37 : cJSON *rpm_info;
38 :
39 : cJSON * JSON_dir_list_x;
40 : } test_stubs_t;
41 :
42 :
43 : // stubfunctions:
44 0 : int get_rpm_fileinfo(void * opaque, const char * filepath, const char * filename,cJSON ** rpm_info)
45 : {
46 0 : return 0;
47 : }
48 :
49 0 : cJSON * JSON_dir_list_x( const char *path,const char *subpath,int recursive, const char * extension)
50 : {
51 0 : return NULL;
52 : }
53 :
54 0 : int update_details(void * userp)
55 : {
56 0 : return 0;
57 : }
58 :
59 : //TODO update, with rpm tests and symlinks
60 :
61 : //int * match_end_points(void * opaque, cJSON* repo_info, cJSON* end_points, cJSON ** matches, cJSON ** skips, cJSON** symlinks);
62 :
63 1 : START_TEST(check_match_end_points)
64 : {
65 :
66 1 : const char rpm_info_str[]= \
67 : "[" \
68 : // fully defined arch/dist => No symlink
69 : "{ \"arch\": \"x86_64\", \"distribution\": \"el7\", \"SRPM\": false, \"type\": \"rpm\"," \
70 : "\"filename\": \"A.el7.centos.x86_64.rpm\"," \
71 : "\"filepath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/\"," \
72 : "\"fullpath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/A.el7.centos.x86_64.rpm\"}, " \
73 :
74 : // no arch but distributio => symlink to all /rpm/el6/*
75 : "{ \"arch\": \"noarch\", \"distribution\": \"el6\", \"SRPM\": false, \"type\": \"rpm\"," \
76 : "\"filename\": \"B.el6.noarch.rpm\"," \
77 : "\"filepath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/\"," \
78 : "\"fullpath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/B.el6.noarch.rpm\"}," \
79 :
80 : // no arch no dist => symlink to /rpm/*
81 : "{ \"arch\": \"noarch\", \"SRPM\": false, \"type\": \"rpm\"," \
82 : "\"filename\": \"C.noarch.rpm\"," \
83 : "\"filepath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/\"," \
84 : "\"fullpath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/C.rpm\"}," \
85 :
86 : // arch, no dist => only symlink to all /rpm/*/x86_64
87 : "{ \"arch\": \"x86_64\", \"SRPM\": false, \"type\": \"rpm\"," \
88 : "\"filename\": \"D.x86_64.rpm\"," \
89 : "\"filepath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/\"," \
90 : "\"fullpath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/D.x86_64.rpm\"}," \
91 :
92 : // arch, no dist / but not in endpoints => into skips
93 : "{ \"arch\": \"armhfp\", \"SRPM\": false, \"type\": \"rpm\"," \
94 : "\"filename\": \"E.armhfp.rpm\"," \
95 : "\"filepath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/\"," \
96 : "\"fullpath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/E.armhfp.rpm\"}," \
97 :
98 : // arch, dist / but not in endpoints => into skips
99 : "{ \"arch\": \"noarch\", \"distribution\": \"el8\",\"SRPM\": false, \"type\": \"rpm\"," \
100 : "\"filename\": \"F.noarch.rpm\"," \
101 : "\"filepath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/\"," \
102 : "\"fullpath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/F.noarch.rpm\"}," \
103 :
104 : // fully defined arch/dist , but error => into skips
105 : "{ \"arch\": \"x86_64\", \"distribution\": \"el7\", \"SRPM\": false, \"type\": \"rpm\"," \
106 : "\"filename\": \"G.el7.centos.x86_64.rpm\"," \
107 : "\"filepath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/\"," \
108 : "\"error\": \"invalid rpm\"," \
109 : "\"fullpath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/G.el7.centos.x86_64.rpm\"}, " \
110 :
111 : // fully defined rpm /dist/arch but SRPM => skips
112 : "{ \"arch\": \"x86_64\", \"distribution\": \"el7\", \"SRPM\": true, \"type\": \"rpm\"," \
113 : "\"filename\": \"S.el7.centos.x86_64.rpm\"," \
114 : "\"filepath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/\"," \
115 : "\"fullpath\": \"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/S.el7.centos.x86_64.rpm\"}, " \
116 :
117 : // debian
118 : // fully defined deb/dist/release and in endpoints => To maches and no symlink
119 : "{ \"type\": \"deb\", \"distribution\": \"ubuntu\", \"release\": \"trusty\", " \
120 : "\"filename\": \"A.deb\"," \
121 : "\"filepath\": \"/projectdir/deb/ubuntu/trusty\"," \
122 : "\"fullpath\": \"/projectdir/deb/ubuntu/trusty/A.deb\"}, " \
123 :
124 : // deb/dist/* and in endpoints => To maches, symlink /deb/ubuntu/*
125 : "{ \"type\": \"deb\", \"distribution\": \"ubuntu\", " \
126 : "\"filename\": \"B.deb\"," \
127 : "\"filepath\": \"projectdir/deb/ubuntu/\"," \
128 : "\"fullpath\": \"projectdir/deb/ubuntu/B.deb\"}, " \
129 :
130 : // deb/* and in endpoints => To maches, symlink /deb/*
131 : "{ \"type\": \"deb\", " \
132 : "\"filename\": \"C.deb\"," \
133 : "\"filepath\": \"projectdir/deb/\"," \
134 : "\"fullpath\": \"projectdir/deb/C.deb\"}, " \
135 :
136 : // fully defined deb/dist/release and not in endpoints => skips
137 : "{ \"type\": \"deb\", \"distribution\": \"raspbian\", \"release\": \"buster\", " \
138 : "\"filename\": \"D.deb\"," \
139 : "\"filepath\": \"/projectdir/deb/raspbian/buster\"," \
140 : "\"fullpath\": \"/projectdir/deb/raspbian/buster/D.deb\"}, " \
141 :
142 : // fully defined deb/dist/release and wrong endpoint => skips
143 : "{ \"type\": \"deb\", \"distribution\": \"ubuntu\", \"release\": \"trusty\", " \
144 : "\"filename\": \"E.deb\"," \
145 : "\"filepath\": \"/projectdir/deb/ubuntu/trusty/x\"," \
146 : "\"error\": \"wrong subpath\"," \
147 : "\"subpath\": \"/deb/ubuntu/trusty/x\"," \
148 : "\"fullpath\": \"/projectdir/deb/ubuntu/trusty/x/E.deb\"} " \
149 :
150 : "]";
151 :
152 : const char endpoints_str[]= \
153 : "[" \
154 : "{ \"/rpm/el7/x86_64\": [\"rpm\", \"el7\", \"x86_64\"] }, " \
155 : "{ \"/rpm/el6/x86_64\": [\"rpm\", \"el6\", \"x86_64\"] }, " \
156 : "{ \"/rpm/el6/i386\": [\"rpm\", \"el6\", \"i386\"]}, " \
157 : "{ \"/deb/ubuntu/trusty\": [\"deb\", \"ubuntu\", \"trusty\"] }, " \
158 : "{ \"/deb/ubuntu/zesty\": [\"deb\", \"ubuntu\", \"zesty\"] }, " \
159 : "{ \"/deb/debian/jessie\": [\"deb\", \"debian\", \"jessie\"] }, " \
160 : "{ \"/deb/debian/wheezy\": [\"deb\", \"debian\", \"wheezy\"] }, " \
161 : "{ \"/deb/debian/stretch\": [\"deb\", \"debian\", \"stretch\"] } " \
162 : "]" ;
163 :
164 :
165 1 : cJSON * rpm_info=cJSON_Parse(rpm_info_str);
166 :
167 1 : const char repos_str[]= \
168 : "[" \
169 : "{\"rpm\": [{ \"el7\": [ {\"amd64\": \"x86_64\"}, { \"i386\": [\"i386\", \"i686\"]}, {\"armhfp\": \"armv7hl\"}]}," \
170 : "{ \"el6\": [ \"x86_64\", { \"i386\": [\"i386\", \"i686\"]}]} "\
171 : "]}, " \
172 : "{\"deb\": [{ \"debian\": [\"wheezy\", \"jessie\", \"stretch\"]}, " \
173 : "{ \"ubuntu\": [\"trusty\", \"zesty\"]} ]" \
174 : "} " \
175 : "]";
176 :
177 1 : cJSON * repos=cJSON_Parse(repos_str);
178 1 : cJSON * end_points=NULL; //cJSON_Parse(endpoints_str);
179 1 : cJSON * trace= cJSON_CreateArray();
180 1 : char * temp=extract_dir_structure(repos,&end_points,trace);
181 1 : if (trace) cJSON_Delete(trace);
182 : //cJSON * end_points=cJSON_Parse(endpoints_str);
183 1 : cJSON * matches=NULL;
184 1 : cJSON * skips=NULL;
185 1 : cJSON * symlinks=NULL;
186 :
187 1 : match_end_points( NULL, rpm_info,end_points, &matches,&skips,&symlinks);
188 :
189 1 : ck_assert_ptr_ne(matches, NULL);
190 2 : ck_assert_ptr_ne(skips, NULL);
191 2 : ck_assert_ptr_ne(symlinks, NULL);
192 :
193 2 : ck_assert_int_eq(cJSON_GetArraySize(skips), 6);
194 2 : ck_assert_int_eq(cJSON_GetArraySize(symlinks), 16);
195 2 : ck_assert_int_eq(cJSON_GetArraySize(matches), 7);
196 :
197 :
198 1 : cJSON * skip= cJSON_GetArrayItem(skips, 0);
199 2 : ck_assert_ptr_ne(skip, NULL);
200 2 : ck_assert_str_eq(cJSON_get_key(skip,"filename"), "E.armhfp.rpm");
201 2 : ck_assert_str_eq(cJSON_get_key(skip,"type"), "rpm");
202 2 : ck_assert_str_eq(cJSON_get_key(skip,"arch"), "armhfp");
203 :
204 :
205 1 : cJSON * match= cJSON_GetArrayItem(matches, 3);
206 2 : ck_assert_ptr_ne(match, NULL);
207 2 : ck_assert_str_eq(cJSON_get_key(match,"fullpath"),"/home/gitlab-runner/builds/26fdc7cb/0/deployctl/cJSON/rpm/D.x86_64.rpm");
208 2 : ck_assert_str_eq(cJSON_get_key(match,"filename"), "D.x86_64.rpm");
209 2 : ck_assert_str_eq(cJSON_get_key(match,"endpoint"), "/rpm");
210 :
211 1 : cJSON * symlink= cJSON_GetArrayItem(symlinks, 9);
212 2 : ck_assert_ptr_ne(symlink, NULL);
213 2 : ck_assert_str_eq(cJSON_get_key(symlink,"source"),"/deb/ubuntu");
214 2 : ck_assert_str_eq(cJSON_get_key(symlink,"filename"), "B.deb");
215 2 : ck_assert_str_eq(cJSON_get_key(symlink,"endpoint"), "/deb/ubuntu/trusty");
216 :
217 :
218 : //char * skips_prt=cJSON_Print(skips);
219 : //char * matches_prt=cJSON_Print(matches);
220 1 : cJSON_Delete(matches);
221 1 : cJSON_Delete(skips);
222 1 : cJSON_Delete(symlinks);
223 :
224 : //fprintf(stderr,"skips:\n%s\n",skips_prt);
225 : //fprintf(stderr,"matches:\n%s\n",matches_prt);
226 :
227 : //free(matches_prt);
228 : //free(skips_prt);
229 : //ck_assert_int_eq(1, 1);
230 : }
231 1 : END_TEST
232 :
233 : //char * extract_dir_structure( cJSON* repo, cJSON** end_points, cJSON * trace_endpoints)
234 1 : START_TEST(check_extract_dir_structure)
235 : {
236 1 : const char repo_json_str[]="{\"projects\": [\"https://gitlab.gioxa.com/deployctl/deployctl\", \"https://gitlab.gioxa.com/deployctl/cJSON\"]," \
237 : "\"repos\": "\
238 : "[" \
239 : "{\"rpm\": [{ \"el7\": [ {\"amd64\": \"x86_64\"}, { \"i386\": [\"i386\", \"i686\"]}, {\"armhfp\": \"armv7hl\"}]}," \
240 : "{ \"el6\": [ \"x86_64\", { \"i386\": [\"i386\", \"i686\"]}]} "\
241 : "]}, " \
242 : "{\"deb\": [{ \"debian\": [\"wheezy\", \"jessie\", \"stretch\"]}, " \
243 : "{ \"ubuntu\": [\"trusty\", \"zesty\"]} ]" \
244 : "} " \
245 : "]}";
246 :
247 1 : cJSON * repo_json=cJSON_Parse(repo_json_str);
248 1 : cJSON * repos=cJSON_GetObjectItem(repo_json,"repos");
249 1 : cJSON * endpoints=NULL;
250 1 : cJSON * trace= cJSON_CreateArray();
251 1 : char * temp=extract_dir_structure(repos,&endpoints,trace);
252 1 : ck_assert_pstr_ne(temp,NULL);
253 2 : ck_assert_str_eq(temp, "{rpm/{el7/{amd64,i386,armhfp},el6/{x86_64,i386}},deb/{debian/{wheezy,jessie,stretch},ubuntu/{trusty,zesty}}}");
254 1 : char * repo_print=cJSON_Print(repos);
255 2 : ck_assert_ptr_ne(endpoints, NULL);
256 1 : cJSON_Delete(trace);
257 1 : cJSON_Delete(repo_json);
258 1 : cJSON_Delete(endpoints);
259 1 : free(temp);
260 1 : free(repo_print);
261 :
262 : }
263 1 : END_TEST
264 :
265 : //char * create_repo_json_js (void * opaque, char * download_url, char * repo_json)
266 1 : START_TEST(check_create_repo_json_js)
267 : {
268 :
269 : }
270 1 : END_TEST
271 :
272 : //int init_repo_dirs(void * opaque, char * basepath, cJSON* endpoints)
273 1 : START_TEST(check_init_repo_dirs)
274 : {
275 : }
276 1 : END_TEST
277 :
278 : // int get_repo_list(void * opaque, const char *projectpath, cJSON** repo_info)
279 1 : START_TEST(check_get_repo_list)
280 : {
281 :
282 : }
283 1 : END_TEST
284 :
285 : // cJSON * create_repo_dirs(void * opaque, char * repo_PATH , cJSON* repos)
286 1 : START_TEST(check_create_repo_dirs)
287 : {
288 : }
289 1 : END_TEST
290 :
291 : //int update_repo_dirs(void * opaque, char * basepath, cJSON* endpoints)
292 1 : START_TEST(check_update_repo_dirs)
293 : {
294 :
295 : }
296 1 : END_TEST
297 :
298 : // char * print_skips(cJSON * skips)
299 1 : START_TEST(check_print_skips)
300 : {
301 : }
302 1 : END_TEST
303 :
304 9 : Suite * test_suite(void)
305 : {
306 : Suite *s;
307 : TCase *tc_core;
308 : //TCase *tc_progress;
309 9 : s = suite_create("test_repo");
310 : /* Core test case */
311 9 : tc_core = tcase_create("Core");
312 : //tcase_add_checked_fixture(tc_core, setup, teardown);
313 9 : tcase_add_unchecked_fixture(tc_core, NULL,NULL);
314 9 : tcase_set_timeout(tc_core,15);
315 9 : tcase_add_test(tc_core, check_match_end_points);
316 9 : tcase_add_test(tc_core, check_extract_dir_structure);
317 9 : tcase_add_test(tc_core, check_get_repo_list);
318 9 : tcase_add_test(tc_core, check_create_repo_json_js);
319 9 : tcase_add_test(tc_core, check_init_repo_dirs);
320 9 : tcase_add_test(tc_core, check_create_repo_dirs);
321 9 : tcase_add_test(tc_core, check_update_repo_dirs);
322 :
323 9 : tcase_add_test(tc_core, check_print_skips);
324 :
325 9 : suite_add_tcase(s, tc_core);
326 9 : return s;
327 : }
328 :
329 :
330 9 : int main(void)
331 : {
332 : int number_failed;
333 : Suite *s;
334 : SRunner *sr;
335 :
336 9 : s = test_suite();
337 9 : sr = srunner_create(s);
338 9 : srunner_run_all(sr, CK_NORMAL);
339 1 : number_failed = srunner_ntests_failed(sr);
340 1 : srunner_free(sr);
341 1 : return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
342 : }
|