~vorlon/ubuntu/raring/upstart/lp.1199778

« back to all changes in this revision

Viewing changes to init/tests/test_conf.c

  • Committer: Scott James Remnant
  • Date: 2007-06-10 16:38:06 UTC
  • Revision ID: scott@netsplit.com-20070610163806-s6vp7kd6cq6rdalw
* init/conf.c (conf_item_free): Don't overwrite any previous
replacement, only mark us for deletion if we wouldn't otherwise
be replaced.  Add some commented possible code for testing.
* init/tests/test_conf.c (test_source_reload): Test replacement of
jobs works properly; test modification with direct write and with
atomic rename replace; test deletion.

Show diffs side-by-side

added added

removed removed

Lines of Context:
201
201
        ConfSource *source;
202
202
        ConfFile   *file;
203
203
        ConfItem   *item;
204
 
        Job        *job;
 
204
        Job        *job, *old_job;
205
205
        FILE       *f;
206
206
        int         ret, fd[4096], i = 0, nfds;
207
207
        char        job_dirname[PATH_MAX];
208
 
        char        filename[PATH_MAX];
 
208
        char        tmpname[PATH_MAX], filename[PATH_MAX];
209
209
        fd_set      readfds, writefds, exceptfds;
210
210
 
211
211
        TEST_FUNCTION ("conf_source_reload");
371
371
         * direct writing technique, it will be automatically parsed and
372
372
         * loaded.
373
373
         */
374
 
        TEST_FEATURE ("with new file in directory");
 
374
        TEST_FEATURE ("with new file in directory (direct write)");
375
375
        source = conf_source_new (NULL, job_dirname, CONF_JOB_DIR);
376
376
        ret = conf_source_reload (source);
377
377
 
416
416
        TEST_EQ (job->process[PROCESS_MAIN]->script, TRUE);
417
417
        TEST_EQ_STR (job->process[PROCESS_MAIN]->command, "echo\n");
418
418
 
419
 
        TEST_EQ_P (item->entry.next, &file->items);
420
 
 
 
419
        TEST_EQ_P (job->replacement, NULL);
 
420
        TEST_EQ_P (job->replacement_for, NULL);
 
421
 
 
422
        TEST_EQ_P (item->entry.next, &file->items);
 
423
 
 
424
        old_job = job;
 
425
 
 
426
 
 
427
        /* Check that a file in the directory we're watching can be modified
 
428
         * using the direct writing technique; it should be parsed and the
 
429
         * previous job marked for deletion.
 
430
         */
 
431
        TEST_FEATURE ("with modified job (direct write)");
 
432
        strcpy (filename, job_dirname);
 
433
        strcat (filename, "/frodo/bar");
 
434
 
 
435
        f = fopen (filename, "w");
 
436
        fprintf (f, "respawn\n");
 
437
        fprintf (f, "script\n");
 
438
        fprintf (f, "  sleep 5\n");
 
439
        fprintf (f, "end script\n");
 
440
        fclose (f);
 
441
 
 
442
        nfds = 0;
 
443
        FD_ZERO (&readfds);
 
444
        FD_ZERO (&writefds);
 
445
        FD_ZERO (&exceptfds);
 
446
 
 
447
        nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);
 
448
        nih_io_handle_fds (&readfds, &writefds, &exceptfds);
 
449
 
 
450
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
451
 
 
452
        TEST_ALLOC_SIZE (file, sizeof (ConfFile));
 
453
        TEST_ALLOC_PARENT (file, source);
 
454
        TEST_EQ (file->flag, source->flag);
 
455
        TEST_LIST_NOT_EMPTY (&file->items);
 
456
 
 
457
        item = (ConfItem *)file->items.next;
 
458
 
 
459
        TEST_ALLOC_SIZE (item, sizeof (ConfItem));
 
460
        TEST_ALLOC_PARENT (item, file);
 
461
        TEST_NE_P (item->job, NULL);
 
462
 
 
463
        job = job_find_by_name ("frodo/bar");
 
464
        TEST_EQ_P (item->job, job);
 
465
 
 
466
        TEST_TRUE (job->respawn);
 
467
        TEST_NE_P (job->process[PROCESS_MAIN], NULL);
 
468
        TEST_EQ (job->process[PROCESS_MAIN]->script, TRUE);
 
469
        TEST_EQ_STR (job->process[PROCESS_MAIN]->command, "sleep 5\n");
 
470
 
 
471
        TEST_EQ_P (job->replacement, NULL);
 
472
        TEST_EQ_P (job->replacement_for, NULL);
 
473
 
 
474
        TEST_EQ_P (old_job->replacement, job);
 
475
        TEST_EQ (old_job->state, JOB_DELETED);
 
476
 
 
477
        TEST_EQ_P (item->entry.next, &file->items);
 
478
 
 
479
        old_job = job;
 
480
 
 
481
 
 
482
        /* Check that a file in the directory we're watching can be modified
 
483
         * using the write and then rename technique; it should be parsed and
 
484
         * the previous job marked for deletion.
 
485
         */
 
