test: Run tests for bash and rust implementation of exporter program#30
Open
locnnil wants to merge 26 commits into
Open
test: Run tests for bash and rust implementation of exporter program#30locnnil wants to merge 26 commits into
bash and rust implementation of exporter program#30locnnil wants to merge 26 commits into
Conversation
locnnil
commented
Apr 8, 2025
Collaborator
- Depends on: feat: Add func to detect nested values and drop it #27
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
- Change test metadata to avoid confusion Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Signed-off-by: Lincoln Wallace <lincoln.wallace@canonical.com>
Collaborator
Author
|
The current error is expected: Already reported on: |
farshidtz
requested changes
May 30, 2025
farshidtz
left a comment
Member
There was a problem hiding this comment.
Thanks for improving the tests.
The bash scripts can benefit from some refactoring. The test results contain redundant text.
Comment on lines
+10
to
+51
| 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" |
Member
There was a problem hiding this comment.
Lines 10 to 49 logically belong to the common script, where the implementation value is verified and consumed.
| #!/bin/bash | ||
|
|
||
| set -eu | ||
| set -eux |
Member
There was a problem hiding this comment.
Given the echos, the xtrace makes the results pretty redundant:
[envtester] Setting app-specific env variable
Set env vars: specific to each app
+ snap set envtester apps.app2.env.specific=City
+ echo '[TEST] - Check if the app-specific env var IS SET for the app '\''app1'\'''
+ check_env_exist envtester.app1 HELLO
+ local snap_app=envtester.app1
[TEST] - Check if the app-specific env var IS SET for the app 'app1'
+ local env_name=HELLO
+ eval envtester.app1
++ envtester.app1
+ grep -q '^HELLO='
+ check_env_value envtester.app1 HELLO Hello
+ local snap_app=envtester.app1
+ local env_name=HELLO
+ local exp_value=Hello
+ local actual_value
+ '[' -z HELLO ']'
++ envtester.app1
++ grep '^HELLO='
++ cut -d= -f2-
+ actual_value=Hello
+ '[' Hello '!=' Hello ']'
+ echo -e '\n[TEST] - Check if the app-specific env var IS NOT SET for the app '\''app2'\'''
+ check_env_not_exist envtester.app2 HELLO
+ local snap_app=envtester.app2
+ local env_name=HELLO
[TEST] - Check if the app-specific env var IS NOT SET for the app 'app2'
+ eval envtester.app2
+ grep -q '^HELLO='
++ envtester.app2
+ echo -e '\n[TEST] - Check if the app-specific env var IS SET for the app '\''app2'\'''
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.