1 /**
2 Copyright: Copyright (c) 2018, Joakim Brännström. All rights reserved.
3 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 
6 #TST-test_configure_env_import_file
7 */
8 module test_configure_env_import_file;
9 
10 import config;
11 
12 shared(bool) g_preCondition;
13 
14 auto preCondition(string test_area) {
15     prepareDistssh;
16 
17     immutable script = `#!/bin/bash
18 echo "smurf is $SMURF"`;
19 
20     // arrange
21     const script_file = buildPath(test_area, "foo.sh");
22     string[string] fake_env;
23 
24     // action
25     File(script_file, "w").write(script);
26     synchronized {
27         if (!g_preCondition) {
28             g_preCondition = true;
29             fake_env["SMURF"] = "42";
30             assert(spawnShell(distssh ~ " --export-env", stdin, stdout, stderr,
31                     fake_env).wait == 0, "failed to export env");
32         }
33     }
34 
35     return script_file;
36 }
37 
38 @(
39         "shall print the environment variable that is part of the export when executing on the remote host")
40 unittest {
41     // arrange
42     auto area = TestArea(__FILE__, __LINE__);
43     const script_file = preCondition(area);
44 
45     // action
46     auto res = executeShell(distssh ~ " --local-run -- bash " ~ script_file);
47 
48     // assert
49     assert(res.status == 0, "failed executing: " ~ res.output);
50     assert(res.output.canFind("42"),
51             "failed finding 42 which should have been part of the imported env");
52 }
53 
54 @("shall NOT import the environment from the default file")
55 unittest {
56     // arrange
57     auto area = TestArea(__FILE__, __LINE__);
58     const script_file = preCondition(area);
59 
60     // action
61     auto res = executeShell(distssh ~ " --no-import-env --local-run -- bash " ~ script_file);
62 
63     // assert
64     assert(res.status == 0, "failed executing: " ~ res.output);
65     assert(!res.output.canFind("42"), "env imported when it shouldn't have been");
66 }
67 
68 @("shall import the env from the specified file specified via CLI")
69 unittest {
70     // arrange
71     auto area = TestArea(__FILE__, __LINE__);
72     const script_file = preCondition(area);
73 
74     // action
75     auto fake_env = ["SMURF" : "43"];
76     const env_file = buildPath(area, "myenv.export");
77     assert(spawnShell(distssh ~ " --export-env --export-env-file " ~ env_file,
78             stdin, stdout, stderr, fake_env).wait == 0, "failed to export env");
79     auto res = executeShell(format(distssh ~ " -i %s --local-run -- bash " ~ script_file, env_file));
80 
81     // assert
82     assert(res.status == 0, "failed executing: " ~ res.output);
83     assert(res.output.canFind("43"), "specified env file not imported");
84 }
85 
86 @("shall import the env from the specified file specified via DISTSSH_IMPORT_ENV")
87 unittest {
88     // arrange
89     auto area = TestArea(__FILE__, __LINE__);
90     const script_file = preCondition(area);
91 
92     // action
93     const env_file = buildPath(area, "myenv.export");
94     auto fake_env = ["SMURF" : "43"];
95     assert(spawnShell(distssh ~ " --export-env --export-env-file " ~ env_file,
96             stdin, stdout, stderr, fake_env).wait == 0, "failed to export env");
97 
98     fake_env = ["DISTSSH_IMPORT_ENV" : env_file];
99     auto res = executeShell(distssh ~ " --local-run -- bash " ~ script_file, fake_env);
100 
101     // assert
102     assert(res.status == 0, "failed executing: " ~ res.output);
103     assert(res.output.canFind("43"), "specified env file not imported");
104 }