From: Zbigniew Jędrzejewski-Szmek Date: Wed, 21 Oct 2020 09:29:00 +0000 (+0200) Subject: tests: add helper function to autodetect CI environments X-Git-Tag: v247-rc1~21^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4eb0c875f8825199a829ddc597874915fbee0a84;p=thirdparty%2Fsystemd.git tests: add helper function to autodetect CI environments Sadly there is no standarized way to check if we're running in some CI environment. So let's try to gather the heuristics in one helper function. --- diff --git a/src/basic/string-util.h b/src/basic/string-util.h index cefbda3577b..a68d88c30bc 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -33,6 +33,12 @@ static inline bool streq_ptr(const char *a, const char *b) { return strcmp_ptr(a, b) == 0; } +static inline char* strstr_ptr(const char *haystack, const char *needle) { + if (!haystack || !needle) + return NULL; + return strstr(haystack, needle); +} + static inline const char* strempty(const char *s) { return s ?: ""; } diff --git a/src/shared/tests.c b/src/shared/tests.c index fe6d9dfbd50..808e2e6040b 100644 --- a/src/shared/tests.c +++ b/src/shared/tests.c @@ -301,3 +301,43 @@ int enter_cgroup_subroot(char **ret_cgroup) { int enter_cgroup_root(char **ret_cgroup) { return enter_cgroup(ret_cgroup, false); } + +const char *ci_environment(void) { + /* We return a string because we might want to provide multiple bits of information later on: not + * just the general CI environment type, but also whether we're sanitizing or not, etc. The caller is + * expected to use strstr on the returned value. */ + static const char *ans = POINTER_MAX; + const char *p; + int r; + + if (ans != POINTER_MAX) + return ans; + + /* We allow specifying the environment with $CITYPE. Nobody uses this so far, but we are ready. */ + p = getenv("CITYPE"); + if (!isempty(p)) + return (ans = p); + + if (getenv_bool("TRAVIS") > 0) + return (ans = "travis"); + if (getenv_bool("SEMAPHORE") > 0) + return (ans = "semaphore"); + if (getenv_bool("GITHUB_ACTIONS") > 0) + return (ans = "github-actions"); + if (getenv("AUTOPKGTEST_ARTIFACTS") || getenv("AUTOPKGTEST_TMP")) + return (ans = "autopkgtest"); + + FOREACH_STRING(p, "CI", "CONTINOUS_INTEGRATION") { + /* Those vars are booleans according to Semaphore and Travis docs: + * https://docs.travis-ci.com/user/environment-variables/#default-environment-variables + * https://docs.semaphoreci.com/ci-cd-environment/environment-variables/#ci + */ + r = getenv_bool(p); + if (r > 0) + return (ans = "unknown"); /* Some other unknown thing */ + if (r == 0) + return (ans = NULL); + } + + return (ans = NULL); +} diff --git a/src/shared/tests.h b/src/shared/tests.h index 505ca39775c..552e0f2c22e 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -40,3 +40,6 @@ bool can_memlock(void); } else { \ printf("systemd not booted skipping '%s'\n", #x); \ } + +/* Provide a convenient way to check if we're running in CI. */ +const char *ci_environment(void); diff --git a/src/test/test-execute.c b/src/test/test-execute.c index c18d68683e1..0bc2a45c303 100644 --- a/src/test/test-execute.c +++ b/src/test/test-execute.c @@ -36,11 +36,6 @@ static int cld_dumped_to_killed(int code) { return code == CLD_DUMPED ? CLD_KILLED : code; } -_unused_ static bool is_run_on_travis_ci(void) { - /* https://docs.travis-ci.com/user/environment-variables#default-environment-variables */ - return streq_ptr(getenv("TRAVIS"), "true"); -} - static void wait_for_service_finish(Manager *m, Unit *unit) { Service *service = NULL; usec_t ts; @@ -897,7 +892,7 @@ int main(int argc, char *argv[]) { test_setup_logging(LOG_DEBUG); #if HAS_FEATURE_ADDRESS_SANITIZER - if (is_run_on_travis_ci()) { + if (strstr_ptr(ci_environment(), "travis")) { log_notice("Running on TravisCI under ASan, skipping, see https://github.com/systemd/systemd/issues/10696"); return EXIT_TEST_SKIP; }