Skip to content
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

test: Use the bash container and a matrix strategy for bash versions #616

Open
wants to merge 28 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d61d750
test: Use the bash container and a matrix strategy for bash versions …
lens0021 Nov 24, 2024
196fe48
Run as non-root user
lens0021 Nov 24, 2024
d83db04
Exclude besh 5.3-alpha
lens0021 Nov 26, 2024
c30ed8e
Rename the name of the test container
lens0021 Nov 26, 2024
856df96
create docker command from PATH
lens0021 Nov 26, 2024
bf3b2de
Use readable matrix names
lens0021 Nov 26, 2024
04b664c
Disable failed bash 3.1 and bash 3.0 tests
lens0021 Nov 26, 2024
04b28b3
re-order matrix
lens0021 Nov 26, 2024
ee671af
Drop bash 3.1
lens0021 Dec 4, 2024
6d59f14
Update .github/workflows/rust.yml
lens0021 Dec 8, 2024
afb1504
Do not mount the code
lens0021 Dec 9, 2024
e173248
Remove `--workdir`; It does not require any storage access
lens0021 Dec 9, 2024
ee10d95
docs: find_bash()
lens0021 Dec 9, 2024
8ea6bc3
Split clippy tests
lens0021 Dec 12, 2024
93e853d
Remove bash 3.2, 4.0 and 4.1 from the matrix
lens0021 Dec 16, 2024
9e3e635
Revert "Remove bash 3.2, 4.0 and 4.1 from the matrix"
lens0021 Dec 16, 2024
20f854e
Update .github/workflows/rust.yml
Mte90 Dec 17, 2024
d193215
use dockerfile instead of downloading packages at compile time
b1ek Dec 17, 2024
1a5a85d
Build once and reuse the target directory
lens0021 Dec 17, 2024
7c92524
Add a Dockerfile and cache the images
lens0021 Dec 17, 2024
aae7abc
Update .github/workflows/rust.yml
lens0021 Dec 17, 2024
d16922d
`mkdir -p`
lens0021 Dec 17, 2024
f237312
`~` is not resolved in double quotes
lens0021 Dec 17, 2024
15067eb
Rename sed build-arg
lens0021 Dec 18, 2024
376f3f9
Move TestEnvDockerfile to .github/workflows
lens0021 Dec 18, 2024
8378ded
Update TestEnvDockerfile
lens0021 Dec 19, 2024
10b02a7
Remove cache of bc and shfmt from clippy tests
lens0021 Dec 19, 2024
ab25604
Revert "Remove bash 3.2, 4.0 and 4.1 from the matrix"
lens0021 Dec 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/TestEnvDockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ARG BASE_BASE_IMAGE=bash:5.2
FROM $BASE_BASE_IMAGE
ARG SED_VERSION

RUN apk add \
curl \
# Includes mktemp
coreutils \
;
RUN if [ "$SED_VERSION" == "GNU_sed" ]; then apk add sed; fi

# A non-root user is required by is_root() test
RUN adduser -D appuser
USER appuser
WORKDIR /home/appuser

CMD ["sleep", "infinity"]
LABEL description="Used to test the compiled bash script for various environment."
99 changes: 93 additions & 6 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,56 @@ env:
CARGO_TERM_COLOR: always

jobs:
build_and_test:
name: Rust and clippy tests
build:
name: Rust build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Cache dependencies installed with cargo
uses: actions/cache@v4
with:
path: |
./target/deps
~/.cargo
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-
- name: Build binary
run: |
# the binary will be used by the cargo test
cargo build
cargo test --no-run --all-targets --all-features
- name: Compress before uploading artifact to preserve permissions # https://github.com/actions/upload-artifact/tree/v4.4.3#permission-loss
run: tar -cvzf target.tgz target/
- uses: actions/upload-artifact@v4
with:
include-hidden-files: true
name: target
path: target.tgz
retention-days: 1
test:
name: Rust tests
runs-on: ubuntu-latest
needs: build
strategy:
fail-fast: false
matrix:
bash_docker_image:
- "bash:5.2"
- "bash:5.1"
- "bash:5.0"
- "bash:4.4"
- "bash:4.3"
- "bash:4.2"
- "bash:4.1"
- "bash:4.0"
- "bash:3.2"
sed:
- GNU_sed
# Uncomment the next matrix when https://github.com/amber-lang/amber/issues/617 is resolved
# include:
# - sed: BusyBox_sed
# bash_version: "bash:latest"
steps:
- uses: actions/checkout@v4
- uses: awalsh128/cache-apt-pkgs-action@latest
Expand All @@ -49,10 +96,50 @@ jobs:
~/.cargo
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-
- name: Build binary
# the binary will be used by the next cargo test step
run: cargo build
- uses: actions/download-artifact@v4
with:
name: target
path: artifact
- run: tar -xvzf artifact/target.tgz && rm artifact/target.tgz
- name: Cache test docker image
uses: actions/cache@v4
with:
path: ~/docker-images/${{ matrix.bash_docker_image }}-${{ matrix.sed }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lens0021 This cache is passed between multiple CI runs, or is it only passed between matrix "threads"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intended it is passed between multiple CI runs. In a current run, there are 6 images which are different to each other and cannot be shared.

key: ${{ matrix.bash_docker_image }}-${{ matrix.sed }}
- name: Load or build docker image
run: |
if [ -f ~/docker-images/"${{ matrix.bash_docker_image }}-${{ matrix.sed }}" ]; then
docker load -i ~/docker-images/"${{ matrix.bash_docker_image }}-${{ matrix.sed }}"
else
docker build --tag testenv -f .github/workflows/TestEnvDockerfile \
--build-arg "BASE_BASH_IMAGE=${{ matrix.bash_docker_image }}" \
--build-arg "SED_VERSION=${{ matrix.sed }}" \
.
mkdir -p ~/docker-images
docker save -o ~/docker-images/"${{ matrix.bash_docker_image }}-${{ matrix.sed }}" testenv
fi
- name: Run cargo tests
run: cargo test --all-targets --all-features
env:
AMBER_TEST_STRATEGY: docker
AMBER_TEST_ARGS: exec test_container bash
run: |
docker run --network host --detach --name test_container testenv
cargo test --all-targets --all-features
docker stop test_container -t 0
docker rm --force test_container
clippy:
name: Clippy tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Cache dependencies installed with cargo
uses: actions/cache@v4
with:
path: |
./target/deps
~/.cargo
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-
- name: Run clippy check
run: cargo clippy --all-targets --all-features -- -D warnings
15 changes: 12 additions & 3 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,19 @@ impl AmberCompiler {
return None;
}

/// Return bash command. In some situations, mainly for testing purposes, this can return a command, for example, containerized execution which is not bash but behaves like bash.
#[cfg(not(windows))]
fn find_bash() -> Option<Command> {
let mut command = Command::new("/usr/bin/env");
command.arg("bash");
Some(command)
if env::var("AMBER_TEST_STRATEGY").is_ok_and(|value| value == "docker") {
let mut command = Command::new("docker");
let args_string = env::var("AMBER_TEST_ARGS").expect("Please pass docker arguments in AMBER_TEST_ARGS environment variable.");
let args: Vec<&str> = args_string.split_whitespace().collect();
command.args(args);
Some(command)
} else {
let mut command = Command::new("/usr/bin/env");
command.arg("bash");
Some(command)
}
}
}
Loading