-
Notifications
You must be signed in to change notification settings - Fork 1
test: Run tests for bash and rust implementation of exporter program
#30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
locnnil
wants to merge
26
commits into
canonical:main
Choose a base branch
from
locnnil:improve-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−139
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
74f5144
feat: Add func to detect nested values and drop it
locnnil 3a7777d
feat: add context to error
locnnil e99521d
fix: use right RegEx
locnnil df71dd2
debug: add trace
locnnil 9b74b4a
refact: add missing quotations
locnnil 6e78601
feat: revert prev regex
locnnil 06cf2cd
feat: exit 1 on error
locnnil 70571dd
feat!: drop trace
locnnil deff3d5
feat: change app names to app1 and app2
locnnil 891013c
feat: patch snapcraft.yaml with app1 and app2
locnnil f9ec1f3
feat: add -i/--impl parameter to set rust or bash implementation
locnnil bf9b14d
feat: add help function
locnnil d9e939c
feat: inject app on GH CI and locally
locnnil 2018d12
fix: patch snapcraft.yaml file only on initialization
locnnil 6971459
ci: run tests for bash and rust implementation
locnnil 52f3a75
feat: add checker for initializer script
locnnil 91e9943
test: enable trace
locnnil 33e5721
fix: pass key to func instead of value of hashmap
locnnil a0f4c1f
fix: add scape characters to proper evaluate $exporter
locnnil efa3e83
test: verify script||binary inside snap bundle
locnnil 1a80d92
feat: only patch when running initialize script
locnnil 6e1242b
feat: avoid name conflict
locnnil a59ba25
feat: set impl on test-snappyenv
locnnil d73a60b
many: refact scripts
locnnil e951af2
feat!: don't pass impl set to test script
locnnil 63cacca
Merge branch 'main' into improve-tests
locnnil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
|
|
||
| declare -A impl_map=( | ||
| [bash]="env-exporter.sh" | ||
| [rust]="env-exporter" | ||
| ) | ||
|
|
||
| SNAP=envtester | ||
| SNAP_COMMON=/var/snap/"${SNAP}"/common/ | ||
|
|
||
| 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() { | ||
| clean | ||
| sudo snap install yq | ||
|
|
||
| if [[ -z "${impl_map[$1]}" ]]; then | ||
| echo "Error: unknown implementation '$1'" | ||
| help | ||
| fi | ||
|
|
||
| local exporter="${impl_map[$1]}" | ||
|
|
||
| # Create app1 | ||
| sudo -u "$SUDO_USER" yq ".apps.app1 = { | ||
| \"environment\": { | ||
| \"env_alias\": \"app1\" | ||
| }, | ||
| \"command-chain\": [ | ||
| \"bin/${exporter}\" | ||
| ], | ||
| \"command\": \"bin/env.sh\" | ||
| }" -i "snap/snapcraft.yaml" | ||
|
|
||
| # Create app2 | ||
| sudo -u "$SUDO_USER" yq ".apps.app2 = { | ||
| \"environment\": { | ||
| \"env_alias\": \"app2\" | ||
| }, | ||
| \"command-chain\": [ | ||
| \"bin/${exporter}\" | ||
| ], | ||
| \"command\": \"bin/env.sh\" | ||
| }" -i "snap/snapcraft.yaml" | ||
|
|
||
| } | ||
|
|
||
| check_impl() { | ||
| if [[ "$1" != "bash" && "$1" != "rust" ]]; then | ||
| echo "Error: --impl/-i must be either 'bash' or 'rust'" >&2 | ||
| help | ||
| fi | ||
| } | ||
|
|
||
| init_tests() { | ||
| set +u | ||
|
|
||
| if [ -z "${GITHUB_ACTIONS}" ]; then | ||
| set -u | ||
| snapcraft -o "${SNAP}".snap | ||
| snap install "${SNAP}".snap --dangerous | ||
| fi | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,100 +1,53 @@ | ||
| #!/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" | ||
| set -x | ||
|
|
||
| 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 | ||
| } | ||
| BASEDIR=$(dirname "$(realpath "$0")") | ||
| PROJECT_ROOT="$BASEDIR/.." | ||
|
|
||
| check_env_not_exist() { | ||
| . $PROJECT_ROOT/tests/common | ||
|
|
||
| local snap_app="$1" | ||
| local env_name="$2" | ||
| help() { | ||
| cat <<EOF | ||
| Usage: $0 -i <impl> | ||
| $0 --impl=<impl> | ||
|
|
||
| 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 | ||
| } | ||
| Options: | ||
| -i, --impl <impl> Specify the implementation of the env-exporter program. | ||
| Must be either 'bash' or 'rust'. | ||
|
|
||
| check_env_value() { | ||
| local snap_app="$1" | ||
| local env_name="$2" | ||
| local exp_value="$3" | ||
| local actual_value | ||
| Examples: | ||
| $0 -i bash | ||
| $0 --impl=rust | ||
|
|
||
| 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 | ||
| } | ||
| Description: | ||
| The 'impl' option selects which implementation of the env-exporter program to use. | ||
| - 'bash': Uses the Bash-based implementation. | ||
| - 'rust': Uses the Rust-based implementation. | ||
|
|
||
| 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 | ||
| EOF | ||
| 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 | ||
| } | ||
| impl="" | ||
|
|
||
| while [[ $# -gt 0 ]]; do | ||
| case "$1" in | ||
| -i=* | --impl=*) | ||
| impl="${1#*=}" | ||
| shift | ||
| ;; | ||
| -i | --impl) | ||
| impl="$2" | ||
| shift 2 | ||
| ;; | ||
| *) | ||
| help | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
| SNAP=envtester | ||
| SNAP_COMMON=/var/snap/"${SNAP}"/common/ | ||
| check_impl "$impl" | ||
|
|
||
| inject_test_app | ||
| inject_test_app "$impl" | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 10 to 49 logically belong to the
commonscript, where the implementation value is verified and consumed.