-
Notifications
You must be signed in to change notification settings - Fork 60
feat: add 'test' command #869
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
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,20 @@ | ||
| name: craft-test | ||
| base: ubuntu@24.04 | ||
| version: "0.1" | ||
| summary: A project used to test 'rockcraft test' | ||
| description: | | ||
| A project used to test 'rockcraft test'. | ||
| platforms: | ||
| amd64: | ||
|
|
||
| parts: | ||
| hello: | ||
| plugin: nil | ||
| stage-packages: [hello] | ||
|
|
||
| services: | ||
| hello: | ||
| override: merge | ||
| command: /usr/bin/hello -g "hello from craft-test rock" | ||
| startup: enabled | ||
| on-success: shutdown |
12 changes: 12 additions & 0 deletions
12
tests/spread/rockcraft/craft-test/spread-tests/hello-world/task.yaml
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,12 @@ | ||
| summary: test the 'craft-test' project | ||
|
|
||
| execute: | | ||
| # Load the rock into docker | ||
| ROCK=$(ls $PROJECT_PATH/craft-test*.rock) | ||
| sudo rockcraft.skopeo --insecure-policy copy "oci-archive:$ROCK" docker-daemon:craft-test:latest | ||
|
|
||
| # Run a container | ||
| docker run --rm craft-test:latest -v | MATCH "\[hello\] hello from craft-test rock" | ||
|
|
||
| restore: | | ||
| docker image rm --force craft-test |
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,25 @@ | ||
| project: craft-test | ||
|
|
||
| backends: | ||
| craft: | ||
| systems: | ||
| - ubuntu-22.04: | ||
| workers: 1 | ||
|
|
||
| suites: | ||
| spread-tests/: | ||
| summary: basic tests | ||
|
|
||
| prepare: | | ||
| sudo snap install docker | ||
|
|
||
| # For 'rockcraft.skopeo' | ||
| sudo snap install --classic rockcraft | ||
|
|
||
| # Wait for docker daemon to come online | ||
| sudo apt install --yes retry | ||
| retry --times=10 --delay 2 -- docker run hello-world | ||
|
|
||
| restore: | | ||
| # Other tests use 'retry' from snapd-testing-tools | ||
| sudo apt remove --yes retry |
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,195 @@ | ||
| #!/bin/bash | ||
|
|
||
| usage() { | ||
| echo "usage: $(basename "$0") [command]" | ||
| echo "valid commands:" | ||
| echo " allocate Create a backend instance to run tests on" | ||
| echo " discard Destroy a backend instance used to run tests" | ||
| echo " backend-prepare Set up the system to run tests" | ||
| echo " backend-restore Restore the system after the tests ran" | ||
| echo " backend-prepare-each Prepare the system before each test" | ||
| echo " backend-restore-each Restore the system after each test run" | ||
| } | ||
|
|
||
| prepare() { | ||
| case "$SPREAD_SYSTEM" in | ||
| fedora*) | ||
| dnf update -y | ||
| dnf install -y snapd | ||
| while ! snap install snapd; do | ||
| echo "waiting for snapd..." | ||
| sleep 2 | ||
| done | ||
| ;; | ||
| debian*) | ||
| apt update | ||
| apt install -y snapd | ||
| while ! snap install snapd; do | ||
| echo "waiting for snapd..." | ||
| sleep 2 | ||
| done | ||
| ;; | ||
| ubuntu*) | ||
| apt update | ||
| ;; | ||
| esac | ||
|
|
||
| snap wait system seed.loaded | ||
| snap refresh --hold | ||
|
|
||
| if systemctl is-enabled unattended-upgades.service; then | ||
| systemctl stop unattended-upgrades.service | ||
| systemctl mask unattended-upgrades.service | ||
| fi | ||
| } | ||
|
|
||
| restore() { | ||
| case "$SPREAD_SYSTEM" in | ||
| ubuntu* | debian*) | ||
| apt autoremove -y --purge | ||
| ;; | ||
| esac | ||
|
|
||
| rm -Rf "$PROJECT_PATH" | ||
| mkdir -p "$PROJECT_PATH" | ||
| } | ||
|
|
||
| prepare_each() { | ||
| true | ||
| } | ||
|
|
||
| restore_each() { | ||
| true | ||
| } | ||
|
|
||
| allocate_lxdvm() { | ||
| name=$(echo "$SPREAD_SYSTEM" | tr '[:punct:]' -) | ||
| system=$(echo "$SPREAD_SYSTEM" | tr / -) | ||
| if [[ "$system" =~ ^ubuntu- ]]; then | ||
| image="ubuntu:${system#ubuntu-}" | ||
| else | ||
| image="images:$(echo "$system" | tr - /)" | ||
| fi | ||
|
|
||
| VM_NAME="${VM_NAME:-spread-${name}-${RANDOM}}" | ||
| DISK="${DISK:-20}" | ||
| CPU="${CPU:-4}" | ||
| MEM="${MEM:-8}" | ||
|
|
||
| lxc launch --vm \ | ||
| "${image}" \ | ||
| "${VM_NAME}" \ | ||
| -c limits.cpu="${CPU}" \ | ||
| -c limits.memory="${MEM}GiB" \ | ||
| -d root,size="${DISK}GiB" | ||
|
|
||
| while ! lxc exec "${VM_NAME}" -- true &>/dev/null; do sleep 0.5; done | ||
| lxc exec "${VM_NAME}" -- sed -i 's/^\s*#\?\s*\(PermitRootLogin\|PasswordAuthentication\)\>.*/\1 yes/' /etc/ssh/sshd_config | ||
| lxc exec "${VM_NAME}" -- bash -c "if [ -d /etc/ssh/sshd_config.d ]; then echo -e 'PermitRootLogin yes\nPasswordAuthentication yes' > /etc/ssh/sshd_config.d/00-spread.conf; fi" | ||
| lxc exec "${VM_NAME}" -- bash -c "echo root:${SPREAD_PASSWORD} | sudo chpasswd || true" | ||
|
|
||
| # Print the instance address to stdout | ||
| ADDR="" | ||
| while [ -z "$ADDR" ]; do ADDR=$(lxc ls -f csv | grep "^${VM_NAME}" | cut -d"," -f3 | cut -d" " -f1); done | ||
| echo "$ADDR" 1>&3 | ||
| } | ||
|
|
||
| discard_lxdvm() { | ||
| instance_name="$(lxc ls -f csv | sed ':a;N;$!ba;s/(docker0)\n/(docker0) /' | grep "$SPREAD_SYSTEM_ADDRESS " | cut -f1 -d",")" | ||
tigarmo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| lxc delete -f "$instance_name" | ||
| } | ||
|
|
||
| allocate_ci() { | ||
| if [ -z "$CI" ]; then | ||
| echo "This backend is intended to be used only in CI systems." | ||
| exit 1 | ||
| fi | ||
| sudo sed -i 's/^\s*#\?\s*\(PermitRootLogin\|PasswordAuthentication\)\>.*/\1 yes/' /etc/ssh/sshd_config | ||
| if [ -d /etc/ssh/sshd_config.d ]; then echo -e 'PermitRootLogin yes\nPasswordAuthentication yes' | sudo tee /etc/ssh/sshd_config.d/00-spread.conf; fi | ||
| sudo systemctl daemon-reload | ||
| sudo systemctl restart ssh | ||
|
|
||
| echo "root:${SPREAD_PASSWORD}" | sudo chpasswd || true | ||
|
|
||
| # Print the instance address to stdout | ||
| echo localhost >&3 | ||
| } | ||
|
|
||
| discard_ci() { | ||
| true | ||
| } | ||
|
|
||
| allocate() { | ||
| exec 3>&1 | ||
| exec 1>&2 | ||
|
|
||
| case "$1" in | ||
| lxd-vm) | ||
| allocate_lxdvm | ||
| ;; | ||
| ci) | ||
| allocate_ci | ||
| ;; | ||
| *) | ||
| echo "unsupported backend $1" 2>&1 | ||
| ;; | ||
| esac | ||
| } | ||
|
|
||
| discard() { | ||
| case "$1" in | ||
| lxd-vm) | ||
| discard_lxdvm | ||
| ;; | ||
| ci) | ||
| discard_ci | ||
| ;; | ||
| *) | ||
| echo "unsupported backend $1" 2>&1 | ||
| ;; | ||
| esac | ||
| } | ||
|
|
||
| set -e | ||
|
|
||
| while getopts "" o; do | ||
| case "${o}" in | ||
| *) | ||
| usage | ||
| exit 1 | ||
| ;; | ||
| esac | ||
| done | ||
| shift $((OPTIND - 1)) | ||
|
|
||
| CMD="$1" | ||
| PARM="$2" | ||
|
|
||
| if [ -z "$CMD" ]; then | ||
| usage | ||
| exit 0 | ||
| fi | ||
|
|
||
| case "$CMD" in | ||
| allocate) | ||
| allocate "$PARM" | ||
| ;; | ||
| discard) | ||
| discard "$PARM" | ||
| ;; | ||
| backend-prepare) | ||
| prepare | ||
| ;; | ||
| backend-restore) | ||
| restore | ||
| ;; | ||
| backend-prepare-each) | ||
| prepare_each | ||
| ;; | ||
| backend-restore-each) | ||
| restore_each | ||
| ;; | ||
| *) | ||
| echo "unknown command $CMD" >&2 | ||
| ;; | ||
| esac | ||
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,7 @@ | ||
| summary: Test 'rockcraft test' | ||
|
|
||
| environment: | ||
| CI: "1" | ||
|
|
||
| execute: | | ||
| rockcraft test -v 2>&1 | MATCH "Successful tasks: 1" |
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.