]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/cats_test.c
Add more tests on cats_test
[bacula/bacula] / bacula / src / tools / cats_test.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2011-2011 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *  Program to test CATS DB routines
30  *
31  *  
32  */
33 #define __SQL_C
34 #define BUILDING_CATS
35
36 #include "bacula.h"
37 #include "cats/cats.h"
38 #include "cats/bvfs.h"
39 #include "findlib/find.h"
40  
41 /* Local variables */
42 static B_DB *db;
43 static const char *file = "COPYRIGHT";
44 //static DBId_t fnid=0;
45 static const char *db_name = "bacula";
46 static const char *db_user = "bacula";
47 static const char *db_password = "";
48 static const char *db_host = NULL;
49 static const char *db_address = NULL;
50 static int db_port = 0;
51 static int64_t pid = 0;
52 static JCR *jcr=NULL;
53
54 #define PLINE "\n============================================================\n"
55 static void usage()
56 {
57    fprintf(stderr, _(
58 PROG_COPYRIGHT
59 "\nVersion: %s (%s)\n"
60 "       -d <nn>           set debug level to <nn>\n"
61 "       -dt               print timestamp in debug output\n"
62 "       -n <name>         specify the database name (default bacula)\n"
63 "       -u <user>         specify database user name (default bacula)\n"
64 "       -P <password      specify database password (default none)\n"
65 "       -h <host>         specify database host (default NULL)\n"
66 "       -w <working>      specify working directory\n"
67 "       -p <path>         specify path\n"
68 "       -f <file>         specify file\n"
69 "       -l <limit>        maximum tuple to fetch\n"
70 "       -q                print only errors\n"
71 "       -v                verbose\n"
72 "       -?                print this message\n\n"), 2011, VERSION, BDATE);
73    exit(1);
74 }
75
76 bool print_ok=true;
77 int _err=0;
78 int _wrn=0;
79 int _nb=0;
80
81 bool _warn(const char *file, int l, const char *op, int value, const char *label)
82 {
83    bool ret=false;
84    _nb++;
85    if (!value) {
86       _wrn++;
87       printf("WRN %.30s %s:%i on %s\n", label, file, l, op);
88    } else {
89       ret=true;
90       printf("OK  %.30s\n", label);
91    }
92    return ret;
93 }
94
95 #define warn(x, label) _warn(__FILE__, __LINE__, #x, (x), label)
96
97 bool _ok(const char *file, int l, const char *op, int value, const char *label)
98 {
99    bool ret=false;
100    _nb++;
101    if (!value) {
102       _err++;
103       printf("ERR %.30s %s:%i on %s\n", label, file, l, op);
104    } else {
105       ret=true;
106       if (print_ok) {
107          printf("OK  %.30s\n", label);
108       }
109    }
110    return ret;
111 }
112
113 #define ok(x, label) _ok(__FILE__, __LINE__, #x, (x), label)
114
115 bool _nok(const char *file, int l, const char *op, int value, const char *label)
116 {
117    bool ret=false;
118    _nb++;
119    if (value) {
120       _err++;
121       printf("ERR %.30s %s:%i on !%s\n", label, file, l, op);
122    } else {
123       ret = true;
124       if (print_ok) {
125          printf("OK  %.30s\n", label);
126       }
127    }
128    return ret;
129 }
130
131 #define nok(x, label) _nok(__FILE__, __LINE__, #x, (x), label)
132
133 int report()
134 {
135    printf("Result %i/%i OK\n", _nb - _err, _nb);
136    return _err>0;
137 }
138
139 static void cmp_pool(POOL_DBR &pr, POOL_DBR &pr2)
140 {
141    ok(pr.MaxVols == pr2.MaxVols,                "  Check Pool MaxVols");
142    ok(pr.UseOnce == pr2.UseOnce,                "  Check Pool UseOnce");
143    ok(pr.UseCatalog == pr2.UseCatalog,          "  Check Pool UseCatalog");
144    ok(pr.AcceptAnyVolume == pr2.AcceptAnyVolume,"  Check Pool AcceptAnyVolume");
145    ok(pr.AutoPrune == pr2.AutoPrune,            "  Check Pool AutoPrune");
146    ok(pr.Recycle == pr2.Recycle,                "  Check Pool Recycle");
147    ok(pr.VolRetention == pr2.VolRetention ,     "  Check Pool VolRetention");
148    ok(pr.VolUseDuration == pr2.VolUseDuration,  "  Check Pool VolUseDuration");
149    ok(pr.MaxVolJobs == pr2.MaxVolJobs,          "  Check Pool MaxVolJobs");
150    ok(pr.MaxVolFiles == pr2.MaxVolFiles,        "  Check Pool MaxVolFiles");
151    ok(pr.MaxVolBytes == pr2.MaxVolBytes,        "  Check Pool MaxVolBytes");
152    ok(!strcmp(pr.PoolType, pr2.PoolType),       "  Check Pool PoolType");
153    ok(pr.LabelType == pr2.LabelType,            "  Check Pool LabelType");
154    ok(!strcmp(pr.LabelFormat, pr2.LabelFormat), "  Check Pool LabelFormat");
155    ok(pr.RecyclePoolId == pr2.RecyclePoolId,    "  Check Pool RecyclePoolId");
156    ok(pr.ScratchPoolId == pr2.ScratchPoolId,    "  Check Pool ScratchPoolId");
157    ok(pr.ActionOnPurge == pr2.ActionOnPurge,    "  Check Pool ActionOnPurge");
158 }
159
160 static void cmp_client(CLIENT_DBR &cr, CLIENT_DBR &cr2)
161 {
162    ok(!strcmp(cr2.Name, cr.Name),           "  Check Client Name");
163    ok(!strcmp(cr2.Uname, cr.Uname),         "  Check Client Uname");
164    ok(cr.AutoPrune == cr2.AutoPrune,        "  Check Client Autoprune");
165    ok(cr.JobRetention == cr2.JobRetention,  "  Check Client JobRetention");   
166    ok(cr.FileRetention == cr2.FileRetention,"  Check Client FileRetention");
167 }
168
169 static void cmp_job(JOB_DBR &jr, JOB_DBR &jr2)
170 {
171    ok(jr.VolSessionId == jr2.VolSessionId,     "  Check VolSessionId");
172    ok(jr.VolSessionTime == jr2.VolSessionTime, "  Check VolSessionTime");
173    ok(jr.PoolId == jr2.PoolId,                 "  Check PoolId");
174    ok(jr.StartTime == jr2.StartTime,           "  Check StartTime");
175    ok(jr.EndTime == jr2.EndTime,               "  Check EndTime");
176    ok(jr.JobFiles == jr2.JobFiles,             "  Check JobFiles");
177    ok(jr.JobBytes == jr2.JobBytes,             "  Check JobBytes");
178    ok(jr.JobTDate == jr2.JobTDate,             "  Check JobTDate");
179    ok(!strcmp(jr.Job, jr2.Job),                "  Check Job");
180    ok(jr.JobStatus == jr2.JobStatus,           "  Check JobStatus");
181    ok(jr.JobType == jr2.JobType,               "  Check Type");
182    ok(jr.JobLevel == jr2.JobLevel,             "  Check Level");
183    ok(jr.ClientId == jr2.ClientId,             "  Check ClientId");
184    ok(!strcmp(jr.Name, jr2.Name),              "  Check Name");
185    ok(jr.PriorJobId == jr2.PriorJobId,         "  Check PriorJobId");
186    ok(jr.RealEndTime == jr2.RealEndTime,       "  Check RealEndTime");
187    ok(jr.JobId == jr2.JobId,                   "  Check JobId");
188    ok(jr.FileSetId == jr2.FileSetId,           "  Check FileSetId");
189    ok(jr.SchedTime == jr2.SchedTime,           "  Check SchedTime");
190    ok(jr.RealEndTime == jr2.RealEndTime,       "  Check RealEndTime");
191    ok(jr.ReadBytes == jr2.ReadBytes,           "  Check ReadBytes");
192    ok(jr.HasBase == jr2.HasBase,               "  Check HasBase");
193    ok(jr.PurgedFiles == jr2.PurgedFiles,       "  Check PurgedFiles");
194 }
195
196
197 #define aPATH "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
198 #define aFILE "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
199
200 static int list_files(void *ctx, int nb_col, char **row)
201 {
202    uint32_t *k = (uint32_t*) ctx;
203    (*k)++;
204    ok(!strcmp(row[0], aPATH aPATH aPATH aPATH "/"), "Check path");
205    ok(!strcmp(row[1], aFILE aFILE ".txt"), "Check filename");
206    ok(str_to_int64(row[2]) == 10, "Check FileIndex");
207    ok(str_to_int64(row[3]) == jcr->JobId, "Check JobId");
208    return 1;
209 }
210
211 static int count_col(void *ctx, int nb_col, char **row)
212 {
213    *((int32_t*) ctx) = nb_col;
214    return 1;
215 }
216
217 /* number of thread started */
218
219 int main (int argc, char *argv[])
220 {
221    int ch;
222    char *path=NULL, *client=NULL;
223    uint64_t limit=0;
224    bool clean=false;
225    bool full_test=false;
226    int dbtype;
227    uint32_t j;
228    char temp[20];
229    POOLMEM *buf = get_pool_memory(PM_FNAME);
230    POOLMEM *buf2 = get_pool_memory(PM_FNAME);
231    POOLMEM *buf3 = get_pool_memory(PM_FNAME);
232
233    setlocale(LC_ALL, "");
234    bindtextdomain("bacula", LOCALEDIR);
235    textdomain("bacula");
236    init_stack_dump();
237    pid = getpid();
238
239    Pmsg0(0, "Starting cats_test tool" PLINE);
240    
241    my_name_is(argc, argv, "");
242    init_msg(NULL, NULL);
243
244    OSDependentInit();
245
246    while ((ch = getopt(argc, argv, "qh:c:l:d:n:P:Su:vFw:?p:f:T")) != -1) {
247       switch (ch) {
248       case 'q':
249          print_ok = false;
250          break;
251       case 'd':                    /* debug level */
252          if (*optarg == 't') {
253             dbg_timestamp = true;
254          } else {
255             debug_level = atoi(optarg);
256             if (debug_level <= 0) {
257                debug_level = 1;
258             }
259          }
260          break;
261       case 'l':
262          limit = str_to_int64(optarg);
263          break;
264
265       case 'c':
266          client = optarg;
267          break;
268
269       case 'h':
270          db_host = optarg;
271          break;
272
273       case 'n':
274          db_name = optarg;
275          break;
276
277       case 'w':
278          working_directory = optarg;
279          break;
280
281       case 'u':
282          db_user = optarg;
283          break;
284
285       case 'P':
286          db_password = optarg;
287          break;
288
289       case 'v':
290          verbose++;
291          break;
292
293       case 'p':
294          path = optarg;
295          break;
296
297       case 'F':
298          full_test = true;
299          break;
300
301       case 'f':
302          file = optarg;
303          break;
304
305       case 'T':
306          clean = true;
307          break;
308
309       case '?':
310       default:
311          usage();
312
313       }
314    }
315    argc -= optind;
316    argv += optind;
317
318    if (argc != 0) {
319       Pmsg0(0, _("Wrong number of arguments: \n"));
320       usage();
321    }
322
323    /* TODO:
324     *  - Open DB
325     *    - With errors
326     *    - With good info
327     *    - With multiple thread in //
328     *  - Test cats.h
329     *  - Test all sql_cmds.c
330     *  - Test all sql.c (db_)
331     *  - Test all sql_create.c
332     *  - Test db_handler
333     */
334
335    jcr = new_jcr(sizeof(JCR), NULL);
336    jcr->set_JobType(JT_CONSOLE);
337    jcr->set_JobLevel(L_NONE);
338    jcr->JobStatus = JS_Running;
339    bstrncpy(jcr->Job, "**dummy**", sizeof(jcr->Job));
340    jcr->JobId = pid;      /* this is JobId on tape */
341    jcr->start_time = jcr->sched_time = time(NULL);
342
343    /* Test DB connexion */
344    Pmsg1(0, PLINE "Test DB connection \"%s\"" PLINE, db_name);
345
346    if (full_test) {
347       db = db_init(jcr /* JCR */, 
348                    NULL /* dbi driver */,
349                    db_name, db_user, db_password, db_address, db_port + 100,
350                    NULL /* db_socket */,
351                    0 /* mult_db_connections */);
352       ok(db != NULL, "Test bad connection");
353       if (!db) {
354          report();
355          exit (1);
356       }
357       nok(db_open_database(jcr, db), "Open bad Database");
358       db_close_database(jcr, db);
359    }
360
361    db = db_init(jcr /* JCR */, 
362                 NULL /* dbi driver */,
363                 db_name, db_user, db_password, db_address, db_port,
364                 NULL /* db_socket */,
365                 0 /* mult_db_connections */);
366
367    ok(db != NULL, "Test db connection");
368    if (!db) {
369       report();
370       exit (1);
371    }
372    if (!ok(db_open_database(jcr, db), "Open Database")) {
373       Pmsg1(000, _("Could not open database \"%s\".\n"), db_name);
374       Jmsg(jcr, M_FATAL, 0, _("Could not open, database \"%s\".\n"), db_name);
375       Jmsg(jcr, M_FATAL, 0, _("%s"), db_strerror(db));
376       Pmsg1(000, "%s", db_strerror(db));
377       db_close_database(jcr, db);
378       report();
379       exit (1);
380    }
381    dbtype = db_type;
382
383    /* Check if the SQL library is thread-safe */
384    db_check_backend_thread_safe();
385    ok(check_tables_version(jcr, db), "Check table version");
386    ok(db_sql_query(db, "SELECT VersionId FROM Version", 
387                    db_int_handler, &j), "SELECT VersionId");
388
389    ok(UPDATE_DB(jcr, db, (char*)"UPDATE Version SET VersionId = 1"),
390       "Update VersionId");
391    nok(check_tables_version(jcr, db), "Check table version");
392    Mmsg(buf, "UPDATE Version SET VersionId = %d", j);
393    ok(UPDATE_DB(jcr, db, buf), "Restore VersionId");
394
395    if (dbtype != SQL_TYPE_SQLITE3) {
396       ok(db_check_max_connections(jcr, db, 1), "Test min Max Connexion");
397       nok(db_check_max_connections(jcr, db, 10000), "Test max Max Connexion");
398    }
399    
400    ok(db_open_batch_connexion(jcr, db), "Opening batch connection");
401    db_close_database(jcr, jcr->db_batch);
402    jcr->db_batch = NULL;
403
404    /* ---------------------------------------------------------------- */
405    
406    uint32_t storageid=0;
407    ok(db_sql_query(db, "SELECT MIN(StorageId) FROM Storage", 
408                    db_int_handler, &storageid), "Get StorageId");
409    ok(storageid > 0, "Check StorageId");
410    if (!storageid) {
411       Pmsg0(0, "Please, run REGRESS_DEBUG=1 tests/bacula-backup-test before this test");
412       exit (1);
413    }
414
415    /* ---------------------------------------------------------------- */
416    Pmsg0(0, PLINE "Doing Basic SQL tests" PLINE);
417    ok(db_sql_query(db, "SELECT 1,2,3,4,5", count_col, &j), "Count 5 rows");
418    ok(j == 5, "Check number of columns");
419    ok(db_sql_query(db, "SELECT 1,2,3,4,5,'a','b','c','d','e'", 
420                    count_col, &j), "Count 10 rows");
421    ok(j == 10, "Check number of columns");
422
423    bsnprintf(temp, sizeof(temp), "t%lld", pid);
424    ok(db_sql_query(db, "SELECT 2", db_int_handler, &j), "Good SELECT query");
425    ok(db_sql_query(db, "SELECT 1 FROM Media WHERE VolumeName='missing'",
426                    db_int_handler, &j), "Good empty SELECT query");
427    
428    db_int64_ctx i64;
429    i64.value = 0; i64.count = 0;
430    ok(db_sql_query(db, "SELECT 1",db_int64_handler, &i64),"db_int64_handler");
431    ok(i64.value == 1, "Check db_int64_handler return");
432
433    db_list_ctx lctx;
434    ok(db_sql_query(db, "SELECT FileId FROM File ORDER By FileId LIMIT 10",
435                    db_list_handler, &lctx), "db_list_ctx");
436    ok(lctx.count == 10, "Check db_list_ctx count ");
437    ok(!strcmp(lctx.list, "1,2,3,4,5,6,7,8,9,10"), "Check db_list_ctx list");
438
439    nok(db_sql_query(db, "blabla", db_int_handler, &j), "Bad query");
440
441    Mmsg(buf, "CREATE Table %s (a int)", temp);
442    ok(db_sql_query(db, buf, NULL, NULL), "CREATE query");
443
444    Mmsg(buf, "INSERT INTO %s (a) VALUES (1)", temp);
445    ok(INSERT_DB(jcr, db, buf), "INSERT query");
446    ok(INSERT_DB(jcr, db, buf), "INSERT query");
447    ok(sql_affected_rows(db) == 1, "Check sql_affected_rows");
448
449    Mmsg(buf, "INSERT INTO aaa%s (a) VALUES (1)", temp);
450    nok(INSERT_DB(jcr, db, buf), "Bad INSERT query");
451    ok(sql_affected_rows(db) == 0, "Check sql_affected_rows");
452
453    Mmsg(buf, "UPDATE %s SET a = 2", temp);
454    ok(UPDATE_DB(jcr, db, buf), "UPDATE query");
455    ok(sql_affected_rows(db) == 2, "Check sql_affected_rows");
456
457    Mmsg(buf, "UPDATE %s SET a = 2 WHERE a = 1", temp);
458    nok(UPDATE_DB(jcr, db, buf), "Empty UPDATE query");
459
460    Mmsg(buf, "UPDATE aaa%s SET a = 2", temp);
461    nok(UPDATE_DB(jcr, db, buf), "Bad UPDATE query");
462
463    Mmsg(buf, "DELETE FROM %s", temp);
464    ok(DELETE_DB(jcr, db, buf), "DELETE query");
465    nok(DELETE_DB(jcr, db, buf), "Empty DELETE query"); /* TODO bug ? */
466       
467    Mmsg(buf, "DELETE FROM aaa%s", temp);
468    ok(DELETE_DB(jcr, db, buf), "Bad DELETE query"); /* TODO bug ? */
469    
470    Mmsg(buf, "DROP TABLE %s", temp);
471    ok(QUERY_DB(jcr, db, buf), "DROP query");
472    nok(QUERY_DB(jcr, db, buf), "Empty DROP query");
473
474    /* ---------------------------------------------------------------- */
475
476    strcpy(buf, "This string should be 'escaped'");
477    db_escape_string(jcr, db, buf2, buf, strlen(buf));
478    ok((strlen(buf) + 2) == strlen(buf2),"Quoted string should be longer");
479    Mmsg(buf, "INSERT INTO Path (Path) VALUES ('%lld-%s')", pid, buf2);
480    ok(db_sql_query(db, buf, NULL, NULL), "Inserting quoted string");
481
482    /* ---------------------------------------------------------------- */
483    Pmsg0(0, PLINE "Doing Job tests" PLINE);   
484
485    JOB_DBR jr, jr2;
486    memset(&jr, 0, sizeof(jr));
487    memset(&jr2, 0, sizeof(jr2));
488    jr.JobId = 1;
489    ok(db_get_job_record(jcr, db, &jr), "Get Job record for JobId=1");
490    ok(jr.JobFiles > 10, "Check number of files");
491    
492    jr.JobId = (JobId_t)pid;
493    Mmsg(buf, "%s-%lld", jr.Job, pid);
494    strcpy(jr.Job, buf);
495    ok(db_create_job_record(jcr, db, &jr), "Create Job record");
496    ok(db_update_job_start_record(jcr, db, &jr), "Update Start Record");
497    ok(db_update_job_end_record(jcr, db, &jr), "Update End Record");
498    jr2.JobId = jr.JobId;
499    ok(db_get_job_record(jcr, db, &jr2), "Get Job record by JobId");
500    cmp_job(jr, jr2);
501
502    memset(&jr2, 0, sizeof(jr2));
503    strcpy(jr2.Job, jr.Job);
504    ok(db_get_job_record(jcr, db, &jr2), "Get Job record by Job name");
505    cmp_job(jr, jr2);
506
507    memset(&jr2, 0, sizeof(jr2));
508    jr2.JobId = 99999;
509    nok(db_get_job_record(jcr, db, &jr2), "Get non existing Job record (JobId)");
510
511    memset(&jr2, 0, sizeof(jr2));
512    strcpy(jr2.Job, "test");
513    nok(db_get_job_record(jcr, db, &jr2), "Get non existing Job record (Job)");
514
515    /* ---------------------------------------------------------------- */
516
517    ATTR_DBR ar;
518    memset(&ar, 0, sizeof(ar));
519    Mmsg(buf2, aPATH aPATH aPATH aPATH "/" aFILE aFILE ".txt");
520    ar.fname = buf2;
521    Mmsg(buf3, "gD ImIZ IGk B Po Po A A A JY BNNvf5 BNKzS7 BNNuwC A A C");
522    ar.attr = buf3;
523    ar.FileIndex = 10;
524    ar.Stream = STREAM_UNIX_ATTRIBUTES;
525    ar.FileType = FT_REG;
526    jcr->JobId = ar.JobId = jr.JobId;
527    jcr->JobStatus = JS_Running;
528    ok(db_create_file_attributes_record(jcr, db, &ar), "Inserting Filename");
529    ok(db_write_batch_file_records(jcr), "Commit batch session");
530    Mmsg(buf, "SELECT FileIndex FROM File WHERE JobId=%lld",(int64_t)jcr->JobId);
531    ok(db_sql_query(db, buf, db_int_handler, &j), "Get Inserted record");
532    ok(j == ar.FileIndex, "Check FileIndex");
533    Mmsg(buf, "SELECT COUNT(1) FROM File WHERE JobId=%lld",(int64_t)jcr->JobId);
534    ok(db_sql_query(db, buf, db_int_handler, &j), "List records");
535    ok(j == 1, "Check batch session records");
536    j = 0;
537    Mmsg(buf, "%lld", (uint64_t)jcr->JobId);
538    ok(db_get_file_list(jcr, jcr->db_batch, buf, false, false, list_files, &j),
539       "List files with db_get_file_list()");
540    ok(j == 1, "Check db_get_file_list results");
541    /* ---------------------------------------------------------------- */
542
543    Pmsg0(0, PLINE "Doing Client tests" PLINE);
544    CLIENT_DBR cr, cr2;
545    memset(&cr, 0, sizeof(cr));
546    memset(&cr2, 0, sizeof(cr2));
547
548    cr.AutoPrune = 1;
549    cr.FileRetention = 10;
550    cr.JobRetention = 15;
551    bsnprintf(cr.Name, sizeof(cr.Name), "client-%lld-fd", pid);
552    bsnprintf(cr.Uname, sizeof(cr.Uname), "uname-%lld", pid);
553
554    ok(db_create_client_record(jcr, db, &cr), "db_create_client_record()");
555    ok(cr.ClientId > 0, "Check ClientId");
556
557    cr2.ClientId = cr.ClientId; /* Save it */
558    cr.ClientId = 0;
559
560    Pmsg0(0, "Search client by ClientId\n");
561    ok(db_create_client_record(jcr, db, &cr),"Should get the client record");
562    ok(cr.ClientId == cr2.ClientId,           "Check if ClientId is the same");
563
564    ok(db_get_client_record(jcr, db, &cr2), "Search client by ClientId");
565    cmp_client(cr, cr2);
566    
567    Pmsg0(0, "Search client by Name\n");
568    memset(&cr2, 0, sizeof(cr2));
569    strcpy(cr2.Name, cr.Name);
570    ok(db_get_client_record(jcr, db, &cr2),"Search client by Name");
571    cmp_client(cr, cr2);
572
573    Pmsg0(0, "Search non existing client by Name\n");
574    memset(&cr2, 0, sizeof(cr2));
575    bsnprintf(cr2.Name, sizeof(cr2.Name), "hollow-client-%lld-fd", pid);
576    nok(db_get_client_record(jcr, db, &cr2), "Search non existing client");
577    ok(cr2.ClientId == 0, "Check ClientId after failed search");
578
579    cr.AutoPrune = 0;
580    strcpy(cr.Uname, "NewUname");
581    ok(db_update_client_record(jcr, db, &cr), "Update Client record");
582    memset(&cr2, 0, sizeof(cr2));
583    cr2.ClientId = cr.ClientId;
584    ok(db_get_client_record(jcr, db, &cr2),"Search client by ClientId");
585    cmp_client(cr, cr2);
586
587    int nb, i;
588    uint32_t *ret_ids;
589    ok(db_get_client_ids(jcr, db, &nb, &ret_ids), "Get Client Ids");
590    ok(nb > 0, "Should find at least 1 Id");
591    for (i = 0; i < nb; i++) {
592       if (ret_ids[i] == cr2.ClientId) {
593          break;
594       }
595    }
596    ok(i < nb, "Check if ClientId was found");
597
598    /* ---------------------------------------------------------------- */
599    Pmsg0(0, PLINE "Doing Pool tests" PLINE);
600    POOL_DBR pr, pr2;
601    memset(&pr, 0, sizeof(pr));
602    memset(&pr2, 0, sizeof(pr2));
603    
604    bsnprintf(pr.Name, sizeof(pr.Name), "pool-%lld", pid);
605    pr.MaxVols = 10;
606    pr.UseOnce = 0;
607    pr.UseCatalog = true;
608    pr.AcceptAnyVolume = true;
609    pr.AutoPrune = true;
610    pr.Recycle = true;
611    pr.VolRetention = 1000;
612    pr.VolUseDuration = 1000;
613    pr.MaxVolJobs = 100;
614    pr.MaxVolFiles = 1000;
615    pr.MaxVolBytes = 1000000;
616    strcpy(pr.PoolType, "Backup");
617    pr.LabelType = 0;
618    pr.LabelFormat[0] = 0;
619    pr.RecyclePoolId = 0;
620    pr.ScratchPoolId = 0;
621    pr.ActionOnPurge = 1;
622    
623    ok(db_create_pool_record(jcr, db, &pr), "db_create_pool_record()");
624    ok(pr.PoolId > 0, "Check PoolId");
625    
626    pr2.PoolId = pr.PoolId;
627    pr.PoolId = 0;
628
629    Pmsg0(0, "Search pool by PoolId\n");
630    nok(db_create_pool_record(jcr, db, &pr),"Can't create pool twice");
631    ok(db_get_pool_record(jcr, db, &pr2), "Search pool by PoolId");
632    cmp_pool(pr, pr2);
633
634    pr2.MaxVols++;
635    pr2.AutoPrune = false;
636    pr2.Recycle = false;
637    pr2.VolRetention++;
638    pr2.VolUseDuration++;
639    pr2.MaxVolJobs++;
640    pr2.MaxVolFiles++;
641    pr2.MaxVolBytes++;
642    strcpy(pr2.PoolType, "Restore");
643    strcpy(pr2.LabelFormat, "VolFormat");
644    pr2.RecyclePoolId = 0;
645    pr2.ScratchPoolId = 0;
646    pr2.ActionOnPurge = 2;
647
648    ok(db_update_pool_record(jcr, db, &pr2), "Update Pool record");
649    memset(&pr, 0, sizeof(pr));
650    pr.PoolId = pr2.PoolId;
651    ok(db_get_pool_record(jcr, db, &pr), "Search pool by PoolId");
652    cmp_pool(pr, pr2);
653
654    ok(db_delete_pool_record(jcr, db, &pr), "Delete Pool");
655    nok(db_delete_pool_record(jcr, db, &pr), "Delete non existing Pool");
656    nok(db_update_pool_record(jcr, db, &pr), "Update non existing Pool");
657    ok(db_create_pool_record(jcr, db, &pr), "Recreate Pool");
658
659    /* ---------------------------------------------------------------- */
660    Pmsg0(0, PLINE "Doing Media tests" PLINE);
661    
662    MEDIA_DBR mr, mr2;
663    memset(&mr, 0, sizeof(mr));
664    memset(&mr2, 0, sizeof(mr2));
665    
666    bsnprintf(mr.VolumeName, sizeof(mr.VolumeName), "media-%lld", pid);
667    bsnprintf(mr.MediaType, sizeof(mr.MediaType), "type-%lld", pid);
668
669    /* from set_pool_dbr_defaults_in_media_dbr(&mr, &pr);  */
670    mr.PoolId = pr.PoolId;
671    bstrncpy(mr.VolStatus, NT_("Append"), sizeof(mr.VolStatus));
672    mr.Recycle = pr.Recycle;
673    mr.VolRetention = pr.VolRetention;
674    mr.VolUseDuration = pr.VolUseDuration;
675    mr.ActionOnPurge = pr.ActionOnPurge;
676    mr.RecyclePoolId = pr.RecyclePoolId;
677    mr.MaxVolJobs = pr.MaxVolJobs;
678    mr.MaxVolFiles = pr.MaxVolFiles;
679    mr.MaxVolBytes = pr.MaxVolBytes;
680    mr.LabelType = pr.LabelType;
681    mr.Enabled = 1;
682
683    mr.VolCapacityBytes = 1000;
684    mr.Slot = 1;
685    mr.VolBytes = 1000;
686    mr.InChanger = 1;
687    mr.VolReadTime = 10000;
688    mr.VolWriteTime = 99999;
689    mr.StorageId = 0;
690    mr.DeviceId = 0;
691    mr.LocationId = 0;
692    mr.ScratchPoolId = 0;
693    mr.RecyclePoolId = 0;
694
695    ok(db_create_media_record(jcr, db, &mr), "Create Media");
696    nok(db_create_media_record(jcr, db, &mr), "Create Media twice");
697
698    /* ---------------------------------------------------------------- */
699    Pmsg0(0, PLINE "Doing ... tests" PLINE);
700    
701    db_close_database(jcr, db);
702    report();
703    free_pool_memory(buf);
704    free_pool_memory(buf2);
705    free_pool_memory(buf3);
706    return 0;
707 }