~jamesodhunt/upstart/stateful-reexec-dbus-connections

« back to all changes in this revision

Viewing changes to init/tests/test_event_operator.c

Merge of test case from Steve showing event_operator issue.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#include "event_operator.h"
30
30
#include "blocked.h"
 
31
#include "parse_job.h"
 
32
#include "conf.h"
31
33
 
32
34
 
33
35
void
1320
1322
        event_poll ();
1321
1323
}
1322
1324
 
 
1325
void
 
1326
test_operator_serialisation (void)
 
1327
{
 
1328
        JobClass        *job = NULL;
 
1329
        EventOperator   *oper1, *oper2;
 
1330
        char            *oper1_string;
 
1331
        char            *oper2_string;
 
1332
 
 
1333
        struct test_operator {
 
1334
                char *description;
 
1335
                char *value;
 
1336
        };
 
1337
 
 
1338
        struct test_operator test_operators[] = {
 
1339
                { "with simple operator", "started JOB=second_test\n" },
 
1340
                { "with or operator", "runlevel [23] or not-container" },
 
1341
                { "with and operator", "(local-filesystems and net-device-up)" },
 
1342
                { "with complex operator", "runlevel [23] and (\n"
 
1343
"not-container or\n"
 
1344
"container CONTAINER=lxc or\n"
 
1345
"container CONTAINER=lxc-libvirt)" },
 
1346
                { NULL, NULL }
 
1347
        };
 
1348
        struct test_operator *test;
 
1349
 
 
1350
        /* Check correct serialisation/deserialisation of complex
 
1351
         * operators, to be sure that we can generate a correct round-trip
 
1352
         * during re-exec.
 
1353
         */
 
1354
        TEST_FUNCTION ("event_operator_serialisation");
 
1355
 
 
1356
        nih_error_init ();
 
1357
        job_class_init ();
 
1358
        conf_init ();
 
1359
 
 
1360
        job = job_class_new (NULL, "operator_test", NULL);
 
1361
 
 
1362
        for (test = test_operators; test && test->value; test++)
 
1363
        {
 
1364
                TEST_FEATURE (test->description);
 
1365
 
 
1366
                oper1 = parse_on_simple (job, "start", test->value);
 
1367
                /* Ideally we would exercise allocation here,
 
1368
                 * but NIH_MUST is being used.
 
1369
                 */
 
1370
                oper1_string = event_operator_collapse (oper1);
 
1371
                nih_message ("oper1_string: '%s' (from '%s')", oper1_string, test->value);
 
1372
 
 
1373
                oper2 = parse_on_simple (job, "start", oper1_string);
 
1374
 
 
1375
                oper2_string = event_operator_collapse (oper2);
 
1376
                nih_message ("oper2_string: '%s' (from '%s')", oper2_string, oper1_string);
 
1377
 
 
1378
                {
 
1379
                        char *str = "(((runlevel [23] and not-container) or container CONTAINER=lxc) or container CONTAINER=lxc-libvirt)";
 
1380
                        EventOperator   *oper3;
 
1381
                        char            *oper3_string;
 
1382
 
 
1383
                        oper3 = parse_on_simple (job, "start", str);
 
1384
 
 
1385
                        oper3_string = event_operator_collapse (oper3);
 
1386
                        nih_message ("oper3_string: '%s' (from '%s')", oper3_string, str);
 
1387
                }
 
1388
                {
 
1389
                        char *str = "runlevel [23] and not-container or container CONTAINER=lxc or container CONTAINER=lxc-libvirt";
 
1390
                        EventOperator   *oper4;
 
1391
                        char            *oper4_string;
 
1392
 
 
1393
                        oper4 = parse_on_simple (job, "start", str);
 
1394
 
 
1395
                        oper4_string = event_operator_collapse (oper4);
 
1396
                        nih_message ("oper4_string: '%s' (from '%s')", oper4_string, str);
 
1397
                }
 
1398
 
 
1399
                TEST_EQ (oper1->value, oper2->value);
 
1400
                TEST_EQ (oper1->type, oper2->type);
 
1401
                if (oper1->name || oper2->name) {
 
1402
                        TEST_EQ_STR (oper1->name, oper2->name);
 
1403
                }
 
1404
 
 
1405
                /* We don't get to use the macros here because we need to
 
1406
                 * walk both trees in tandem */
 
1407
                for (NihTree *iter1 = nih_tree_next (&oper1->node, NULL),
 
1408
                             *iter2 = nih_tree_next (&oper2->node, NULL);
 
1409
                     iter1 != NULL && iter2 != NULL;
 
1410
                     iter1 = nih_tree_next (&oper1->node, iter1),
 
1411
                     iter2 = nih_tree_next (&oper2->node, iter2))
 
1412
                {
 
1413
                        EventOperator *oper1_sub = (EventOperator *)iter1;
 
1414
                        EventOperator *oper2_sub = (EventOperator *)iter2;
 
1415
 
 
1416
                        TEST_EQ (oper1_sub->value, oper2_sub->value);
 
1417
                        TEST_EQ (oper1_sub->type, oper2_sub->type);
 
1418
                        if (oper1_sub->name || oper2_sub->name) {
 
1419
                                TEST_EQ_STR (oper1_sub->name, oper2_sub->name);
 
1420
                        }
 
1421
                }
 
1422
        }
 
1423
 
 
1424
}
1323
1425
 
1324
1426
int
1325
1427
main (int   argc,
1337
1439
        test_operator_environment ();
1338
1440
        test_operator_events ();
1339
1441
        test_operator_reset ();
 
1442
        test_operator_serialisation ();
1340
1443
 
1341
1444
        return 0;
1342
1445
}