486
        TEST_FEATURE ("with modified job (atomic rename)");
 
487
        strcpy (filename, job_dirname);
 
488
        strcat (filename, "/frodo/bar");
 
489
 
 
490
        strcpy (tmpname, job_dirname);
 
491
        strcat (tmpname, "/frodo/.bar.swp");
 
492
 
 
493
        f = fopen (tmpname, "w");
 
494
        fprintf (f, "respawn\n");
 
495
        fprintf (f, "script\n");
 
496
        fprintf (f, "  sleep 15\n");
 
497
        fprintf (f, "end script\n");
 
498
        fclose (f);
 
499
 
 
500
        rename (tmpname, filename);
 
501
 
 
502
        nfds = 0;
 
503
        FD_ZERO (&readfds);
 
504
        FD_ZERO (&writefds);
 
505
        FD_ZERO (&exceptfds);
 
506
 
 
507
        nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);
 
508
        nih_io_handle_fds (&readfds, &writefds, &exceptfds);
 
509
 
 
510
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
511
 
 
512
        TEST_ALLOC_SIZE (file, sizeof (ConfFile));
 
513
        TEST_ALLOC_PARENT (file, source);
 
514
        TEST_EQ (file->flag, source->flag);
 
515
        TEST_LIST_NOT_EMPTY (&file->items);
 
516
 
 
517
        item = (ConfItem *)file->items.next;
 
518
 
 
519
        TEST_ALLOC_SIZE (item, sizeof (ConfItem));
 
520
        TEST_ALLOC_PARENT (item, file);
 
521
        TEST_NE_P (item->job, NULL);
 
522
 
 
523
        job = job_find_by_name ("frodo/bar");
 
524
        TEST_EQ_P (item->job, job);
 
525
 
 
526
        TEST_TRUE (job->respawn);
 
527
        TEST_NE_P (job->process[PROCESS_MAIN], NULL);
 
528
        TEST_EQ (job->process[PROCESS_MAIN]->script, TRUE);
 
529
        TEST_EQ_STR (job->process[PROCESS_MAIN]->command, "sleep 15\n");
 
530
 
 
531
        TEST_EQ_P (job->replacement, NULL);
 
532
        TEST_EQ_P (job->replacement_for, NULL);
 
533
 
 
534
        TEST_EQ_P (old_job->replacement, job);
 
535
        TEST_EQ (old_job->state, JOB_DELETED);
 
536
 
 
537
        TEST_EQ_P (item->entry.next, &file->items);
 
538
 
 
539
        old_job = job;
 
540
 
 
541
 
 
542
        /* Check that we can delete a file from the directory, the metadata
 
543
         * for it should be lost and the job should be queued for deletion.
 
544
         */
 
545
        TEST_FEATURE ("with deleted job");
 
546
        strcpy (filename, job_dirname);
 
547
        strcat (filename, "/frodo/bar");
421
548
 
422
549
        unlink (filename);
 
550
 
 
551
        nfds = 0;
 
552
        FD_ZERO (&readfds);
 
553
        FD_ZERO (&writefds);
 
554
        FD_ZERO (&exceptfds);
 
555
 
 
556
        nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);
 
557
        nih_io_handle_fds (&readfds, &writefds, &exceptfds);
 
558
 
 
559
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
560
 
 
561
        TEST_EQ_P (file, NULL);
 
562
 
 
563
        job = job_find_by_name ("frodo/bar");
 
564
        TEST_EQ_P (job, NULL);
 
565
 
 
566
        TEST_EQ_P (old_job->replacement, (void *)-1);
 
567
        TEST_EQ (old_job->state, JOB_DELETED);
 
568
 
 
569
 
423
570
        conf_source_free (source);
424
571
 
425
 
 
426
572
        /* Consume all available inotify instances so that the following
427
573
         * tests run without inotify.
428
574
         */
558
704
        TEST_EQ (source->flag, TRUE);
559
705
 
560
706
        strcpy (filename, job_dirname);
 
707
        strcat (filename, "/foo");
 
708
 
 
709
        f = fopen (filename, "w");
 
710
        fprintf (f, "exec /sbin/daemon --foo\n");
 
711
        fprintf (f, "respawn\n");
 
712
        fclose (f);
 
713
 
 
714
        strcpy (filename, job_dirname);
561
715
        strcat (filename, "/frodo/foo");
562
716
 
563
717
        unlink (filename);
597
751
        TEST_TRUE (job->respawn);
598
752
        TEST_NE_P (job->process[PROCESS_MAIN], NULL);
599
753
        TEST_EQ (job->process[PROCESS_MAIN]->script, FALSE);
600
 
        TEST_EQ_STR (job->process[PROCESS_MAIN]->command, "/sbin/daemon");
 
754
        TEST_EQ_STR (job->process[PROCESS_MAIN]->command,
 
755
                     "/sbin/daemon --foo");
601
756
 
602
757
        nih_list_free (&job->entry);
603
758
        nih_list_free (&item->entry);