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

« back to all changes in this revision

Viewing changes to init/tests/test_conf.c

Merge of lp:~jamesodhunt/upstart/upstream-override-support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
#include "job.h"
47
47
#include "conf.h"
48
48
 
 
49
/* macro to try and ensure the environment is as pristine as possible
 
50
 * (to avoid follow-on errors caused by not freeing objects in a
 
51
 * previous test, say)
 
52
 */
 
53
#define TEST_ENSURE_CLEAN_ENV()                                      \
 
54
{                                                                    \
 
55
        setvbuf(stdout, NULL, _IONBF, 0);                            \
 
56
                                                                     \
 
57
        if (job_classes) {                                           \
 
58
                TEST_HASH_EMPTY (job_classes);                       \
 
59
        }                                                            \
 
60
                                                                     \
 
61
        if (conf_sources) {                                          \
 
62
                TEST_LIST_EMPTY (conf_sources);                      \
 
63
        }                                                            \
 
64
                                                                     \
 
65
        if (nih_io_watches) {                                        \
 
66
                TEST_LIST_EMPTY (nih_io_watches);                    \
 
67
        }                                                            \
 
68
                                                                     \
 
69
        if (nih_timers) {                                            \
 
70
                TEST_LIST_EMPTY (nih_timers);                        \
 
71
        }                                                            \
 
72
                                                                     \
 
73
        if (events) {                                                \
 
74
                TEST_LIST_EMPTY (events);                            \
 
75
        }                                                            \
 
76
}
 
77
 
 
78
/* Force an inotify watch update */
 
79
#define TEST_FORCE_WATCH_UPDATE()                                    \
 
80
{                                                                    \
 
81
        int         nfds = 0;                                        \
 
82
        fd_set      readfds, writefds, exceptfds;                    \
 
83
                                                                     \
 
84
        FD_ZERO (&readfds);                                          \
 
85
        FD_ZERO (&writefds);                                         \
 
86
        FD_ZERO (&exceptfds);                                        \
 
87
                                                                     \
 
88
        nih_debug("calling nih_io_select_fds");                      \
 
89
        nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);  \
 
90
        nih_debug("calling nih_io_handle_fds");                      \
 
91
        nih_io_handle_fds (&readfds, &writefds, &exceptfds);         \
 
92
}
49
93
 
50
94
void
51
95
test_source_new (void)
2416
2460
        }
2417
2461
}
2418
2462
 
 
2463
void
 
2464
test_toggle_conf_name (void)
 
2465
{
 
2466
        char override_ext[] = ".override";
 
2467
        char dirname[PATH_MAX];
 
2468
        char filename[PATH_MAX];
 
2469
        JobClass *job;
 
2470
        char *f;
 
2471
        char *p;
 
2472
 
 
2473
        TEST_FUNCTION_FEATURE ("toggle_conf_name",
 
2474
                        "changing conf to override");
 
2475
 
 
2476
        TEST_FILENAME (dirname);
 
2477
        strcpy (filename, dirname);
 
2478
        strcat (filename, "/foo.conf");
 
2479
        f = toggle_conf_name (NULL, filename);
 
2480
        TEST_NE_P (f, NULL);
 
2481
 
 
2482
        p = strstr (f, ".override");
 
2483
        TEST_NE_P (p, NULL);
 
2484
        TEST_EQ_P (p, f+strlen (f) - strlen (override_ext));
 
2485
        nih_free (f);
 
2486
 
 
2487
        TEST_FEATURE ("changing override to conf");
 
2488
        strcpy (filename, dirname);
 
2489
        strcat (filename, "/bar.override");
 
2490
        f = toggle_conf_name (NULL, filename);
 
2491
        TEST_NE_P (f, NULL);
 
2492
 
 
2493
        p = strstr (f, ".conf");
 
2494
        TEST_NE_P (p, NULL);
 
2495
        TEST_EQ_P (p, f+strlen (f) - strlen (".conf"));
 
2496
        nih_free (f);
 
2497
 
 
2498
        /* test parent param */
 
2499
        job = job_class_new (NULL, "foo", NULL);
 
2500
        TEST_NE_P (job, NULL);
 
2501
 
 
2502
        f = toggle_conf_name (job, filename);
 
2503
        TEST_NE_P (f, NULL);
 
2504
 
 
2505
        TEST_EQ (TRUE, nih_alloc_parent (f, job));
 
2506
 
 
2507
        nih_free (job);
 
2508
}
 
2509
 
 
2510
void
 
2511
test_override (void)
 
