1 #include "../../test.h"
2 #include "../../builders/build.h"
3 #include "../../../src/action.h"
4 #include "../../../src/alloc.h"
5 #include "../../../src/asfd.h"
6 #include "../../../src/bu.h"
7 #include "../../../src/cstat.h"
8 #include "../../../src/iobuf.h"
9 #include "../../../src/client/monitor/json_input.h"
10 #include "../../../src/client/monitor/lline.h"
11 #include "../../../src/client/monitor/sel.h"
12 #include "../../../src/client/monitor/status_client_ncurses.h"
13 #include "../../builders/build_asfd_mock.h"
14
15 #define SRC_DIR TOP_SRCDIR "/utest/json_output"
16
START_TEST(test_json_error)17 START_TEST(test_json_error)
18 {
19 struct sel *sel;
20 struct asfd *asfd;
21 fail_unless((asfd=asfd_alloc())!=NULL);
22 fail_unless((asfd->rbuf=iobuf_alloc())!=NULL);
23 fail_unless((sel=sel_alloc())!=NULL);
24 fail_unless(json_input(asfd, sel)==-1);
25 json_input_free();
26 sel_free(&sel);
27 asfd_free(&asfd);
28 alloc_check();
29 }
30 END_TEST
31
32 #define CHUNK_SIZE 10
33
setup_tz(void)34 static char *setup_tz(void)
35 {
36 char *tz;
37 if((tz=getenv("TZ")))
38 fail_unless((tz=strdup_w(tz, __func__))!=NULL);
39 setenv("TZ", "UTC-10", 1);
40 return tz;
41 }
42
tear_down_tz(char ** tz)43 static void tear_down_tz(char **tz)
44 {
45 if(tz && *tz)
46 {
47 setenv("TZ", *tz, 1);
48 free_w(tz);
49 }
50 else
51 unsetenv("TZ");
52 }
53
do_read_in_file(const char * path,struct sel * sel,int expected_ret)54 static void do_read_in_file(const char *path, struct sel *sel, int expected_ret)
55 {
56 int lastret=-1;
57 struct fzp *fzp;
58 struct asfd *asfd;
59 char buf[CHUNK_SIZE+1];
60
61 fail_unless((asfd=asfd_alloc())!=NULL);
62 fail_unless((asfd->rbuf=iobuf_alloc())!=NULL);
63 asfd->rbuf->buf=buf;
64 fail_unless((fzp=fzp_open(path, "rb"))!=NULL);
65 while(1)
66 {
67 if((asfd->rbuf->len=fzp_read(fzp,
68 asfd->rbuf->buf, CHUNK_SIZE))<=0)
69 break;
70 asfd->rbuf->buf[asfd->rbuf->len]='\0';
71 switch((lastret=json_input(asfd, sel)))
72 {
73 case 0: continue;
74 case 1:
75 case 2: break;
76 default: break;
77 }
78 }
79 fail_unless(lastret==expected_ret);
80 fzp_close(&fzp);
81 asfd->rbuf->buf=NULL;
82 asfd_free(&asfd);
83 }
84
read_in_file(const char * path,int times,int expected_ret)85 static struct sel *read_in_file(const char *path, int times, int expected_ret)
86 {
87 int i;
88 struct sel *sel;
89 fail_unless((sel=sel_alloc())!=NULL);
90 for(i=0; i<times; i++)
91 do_read_in_file(path, sel, expected_ret);
92 json_input_free();
93 return sel;
94 }
95
tear_down(struct sel ** sel,char ** tz)96 static void tear_down(struct sel **sel, char **tz)
97 {
98 sel_free(sel);
99 tear_down_tz(tz);
100 alloc_check();
101 }
102
do_test_json_warning(int times)103 static void do_test_json_warning(int times)
104 {
105 char *tz;
106 struct sel *sel;
107 tz=setup_tz();
108 fail_unless((sel=read_in_file(SRC_DIR "/warning", times,
109 /*expected_ret*/2))!=NULL);
110 fail_unless(json_input_get_warnings()!=NULL);
111 json_input_clear_warnings();
112 tear_down(&sel, &tz);
113 }
114
START_TEST(test_json_warning)115 START_TEST(test_json_warning)
116 {
117 do_test_json_warning(1);
118 do_test_json_warning(4);
119 }
120 END_TEST
121
do_test_json_empty(int times)122 static void do_test_json_empty(int times)
123 {
124 char *tz=NULL;
125 struct sel *sel;
126 fail_unless((sel=read_in_file(SRC_DIR "/empty", times,
127 /*expected_ret*/1))!=NULL);
128 fail_unless(sel->clist==NULL);
129 tear_down(&sel, &tz);
130 }
131
START_TEST(test_json_empty)132 START_TEST(test_json_empty)
133 {
134 do_test_json_empty(1);
135 do_test_json_empty(4);
136 }
137 END_TEST
138
do_test_json_clients(int times)139 static void do_test_json_clients(int times)
140 {
141 char *tz;
142 struct sel *sel;
143 const char *cnames[] ={"cli1", "cli2", "cli3", NULL};
144 tz=setup_tz();
145 fail_unless((sel=read_in_file(SRC_DIR "/clients", times,
146 /*expected_ret*/1))!=NULL);
147 fail_unless(sel->clist!=NULL);
148 assert_cstat_list(sel->clist, cnames);
149 tear_down(&sel, &tz);
150 }
151
START_TEST(test_json_clients)152 START_TEST(test_json_clients)
153 {
154 do_test_json_clients(1);
155 do_test_json_clients(4);
156 }
157 END_TEST
158
159 static struct sd sd1[] = {
160 { "0000001 1971-01-01 10:00:00 +1000", 1, 1, BU_DELETABLE|BU_CURRENT },
161 };
162
assert_bu_minimal(struct bu * bu,struct sd * s)163 static void assert_bu_minimal(struct bu *bu, struct sd *s)
164 {
165 const char *sd_timestamp;
166 fail_unless(bu!=NULL);
167 fail_unless(s->bno==bu->bno);
168 fail_unless(s->flags==bu->flags);
169 fail_unless((sd_timestamp=strchr(s->timestamp, ' '))!=NULL);
170 sd_timestamp++;
171 ck_assert_str_eq(sd_timestamp, bu->timestamp);
172 }
173
do_test_json_clients_with_backup(const char * path,struct sd * sd_current,struct sd * sd_working,int times)174 static void do_test_json_clients_with_backup(const char *path,
175 struct sd *sd_current, struct sd *sd_working, int times)
176 {
177 char *tz;
178 struct cstat *c;
179 struct sel *sel;
180 const char *cnames[] ={"cli1", "cli2", "cli3", NULL};
181 tz=setup_tz();
182 fail_unless((sel=read_in_file(path, times,
183 /*expected_ret*/1))!=NULL);
184 fail_unless(sel->clist!=NULL);
185 assert_cstat_list(sel->clist, cnames);
186 for(c=sel->clist; c; c=c->next)
187 {
188 if(sd_current) assert_bu_minimal(c->bu, sd_current);
189 if(sd_working) assert_bu_minimal(c->bu->next, sd_working);
190 }
191 tear_down(&sel, &tz);
192 }
193
START_TEST(test_json_clients_with_backup)194 START_TEST(test_json_clients_with_backup)
195 {
196 const char *path=SRC_DIR "/clients_with_backup";
197 do_test_json_clients_with_backup(path, &sd1[0], NULL, 1);
198 // do_test_json_clients_with_backup(path, &sd1[0], NULL, 4);
199 }
200 END_TEST
201
202 static struct sd sd5[] = {
203 { "0000005 1971-01-05 10:00:00 +1000", 5, 5, BU_CURRENT|BU_MANIFEST}
204 };
205
START_TEST(test_json_clients_with_backups)206 START_TEST(test_json_clients_with_backups)
207 {
208 const char *path=SRC_DIR "/clients_with_backups";
209 do_test_json_clients_with_backup(path, &sd5[0], NULL, 1);
210 do_test_json_clients_with_backup(path, &sd5[0], NULL, 4);
211 }
212 END_TEST
213
214 static struct sd sd23w[] = {
215 { "0000002 1971-01-02 10:00:00 +1000", 2, 2, BU_CURRENT|BU_MANIFEST },
216 { "0000003 1971-01-03 10:00:00 +1000", 3, 3, BU_WORKING },
217 };
218
START_TEST(test_json_clients_with_backups_working)219 START_TEST(test_json_clients_with_backups_working)
220 {
221 const char *path=SRC_DIR "/clients_with_backups_working";
222 do_test_json_clients_with_backup(path, &sd23w[1], &sd23w[0], 1);
223 do_test_json_clients_with_backup(path, &sd23w[1], &sd23w[0], 4);
224 }
225 END_TEST
226
227 static struct sd sd23f[] = {
228 { "0000002 1971-01-02 10:00:00 +1000", 2, 2, BU_CURRENT|BU_MANIFEST },
229 { "0000003 1971-01-03 10:00:00 +1000", 3, 3, BU_FINISHING },
230 };
231
START_TEST(test_json_clients_with_backups_finishing)232 START_TEST(test_json_clients_with_backups_finishing)
233 {
234 const char *path=SRC_DIR "/clients_with_backups_finishing";
235 do_test_json_clients_with_backup(path, &sd23f[1], &sd23f[0], 1);
236 do_test_json_clients_with_backup(path, &sd23f[1], &sd23f[0], 4);
237 }
238 END_TEST
239
240 static struct sd sd12345[] = {
241 { "0000001 1971-01-01 10:00:00 +1000", 1, 1, BU_DELETABLE|BU_MANIFEST },
242 { "0000002 1971-01-02 10:00:00 +1000", 2, 2, 0 },
243 { "0000003 1971-01-03 10:00:00 +1000", 3, 3, BU_HARDLINKED },
244 { "0000004 1971-01-04 10:00:00 +1000", 4, 4, BU_DELETABLE },
245 { "0000005 1971-01-05 10:00:00 +1000", 5, 5, BU_CURRENT|BU_MANIFEST }
246 };
247
do_test_json_client_specific(const char * path,struct sd * sd,int len,int times)248 static void do_test_json_client_specific(const char *path,
249 struct sd *sd, int len, int times)
250 {
251 int s;
252 struct sel *sel;
253 struct bu *bu;
254 char *tz;
255 const char *cnames[] ={"cli2", NULL};
256 tz=setup_tz();
257 fail_unless((sel=read_in_file(path, times,
258 /*expected_ret*/1))!=NULL);
259 fail_unless(sel->clist!=NULL);
260 assert_cstat_list(sel->clist, cnames);
261
262 for(bu=sel->clist->bu, s=len-1; bu && s>=0; bu=bu->next, s--)
263 assert_bu_minimal(bu, &sd[s]);
264 fail_unless(s==-1);
265 fail_unless(!bu);
266 tear_down(&sel, &tz);
267 }
268
START_TEST(test_json_client_specific)269 START_TEST(test_json_client_specific)
270 {
271 const char *path=SRC_DIR "/client_specific";
272 do_test_json_client_specific(path, sd12345, ARR_LEN(sd12345), 1);
273 do_test_json_client_specific(path, sd12345, ARR_LEN(sd12345), 4);
274 }
275 END_TEST
276
suite_client_monitor_json_input(void)277 Suite *suite_client_monitor_json_input(void)
278 {
279 Suite *s;
280 TCase *tc_core;
281
282 s=suite_create("client_monitor_json_input");
283
284 tc_core=tcase_create("Core");
285
286 tcase_add_test(tc_core, test_json_error);
287 tcase_add_test(tc_core, test_json_warning);
288 tcase_add_test(tc_core, test_json_empty);
289 tcase_add_test(tc_core, test_json_clients);
290 tcase_add_test(tc_core, test_json_clients_with_backup);
291 tcase_add_test(tc_core, test_json_clients_with_backups);
292 tcase_add_test(tc_core, test_json_clients_with_backups_working);
293 tcase_add_test(tc_core, test_json_clients_with_backups_finishing);
294 tcase_add_test(tc_core, test_json_client_specific);
295 suite_add_tcase(s, tc_core);
296
297 return s;
298 }
299