~jamesodhunt/upstart/bug-1197225

« back to all changes in this revision

Viewing changes to util/tests/test_initctl.c

  • Committer: James Hunt
  • Date: 2013-06-20 09:50:07 UTC
  • mfrom: (1475.3.5 upstart-bug-1159895)
  • Revision ID: james.hunt@ubuntu.com-20130620095007-584ppy0zhxfslcbs
* Merged lp:~jamesodhunt/upstart/bug-1159895.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16436
16436
        assert (upstart_pid);
16437
16437
        assert (dbus_pid);
16438
16438
 
16439
 
        /*******************************************************************/
16440
 
        /* Ensure basic variables are set in the current environment */
16441
 
 
16442
 
        if (! getenv ("TERM")) {
16443
 
                fprintf (stderr, "WARNING: setting TERM to '%s' as not set\n",
16444
 
                                TEST_INITCTL_DEFAULT_TERM);
16445
 
                assert0 (setenv ("TERM", TEST_INITCTL_DEFAULT_TERM, 1));
16446
 
        }
16447
 
 
16448
 
        if (! getenv ("PATH")) {
16449
 
                fprintf (stderr, "WARNING: setting PATH to '%s' as not set\n",
16450
 
                                TEST_INITCTL_DEFAULT_PATH);
16451
 
                assert0 (setenv ("PATH", TEST_INITCTL_DEFAULT_PATH, 1));
16452
 
        }
16453
 
 
16454
16439
        cmd = nih_sprintf (NULL, "%s reset-env 2>&1", get_initctl ());
16455
16440
        TEST_NE_P (cmd, NULL);
16456
16441
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16463
16448
        TEST_NE_P (cmd, NULL);
16464
16449
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16465
16450
 
16466
 
        TEST_EQ (line_count, 2);
16467
 
        TEST_STR_MATCH (output[0], "PATH=*");
16468
 
        TEST_STR_MATCH (output[1], "TERM=*");
 
16451
        TEST_GE (line_count, 2);
 
16452
        TEST_STR_ARRAY_CONTAINS (output, "PATH=*");
 
16453
        TEST_STR_ARRAY_CONTAINS (output, "TERM=*");
16469
16454
        nih_free (output);
16470
16455
 
16471
16456
        /*******************************************************************/
16475
16460
        TEST_NE_P (cmd, NULL);
16476
16461
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16477
16462
 
16478
 
        TEST_EQ (line_count, 2);
16479
 
        TEST_STR_MATCH (output[0], "PATH=*");
16480
 
        TEST_STR_MATCH (output[1], "TERM=*");
 
16463
        TEST_GE (line_count, 2);
 
16464
        TEST_STR_ARRAY_CONTAINS (output, "PATH=*");
 
16465
        TEST_STR_ARRAY_CONTAINS (output, "TERM=*");
16481
16466
        nih_free (output);
16482
16467
 
16483
16468
        /*******************************************************************/
16542
16527
 
16543
16528
        fi = fopen (logfile, "r");
16544
16529
        TEST_NE_P (fi, NULL);
16545
 
        TEST_FILE_MATCH (fi, "PATH=*");
16546
 
        TEST_FILE_MATCH (fi, "TERM=*");
 
16530
        TEST_FILE_CONTAINS (fi, "PATH=*");
 
16531
        TEST_FILE_CONTAINS (fi, "TERM=*");
16547
16532
 
16548
16533
        /* asterisk required to match '\r\n' */
16549
 
        TEST_FILE_MATCH (fi, "UPSTART_JOB=foo*");
16550
 
        TEST_FILE_MATCH (fi, "UPSTART_INSTANCE=*");
16551
 
        TEST_FILE_MATCH (fi, "UPSTART_SESSION=*");
16552
 
        TEST_FILE_END (fi);
 
16534
        TEST_FILE_CONTAINS (fi, "UPSTART_JOB=foo*");
 
16535
        TEST_FILE_CONTAINS (fi, "UPSTART_INSTANCE=*");
 
