Skip to content

Commit

Permalink
add: smoke tests and usage instructions (#14)
Browse files Browse the repository at this point in the history
* add: smoke tests and usage instructions

* feat: drop bash tests and patch snapcraft.yaml for test

* add(CI): To build and tests and refact to support ci and non-ci environments

---------

Signed-off-by: Lincoln Wallace <[email protected]>
  • Loading branch information
locnnil authored Sep 21, 2024
1 parent 37ed73d commit 6d4ca1c
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Build and Test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
# Allow manual trigger
workflow_dispatch:

env:
SNAP: envtester_${{ github.run_id}}.snap

jobs:
build-and-test:
outputs:
snap: ${{ steps.snapcraft.outputs.snap }}
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Initialize tests
run: sudo ./tests/initialize

- name: Build snap
uses: snapcore/action-build@v1
id: snapcraft

- name: Install snap
run: sudo snap install *.snap --dangerous

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ env.SNAP }}
path: ${{ steps.snapcraft.outputs.snap }}
if-no-files-found: error

- name: Run tests
run: sudo ./tests/test-snappyenv
12 changes: 12 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# snappy-env smoke tests

These are smoke tests that check if the main functionalities of the `rust` implementation of `snappy-env` program are working.

## Running tests

To run tests, execute the `test-snappyenv` script with root privileges:

```
sudo ./test-snappyenv
```

100 changes: 100 additions & 0 deletions tests/initialize
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash

check_syntax() {
local snap_app="$1"
local env_value="$2"
if eval "$snap_app" | grep -q "^${env_value}="; then
echo -e "\n[ERROR] Value '$env_name' SHOULD NOT BE set.\n"
return 1
fi
return 0
}

check_env_exist() {
local snap_app="$1"
local env_name="$2"

if ! eval "$snap_app" | grep -q "^${env_name}="; then
echo -e "\n[ERROR] Environment variable '$env_name' is not set, for the app: $snap_app.\n"
return 1
fi
}

check_env_not_exist() {

local snap_app="$1"
local env_name="$2"

if eval "$snap_app" | grep -q "^${env_name}="; then
echo -e "\n[ERROR] Environment variable '$env_name' SHOULD NOT be set, for the app: $snap_app.\n"
return 1
fi
}

check_env_value() {
local snap_app="$1"
local env_name="$2"
local exp_value="$3"
local actual_value

if [ -z "$env_name" ]; then
echo -e "\n\nHERE HERE\n\n "
empty=$("$snap_app" | grep "=${exp_value}")
[ -z "$empty" ] || return 1
fi
actual_value=$("$snap_app" | grep "^${env_name}=" | cut -d'=' -f2-)
if [ "$actual_value" != "$exp_value" ]; then
echo -e "\n[ERROR] Environment variable '$env_name' does not match the expected value, for the app: $snap_app"
echo -e "[ERROR] Expected: '$env_name=$exp_value', but got: '$env_name=$actual_value'\n"
return 1
fi
}

check_root() {
if [ "$USER" != "root" ]; then
echo -e "Please run as root.\n"
exit 1
fi
}

clean() {
echo "Cleaning..."
snap remove --purge "${SNAP}"
git restore snap/snapcraft.yaml
sudo chown $SUDO_USER:$SUDO_USER snap/snapcraft.yaml

rm -rf squashfs-root
}

fail() {
clean
exit 1
}

inject_test_app() {
sudo snap install yq
sudo -u "$SUDO_USER" yq '.apps.app-rust-2 = {
"environment": {
"env_alias": "app-rust-2"
},
"command-chain": [
"bin/env-exporter"
],
"command": "bin/env.sh"
}' -i "snap/snapcraft.yaml"
}

init_tests() {
set +u
if [ -z "${GITHUB_ACTIONS}" ]; then
set -u
inject_test_app
snapcraft -o "${SNAP}".snap
snap install "${SNAP}".snap --dangerous
fi
}

SNAP=envtester
SNAP_COMMON=/var/snap/"${SNAP}"/common/

inject_test_app
113 changes: 113 additions & 0 deletions tests/test-snappyenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/bin/bash

set -eu

BASEDIR=$(dirname "$(realpath "$0")")
PROJECT_ROOT="$BASEDIR/.."

. $PROJECT_ROOT/tests/initialize

check_root
clean
init_tests

echo "SNAP NAME: ${SNAP}"

pushd "$PROJECT_ROOT" || exit 1

# 'sudo -u "$RUN_AS"' prevents a permission denied error when using yq as snap.
sudo -u "$SUDO_USER" yq '.apps.app-rust-2 = {
"environment": {
"env_alias": "app-rust-2"
},
"command-chain": [
"bin/env-exporter"
],
"command": "bin/env.sh"
}' -i "snap/snapcraft.yaml"

