Repository navigation
ARO-26543: add ShellSpec and shfmt validation for first-wave bash scripts #4959
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
Closed
Affinito Alessandro (tuxerrante)
wants to merge
3
commits into
master
from
tuxerrante/ARO-26543/bash-unit-tests-followup
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| name: ci-bash | ||
|
|
||
| on: | ||
| push: | ||
| tags: | ||
| - v* | ||
| branches: | ||
| - master | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| bash-unit-tests: | ||
| name: bash-unit-tests | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Harden Runner | ||
| uses: step-security/harden-runner@fe104658747b27e96e4f7e80cd0a94068e53901d # v2.16.1 | ||
| with: | ||
| egress-policy: audit | ||
| - name: Checkout | ||
| uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | ||
| - name: Install shfmt | ||
| run: | | ||
| sudo apt-get update | ||
| sudo apt-get install -y shfmt | ||
| - name: Run bash validation | ||
| env: | ||
| BASH_TEST_JUNIT: "0" | ||
| run: make validate-bash |
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,4 @@ | ||
| --default-path test/bash/spec | ||
| --helperdir test/bash/spec | ||
| --shell bash | ||
| --execdir @project |
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,179 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -o errexit | ||
| set -o nounset | ||
| set -o pipefail | ||
|
|
||
| repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| readonly repo_root | ||
| readonly shellspec_image="${BASH_TEST_IMAGE:-docker.io/shellspec/shellspec-debian:0.28.1}" | ||
| readonly default_report_base="${TMPDIR:-/tmp}/aro-bash-test-report" | ||
|
Comment on lines
+9
to
+10
|
||
|
|
||
| # Reject report paths that could make host-side cleanup delete the wrong tree. | ||
| validate_report_dir() { | ||
| local report_dir="$1" | ||
| local tmpdir="${TMPDIR:-/tmp}" | ||
| local path_segment | ||
|
|
||
| tmpdir="${tmpdir%/}" | ||
|
|
||
| if [[ -z "${report_dir}" || "${report_dir}" != /* || -z "${report_dir//\//}" ]]; then | ||
| echo "error: refusing unsafe report directory '${report_dir}'" >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| # Only allow report directories under the temp dir with the expected prefix. | ||
| case "${report_dir}" in | ||
| "${tmpdir}"/aro-bash-test-report*) | ||
| ;; | ||
| *) | ||
| echo "error: refusing unsafe report directory '${report_dir}'" >&2 | ||
| return 1 | ||
| ;; | ||
| esac | ||
|
|
||
| IFS='/' read -r -a path_segments <<< "${report_dir#/}" | ||
| for path_segment in "${path_segments[@]}"; do | ||
| if [[ "${path_segment}" == "." || "${path_segment}" == ".." ]]; then | ||
| echo "error: refusing unsafe report directory '${report_dir}'" >&2 | ||
| return 1 | ||
| fi | ||
| done | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| # Recreate the host report directory that ShellSpec writes into from the container. | ||
| prepare_report_dir() { | ||
| local report_dir | ||
| report_dir="${BASH_TEST_REPORT_DIR:-${default_report_base}}" | ||
| validate_report_dir "${report_dir}" || return 1 | ||
| rm -rf -- "${report_dir}" | ||
| mkdir -p -- "${report_dir}" | ||
| printf '%s\n' "${report_dir}" | ||
| } | ||
|
|
||
| # Treat an engine as usable only when its info command succeeds. | ||
| container_engine_usable() { | ||
| "$1" info > /dev/null 2>&1 | ||
| } | ||
|
|
||
| # Detect podman-docker wrappers so we can apply Podman-specific run flags. | ||
| docker_is_podman() { | ||
| if ! command -v docker > /dev/null 2>&1; then | ||
| return 1 | ||
| fi | ||
|
|
||
| local docker_path podman_path docker_version docker_info | ||
|
|
||
| # Same inode: `docker` is a symlink/hardlink to the podman binary. | ||
| docker_path="$(type -P docker || true)" | ||
| podman_path="$(type -P podman || true)" | ||
| if [[ -n "${docker_path}" && -n "${podman_path}" && "${docker_path}" -ef "${podman_path}" ]]; then | ||
| return 0 | ||
| fi | ||
|
|
||
| # Authoritative signals that do not depend on the podman-docker wrapper's | ||
| # "Emulate Docker CLI using podman" stderr message, which is suppressed when | ||
| # /etc/containers/nodocker exists. | ||
| docker_version="$(docker version --format '{{.Client.Name}}' 2> /dev/null || true)" | ||
| if [[ "${docker_version}" == *[Pp]odman* ]]; then | ||
| return 0 | ||
| fi | ||
|
|
||
| docker_version="$(docker --version 2> /dev/null || true)" | ||
| if [[ "${docker_version}" == *[Pp]odman* ]]; then | ||
| return 0 | ||
| fi | ||
|
|
||
| # Fallback for older podman-docker wrappers without /etc/containers/nodocker. | ||
| docker_info="$(docker info 2>&1 || true)" | ||
| [[ "${docker_info}" == *"Emulate Docker CLI using podman"* ]] | ||
| } | ||
|
|
||
| # Prefer docker when available, unless it is really Podman underneath. | ||
| detect_container_engine() { | ||
| if command -v docker > /dev/null 2>&1 && container_engine_usable docker; then | ||
| if docker_is_podman; then | ||
| echo podman | ||
| else | ||
| echo docker | ||
| fi | ||
| return 0 | ||
| fi | ||
|
|
||
| if command -v podman > /dev/null 2>&1 && container_engine_usable podman; then | ||
| echo podman | ||
| return 0 | ||
| fi | ||
|
|
||
| echo "error: a working docker or podman daemon is required to run bash tests" >&2 | ||
| return 1 | ||
| } | ||
|
|
||
| # Keep this destructive suite serial unless the caller explicitly opts in. | ||
| detect_jobs() { | ||
| if [[ -n "${BASH_TEST_JOBS:-}" ]]; then | ||
| echo "${BASH_TEST_JOBS}" | ||
| return 0 | ||
| fi | ||
| echo 1 | ||
| } | ||
|
|
||
| # Force linux/amd64 only on macOS hosts that default to Apple Silicon containers. | ||
| detect_platform() { | ||
| if [[ -n "${BASH_TEST_PLATFORM:-}" ]]; then | ||
| echo "${BASH_TEST_PLATFORM}" | ||
| return 0 | ||
| fi | ||
|
|
||
| if [[ "$(uname -s)" == "Darwin" ]]; then | ||
| echo "linux/amd64" | ||
| fi | ||
| } | ||
|
|
||
| # Assemble engine-specific mounts and run ShellSpec inside the container. | ||
| main() { | ||
| local engine jobs mount_arg report_dir report_mount | ||
| engine="$(detect_container_engine)" | ||
| jobs="$(detect_jobs)" | ||
| report_dir="$(prepare_report_dir)" || return 1 | ||
|
|
||
| local -a engine_args=() | ||
| if [[ "${engine}" == "podman" ]]; then | ||
| mount_arg="${repo_root}:/work:Z" | ||
| report_mount="${report_dir}:/work/.bash-test-report:Z" | ||
| engine_args+=("--userns=keep-id:uid=0,gid=0") | ||
| else | ||
| mount_arg="${repo_root}:/work" | ||
| report_mount="${report_dir}:/work/.bash-test-report" | ||
| fi | ||
|
|
||
| local -a platform_args=() | ||
| local platform | ||
| platform="$(detect_platform)" | ||
| if [[ -n "${platform}" ]]; then | ||
| platform_args=(--platform "${platform}") | ||
| fi | ||
|
|
||
| local junit_enabled="${BASH_TEST_JUNIT:-${CI:-0}}" | ||
| local -a report_args=() | ||
| if [[ "${junit_enabled}" == "true" || "${junit_enabled}" == "1" ]]; then | ||
| report_args=(--output junit --reportdir /work/.bash-test-report) | ||
| echo "ShellSpec JUnit report directory: ${report_dir}" | ||
| fi | ||
|
|
||
| echo "Running ShellSpec with ${engine} using image ${shellspec_image}" | ||
|
|
||
| "${engine}" run --rm \ | ||
| "${platform_args[@]}" \ | ||
| "${engine_args[@]}" \ | ||
| -v "${mount_arg}" \ | ||
| -v "${report_mount}" \ | ||
| -w /work \ | ||
| "${shellspec_image}" \ | ||
| --jobs "${jobs}" \ | ||
| --format documentation \ | ||
| "${report_args[@]}" \ | ||
| "$@" | ||
| } | ||
|
|
||
| main "$@" | ||
2 changes: 1 addition & 1 deletion
2
pkg/operator/controllers/dnsmasq/scripts/99-dnsmasq-restart.gotmpl
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,84 @@ | ||
| Describe 'backupandfixetcd.sh' | ||
| Include ./test/bash/spec/support/helpers.sh | ||
| script_path="${REPO_ROOT}/pkg/frontend/scripts/backupandfixetcd.sh" | ||
|
|
||
| run_backup_mode() { | ||
| ensure_test_workspace | ||
| mkdir -p /etc/kubernetes/manifests /var/lib/etcd | ||
| printf 'manifest-data' > /etc/kubernetes/manifests/etcd-pod.yaml | ||
| printf 'etcd-data' > /var/lib/etcd/member | ||
|
|
||
| BACKUP=1 FIX_PEERS='' bash "${script_path}" | ||
|
|
||
| backup_dir="$(echo /var/lib/etcd-backup-*)" | ||
| printf '%s\n' "${backup_dir}" | ||
| printf '%s\n' "$(cat "${backup_dir}/etcd-pod.yaml")" | ||
| printf '%s\n' "$(cat "${backup_dir}/etcd/member")" | ||
| if backup_paths_moved; then | ||
| printf '%s\n' 'moved=yes' | ||
| else | ||
| printf '%s\n' 'moved=no' | ||
| fi | ||
| } | ||
|
|
||
| backup_paths_moved() { | ||
| [ ! -e /var/lib/etcd ] && [ ! -e /etc/kubernetes/manifests/etcd-pod.yaml ] | ||
| } | ||
|
|
||
| run_fix_peers_mode() { | ||
| ensure_test_workspace | ||
|
|
||
| append_mock_command oc ' | ||
| printf "oc %s\n" "$*" >> "${CALL_LOG}" | ||
| if [[ "$*" == *"member list"* ]]; then | ||
| printf "%s\n" "{\"members\":[{\"name\":\"master-0\",\"ID\":\"member-123\"}]}" | ||
| fi | ||
| ' | ||
| append_mock_command jq 'printf "%s\n" "member-123"' | ||
| cp "${MOCK_BIN}/oc" /host/usr/bin/oc | ||
| cp "${MOCK_BIN}/jq" /host/usr/bin/jq | ||
|
|
||
| FIX_PEERS=1 \ | ||
| BACKUP='' \ | ||
| PEER_PODS="etcd-0 etcd-1" \ | ||
| DEGRADED_NODE="master-0" \ | ||
| bash "${script_path}" | ||
|
|
||
| cat "${CALL_LOG}" | ||
| } | ||
|
|
||
| verify_fix_peers_host_mocks_are_cleaned() { | ||
| ensure_test_workspace | ||
|
|
||
| append_mock_command oc ':' | ||
| append_mock_command jq ':' | ||
| cp "${MOCK_BIN}/oc" /host/usr/bin/oc | ||
| cp "${MOCK_BIN}/jq" /host/usr/bin/jq | ||
|
|
||
| reset_absolute_test_state | ||
|
|
||
| [ ! -e /host/usr/bin/oc ] && [ ! -e /host/usr/bin/jq ] | ||
| } | ||
|
|
||
| It 'backs up etcd manifests and data into a timestamped directory' | ||
| When call run_backup_mode | ||
| The status should be success | ||
| The output should include '/var/lib/etcd-backup-' | ||
| The output should include 'manifest-data' | ||
| The output should include 'etcd-data' | ||
| The output should include 'moved=yes' | ||
| End | ||
|
|
||
| It 'removes degraded peer members when FIX_PEERS is set' | ||
| When call run_fix_peers_mode | ||
| The status should be success | ||
| The output should include 'Starting peer etcd member removal' | ||
| The output should include 'oc rsh -n openshift-etcd -c etcdctl pod/etcd-0 etcdctl member remove member-123' | ||
| The output should include 'oc rsh -n openshift-etcd -c etcdctl pod/etcd-1 etcdctl member remove member-123' | ||
| End | ||
|
|
||
| It 'cleans fix-peers host mocks during helper reset' | ||
| When run verify_fix_peers_host_mocks_are_cleaned | ||
| The status should be success | ||
| End | ||
| End |
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.
Uh oh!
There was an error while loading. Please reload this page.