16536
        TEST_FILE_CONTAINS (fi, "UPSTART_SESSION=*");
16553
16537
        fclose (fi);
16554
16538
 
16555
 
 
16556
16539
        DELETE_FILE (confdir, "foo.conf");
16557
16540
        TEST_EQ (unlink (logfile), 0);
16558
16541
 
16729
16712
        cmd = nih_sprintf (NULL, "%s reset-env 2>&1", get_initctl ());
16730
16713
        TEST_NE_P (cmd, NULL);
16731
16714
        RUN_COMMAND (NULL, cmd, &output, &line_count);
 
16715
        TEST_EQ (line_count, 0);
16732
16716
        nih_free (output);
16733
16717
 
16734
16718
        /* Ensure nothing changed */
16746
16730
                        name, value);
16747
16731
        TEST_NE_P (cmd, NULL);
16748
16732
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16749
 
        nih_free (output);
 
16733
        TEST_EQ (line_count, 0);
16750
16734
 
16751
16735
        cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
16752
16736
                        name);
16767
16751
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16768
16752
        TEST_EQ (line_count, 1);
16769
16753
        TEST_EQ_STR (output[0], "initctl: No such variable: foo");
 
16754
        nih_free (output);
16770
16755
 
16771
16756
        /*******************************************************************/
16772
16757
        TEST_FEATURE ("set-env in 'name=' form");
16777
16762
                        name);
16778
16763
        TEST_NE_P (cmd, NULL);
16779
16764
        RUN_COMMAND (NULL, cmd, &output, &line_count);
 
16765
        TEST_EQ (line_count, 0);
16780
16766
        nih_free (output);
16781
16767
 
16782
16768
        cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
16802
16788
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16803
16789
        TEST_EQ (line_count, 1);
16804
16790
        TEST_EQ_STR (output[0], "initctl: No such variable: foo");
 
16791
        nih_free (output);
16805
16792
 
16806
16793
        /*******************************************************************/
16807
16794
        TEST_FEATURE ("set-env in 'name' form");
16837
16824
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16838
16825
        TEST_EQ (line_count, 1);
16839
16826
        TEST_EQ_STR (output[0], "initctl: No such variable: foo");
 
16827
        nih_free (output);
16840
16828
 
16841
16829
        /*******************************************************************/
16842
16830
        TEST_FEATURE ("set-env for already set variable");
16849
16837
                        name, value);
16850
16838
        TEST_NE_P (cmd, NULL);
16851
16839
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16852
 
        nih_free (output);
 
16840
        TEST_EQ (line_count, 0);
16853
16841
 
16854
16842
        /* check it */
16855
16843
        cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
16866
16854
                        name, value);
16867
16855
        TEST_NE_P (cmd, NULL);
16868
16856
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16869
 
        nih_free (output);
 
16857
        TEST_EQ (line_count, 0);
16870
16858
 
16871
16859
        /* check it again */
16872
16860
        cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
16889
16877
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16890
16878
        TEST_EQ (line_count, 1);
16891
16879
        TEST_EQ_STR (output[0], "initctl: No such variable: foo");
 
16880
        nih_free (output);
16892
16881
 
16893
16882
        /*******************************************************************/
16894
16883
        TEST_FEATURE ("set-env --retain");
16901
16890
                        name, value);
16902
16891
        TEST_NE_P (cmd, NULL);
16903
16892
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16904
 
        nih_free (output);
 
16893
        TEST_EQ (line_count, 0);
16905
16894
 
16906
16895
        /* check it */
16907
16896
        cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
16917
16906
                        get_initctl (), name, "HELLO");
16918
16907
        TEST_NE_P (cmd, NULL);
16919
16908
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16920
 
        nih_free (output);
 
16909
        TEST_EQ (line_count, 0);
16921
16910
 
16922
16911
        /* check that value did *NOT* change */
16923
16912
        cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
16939
16928
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16940
16929
        TEST_EQ (line_count, 1);