unsquashfs "${PROJECT_ROOT}/${SNAP}.snap"

echo "Check that the env-exporter program is present"
[ -f squashfs-root/bin/env-exporter ] || fail

echo "Check that the exec-env script is present"
[ -f squashfs-root/bin/env.sh ] || fail

echo -e "\n[envtester] Creating global envfile"
echo 'HELLO_WORLD="Hello World"' >>"$SNAP_COMMON"/global.env

echo "Load global envfile"
snap set "${SNAP}" envfile="$SNAP_COMMON"/global.env

echo "[TEST] - Check if the global envfile is loaded the app using Rust snappy-env programs"
check_env_exist "${SNAP}.app-rust" "HELLO_WORLD" || fail
check_env_value "${SNAP}.app-rust" "HELLO_WORLD" "Hello World" || fail

check_env_exist "${SNAP}.app-rust-2" "HELLO_WORLD" || fail
check_env_value "${SNAP}.app-rust-2" "HELLO_WORLD" "Hello World" || fail

echo -e "\n[envtester] Creating app-specific envfile"
echo 'SCOPED=RustVersion' >>"$SNAP_COMMON"/rust.env
echo 'SCOPED=Rust2Version' >>"$SNAP_COMMON"/rust-2.env

echo "Load app-specific envfile"
snap set "${SNAP}" apps.app-rust.envfile="$SNAP_COMMON"/rust.env
snap set "${SNAP}" apps.app-rust-2.envfile="$SNAP_COMMON"/rust-2.env

echo "[TEST] - Check if the app-specific envfile is loaded for the apps"
check_env_exist "${SNAP}.app-rust" "SCOPED" || fail
check_env_value "${SNAP}.app-rust" "SCOPED" "RustVersion" || fail

check_env_exist "${SNAP}.app-rust-2" "SCOPED" || fail
check_env_value "${SNAP}.app-rust-2" "SCOPED" "Rust2Version" || fail

echo -e "\n[envtester] Setting global env variable"

echo "Set env vars: Global"
snap set "${SNAP}" env.global="World"

echo "[TEST] - Check if the global env var is set for all apps"
check_env_exist "${SNAP}.app-rust" "GLOBAL" || fail
check_env_value "${SNAP}.app-rust" "GLOBAL" "World" || fail

check_env_exist "${SNAP}.app-rust-2" "GLOBAL" || fail
check_env_value "${SNAP}.app-rust-2" "GLOBAL" "World" || fail

echo -e "\n[envtester] Setting app-specific env variable"
echo "Set env vars: specific to each app"
snap set "${SNAP}" apps.app-rust.env.hello="Hello"
snap set "${SNAP}" apps.app-rust-2.env.specific="City"

echo "[TEST] - Check if the app-specific env var IS SET for the app 'app-rust'"
check_env_exist "${SNAP}.app-rust" "HELLO" || fail
check_env_value "${SNAP}.app-rust" "HELLO" "Hello" || fail

echo -e "\n[TEST] - Check if the app-specific env var IS NOT SET for the app 'app-rust-2'"
check_env_not_exist "${SNAP}.app-rust-2" "HELLO" || fail

echo -e "\n[TEST] - Check if the app-specific env var IS SET for the app 'app-rust-2'"
check_env_exist "${SNAP}.app-rust-2" "SPECIFIC" || fail
check_env_value "${SNAP}.app-rust-2" "SPECIFIC" "City" || fail

echo -e "\n[TEST] - Check if the app-specific env var IS NOT SET for the app 'app-rust'"
check_env_not_exist "${SNAP}.app-rust" "SPECIFIC" || fail

echo -e "\n[envtester] Testing order of env vars"
echo 'ORDER="From envfile"' >>"$SNAP_COMMON"/local.env
snap set "${SNAP}" apps.app-rust.env.order="from snap option"
snap set "${SNAP}" apps.app-rust.envfile="$SNAP_COMMON"/local.env
echo "[TEST] - Check if local overrides global"

check_env_exist "${SNAP}.app-rust" "ORDER" || fail
check_env_value "${SNAP}.app-rust" "ORDER" "from snap option" || fail

echo -e "\n[envtester] Testing options syntax"
snap set "${SNAP}" env.word.dot="wrong"
echo "[TEST] - Check if the key with dot was ignored"
check_syntax "${SNAP}.app-rust" "wrong" || fail
snap unset "${SNAP}" env.word.dot

clean
popd || exit 1
exit 0

0 comments on commit 6d4ca1c

Please sign in to comment.