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