The start_up routine sets an upper-limit on CPU seconds so that
[idzebra-moved-to-github.git] / test / api / testlib.c
1 /* $Id: testlib.c,v 1.22 2005-06-14 20:02:30 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 /** testlib - utilities for the api tests */
24
25 #include <sys/time.h>
26 #include <sys/resource.h>
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #include <assert.h>
32 #include <yaz/log.h>
33 #include <yaz/pquery.h>
34 #include <idzebra/api.h>
35 #include "testlib.h"
36
37 /** start_log: open a log file */
38 /*    FIXME - parse command line arguments to set log levels etc */
39 int log_level=0; /* not static, t*.c may use it */
40
41 void start_log(int argc, char **argv)
42 {
43     int cmd_level = 0;
44     char logname[2048];
45     if (!argv) 
46         return;
47     if (!argv[0])
48         return;
49     sprintf(logname, "%s.log", argv[0]);
50     yaz_log_init_file(logname);
51     log_level = yaz_log_mask_str_x(argv[0], 0);
52     if (argc >= 2)
53         cmd_level |= yaz_log_mask_str_x(argv[1], 0);
54     yaz_log_init_level(YLOG_DEFAULT_LEVEL | log_level | cmd_level);
55     yaz_log(log_level, "starting %s", argv[0]);
56 }
57
58 /** 
59  * start_up : do common start things, and a zebra_start
60  *    - nmem_init
61  *    - build the name of logfile from argv[0], and open it
62  *      if no argv passed, do not open a log
63  *    - read zebra.cfg from env var srcdir if it exists; otherwise current dir 
64  *      default to zebra.cfg, if no name is given
65  */
66 ZebraService start_up(char *cfgname, int argc, char **argv)
67 {
68     struct rlimit rlim;
69     rlim.rlim_cur = 20;
70     rlim.rlim_max = 20;
71     setrlimit(RLIMIT_CPU, &rlim);
72     nmem_init();
73     start_log(argc, argv);
74     return start_service(cfgname);
75 }
76
77 /**
78  * get_srcdir: return env srcdir or . (if does does not exist)
79  */
80 const char *get_srcdir()
81 {
82     const char *srcdir = getenv("srcdir");
83     if (!srcdir || ! *srcdir)
84         srcdir=".";
85     return srcdir;
86
87 }
88 /** start_service - do a zebra_start with a decent config name */
89 ZebraService start_service(char *cfgname)
90 {
91     char cfg[256];
92     const char *srcdir = get_srcdir();
93     ZebraService zs;
94     if (!cfgname || ! *cfgname )
95         cfgname="zebra.cfg";
96
97     sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
98     zs=zebra_start(cfg);
99     if (!zs)
100     {
101         printf("zebra_start failed, probably because missing config file \n"
102                "check %s\n", cfg);
103         exit(9);
104     }
105     return zs;
106 }
107
108
109 /** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
110 int close_down(ZebraHandle zh, ZebraService zs, int retcode)
111 {
112     if (zh)
113         zebra_close(zh);
114     if (zs)
115         zebra_stop(zs);
116
117     if (retcode)
118         yaz_log(log_level,"========= Exiting with return code %d", retcode);
119     else
120         yaz_log(log_level,"========= All tests OK");
121     nmem_exit();
122     xmalloc_trav("x");
123     return retcode;
124 }
125
126 /** inits the database and inserts test data */
127
128 void init_data(ZebraHandle zh, const char **recs)
129 {
130     int i;
131     char *addinfo;
132     assert(zh);
133     zebra_select_database(zh, "Default");
134     yaz_log(log_level, "going to call init");
135     i = zebra_init(zh);
136     yaz_log(log_level, "init returned %d", i);
137     if (i) 
138     {
139         printf("init failed with %d\n",i);
140         zebra_result(zh, &i, &addinfo);
141         printf("  Error %d   %s\n", i, addinfo);
142         exit(1);
143     }
144     if (recs)
145     {
146         zebra_begin_trans (zh, 1);
147         for (i = 0; recs[i]; i++)
148             zebra_add_record(zh, recs[i], strlen(recs[i]));
149         zebra_end_trans(zh);
150         zebra_commit(zh);
151     }
152
153 }
154
155 int do_query_x(int lineno, ZebraHandle zh, const char *query, zint exphits,
156                int experror)
157 {
158     ODR odr;
159     YAZ_PQF_Parser parser;
160     Z_RPNQuery *rpn;
161     const char *setname="rsetname";
162     zint hits;
163     ZEBRA_RES rc;
164
165     yaz_log(log_level, "======================================");
166     yaz_log(log_level, "qry[%d]: %s", lineno, query);
167     odr = odr_createmem (ODR_DECODE);    
168
169     parser = yaz_pqf_create();
170     rpn = yaz_pqf_parse(parser, odr, query);
171     yaz_pqf_destroy(parser);
172     if (!rpn) {
173         printf("Error: Parse failed \n%s\n", query);
174         exit(1);
175     }
176     rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
177     if (experror)
178     {
179         int code;
180         if (rc != ZEBRA_FAIL)
181         {
182             printf("Error: search returned %d (OK), but error was expected\n"
183                    "%s\n",  rc, query);
184             exit(1);
185         }
186         code = zebra_errCode(zh);
187         if (code != experror)
188         {
189             printf("Error: search returned error code %d, but error %d was "
190                    "expected\n%s\n",
191                    code, experror, query);
192             exit(1);
193         }
194     }
195     else
196     {
197         if (rc == ZEBRA_FAIL) {
198             int code = zebra_errCode(zh);
199             printf("Error: search returned %d. Code %d\n%s\n", rc, 
200                    code, query);
201             exit (1);
202         }
203         if (exphits != -1 && hits != exphits) {
204             printf("Error: search returned " ZINT_FORMAT 
205                    " hits instead of " ZINT_FORMAT "\n%s\n",
206                    hits, exphits, query);
207             exit (1);
208         }
209     }
210     odr_destroy (odr);
211     return hits;
212 }
213
214
215 int do_query(int lineno, ZebraHandle zh, const char *query, zint exphits)
216 {
217     return do_query_x(lineno, zh, query, exphits, 0);
218 }
219
220 void do_scan(int lineno, ZebraHandle zh, const char *query,
221              int pos, int num,
222              int exp_pos, int exp_num, int exp_partial,
223              const char **exp_entries)
224 {
225     ODR odr = odr_createmem(ODR_ENCODE);
226     ZebraScanEntry *entries = 0;
227     int partial = -123;
228     ZEBRA_RES res;
229
230     yaz_log(log_level, "======================================");
231     yaz_log(log_level, "scan[%d]: pos=%d num=%d %s", lineno, pos, num, query);
232
233     res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial);
234     if (res != ZEBRA_OK)
235     {
236         printf("Error: scan returned %d (FAIL), but no error was expected\n"
237                "%s\n",  res, query);
238         exit(1);
239     }
240     else
241     {
242         int fails = 0;
243         if (partial == -123)
244         {
245             printf("Error: scan returned OK, but partial was not set\n"
246                    "%s\n", query);
247             fails++;
248         }
249         if (partial != exp_partial)
250         {
251             printf("Error: scan returned OK, with partial/expected %d/%d\n"
252                    "%s\n", partial, exp_partial, query);
253             fails++;
254         }
255         if (num != exp_num)
256         {
257             printf("Error: scan returned OK, with num/expected %d/%d\n"
258                    "%s\n", num, exp_num, query);
259             fails++;
260         }
261         if (pos != exp_pos)
262         {
263             printf("Error: scan returned OK, with pos/expected %d/%d\n"
264                    "%s\n", pos, exp_pos, query);
265             fails++;
266         }
267         if (fails)
268             exit(1);
269         fails = 0;
270         if (exp_entries)
271         {
272             int i;
273             for (i = 0; i<num; i++)
274             {
275                 if (strcmp(exp_entries[i], entries[i].term))
276                 {
277                     printf("Error: scan OK, but entry %d term/exp %s/%s\n"
278                            "%s\n",
279                            i, entries[i].term, exp_entries[i], query);
280                     fails++;
281                 }
282             }
283         }
284         if (fails)
285             exit(0);
286     }
287     odr_destroy(odr);
288 }
289
290 /** 
291  * makes a query, checks number of hits, and for the first hit, that 
292  * it contains the given string, and that it gets the right score
293  */
294 void ranking_query(int lineno, ZebraHandle zh, char *query, 
295                    int exphits, char *firstrec, int firstscore)
296 {
297     ZebraRetrievalRecord retrievalRecord[10];
298     ODR odr_output = odr_createmem (ODR_ENCODE);    
299     const char *setname="rsetname";
300     int hits;
301     int rc;
302     int i;
303         
304     hits = do_query(lineno, zh, query, exphits);
305
306     for (i = 0; i<10; i++)
307         retrievalRecord[i].position = i+1;
308
309     rc = zebra_records_retrieve (zh, odr_output, setname, 0,
310                                  VAL_TEXT_XML, hits, retrievalRecord);
311     
312     if (rc)
313     {
314         printf("Error: retrieve returned %d \n%s\n",rc,query);
315         exit (1);
316     }
317
318     if (!strstr(retrievalRecord[0].buf, firstrec))
319     {
320         printf("Error: Got the wrong record first\n");
321         printf("Expected '%s' but got\n", firstrec);
322         printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
323         exit(1);
324     }
325     
326     if (retrievalRecord[0].score != firstscore)
327     {
328         printf("Error: first rec got score %d instead of %d\n",
329                retrievalRecord[0].score, firstscore);
330         exit(1);
331     }
332     odr_destroy (odr_output);
333 }
334
335 void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
336                 zint *ids)
337 {
338     ZebraMetaRecord *meta;
339     ODR odr_output = odr_createmem (ODR_ENCODE);    
340     const char *setname="rsetname";
341     zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
342     int hits;
343     int i;
344         
345     hits = do_query(lineno, zh, query, exphits);
346     
347     for (i = 0; i<exphits; i++)
348         positions[i] = i+1;
349
350     meta = zebra_meta_records_create (zh, setname,  exphits, positions);
351     
352     if (!meta)
353     {
354         printf("Error: retrieve returned error\n%s\n", query);
355         exit (1);
356     }
357
358     for (i = 0; i<exphits; i++)
359     {
360         if (meta[i].sysno != ids[i])
361         {
362             printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
363                    ids[i], meta[i].sysno);
364             exit(1);
365         }
366     }
367     zebra_meta_records_destroy(zh, meta, exphits);
368     odr_destroy (odr_output);
369     free(positions);
370 }
371
372 struct finfo {
373     const char *name;
374     int occurred;
375 };
376
377 static void filter_cb(void *cd, const char *name)
378 {
379     struct finfo *f = (struct finfo*) cd;
380     if (!strcmp(f->name, name))
381         f->occurred = 1;
382 }
383
384 void check_filter(ZebraService zs, const char *name)
385 {
386     struct finfo f;
387
388     f.name = name;
389     f.occurred = 0;
390     zebra_filter_info(zs, &f, filter_cb);
391     if (!f.occurred)
392     {
393         yaz_log(YLOG_WARN, "Filter %s does not exist.", name);
394         exit(0);
395     }
396 }
397
398
399
400