diff --git a/tests/spread/extensions/env-injector/task.yaml b/tests/spread/extensions/env-injector/task.yaml new file mode 100644 index 0000000000..3ab765df02 --- /dev/null +++ b/tests/spread/extensions/env-injector/task.yaml @@ -0,0 +1,53 @@ +summary: Build and run a basic hello-world snap using extensions + +systems: + - ubuntu-24.04 + +environment: + + SNAPCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS: "1" + SNAP_DIR: ../snaps/env-injector-hello + SNAP: env-injector-hello + +prepare: | + + #shellcheck source=tests/spread/tools/snapcraft-yaml.sh + . "$TOOLS_DIR/snapcraft-yaml.sh" + set_base "$SNAP_DIR/snap/snapcraft.yaml" + +restore: | + + cd "$SNAP_DIR" + snapcraft clean + rm -f ./*.snap + rm -rf ./squashfs-root + #shellcheck source=tests/spread/tools/snapcraft-yaml.sh + . "$TOOLS_DIR/snapcraft-yaml.sh" + restore_yaml "snap/snapcraft.yaml" + +execute: | + + cd "$SNAP_DIR" + snapcraft + + unsquashfs "${SNAP}"_1.0_*.snap + + # Check that the env-exporter program is present + [ -f squashfs-root/bin/command-chain/env-exporter ] + + # Check that the hello command is present + [ -f squashfs-root/usr/local/bin/hello ] + + snap install "${SNAP}"_1.0_*.snap --dangerous + + # Create envfile + echo 'HELLO_WORLD="Hello, World"' >> envfile.env + + # Set env vars: Global, App specific, and envfile + snap set env-injector-hello env.hello="Hello" + snap set env-injector-hello apps.hello.env.world="World" + snap set env-injector-hello envfile="${SNAP_DIR}"/enfile.env + + # Run the hello command + env-injector-hello.hello + diff --git a/tests/spread/extensions/snaps/env-injector-hello/CMakeLists.txt b/tests/spread/extensions/snaps/env-injector-hello/CMakeLists.txt new file mode 100644 index 0000000000..7c574d9c5b --- /dev/null +++ b/tests/spread/extensions/snaps/env-injector-hello/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.5) +project(hello C) +add_executable(hello hello.c) +install(TARGETS hello RUNTIME DESTINATION bin) diff --git a/tests/spread/extensions/snaps/env-injector-hello/hello.c b/tests/spread/extensions/snaps/env-injector-hello/hello.c new file mode 100644 index 0000000000..4d442b0068 --- /dev/null +++ b/tests/spread/extensions/snaps/env-injector-hello/hello.c @@ -0,0 +1,37 @@ +#include +#include +#include + +int main(int argc, char const *argv[]) { + const char *envs[] = { + "HELLO", // This is set globally + "WORLD", // This is set for the app + "HELLO_WORLD", // This is set from env file + }; + + const char *expected[] = { + "Hello", + "World", + "Hello World", + }; + + int num_envs = sizeof(envs) / sizeof(envs[0]); + + for (int i = 0; i < num_envs; ++i) { + const char *env = getenv(envs[i]); + + if (env == NULL) { + fprintf(stderr, "\n[ERROR] Env. variable %s is not set.\n", envs[i]); + return 1; + } + + if (strcmp(env, expected[i]) != 0) { + fprintf(stderr, "\n[ERROR] Env. variable %s isn't set to the expected value.\n", envs[i]); + fprintf(stderr, "Expected: %s\n", expected[i]); + fprintf(stderr, "Got: %s\n", env); + return 1; + } + } + + return 0; +} diff --git a/tests/spread/extensions/snaps/env-injector-hello/snap/snapcraft.yaml b/tests/spread/extensions/snaps/env-injector-hello/snap/snapcraft.yaml new file mode 100644 index 0000000000..1fd271d2d9 --- /dev/null +++ b/tests/spread/extensions/snaps/env-injector-hello/snap/snapcraft.yaml @@ -0,0 +1,18 @@ +name: env-injector-hello +version: "1.0" +summary: test the env-injector extension +description: This is a basic snap for testing env-injector extension + +grade: devel +base: core24 +confinement: strict + +apps: + env-hello: + command: usr/local/bin/hello + extensions: [ env-injector ] + +parts: + hello: + plugin: cmake + source: . diff --git a/tests/unit/commands/test_list_extensions.py b/tests/unit/commands/test_list_extensions.py index 1900e1a4d7..6141ce0060 100644 --- a/tests/unit/commands/test_list_extensions.py +++ b/tests/unit/commands/test_list_extensions.py @@ -38,6 +38,7 @@ def test_command(emitter, command): """\ Extension name Supported bases ---------------------- ---------------------- + env-injector core24 fake-extension core22, core24 flutter-beta core18 flutter-dev core18 diff --git a/tests/unit/extensions/test_registry.py b/tests/unit/extensions/test_registry.py index c823c92c60..0fefd4048c 100644 --- a/tests/unit/extensions/test_registry.py +++ b/tests/unit/extensions/test_registry.py @@ -24,6 +24,7 @@ @pytest.mark.usefixtures("fake_extension_experimental") def test_get_extension_names(): assert extensions.get_extension_names() == [ + "env-injector", "gnome", "ros2-humble", "ros2-humble-ros-core",