ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
b8c242
From 6fbbf368f5a6d181b21f448255d5a4182dc2ab3a Mon Sep 17 00:00:00 2001
b8c242
From: Frantisek Sumsal <frantisek@sumsal.cz>
b8c242
Date: Mon, 29 Nov 2021 13:00:21 +0100
b8c242
Subject: [PATCH] tests: add helper function to autodetect CI environments
b8c242
b8c242
Sadly there is no standarized way to check if we're running in some
b8c242
CI environment. So let's try to gather the heuristics in one helper
b8c242
function.
b8c242
b8c242
Loosely cherry-picked from 4eb0c875f8825199a829ddc597874915fbee0a84.
b8c242
b8c242
Related: #2017033
b8c242
---
b8c242
 src/basic/string-util.h |  6 ++++++
b8c242
 src/shared/tests.c      | 42 +++++++++++++++++++++++++++++++++++++++++
b8c242
 src/shared/tests.h      |  3 +++
b8c242
 3 files changed, 51 insertions(+)
b8c242
b8c242
diff --git a/src/basic/string-util.h b/src/basic/string-util.h
b8c242
index 96a9260f93..742b566932 100644
b8c242
--- a/src/basic/string-util.h
b8c242
+++ b/src/basic/string-util.h
b8c242
@@ -32,6 +32,12 @@ static inline bool streq_ptr(const char *a, const char *b) {
b8c242
         return strcmp_ptr(a, b) == 0;
b8c242
 }
b8c242
 
b8c242
+static inline char* strstr_ptr(const char *haystack, const char *needle) {
b8c242
+        if (!haystack || !needle)
b8c242
+                return NULL;
b8c242
+        return strstr(haystack, needle);
b8c242
+}
b8c242
+
b8c242
 static inline const char* strempty(const char *s) {
b8c242
         return s ?: "";
b8c242
 }
b8c242
diff --git a/src/shared/tests.c b/src/shared/tests.c
b8c242
index 100b62b9b0..1da80d653f 100644
b8c242
--- a/src/shared/tests.c
b8c242
+++ b/src/shared/tests.c
b8c242
@@ -7,7 +7,9 @@
b8c242
 #include <util.h>
b8c242
 
b8c242
 #include "tests.h"
b8c242
+#include "env-util.h"
b8c242
 #include "path-util.h"
b8c242
+#include "strv.h"
b8c242
 
b8c242
 char* setup_fake_runtime_dir(void) {
b8c242
         char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
b8c242
@@ -75,3 +77,43 @@ void test_setup_logging(int level) {
b8c242
         log_parse_environment();
b8c242
         log_open();
b8c242
 }
b8c242
+
b8c242
+const char *ci_environment(void) {
b8c242
+        /* We return a string because we might want to provide multiple bits of information later on: not
b8c242
+         * just the general CI environment type, but also whether we're sanitizing or not, etc. The caller is
b8c242
+         * expected to use strstr on the returned value. */
b8c242
+        static const char *ans = (void*) UINTPTR_MAX;
b8c242
+        const char *p;
b8c242
+        int r;
b8c242
+
b8c242
+        if (ans != (void*) UINTPTR_MAX)
b8c242
+                return ans;
b8c242
+
b8c242
+        /* We allow specifying the environment with $CITYPE. Nobody uses this so far, but we are ready. */
b8c242
+        p = getenv("CITYPE");
b8c242
+        if (!isempty(p))
b8c242
+                return (ans = p);
b8c242
+
b8c242
+        if (getenv_bool("TRAVIS") > 0)
b8c242
+                return (ans = "travis");
b8c242
+        if (getenv_bool("SEMAPHORE") > 0)
b8c242
+                return (ans = "semaphore");
b8c242
+        if (getenv_bool("GITHUB_ACTIONS") > 0)
b8c242
+                return (ans = "github-actions");
b8c242
+        if (getenv("AUTOPKGTEST_ARTIFACTS") || getenv("AUTOPKGTEST_TMP"))
b8c242
+                return (ans = "autopkgtest");
b8c242
+
b8c242
+        FOREACH_STRING(p, "CI", "CONTINOUS_INTEGRATION") {
b8c242
+                /* Those vars are booleans according to Semaphore and Travis docs:
b8c242
+                 * https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
b8c242
+                 * https://docs.semaphoreci.com/ci-cd-environment/environment-variables/#ci
b8c242
+                 */
b8c242
+                r = getenv_bool(p);
b8c242
+                if (r > 0)
b8c242
+                        return (ans = "unknown"); /* Some other unknown thing */
b8c242
+                if (r == 0)
b8c242
+                        return (ans = NULL);
b8c242
+        }
b8c242
+
b8c242
+        return (ans = NULL);
b8c242
+}
b8c242
diff --git a/src/shared/tests.h b/src/shared/tests.h
b8c242
index 3d696d02fd..4f8f349097 100644
b8c242
--- a/src/shared/tests.h
b8c242
+++ b/src/shared/tests.h
b8c242
@@ -5,3 +5,6 @@ char* setup_fake_runtime_dir(void);
b8c242
 bool test_is_running_from_builddir(char **exedir);
b8c242
 const char* get_testdata_dir(void);
b8c242
 void test_setup_logging(int level);
b8c242
+
b8c242
+/* Provide a convenient way to check if we're running in CI. */
b8c242
+const char *ci_environment(void);