16941
16930
        TEST_EQ_STR (output[0], "initctl: No such variable: foo");
 
16931
        nih_free (output);
16942
16932
 
16943
16933
        /*******************************************************************/
16944
16934
        TEST_FEATURE ("set-env with space within value and trailing tab");
16950
16940
                        name, value);
16951
16941
        TEST_NE_P (cmd, NULL);
16952
16942
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16953
 
        nih_free (output);
 
16943
        TEST_EQ (line_count, 0);
16954
16944
 
16955
16945
        cmd = nih_sprintf (NULL, "%s get-env %s 2>&1", get_initctl (),
16956
16946
                        name);
16971
16961
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16972
16962
        TEST_EQ (line_count, 1);
16973
16963
        TEST_EQ_STR (output[0], "initctl: No such variable: foo");
 
16964
        nih_free (output);
16974
16965
 
16975
16966
        /*******************************************************************/
16976
16967
        TEST_FEATURE ("list-env output order");
16981
16972
                        "zygote", "cell");
16982
16973
        TEST_NE_P (cmd, NULL);
16983
16974
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16984
 
        nih_free (output);
 
16975
        TEST_EQ (line_count, 0);
16985
16976
 
16986
16977
        cmd = nih_sprintf (NULL, "%s set-env %s='%s' 2>&1", get_initctl (),
16987
16978
                        "median", "middle");
16988
16979
        TEST_NE_P (cmd, NULL);
16989
16980
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16990
 
        nih_free (output);
 
16981
        TEST_EQ (line_count, 0);
16991
16982
 
16992
16983
        cmd = nih_sprintf (NULL, "%s set-env %s='%s' 2>&1", get_initctl (),
16993
16984
                        "aardvark", "mammal");
16994
16985
        TEST_NE_P (cmd, NULL);
16995
16986
        RUN_COMMAND (NULL, cmd, &output, &line_count);
16996
 
        nih_free (output);
 
16987
        TEST_EQ (line_count, 0);
16997
16988
 
16998
16989
        cmd = nih_sprintf (NULL, "%s list-env 2>&1", get_initctl ());
16999
16990
        TEST_NE_P (cmd, NULL);
17037
17028
                        "aardvark", "mammal");
17038
17029
        TEST_NE_P (cmd, NULL);
17039
17030
        RUN_COMMAND (NULL, cmd, &output, &line_count);
17040
 
        nih_free (output);
 
17031
        TEST_EQ (line_count, 0);
17041
17032
 
17042
17033
        cmd = nih_sprintf (NULL, "%s set-env %s='%s' 2>&1", get_initctl (),
17043
17034
                        "zygote", "cell");
17044
17035
        TEST_NE_P (cmd, NULL);
17045
17036
        RUN_COMMAND (NULL, cmd, &output, &line_count);
17046
 
        nih_free (output);
 
17037
        TEST_EQ (line_count, 0);
17047
17038
 
17048
17039
        cmd = nih_sprintf (NULL, "%s list-env 2>&1", get_initctl ());
17049
17040
        TEST_NE_P (cmd, NULL);
17067
17058
                        "aardvark", "mammal");
17068
17059
        TEST_NE_P (cmd, NULL);
17069
17060
        RUN_COMMAND (NULL, cmd, &output, &line_count);
17070
 
        nih_free (output);
 
17061
        TEST_EQ (line_count, 0);
17071
17062
 
17072
17063
        cmd = nih_sprintf (NULL, "%s set-env %s='%s' 2>&1", get_initctl (),
17073
17064
                        "FOO", "BAR");
17074
17065
        TEST_NE_P (cmd, NULL);
17075
17066
        RUN_COMMAND (NULL, cmd, &output, &line_count);
17076
 
        nih_free (output);
 
17067
        TEST_EQ (line_count, 0);
17077
17068
 
17078
17069
        cmd = nih_sprintf (NULL, "%s set-env %s='%s' 2>&1", get_initctl (),
17079
17070
                        "_________", "_________");