2512
{
 
2513
        ConfSource *source;
 
2514
        ConfFile   *file;
 
2515
        FILE       *f;
 
2516
        int         ret, fd[4096], i = 0;
 
2517
        char        dirname[PATH_MAX];
 
2518
        char        filename[PATH_MAX], override[PATH_MAX];
 
2519
        JobClass   *job;
 
2520
        NihError   *err;
 
2521
 
 
2522
        program_name = "test";
 
2523
        nih_log_set_priority (NIH_LOG_FATAL);
 
2524
 
 
2525
        TEST_ENSURE_CLEAN_ENV ();
 
2526
        TEST_GROUP ("override files");
 
2527
 
 
2528
        /* Make sure that we have inotify before performing some tests... */
 
2529
        if ((fd[0] = inotify_init ()) < 0) {
 
2530
                printf ("SKIP: inotify not available\n");
 
2531
                goto no_inotify;
 
2532
        }
 
2533
        close (fd[0]);
 
2534
 
 
2535
 
 
2536
        /* Explicit test of behaviour prior to introduction of override files.
 
2537
         * 
 
2538
         * conf with no override before watch:
 
2539
         *   create conf
 
2540
         *   create watch
 
2541
         *   ensure conf loaded
 
2542
         *   update conf
 
2543
         *   ensure conf updated
 
2544
         *   delete conf
 
2545
         *   ensure conf deleted
 
2546
         */
 
2547
        TEST_FEATURE ("with pre-override environment (conf with no override before watch)");
 
2548
        TEST_ENSURE_CLEAN_ENV ();
 
2549
        TEST_FILENAME (dirname);
 
2550
        TEST_EQ (mkdir (dirname, 0755), 0);
 
2551
 
 
2552
        /* create conf */
 
2553
        strcpy (filename, dirname);
 
2554
        strcat (filename, "/foo.conf");
 
2555
        f = fopen (filename, "w");
 
2556
        TEST_NE_P (f, NULL);
 
2557
        fprintf (f, "start on started\n");
 
2558
        fprintf (f, "emits hello\n");
 
2559
        fclose (f);
 
2560
 
 
2561
        /* create watch */
 
2562
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
2563
        TEST_NE_P (source, NULL);
 
2564
        ret = conf_source_reload (source);
 
2565
        TEST_EQ (ret, 0);
 
2566
 
 
2567
        /* ensure conf loaded */
 
2568
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2569
        TEST_NE_P (file, NULL);
 
2570
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2571
        TEST_NE_P (job, NULL);
 
2572
        TEST_EQ_P (file->job, job);
 
2573
        TEST_EQ_STR ((job->emits)[0], "hello");
 
2574
        TEST_NE_P (job->start_on, NULL);
 
2575
 
 
2576
        /* update conf */
 
2577
        f = fopen (filename, "a");
 
2578
        TEST_NE_P (f, NULL);
 
2579
        fprintf (f, "manual\n");
 
2580
        fclose (f);
 
2581
 
 
2582
        TEST_FORCE_WATCH_UPDATE();
 
2583
 
 
2584
        /* ensure conf updated */
 
2585
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2586
        TEST_NE_P (file, NULL);
 
2587
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2588
        TEST_NE_P (job, NULL);
 
2589
        TEST_EQ_P (job->start_on, NULL);
 
2590
 
 
2591
        /* delete conf */
 
2592
        unlink (filename);
 
2593
 
 
2594
        TEST_FORCE_WATCH_UPDATE();
 
2595
 
 
2596
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2597
        TEST_EQ_P (job, NULL);
 
2598
        TEST_HASH_EMPTY (job_classes);
 
2599
        TEST_HASH_EMPTY (source->files);
 
2600
 
 
2601
        TEST_FORCE_WATCH_UPDATE();
 
2602
 
 
2603
        /* ensure conf deleted */
 
2604
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2605
        TEST_EQ_P (file, NULL);
 
2606
 
 
2607
        nih_free (source);
 
2608
        TEST_EQ (rmdir (dirname), 0);
 
2609
 
 
2610
 
 
2611
        /* Explicit test of behaviour prior to introduction of override files.
 
2612
         * 
 
2613
         * conf with no override after watch:
 
2614
         *   create watch
 
2615
         *   create conf
 
2616
         *   ensure conf loaded
 
2617
         *   update conf
 
2618
         *   ensure conf updated
 
2619
         *   delete conf
 
2620
         *   ensure conf deleted
 
2621
         */
 
2622
        TEST_ENSURE_CLEAN_ENV ();
 
2623
        TEST_FEATURE ("with pre-override environment (conf with no override after watch)");
 
2624
        TEST_FILENAME (dirname);
 
2625
        TEST_EQ (mkdir (dirname, 0755), 0);
 
2626
 
 
2627
        /* create watch */
 
2628
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
2629
        TEST_NE_P (source, NULL);
 
2630
        ret = conf_source_reload (source);
 
2631
        TEST_EQ (ret, 0);
 
2632
 
 
2633
        /* create conf */
 
2634
        strcpy (filename, dirname);
 
2635
        strcat (filename, "/foo.conf");
 
2636
        f = fopen (filename, "w");
 
2637
        TEST_NE_P (f, NULL);
 
2638
        fprintf (f, "start on started\n");
 
2639
        fprintf (f, "emits hello\n");
 
2640
        fclose (f);
 
2641
 
 
2642
        TEST_FORCE_WATCH_UPDATE();
 
2643
 
 
2644
        /* ensure conf loaded */
 
2645
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2646
        TEST_NE_P (file, NULL);
 
2647
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2648
        TEST_NE_P (job, NULL);
 
2649
        TEST_EQ_P (file->job, job);
 
2650
        TEST_EQ_STR ((job->emits)[0], "hello");
 
2651
 
 
2652
        /* update conf */
 
2653
        f = fopen (filename, "a");
 
2654
        TEST_NE_P (f, NULL);
 
2655
        fprintf (f, "manual\n");
 
2656
        fclose (f);
 
2657
 
 
2658
        TEST_FORCE_WATCH_UPDATE();
 
2659
 
 
2660
        /* ensure conf updated */
 
2661
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2662
        TEST_NE_P (job, NULL);
 
2663
        TEST_EQ_P (job->start_on, NULL);
 
2664
 
 
2665
        /* delete conf */
 
2666
        unlink (filename);
 
2667
 
 
2668
        TEST_FORCE_WATCH_UPDATE();
 
2669
 
 
2670
        /* ensure conf deleted */
 
2671
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2672
        TEST_EQ_P (file, NULL);
 
2673
        nih_free (source);
 
2674
        TEST_EQ (rmdir (dirname), 0);
 
2675
 
 
2676
 
 
2677
        TEST_FEATURE ("ensure lone override ignored before watch");
 
2678
        TEST_ENSURE_CLEAN_ENV ();
 
2679
        TEST_FILENAME (dirname);
 
2680
        TEST_EQ (mkdir (dirname, 0755), 0);
 
2681
 
 
2682
        /* create override */
 
2683
        strcpy (filename, dirname);
 
2684
        strcat (filename, "/foo.override");
 
2685
        f = fopen (filename, "w");
 
2686
        TEST_NE_P (f, NULL);
 
2687
        fprintf (f, "manual\n");
 
2688
        fclose (f);
 
2689
 
 
2690
        /* create watch */
 
2691
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
2692
        TEST_NE_P (source, NULL);
 
2693
        ret = conf_source_reload (source);
 
2694
        TEST_EQ (ret, 0);
 
2695
 
 
2696
        TEST_FORCE_WATCH_UPDATE();
 
2697
 
 
2698
        /* ensure no conf object created */
 
2699
        TEST_HASH_EMPTY (source->files);
 
2700
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2701
        TEST_EQ_P (file, NULL);
 
2702
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2703
        TEST_EQ_P (job, NULL);
 
2704
 
 
2705
        /* update override */
 
2706
        f = fopen (filename, "a");
 
2707
        TEST_NE_P (f, NULL);
 
2708
        fprintf (f, "author \"me\"\n");
 
2709
        fclose (f);
 
2710
 
 
2711
        TEST_FORCE_WATCH_UPDATE();
 
2712
 
 
2713
        /* ensure no conf object created */
 
2714
        TEST_HASH_EMPTY (source->files);
 
2715
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2716
        TEST_EQ_P (file, NULL);
 
2717
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2718
        TEST_EQ_P (job, NULL);
 
2719
 
 
2720
        /* delete override */
 
2721
        unlink (filename);
 
2722
        nih_free (source);
 
2723
        TEST_EQ (rmdir (dirname), 0);
 
2724
 
 
2725
 
 
2726
        TEST_FEATURE ("ensure lone override ignored after watch");
 
2727
        TEST_ENSURE_CLEAN_ENV ();
 
2728
        TEST_FILENAME (dirname);
 
2729
        TEST_EQ (mkdir (dirname, 0755), 0);
 
2730
 
 
2731
        /* create watch */
 
2732
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
2733
        TEST_NE_P (source, NULL);
 
2734
        ret = conf_source_reload (source);
 
2735
        TEST_EQ (ret, 0);
 
2736
 
 
2737
        strcpy (filename, dirname);
 
2738
        strcat (filename, "/bar.override");
 
2739
        f = fopen (filename, "w");
 
2740
        TEST_NE_P (f, NULL);
 
2741
        fprintf (f, "manual\n");
 
2742
        fclose (f);
 
2743
 
 
2744
        TEST_FORCE_WATCH_UPDATE();
 
2745
 
 
2746
        /* ensure no conf object created */
 
2747
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2748
        TEST_EQ_P (file, NULL);
 
2749
        job = (JobClass *)nih_hash_lookup (job_classes, "bar");
 
2750
        TEST_EQ_P (job, NULL);
 
2751
 
 
2752
        /* delete override */
 
2753
        unlink (filename);
 
2754
 
 
2755
        TEST_FORCE_WATCH_UPDATE();
 
2756
 
 
2757
        /* ensure override still not present */
 
2758
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2759
        TEST_EQ_P (file, NULL);
 
2760
        job = (JobClass *)nih_hash_lookup (job_classes, "bar");
 
2761
        TEST_EQ_P (job, NULL);
 
2762
 
 
2763
        nih_free (source);
 
2764
        TEST_EQ (rmdir (dirname), 0);
 
2765
 
 
2766
 
 
2767
        TEST_FEATURE ("create conf, watch, then create/modify/delete override");
 
2768
        TEST_ENSURE_CLEAN_ENV ();
 
2769
        TEST_FILENAME (dirname);
 
2770
        TEST_EQ (mkdir (dirname, 0755), 0);
 
2771
 
 
2772
        /* create conf */
 
2773
        strcpy (filename, dirname);
 
2774
        strcat (filename, "/foo.conf");
 
2775
        f = fopen (filename, "w");
 
2776
        TEST_NE_P (f, NULL);
 
2777
        fprintf (f, "start on started\n");
 
2778
        fprintf (f, "emits hello\n");
 
2779
        fclose (f);
 
2780
 
 
2781
        /* create watch */
 
2782
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
2783
        TEST_NE_P (source, NULL);
 
2784
        ret = conf_source_reload (source);
 
2785
        TEST_EQ (ret, 0);
 
2786
 
 
2787
        /* ensure conf loaded */
 
2788
        TEST_HASH_NOT_EMPTY (source->files);
 
2789
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2790
        TEST_NE_P (file, NULL);
 
2791
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2792
        TEST_NE_P (job, NULL);
 
2793
        TEST_EQ_P (file->job, job);
 
2794
        TEST_EQ_STR ((job->emits)[0], "hello");
 
2795
        TEST_EQ_P ((job->emits)[1], NULL);
 
2796
        TEST_NE_P (job->start_on, NULL);
 
2797
 
 
2798
        /* create override */
 
2799
        strcpy (override, dirname);
 
2800
        strcat (override, "/foo.override");
 
2801
        f = fopen (override, "w");
 
2802
        TEST_NE_P (f, NULL);
 
2803
        fprintf (f, "manual\n");
 
2804
        fclose (f);
 
2805
 
 
2806
        TEST_HASH_NOT_EMPTY (source->files);
 
2807
        TEST_FORCE_WATCH_UPDATE();
 
2808
 
 
2809
        /* ensure conf updated */
 
2810
        TEST_HASH_NOT_EMPTY (source->files);
 
2811
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2812
        TEST_NE_P (file, NULL);
 
2813
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2814
        TEST_NE_P (job, NULL);
 
2815
        TEST_EQ_P (job->start_on, NULL);
 
2816
 
 
2817
        /* ensure no override in hash */
 
2818
        file = (ConfFile *)nih_hash_lookup (source->files, override);
 
2819
        TEST_EQ_P (file, NULL);
 
2820
 
 
2821
        /* modify override */
 
2822
        f = fopen (override, "a");
 
2823
        TEST_NE_P (f, NULL);
 
2824
        fprintf (f, "emits world\n");
 
2825
        fclose (f);
 
2826
 
 
2827
        TEST_FORCE_WATCH_UPDATE();
 
2828
 
 
2829
        /* ensure conf updated */
 
2830
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2831
        TEST_NE_P (file, NULL);
 
2832
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2833
        TEST_NE_P (job, NULL);
 
2834
        TEST_EQ_P (job->start_on, NULL);
 
2835
        TEST_EQ_STR ((job->emits)[0], "hello");
 
2836
        TEST_EQ_STR ((job->emits)[1], "world");
 
2837
 
 
2838
        /* delete override */
 
2839
        unlink (override);
 
2840
 
 
2841
        TEST_FORCE_WATCH_UPDATE();
 
2842
 
 
2843
        /* ensure conf reverted */
 
2844
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2845
        TEST_NE_P (file, NULL);
 
2846
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2847
        TEST_NE_P (job, NULL);
 
2848
        TEST_NE_P (job->start_on, NULL);
 
2849
        TEST_EQ_STR ((job->emits)[0], "hello");
 
2850
        TEST_EQ_P ((job->emits)[1], NULL);
 
2851
 
 
2852
        nih_free (source);
 
2853
        unlink (filename);
 
2854
        TEST_EQ (rmdir (dirname), 0);
 
2855
 
 
2856
 
 
2857
        TEST_FEATURE ("create watch, conf, then create/modify/delete override");
 
2858
        TEST_ENSURE_CLEAN_ENV ();
 
2859
        TEST_FILENAME (dirname);
 
2860
        TEST_EQ (mkdir (dirname, 0755), 0);
 
2861
 
 
2862
        /* create watch */
 
2863
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
2864
        TEST_NE_P (source, NULL);
 
2865
        ret = conf_source_reload (source);
 
2866
        TEST_EQ (ret, 0);
 
2867
 
 
2868
        /* create conf */
 
2869
        strcpy (filename, dirname);
 
2870
        strcat (filename, "/foo.conf");
 
2871
        f = fopen (filename, "w");
 
2872
        TEST_NE_P (f, NULL);
 
2873
        fprintf (f, "start on started\n");
 
2874
        fprintf (f, "emits hello\n");
 
2875
        fclose (f);
 
2876
 
 
2877
        TEST_FORCE_WATCH_UPDATE();
 
2878
 
 
2879
        /* ensure conf loaded */
 
2880
        TEST_HASH_NOT_EMPTY (source->files);
 
2881
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2882
        TEST_NE_P (file, NULL);
 
2883
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2884
        TEST_NE_P (job, NULL);
 
2885
        TEST_EQ_P (file->job, job);
 
2886
        TEST_EQ_STR ((job->emits)[0], "hello");
 
2887
        TEST_EQ_P ((job->emits)[1], NULL);
 
2888
        TEST_NE_P (job->start_on, NULL);
 
2889
 
 
2890
        /* create override */
 
2891
        strcpy (override, dirname);
 
2892
        strcat (override, "/foo.override");
 
2893
        f = fopen (override, "w");
 
2894
        TEST_NE_P (f, NULL);
 
2895
        fprintf (f, "manual\n");
 
2896
        fclose (f);
 
2897
 
 
2898
        TEST_HASH_NOT_EMPTY (source->files);
 
2899
        TEST_FORCE_WATCH_UPDATE();
 
2900
 
 
2901
        /* ensure conf updated */
 
2902
        TEST_HASH_NOT_EMPTY (source->files);
 
2903
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2904
        TEST_NE_P (file, NULL);
 
2905
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2906
        TEST_NE_P (job, NULL);
 
2907
        TEST_EQ_P (job->start_on, NULL);
 
2908
 
 
2909
        /* ensure no override in hash */
 
2910
        file = (ConfFile *)nih_hash_lookup (source->files, override);
 
2911
        TEST_EQ_P (file, NULL);
 
2912
 
 
2913
        /* modify override */
 
2914
        f = fopen (override, "a");
 
2915
        TEST_NE_P (f, NULL);
 
2916
        fprintf (f, "emits world\n");
 
2917
        fclose (f);
 
2918
 
 
2919
        TEST_FORCE_WATCH_UPDATE();
 
2920
 
 
2921
        /* ensure conf updated */
 
2922
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2923
        TEST_NE_P (file, NULL);
 
2924
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2925
        TEST_NE_P (job, NULL);
 
2926
        TEST_EQ_P (job->start_on, NULL);
 
2927
        TEST_EQ_STR ((job->emits)[0], "hello");
 
2928
        TEST_EQ_STR ((job->emits)[1], "world");
 
2929
 
 
2930
        /* delete override */
 
2931
        unlink (override);
 
2932
 
 
2933
        TEST_FORCE_WATCH_UPDATE();
 
2934
 
 
2935
        /* ensure conf reverted */
 
2936
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2937
        TEST_NE_P (file, NULL);
 
2938
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2939
        TEST_NE_P (job, NULL);
 
2940
        TEST_NE_P (job->start_on, NULL);
 
2941
        TEST_EQ_STR ((job->emits)[0], "hello");
 
2942
        TEST_EQ_P ((job->emits)[1], NULL);
 
2943
 
 
2944
        nih_free (source);
 
2945
        unlink (filename);
 
2946
        TEST_EQ (rmdir (dirname), 0);
 
2947
 
 
2948
 
 
2949
        TEST_FEATURE ("create override, watch, then create/modify/delete conf");
 
2950
        TEST_ENSURE_CLEAN_ENV ();
 
2951
        TEST_FILENAME (dirname);
 
2952
        TEST_EQ (mkdir (dirname, 0755), 0);
 
2953
 
 
2954
        strcpy (filename, dirname);
 
2955
        strcat (filename, "/foo.conf");
 
2956
        strcpy (override, dirname);
 
2957
        strcat (override, "/foo.override");
 
2958
 
 
2959
        /* create override */
 
2960
        f = fopen (override, "w");
 
2961
        TEST_NE_P (f, NULL);
 
2962
        fprintf (f, "manual\n");
 
2963
        fprintf (f, "author \"bar\"\n");
 
2964
        fclose (f);
 
2965
 
 
2966
        /* create watch */
 
2967
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
2968
        TEST_NE_P (source, NULL);
 
2969
        ret = conf_source_reload (source);
 
2970
        TEST_EQ (ret, 0);
 
2971
 
 
2972
        TEST_FORCE_WATCH_UPDATE();
 
2973
 
 
2974
        /* ensure no conf object created */
 
2975
        TEST_HASH_EMPTY (source->files);
 
2976
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2977
        TEST_EQ_P (file, NULL);
 
2978
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2979
        TEST_EQ_P (job, NULL);
 
2980
 
 
2981
        /* create conf */
 
2982
        f = fopen (filename, "w");
 
2983
        TEST_NE_P (f, NULL);
 
2984
        fprintf (f, "start on started\n");
 
2985
        fprintf (f, "emits hello\n");
 
2986
        fprintf (f, "author \"foo\"\n");
 
2987
        fclose (f);
 
2988
 
 
2989
        TEST_FORCE_WATCH_UPDATE();
 
2990
 
 
2991
        /* ensure conf loaded */
 
2992
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
2993
        TEST_NE_P (file, NULL);
 
2994
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
2995
        TEST_NE_P (job, NULL);
 
2996
        TEST_EQ_P (file->job, job);
 
2997
        TEST_EQ_STR ((job->emits)[0], "hello");
 
2998
 
 
2999
        /* should pick up override, *NOT* conf */
 
3000
        TEST_EQ_P (job->start_on, NULL);
 
3001
        TEST_EQ_STR (job->author, "bar");
 
3002
 
 
3003
        /* modify conf */
 
3004
        f = fopen (filename, "w");
 
3005
        TEST_NE_P (f, NULL);
 
3006
        fprintf (f, "start on wibble\n");
 
3007
        fprintf (f, "emits moo\n");
 
3008
        fclose (f);
 
3009
 
 
3010
        TEST_FORCE_WATCH_UPDATE();
 
3011
 
 
3012
        /* ensure conf reloaded and updated with override */
 
3013
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3014
        TEST_NE_P (file, NULL);
 
3015
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3016
        TEST_NE_P (job, NULL);
 
3017
        TEST_EQ_P (file->job, job);
 
3018
        TEST_EQ_STR ((job->emits)[0], "moo");
 
3019
 
 
3020
        /* should pick up override, *NOT* conf */
 
3021
        TEST_EQ_P (job->start_on, NULL);
 
3022
        TEST_EQ_STR (job->author, "bar");
 
3023
 
 
3024
        /* delete conf */
 
3025
        unlink (filename);
 
3026
 
 
3027
        TEST_FORCE_WATCH_UPDATE();
 
3028
 
 
3029
        /* ensure conf object deleted */
 
3030
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3031
        TEST_EQ_P (file, NULL);
 
3032
        TEST_HASH_EMPTY (source->files);
 
3033
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3034
        TEST_EQ_P (job, NULL);
 
3035
        file = (ConfFile *)nih_hash_lookup (source->files, override);
 
3036
        TEST_EQ_P (file, NULL);
 
3037
 
 
3038
        unlink (override);
 
3039
 
 
3040
        /* ensure no conf object still */
 
3041
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3042
        TEST_EQ_P (file, NULL);
 
3043
        TEST_HASH_EMPTY (source->files);
 
3044
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3045
        TEST_EQ_P (job, NULL);
 
3046
        file = (ConfFile *)nih_hash_lookup (source->files, override);
 
3047
        TEST_EQ_P (file, NULL);
 
3048
 
 
3049
        nih_free (source);
 
3050
        TEST_EQ (rmdir (dirname), 0);
 
3051
 
 
3052
 
 
3053
        TEST_FEATURE ("create watch, override, then create/modify/delete conf");
 
3054
        TEST_ENSURE_CLEAN_ENV ();
 
3055
        TEST_FILENAME (dirname);
 
3056
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3057
 
 
3058
        /* create watch */
 
3059
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
3060
        TEST_NE_P (source, NULL);
 
3061
        ret = conf_source_reload (source);
 
3062
        TEST_EQ (ret, 0);
 
3063
 
 
3064
        strcpy (filename, dirname);
 
3065
        strcat (filename, "/foo.conf");
 
3066
        strcpy (override, dirname);
 
3067
        strcat (override, "/foo.override");
 
3068
 
 
3069
        /* create override */
 
3070
        f = fopen (override, "w");
 
3071
        TEST_NE_P (f, NULL);
 
3072
        fprintf (f, "manual\n");
 
3073
        fprintf (f, "author \"bar\"\n");
 
3074
        fclose (f);
 
3075
 
 
3076
        TEST_FORCE_WATCH_UPDATE();
 
3077
 
 
3078
        /* ensure no conf object created */
 
3079
        TEST_HASH_EMPTY (source->files);
 
3080
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3081
        TEST_EQ_P (file, NULL);
 
3082
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3083
        TEST_EQ_P (job, NULL);
 
3084
 
 
3085
        /* create conf */
 
3086
        f = fopen (filename, "w");
 
3087
        TEST_NE_P (f, NULL);
 
3088
        fprintf (f, "start on started\n");
 
3089
        fprintf (f, "emits hello\n");
 
3090
        fprintf (f, "author \"foo\"\n");
 
3091
        fclose (f);
 
3092
 
 
3093
        TEST_FORCE_WATCH_UPDATE();
 
3094
 
 
3095
        /* ensure conf loaded */
 
3096
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3097
        TEST_NE_P (file, NULL);
 
3098
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3099
        TEST_NE_P (job, NULL);
 
3100
        TEST_EQ_P (file->job, job);
 
3101
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3102
 
 
3103
        /* should pick up override, *NOT* conf */
 
3104
        TEST_EQ_P (job->start_on, NULL);
 
3105
        TEST_EQ_STR (job->author, "bar");
 
3106
 
 
3107
        /* modify conf */
 
3108
        f = fopen (filename, "w");
 
3109
        TEST_NE_P (f, NULL);
 
3110
        fprintf (f, "start on wibble\n");
 
3111
        fprintf (f, "emits moo\n");
 
3112
        fclose (f);
 
3113
 
 
3114
        TEST_FORCE_WATCH_UPDATE();
 
3115
 
 
3116
        /* ensure conf reloaded and updated with override */
 
3117
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3118
        TEST_NE_P (file, NULL);
 
3119
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3120
        TEST_NE_P (job, NULL);
 
3121
        TEST_EQ_P (file->job, job);
 
3122
        TEST_EQ_STR ((job->emits)[0], "moo");
 
3123
 
 
3124
        /* should pick up override, *NOT* conf */
 
3125
        TEST_EQ_P (job->start_on, NULL);
 
3126
        TEST_EQ_STR (job->author, "bar");
 
3127
 
 
3128
        /* delete conf */
 
3129
        unlink (filename);
 
3130
 
 
3131
        TEST_FORCE_WATCH_UPDATE();
 
3132
 
 
3133
        /* ensure conf object deleted */
 
3134
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3135
        TEST_EQ_P (file, NULL);
 
3136
        TEST_HASH_EMPTY (source->files);
 
3137
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3138
        TEST_EQ_P (job, NULL);
 
3139
        file = (ConfFile *)nih_hash_lookup (source->files, override);
 
3140
        TEST_EQ_P (file, NULL);
 
3141
 
 
3142
        unlink (override);
 
3143
 
 
3144
        /* ensure no conf object still */
 
3145
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3146
        TEST_EQ_P (file, NULL);
 
3147
        TEST_HASH_EMPTY (source->files);
 
3148
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3149
        TEST_EQ_P (job, NULL);
 
3150
        file = (ConfFile *)nih_hash_lookup (source->files, override);
 
3151
        TEST_EQ_P (file, NULL);
 
3152
 
 
3153
        nih_free (source);
 
3154
        TEST_EQ (rmdir (dirname), 0);
 
3155
 
 
3156
 
 
3157
        TEST_FEATURE ("create override, watch, conf, then modify/delete override");
 
3158
        TEST_ENSURE_CLEAN_ENV ();
 
3159
        TEST_FILENAME (dirname);
 
3160
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3161
 
 
3162
        strcpy (filename, dirname);
 
3163
        strcat (filename, "/foo.conf");
 
3164
        strcpy (override, dirname);
 
3165
        strcat (override, "/foo.override");
 
3166
 
 
3167
        /* create override */
 
3168
        f = fopen (override, "w");
 
3169
        TEST_NE_P (f, NULL);
 
3170
        fprintf (f, "manual\n");
 
3171
        fprintf (f, "author \"bar\"\n");
 
3172
        fclose (f);
 
3173
 
 
3174
        /* create watch */
 
3175
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
3176
        TEST_NE_P (source, NULL);
 
3177
        ret = conf_source_reload (source);
 
3178
        TEST_EQ (ret, 0);
 
3179
 
 
3180
        TEST_FORCE_WATCH_UPDATE();
 
3181
 
 
3182
        /* ensure no conf object created */
 
3183
        TEST_HASH_EMPTY (source->files);
 
3184
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3185
        TEST_EQ_P (file, NULL);
 
3186
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3187
        TEST_EQ_P (job, NULL);
 
3188
 
 
3189
        /* create conf */
 
3190
        f = fopen (filename, "w");
 
3191
        TEST_NE_P (f, NULL);
 
3192
        fprintf (f, "start on started\n");
 
3193
        fprintf (f, "emits hello\n");
 
3194
        fprintf (f, "author \"foo\"\n");
 
3195
        fclose (f);
 
3196
 
 
3197
        /* FIXME: crashes here */
 
3198
        TEST_FORCE_WATCH_UPDATE();
 
3199
 
 
3200
        /* ensure conf loaded */
 
3201
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3202
        TEST_NE_P (file, NULL);
 
3203
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3204
        TEST_NE_P (job, NULL);
 
3205
        TEST_EQ_P (file->job, job);
 
3206
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3207
 
 
3208
        /* should pick up override, *NOT* conf */
 
3209
        TEST_EQ_P (job->start_on, NULL);
 
3210
        TEST_EQ_STR (job->author, "bar");
 
3211
 
 
3212
        /* modify override */
 
3213
        f = fopen (override, "w");
 
3214
        TEST_NE_P (f, NULL);
 
3215
        fprintf (f, "author \"meh\"\n");
 
3216
        fprintf (f, "env wibble=wobble\n");
 
3217
        fprintf (f, "manual\n");
 
3218
        fclose (f);
 
3219
 
 
3220
        TEST_FORCE_WATCH_UPDATE();
 
3221
 
 
3222
        /* ensure conf reloaded and updated with override */
 
3223
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3224
        TEST_NE_P (file, NULL);
 
3225
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3226
        TEST_NE_P (job, NULL);
 
3227
        TEST_EQ_P (file->job, job);
 
3228
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3229
 
 
3230
        /* should pick up override, *NOT* conf */
 
3231
        TEST_EQ_P (job->start_on, NULL);
 
3232
        TEST_EQ_STR (job->author, "meh");
 
3233
        TEST_EQ_STR ((job->env)[0], "wibble=wobble");
 
3234
 
 
3235
        /* delete override */
 
3236
        unlink (override);
 
3237
 
 
3238
        TEST_FORCE_WATCH_UPDATE();
 
3239
 
 
3240
        /* ensure conf object reverted */
 
3241
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3242
        TEST_NE_P (file, NULL);
 
3243
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3244
        TEST_NE_P (job, NULL);
 
3245
        TEST_NE_P (job->start_on, NULL);
 
3246
        TEST_EQ_STR (job->author, "foo");
 
3247
        TEST_EQ_P (job->env, NULL);
 
3248
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3249
 
 
3250
        unlink (filename);
 
3251
        nih_free (source);
 
3252
        TEST_EQ (rmdir (dirname), 0);
 
3253
 
 
3254
 
 
3255
        TEST_FEATURE ("create watch, override, conf, then modify/delete override");
 
3256
        TEST_ENSURE_CLEAN_ENV ();
 
3257
        TEST_FILENAME (dirname);
 
3258
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3259
 
 
3260
        strcpy (filename, dirname);
 
3261
        strcat (filename, "/foo.conf");
 
3262
        strcpy (override, dirname);
 
3263
        strcat (override, "/foo.override");
 
3264
 
 
3265
        /* create watch */
 
3266
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
3267
        TEST_NE_P (source, NULL);
 
3268
        ret = conf_source_reload (source);
 
3269
        TEST_EQ (ret, 0);
 
3270
 
 
3271
        TEST_FORCE_WATCH_UPDATE();
 
3272
 
 
3273
        /* create override */
 
3274
        f = fopen (override, "w");
 
3275
        TEST_NE_P (f, NULL);
 
3276
        fprintf (f, "manual\n");
 
3277
        fprintf (f, "author \"bar\"\n");
 
3278
        fclose (f);
 
3279
 
 
3280
        TEST_FORCE_WATCH_UPDATE();
 
3281
 
 
3282
        /* create conf */
 
3283
        f = fopen (filename, "w");
 
3284
        TEST_NE_P (f, NULL);
 
3285
        fprintf (f, "start on started\n");
 
3286
        fprintf (f, "emits hello\n");
 
3287
        fprintf (f, "author \"foo\"\n");
 
3288
        fclose (f);
 
3289
 
 
3290
        TEST_FORCE_WATCH_UPDATE();
 
3291
 
 
3292
        /* ensure conf loaded */
 
3293
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3294
        TEST_NE_P (file, NULL);
 
3295
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3296
        TEST_NE_P (job, NULL);
 
3297
        TEST_EQ_P (file->job, job);
 
3298
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3299
 
 
3300
        /* should pick up override, *NOT* conf */
 
3301
        TEST_EQ_P (job->start_on, NULL);
 
3302
        TEST_EQ_STR (job->author, "bar");
 
3303
 
 
3304
        /* update override */
 
3305
        f = fopen (override, "a");
 
3306
        TEST_NE_P (f, NULL);
 
3307
        fprintf (f, "author \"me\"\n");
 
3308
        fprintf (f, "env wibble=wobble\n");
 
3309
        fclose (f);
 
3310
 
 
3311
        TEST_FORCE_WATCH_UPDATE();
 
3312
 
 
3313
        /* ensure conf reloaded and updated with override */
 
3314
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3315
        TEST_NE_P (file, NULL);
 
3316
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3317
        TEST_NE_P (job, NULL);
 
3318
        TEST_EQ_P (file->job, job);
 
3319
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3320
 
 
3321
        /* should pick up override, *NOT* conf */
 
3322
        TEST_EQ_P (job->start_on, NULL);
 
3323
        TEST_EQ_STR (job->author, "me");
 
3324
        TEST_EQ_STR ((job->env)[0], "wibble=wobble");
 
3325
 
 
3326
        TEST_FORCE_WATCH_UPDATE();
 
3327
 
 
3328
        /* delete override */
 
3329
        unlink (override);
 
3330
 
 
3331
        TEST_FORCE_WATCH_UPDATE();
 
3332
 
 
3333
        /* ensure conf loaded */
 
3334
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3335
        TEST_NE_P (file, NULL);
 
3336
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3337
        TEST_NE_P (job, NULL);
 
3338
        TEST_EQ_P (file->job, job);
 
3339
        TEST_EQ_STR (job->author, "foo");
 
3340
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3341
        TEST_NE_P (job->start_on, NULL);
 
3342
        TEST_EQ_P (job->env, NULL);
 
3343
 
 
3344
        unlink (filename);
 
3345
        nih_free (source);
 
3346
        TEST_EQ (rmdir (dirname), 0);
 
3347
 
 
3348
 
 
3349
        TEST_FEATURE ("create both conf+override files, watch, then modify/delete conf");
 
3350
        TEST_ENSURE_CLEAN_ENV ();
 
3351
        TEST_FILENAME (dirname);
 
3352
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3353
 
 
3354
        /* create conf */
 
3355
        strcpy (filename, dirname);
 
3356
        strcat (filename, "/foo.conf");
 
3357
        f = fopen (filename, "w");
 
3358
        TEST_NE_P (f, NULL);
 
3359
        fprintf (f, "start on started\n");
 
3360
        fprintf (f, "author \"me\"\n");
 
3361
        fprintf (f, "env foo=bar\n");
 
3362
        fprintf (f, "emits hello\n");
 
3363
        fclose (f);
 
3364
 
 
3365
        /* create override */
 
3366
        strcpy (override, dirname);
 
3367
        strcat (override, "/foo.override");
 
3368
        f = fopen (override, "w");
 
3369
        TEST_NE_P (f, NULL);
 
3370
        fprintf (f, "manual\n");
 
3371
        fprintf (f, "author \"you\"\n");
 
3372
        fclose (f);
 
3373
 
 
3374
        /* create watch */
 
3375
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
3376
        TEST_NE_P (source, NULL);
 
3377
        ret = conf_source_reload (source);
 
3378
        TEST_EQ (ret, 0);
 
3379
 
 
3380
        TEST_FORCE_WATCH_UPDATE();
 
3381
 
 
3382
        /* ensure conf loaded */
 
3383
        TEST_HASH_NOT_EMPTY (source->files);
 
3384
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3385
        TEST_NE_P (file, NULL);
 
3386
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3387
        TEST_NE_P (job, NULL);
 
3388
        TEST_EQ_P (file->job, job);
 
3389
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3390
        TEST_EQ_STR (job->author, "you");
 
3391
        TEST_EQ_P (job->start_on, NULL);
 
3392
        TEST_EQ_STR ((job->env)[0], "foo=bar");
 
3393
        TEST_EQ_P (job->export, NULL);
 
3394
 
 
3395
        /* modify conf */
 
3396
        f = fopen (filename, "a");
 
3397
        TEST_NE_P (f, NULL);
 
3398
        fprintf (f, "export foo\n");
 
3399
        fclose (f);
 
3400
 
 
3401
        TEST_FORCE_WATCH_UPDATE();
 
3402
 
 
3403
        /* ensure conf updated */
 
3404
        TEST_HASH_NOT_EMPTY (source->files);
 
3405
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3406
        TEST_NE_P (file, NULL);
 
3407
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3408
        TEST_NE_P (job, NULL);
 
3409
        TEST_EQ_STR ((job->env)[0], "foo=bar");
 
3410
        TEST_NE_P (job->export, NULL);
 
3411
        TEST_EQ_STR ((job->export)[0], "foo");
 
3412
 
 
3413
        /* delete conf */
 
3414
        unlink (filename);
 
3415
 
 
3416
        TEST_FORCE_WATCH_UPDATE();
 
3417
 
 
3418
        /* ensure conf object deleted */
 
3419
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3420
        TEST_EQ_P (file, NULL);
 
3421
        TEST_HASH_EMPTY (source->files);
 
3422
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3423
        TEST_EQ_P (job, NULL);
 
3424
        file = (ConfFile *)nih_hash_lookup (source->files, override);
 
3425
        TEST_EQ_P (file, NULL);
 
3426
 
 
3427
        unlink (override);
 
3428
        nih_free (source);
 
3429
        TEST_EQ (rmdir (dirname), 0);
 
3430
 
 
3431
        TEST_FEATURE ("create both conf+override files, watch, then modify/delete override");
 
3432
        TEST_ENSURE_CLEAN_ENV ();
 
3433
        TEST_FILENAME (dirname);
 
3434
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3435
 
 
3436
        /* create conf */
 
3437
        strcpy (filename, dirname);
 
3438
        strcat (filename, "/foo.conf");
 
3439
        f = fopen (filename, "w");
 
3440
        TEST_NE_P (f, NULL);
 
3441
        fprintf (f, "start on started\n");
 
3442
        fprintf (f, "author \"me\"\n");
 
3443
        fprintf (f, "env foo=bar\n");
 
3444
        fprintf (f, "emits hello\n");
 
3445
        fclose (f);
 
3446
 
 
3447
        /* create override */
 
3448
        strcpy (override, dirname);
 
3449
        strcat (override, "/foo.override");
 
3450
        f = fopen (override, "w");
 
3451
        TEST_NE_P (f, NULL);
 
3452
        fprintf (f, "manual\n");
 
3453
        fprintf (f, "author \"you\"\n");
 
3454
        fclose (f);
 
3455
 
 
3456
        /* create watch */
 
3457
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
3458
        TEST_NE_P (source, NULL);
 
3459
        ret = conf_source_reload (source);
 
3460
        TEST_EQ (ret, 0);
 
3461
 
 
3462
        TEST_FORCE_WATCH_UPDATE();
 
3463
 
 
3464
        /* ensure conf loaded */
 
3465
        TEST_HASH_NOT_EMPTY (source->files);
 
3466
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3467
        TEST_NE_P (file, NULL);
 
3468
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3469
        TEST_NE_P (job, NULL);
 
3470
        TEST_EQ_P (file->job, job);
 
3471
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3472
        TEST_EQ_STR (job->author, "you");
 
3473
        TEST_EQ_P (job->start_on, NULL);
 
3474
        TEST_EQ_STR ((job->env)[0], "foo=bar");
 
3475
        TEST_EQ_P (job->export, NULL);
 
3476
 
 
3477
        /* modify override */
 
3478
        f = fopen (override, "w");
 
3479
        TEST_NE_P (f, NULL);
 
3480
        fprintf (f, "description \"hello world\"\n");
 
3481
        fprintf (f, "author \"ubuntu\"\n");
 
3482
        fclose (f);
 
3483
 
 
3484
        TEST_FORCE_WATCH_UPDATE();
 
3485
 
 
3486
        /* ensure conf updated */
 
3487
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3488
        TEST_NE_P (file, NULL);
 
3489
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3490
        TEST_NE_P (job, NULL);
 
3491
        TEST_EQ_P (file->job, job);
 
3492
        TEST_EQ_STR (job->author, "ubuntu");
 
3493
        TEST_NE_P (job->description, NULL);
 
3494
        TEST_EQ_STR (job->description, "hello world");
 
3495
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3496
        TEST_NE_P (job->start_on, NULL);
 
3497
 
 
3498
        /* delete override */
 
3499
        unlink (override);
 
3500
 
 
3501
        TEST_FORCE_WATCH_UPDATE();
 
3502
 
 
3503
        /* ensure conf updated */
 
3504
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3505
        TEST_NE_P (file, NULL);
 
3506
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3507
        TEST_NE_P (job, NULL);
 
3508
        TEST_EQ_P (file->job, job);
 
3509
        TEST_NE_P (job->start_on, NULL);
 
3510
        TEST_EQ_STR ((job->env)[0], "foo=bar");
 
3511
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3512
        TEST_EQ_STR (job->author, "me");
 
3513
        TEST_EQ_P (job->description, NULL);
 
3514
 
 
3515
        nih_free (source);
 
3516
        unlink (filename);
 
3517
        TEST_EQ (rmdir (dirname), 0);
 
3518
 
 
3519
 
 
3520
        TEST_FEATURE ("create conf, watch, then create invalid override, delete override");
 
3521
        TEST_ENSURE_CLEAN_ENV ();
 
3522
        TEST_FILENAME (dirname);
 
3523
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3524
 
 
3525
        /* create conf */
 
3526
        strcpy (filename, dirname);
 
3527
        strcat (filename, "/foo.conf");
 
3528
        f = fopen (filename, "w");
 
3529
        TEST_NE_P (f, NULL);
 
3530
        fprintf (f, "start on started\n");
 
3531
        fprintf (f, "author \"wibble\"\n");
 
3532
        fprintf (f, "emits hello\n");
 
3533
        fclose (f);
 
3534
 
 
3535
        /* create watch */
 
3536
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
3537
        TEST_NE_P (source, NULL);
 
3538
        ret = conf_source_reload (source);
 
3539
        TEST_EQ (ret, 0);
 
3540
 
 
3541
        TEST_FORCE_WATCH_UPDATE();
 
3542
 
 
3543
        /* ensure conf loaded */
 
3544
        TEST_HASH_NOT_EMPTY (source->files);
 
3545
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3546
        TEST_NE_P (file, NULL);
 
3547
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3548
        TEST_NE_P (job, NULL);
 
3549
        TEST_EQ_P (file->job, job);
 
3550
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3551
        TEST_NE_P (job->start_on, NULL);
 
3552
 
 
3553
        /* create (partially) invalid override (which should be
 
3554
         * fully ignored)
 
3555
         */
 
3556
        strcpy (override, dirname);
 
3557
        strcat (override, "/foo.override");
 
3558
        f = fopen (override, "w");
 
3559
        TEST_NE_P (f, NULL);
 
3560
        fprintf (f, "manual\n");
 
3561
        fprintf (f, "bleaugh!\n");
 
3562
        fprintf (f, "wha...?\n");
 
3563
        fprintf (f, "author \"moo\"\n");
 
3564
        fclose (f);
 
3565
 
 
3566
        TEST_FORCE_WATCH_UPDATE();
 
3567
 
 
3568
        unlink (override);
 
3569
 
 
3570
        TEST_FORCE_WATCH_UPDATE();
 
3571
 
 
3572
        /* ensure conf still loaded */
 
3573
        TEST_HASH_NOT_EMPTY (source->files);
 
3574
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3575
        TEST_NE_P (file, NULL);
 
3576
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3577
        TEST_NE_P (job, NULL);
 
3578
        TEST_EQ_P (file->job, job);
 
3579
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3580
        TEST_NE_P (job->start_on, NULL);
 
3581
        TEST_EQ_STR (job->author, "wibble");
 
3582
 
 
3583
        nih_free (source);
 
3584
        unlink (filename);
 
3585
        TEST_EQ (rmdir (dirname), 0);
 
3586
 
 
3587
        TEST_FEATURE ("ensure override ignored for CONF_FILE");
 
3588
        TEST_ENSURE_CLEAN_ENV ();
 
3589
        TEST_FILENAME (dirname);
 
3590
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3591
 
 
3592
        /* create empty conf */
 
3593
        strcpy (filename, dirname);
 
3594
        strcat (filename, "/init.conf");
 
3595
        f = fopen (filename, "w");
 
3596
        TEST_NE_P (f, NULL);
 
3597
        fclose (f);
 
3598
 
 
3599
        /* create watch */
 
3600
        source = conf_source_new (NULL, dirname, CONF_FILE);
 
3601
        TEST_NE_P (source, NULL);
 
3602
        ret = conf_source_reload (source);
 
3603
 
 
3604
        /* We expect conf_source_reload to fail in this situation since
 
3605
         * although "init.conf" is a supported config file, it is not
 
3606
         * allowed to contain any stanzas, implying that it can only
 
3607
         * contain comments. In fact, if the file exists but is zero
 
3608
         * size that is currently an error since upstart blindly calls
 
3609
         * nih_file_read(), which will fail since there are no bytes to
 
3610
         * read.
 
3611
         */
 
3612
        TEST_NE (ret, 0);
 
3613
        err = nih_error_steal ();
 
3614
        TEST_EQ (err->number, EILSEQ);
 
3615
        nih_free (err);
 
3616
 
 
3617
        TEST_FORCE_WATCH_UPDATE();
 
3618
 
 
3619
        /* ensure conf NOT loaded */
 
3620
        TEST_HASH_EMPTY (source->files);
 
3621
 
 
3622
        /* create override */
 
3623
        strcpy (override, dirname);
 
3624
        strcat (override, "/init.override");
 
3625
        f = fopen (override, "w");
 
3626
        TEST_NE_P (f, NULL);
 
3627
        fprintf (f, "manual\n");
 
3628
        fclose (f);
 
3629
 
 
3630
        TEST_FORCE_WATCH_UPDATE();
 
3631
 
 
3632
        /* ensure conf still NOT loaded */
 
3633
        TEST_HASH_EMPTY (source->files);
 
3634
 
 
3635
        nih_free (source);
 
3636
        unlink (filename);
 
3637
        unlink (override);
 
3638
        TEST_EQ (rmdir (dirname), 0);
 
3639
 
 
3640
        /* Consume all available inotify instances so that the following
 
3641
         * tests run without inotify.
 
3642
         */
 
3643
        for (i = 0; i < 4096; i++)
 
3644
                if ((fd[i] = inotify_init ()) < 0)
 
3645
                        break;
 
3646
 
 
3647
no_inotify:
 
3648
        /* If you don't have inotify, any override file must exist
 
3649
         * before the system boots.
 
3650
         */ 
 
3651
 
 
3652
        TEST_FEATURE ("both conf+override files with no inotify support");
 
3653
        TEST_ENSURE_CLEAN_ENV ();
 
3654
        TEST_FILENAME (dirname);
 
3655
        TEST_EQ (mkdir (dirname, 0755), 0);
 
3656
 
 
3657
        /* create conf */
 
3658
        strcpy (filename, dirname);
 
3659
        strcat (filename, "/foo.conf");
 
3660
        f = fopen (filename, "w");
 
3661
        TEST_NE_P (f, NULL);
 
3662
        fprintf (f, "start on started\n");
 
3663
        fprintf (f, "author \"me\"\n");
 
3664
        fprintf (f, "env foo=bar\n");
 
3665
        fprintf (f, "emits hello\n");
 
3666
        fclose (f);
 
3667
 
 
3668
        /* create override */
 
3669
        strcpy (override, dirname);
 
3670
        strcat (override, "/foo.override");
 
3671
        f = fopen (override, "w");
 
3672
        TEST_NE_P (f, NULL);
 
3673
        fprintf (f, "manual\n");
 
3674
        fprintf (f, "author \"you\"\n");
 
3675
        fclose (f);
 
3676
 
 
3677
        /* create watch */
 
3678
        source = conf_source_new (NULL, dirname, CONF_JOB_DIR);
 
3679
        TEST_NE_P (source, NULL);
 
3680
        ret = conf_source_reload (source);
 
3681
        TEST_EQ (ret, 0);
 
3682
 
 
3683
        /* ensure conf loaded */
 
3684
        TEST_HASH_NOT_EMPTY (source->files);
 
3685
        file = (ConfFile *)nih_hash_lookup (source->files, filename);
 
3686
        TEST_NE_P (file, NULL);
 
3687
        job = (JobClass *)nih_hash_lookup (job_classes, "foo");
 
3688
        TEST_NE_P (job, NULL);
 
3689
        TEST_EQ_P (file->job, job);
 
3690
        TEST_EQ_STR ((job->emits)[0], "hello");
 
3691
        TEST_EQ_STR (job->author, "you");
 
3692
        TEST_EQ_P (job->start_on, NULL);
 
3693
        TEST_EQ_STR ((job->env)[0], "foo=bar");
 
3694
        TEST_EQ_P (job->export, NULL);
 
3695
 
 
3696
        nih_free (source);
 
3697
        unlink (filename);
 
3698
        unlink (override);
 
3699
        TEST_EQ (rmdir (dirname), 0);
 
3700
 
 
3701
 
 
3702
        nih_log_set_priority (NIH_LOG_MESSAGE);
 
3703
 
 
3704
        /* Release consumed instances */
 
3705
        for (i = 0; i < 4096; i++) {
 
3706
                if (fd[i] < 0)
 
3707
                        break;
 
3708
 
 
3709
                close (fd[i]);
 
3710
        }
 
3711
}
2419
3712
 
2420
3713
void
2421
3714
test_source_reload_file (void)
2968
4261
        TEST_HASH_EMPTY (source->files);
2969
4262
 
2970
4263
        nih_free (source);
2971
 
 
2972
 
 
2973
4264
        /* Consume all available inotify instances so that the following
2974
4265
         * tests run without inotify.
2975
4266
         */
3458
4749
        test_source_reload_conf_dir ();
3459
4750
        test_source_reload_file ();
3460
4751
        test_source_reload ();
 
4752
        test_toggle_conf_name ();
 
4753
        test_override ();
3461
4754
        test_file_destroy ();
3462
4755
        test_select_job ();
3463
4756