Skip to content

Commit

Permalink
test: add spread tests for env-injector extension
Browse files Browse the repository at this point in the history
- add: spread test:
  * hello.c: application which consumes env variables
  * task.yaml: Tests to verify env-injector functionality

Signed-off-by: Lincoln Wallace <[email protected]>
  • Loading branch information
locnnil committed Jul 23, 2024
1 parent 137c302 commit 1f9db53
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
53 changes: 53 additions & 0 deletions tests/spread/extensions/env-injector/task.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.5)
project(hello C)
add_executable(hello hello.c)
install(TARGETS hello RUNTIME DESTINATION bin)
37 changes: 37 additions & 0 deletions tests/spread/extensions/snaps/env-injector-hello/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
Original file line number Diff line number Diff line change
@@ -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: .
1 change: 1 addition & 0 deletions tests/unit/commands/test_list_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/unit/extensions/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 1f9db53

Please sign in to comment.