17080
17071
        TEST_NE_P (cmd, NULL);
17081
17072
        RUN_COMMAND (NULL, cmd, &output, &line_count);
17082
 
        nih_free (output);
 
17073
        TEST_EQ (line_count, 0);
17083
17074
 
17084
17075
        CREATE_FILE (confdir, "modified-env.conf", "exec env");
17085
17076
 
17086
17077
        cmd = nih_sprintf (NULL, "%s start modified-env 2>&1", get_initctl ());
17087
17078
        TEST_NE_P (cmd, NULL);
17088
17079
        RUN_COMMAND (NULL, cmd, &output, &line_count);
 
17080
        TEST_EQ (line_count, 1);
17089
17081
        nih_free (output);
17090
17082
 
17091
17083
        logfile = NIH_MUST (nih_sprintf (NULL, "%s/%s",
17211
17203
        TEST_NE_P (cmd, NULL);
17212
17204
 
17213
17205
        RUN_COMMAND (NULL, cmd, &output, &line_count);
 
17206
        TEST_EQ (line_count, 1);
17214
17207
        nih_free (output);
17215
17208
 
17216
17209
        logfile = NIH_MUST (nih_sprintf (NULL, "%s/%s",
17254
17247
        cmd = nih_sprintf (NULL, "%s start foo 2>&1", get_initctl ());
17255
17248
        TEST_NE_P (cmd, NULL);
17256
17249
        RUN_COMMAND (NULL, cmd, &output, &line_count);
 
17250
        TEST_EQ (line_count, 1);
17257
17251
        nih_free (output);
17258
17252
 
17259
17253
        logfile = NIH_MUST (nih_sprintf (NULL, "%s/%s",
17333
17327
        /*******************************************************************/
17334
17328
}
17335
17329
 
 
17330
void
 
17331
test_no_inherit_job_env (const char *runtimedir, const char *confdir, const char *logdir)
 
17332
{
 
17333
        nih_local char  *cmd = NULL;
 
17334
        char           **output;
 
17335
        size_t           lines;
 
17336
        pid_t            upstart_pid = 0;
 
17337
        char            *extra[] = { "--no-inherit-env", NULL };
 
17338
        nih_local char  *logfile = NULL;
 
17339
        nih_local char  *session_file = NULL;
 
17340
        FILE            *fi;
 
17341
 
 
17342
        start_upstart_common (&upstart_pid, TRUE, confdir, logdir, extra);
 
17343
 
 
17344
        /*******************************************************************/
 
17345
        TEST_FEATURE ("ensure list-env in '--user --no-inherit-env' environment gives expected output");
 
17346
 
 
17347
        cmd = nih_sprintf (NULL, "%s list-env 2>&1", get_initctl ());
 
17348
        TEST_NE_P (cmd, NULL);
 
17349
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
17350
 
 
17351
        /* environment should comprise the default environment only */
 
17352
        TEST_EQ (lines, 2);
 
17353
        TEST_STR_MATCH (output[0], "PATH=*");
 
17354
        TEST_STR_MATCH (output[1], "TERM=*");
 
17355
        nih_free (output);
 
17356
 
 
17357
        /*******************************************************************/
 
17358
        TEST_FEATURE ("ensure '--user --no-inherit-env' provides expected job environment");
 
17359
 
 
17360
        CREATE_FILE (confdir, "foo.conf", "exec env");
 
17361
 
 
17362
        cmd = nih_sprintf (NULL, "%s start foo 2>&1", get_initctl ());
 
17363
        TEST_NE_P (cmd, NULL);
 
17364
        RUN_COMMAND (NULL, cmd, &output, &lines);
 
17365
        nih_free (output);
 
17366
 
 
17367
        logfile = NIH_MUST (nih_sprintf (NULL, "%s/%s",
 
17368
                                logdir,
 
17369
                                "foo.log"));
 
17370
 
 
17371
        WAIT_FOR_FILE (logfile);
 
17372
 
 
17373
        fi = fopen (logfile, "r");
 
17374
        TEST_NE_P (fi, NULL);
 
17375
        TEST_FILE_CONTAINS (fi, "PATH=*");
 
17376
        TEST_FILE_CONTAINS (fi, "TERM=*");
 
17377
 
 
17378
        /* asterisk required to match '\r\n' */
 
17379
        TEST_FILE_CONTAINS (fi, "UPSTART_JOB=foo*");
 
17380
        TEST_FILE_CONTAINS (fi, "UPSTART_INSTANCE=*");
 
17381
        TEST_FILE_CONTAINS (fi, "UPSTART_SESSION=*");
 
17382
        fclose (fi);
 
17383
 
 
17384
        DELETE_FILE (confdir, "foo.conf");
 
17385
        TEST_EQ (unlink (logfile), 0);
 
17386
 
 
17387
        /*******************************************************************/
 
17388
 
 
17389
        session_file = NIH_MUST (nih_sprintf (NULL, "%s/upstart/sessions/%d.session",
 
17390
                                runtimedir, (int)upstart_pid));
 
17391
 
 
17392
        STOP_UPSTART (upstart_pid);
 
17393
 
 
17394
        unlink (session_file);
 
17395
}
 
17396
 
17336
17397
/*
17337
17398
 * Test all the commands which affect the job environment table together
17338
17399
 * as they are so closely related.
17376
17437
 
17377
17438
        TEST_EQ (setenv ("XDG_RUNTIME_DIR", runtimedir, 1), 0);
17378
17439
 
 
17440
        /*******************************************************************/
 
17441
        /* Ensure basic variables are set in the current environment */
 
17442
 
 
17443
        if (! getenv ("TERM")) {
 
17444
                fprintf (stderr, "WARNING: setting TERM to '%s' as not set\n",
 
17445
                                TEST_INITCTL_DEFAULT_TERM);
 
17446
                assert0 (setenv ("TERM", TEST_INITCTL_DEFAULT_TERM, 1));
 
17447
        }
 
17448
 
 
17449
        if (! getenv ("PATH")) {
 
17450
                fprintf (stderr, "WARNING: setting PATH to '%s' as not set\n",
 
17451
                                TEST_INITCTL_DEFAULT_PATH);
 
17452
                assert0 (setenv ("PATH", TEST_INITCTL_DEFAULT_PATH, 1));
 
17453
        }
 
17454
 
17379
17455
        TEST_DBUS (dbus_pid);
17380
17456
        start_upstart_common (&upstart_pid, TRUE, confdir, logdir, NULL);
17381
17457
 
17410
17486
        /*******************************************************************/
17411
17487
 
17412
17488
        STOP_UPSTART (upstart_pid);
 
17489
        session_file = NIH_MUST (nih_sprintf (NULL, "%s/upstart/sessions/%d.session",
 
17490
                                runtimedir, (int)upstart_pid));
 
17491
        unlink (session_file);
 
17492
 
 
17493
        /*******************************************************************/
 
17494
 
 
17495
        test_no_inherit_job_env (runtimedir, confdir, logdir);
 
17496
 
 
17497
        /*******************************************************************/
 
17498
 
17413
17499
        TEST_DBUS_END (dbus_pid);
17414
17500
        assert0 (unsetenv ("UPSTART_CONFDIR"));
17415
17501
        assert0 (unsetenv ("UPSTART_LOGDIR"));
17416
17502
        assert0 (unsetenv ("UPSTART_SESSION"));
17417
17503
 
17418
 
        session_file = NIH_MUST (nih_sprintf (NULL, "%s/upstart/sessions/%d.session",
17419
 
                                runtimedir, (int)upstart_pid));
17420
 
        unlink (session_file);
17421
17504
        session_file = NIH_MUST (nih_sprintf (NULL, "%s/upstart/sessions", runtimedir));
17422
17505
        TEST_EQ (rmdir (session_file), 0);
17423
17506
        session_file = NIH_MUST (nih_sprintf (NULL, "%s/upstart", runtimedir));