From: Davin McCall Date: Thu, 7 Jun 2018 18:05:48 +0000 (+0100) Subject: Add loading test for environment variable substitution. X-Git-Tag: v0.2.0~7 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=6c8ab2975ab84187aa156b0099f888d97d3d61cc;p=oweals%2Fdinit.git Add loading test for environment variable substitution. --- diff --git a/src/includes/proc-service.h b/src/includes/proc-service.h index 2ebe32e..76fef21 100644 --- a/src/includes/proc-service.h +++ b/src/includes/proc-service.h @@ -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 & get_exec_arg_parts() noexcept + { + return exec_arg_parts; + } }; // Standard process service. diff --git a/src/tests/loadtests.cc b/src/tests/loadtests.cc index 3341002..ec0e2a4 100644 --- a/src/tests/loadtests.cc +++ b/src/tests/loadtests.cc @@ -1,8 +1,11 @@ #include #include #include +#include +#include #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(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 index 0000000..b9cdf1d --- /dev/null +++ b/src/tests/test-services/t2 @@ -0,0 +1,3 @@ +type = process +load-options = sub-vars +command = echo $ONEVAR $TWOVAR $THREEVAR