Add loading test for environment variable substitution.
authorDavin McCall <davmac@davmac.org>
Thu, 7 Jun 2018 18:05:48 +0000 (19:05 +0100)
committerDavin McCall <davmac@davmac.org>
Thu, 7 Jun 2018 20:22:31 +0000 (21:22 +0100)
src/includes/proc-service.h
src/tests/loadtests.cc
src/tests/test-services/t2 [new file with mode: 0644]

index 2ebe32eedf8d05a8a7404f3eabe3b58bd17e3529..76fef21df6ec46f3de7f3098b702a54cc41c3db9 100644 (file)
@@ -203,6 +203,12 @@ class base_process_service : public service_record
 
     // The restart/stop timer expired.
     void timer_expired() noexcept;
+
+    // Accessor for testing:
+    const std::vector<const char *> & get_exec_arg_parts() noexcept
+    {
+        return exec_arg_parts;
+    }
 };
 
 // Standard process service.
index 334100225782496646ed8a35d6aee186dbb0d52b..ec0e2a439067357e2fd2639a0eaad75dfcabce6a 100644 (file)
@@ -1,8 +1,11 @@
 #include <string>
 #include <iostream>
 #include <cassert>
+#include <cstdlib>
+#include <cstring>
 
 #include "service.h"
+#include "proc-service.h"
 
 std::string test_service_dir;
 
@@ -11,13 +14,27 @@ void init_test_service_dir()
     test_service_dir = "./test-services";
 }
 
-void test1()
+void test_basic()
 {
     dirload_service_set sset(test_service_dir.c_str());
     auto t1 = sset.load_service("t1");
     assert(t1->get_name() == "t1");
 }
 
+void test_env_subst()
+{
+    dirload_service_set sset(test_service_dir.c_str());
+    setenv("ONEVAR", "a", true);
+    setenv("TWOVAR", "hellohello", true);
+    // leave THREEVAR undefined
+    auto t2 = static_cast<base_process_service *>(sset.load_service("t2"));
+    auto exec_parts = t2->get_exec_arg_parts();
+    assert(strcmp("echo", exec_parts[0]) == 0);
+    assert(strcmp("a", exec_parts[1]) == 0);
+    assert(strcmp("hellohello", exec_parts[2]) == 0);
+    assert(strcmp("", exec_parts[3]) == 0);
+}
+
 #define RUN_TEST(name, spacing) \
     std::cout << #name "..." spacing; \
     name(); \
@@ -26,6 +43,7 @@ void test1()
 int main(int argc, char **argv)
 {
     init_test_service_dir();
-    RUN_TEST(test1, "    ");
+    RUN_TEST(test_basic, "    ");
+    RUN_TEST(test_env_subst, "");
     return 0;
 }
diff --git a/src/tests/test-services/t2 b/src/tests/test-services/t2
new file mode 100644 (file)
index 0000000..b9cdf1d
--- /dev/null
@@ -0,0 +1,3 @@
+type = process
+load-options = sub-vars
+command = echo $ONEVAR $TWOVAR $THREEVAR