-
Notifications
You must be signed in to change notification settings - Fork 10
ENT-14443: Added cfengine test, a one-stop command to lint/build/deploy/run a policy-set
#232
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
SimonThalvorsen
wants to merge
3
commits into
cfengine:main
Choose a base branch
from
SimonThalvorsen:ENT-14443
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
4876911
Added `cfengine test`, a one-stop command to lint/build/deploy/run a …
SimonThalvorsen baaeda3
Changed `local` to `localhost` for interoperability with`cf-remote`
SimonThalvorsen 30e5f8f
Added coverage variables to target in Makfile and $(pwd) before src/ …
SimonThalvorsen 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| [run] | ||
| parallel = True | ||
| source = src | ||
| source = ${PROJECT_ROOT}/src | ||
| concurrency = multiprocessing | ||
| sigterm = True |
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
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
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,50 @@ | ||
| import os | ||
| import shutil | ||
| import subprocess | ||
|
|
||
| from cfengine_cli.utils import UserError | ||
|
|
||
| _DOCKERFILE_DIR = os.path.join(os.path.dirname(__file__), "docker", "test-agent") | ||
| _IMAGE_TAG = "cfengine-cli-test-agent:latest" | ||
|
|
||
|
|
||
| def require_docker() -> None: | ||
| if shutil.which("docker") is None: | ||
| raise UserError( | ||
| "`cfengine test` runs the built policy set inside a Docker container -- install Docker to use it." | ||
| ) | ||
|
|
||
|
|
||
| def _ensure_image_built() -> None: | ||
| result = subprocess.run( | ||
| ["docker", "build", "-q", "-t", _IMAGE_TAG, _DOCKERFILE_DIR] | ||
| ) | ||
| if result.returncode != 0: | ||
| raise UserError("Failed to build the cfengine-test Docker image.") | ||
|
|
||
|
|
||
| def run_in_container(masterfiles_dir: str) -> int: | ||
| """ | ||
| Runs cf-agent against the given built masterfiles directory inside a container | ||
| """ | ||
| require_docker() | ||
| _ensure_image_built() | ||
|
|
||
| abs_masterfiles = os.path.abspath(masterfiles_dir) | ||
| result = subprocess.run( | ||
| [ | ||
| "docker", | ||
| "run", | ||
| "--rm", | ||
| "-v", | ||
| f"{abs_masterfiles}:/mnt/masterfiles:ro", | ||
| _IMAGE_TAG, | ||
| "sh", | ||
| "-c", | ||
| "rm -rf /var/cfengine/inputs " | ||
| "&& cp -r /mnt/masterfiles /var/cfengine/inputs " | ||
| "&& /var/cfengine/bin/cf-agent -KIf update.cf " | ||
| "&& /var/cfengine/bin/cf-agent -KI", | ||
| ] | ||
| ) | ||
| return result.returncode |
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,17 @@ | ||
| # Install cfengine community (client) | ||
| # `cfengine test` never needs a real hub, given that it is only supposed | ||
| # to check and execute a policy set. | ||
| FROM debian:12-slim | ||
| ENV PATH="/root/.local/bin:${PATH}" | ||
|
|
||
| RUN apt-get update \ | ||
| && apt-get install -y --no-install-recommends \ | ||
| python3 pipx sudo | ||
| RUN pipx install cfengine --force | ||
| # RUN cfengine install --hub localhost --edition community | ||
| # Does not work since localhost resolves to `local` in cfengine cli | ||
| RUN cfengine install --clients localhost --edition community | ||
|
|
||
| # The package promise module wrapper (and other Python-backed modules) look | ||
| # for an interpreter here | ||
| RUN ln -s "$(command -v python3)" /var/cfengine/bin/cfengine-selected-python | ||
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,26 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
| set -x | ||
|
|
||
| # Setup: create a temp directory for test files | ||
| tmpdir=$(mktemp -d) | ||
| trap "rm -rf $tmpdir" EXIT | ||
|
|
||
| cfengine test --help | ||
|
|
||
| # A file with a lint error should make `cfengine test` fail on the lint step | ||
| # and not go through to build/deploy/run. | ||
| printf 'bundle agent main\n{\n reports:\n "missing semicolon"\n}\n' > "$tmpdir/bad.cf" | ||
|
|
||
| output_file=$(mktemp) | ||
| trap "rm -rf $tmpdir $output_file" EXIT | ||
|
|
||
| if cfengine test "$tmpdir/bad.cf" > "$output_file" 2>&1; then | ||
| cat "$output_file" | ||
| echo "FAIL: expected cfengine test to fail on a lint error" | ||
| exit 1 | ||
| fi | ||
| cat "$output_file" | ||
| grep -q "Lint failed" "$output_file" | ||
| grep -q "Skipping build/deploy/run" "$output_file" |
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,16 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
| set -x | ||
|
|
||
| # Setup: create a temp directory for test files | ||
| tmpdir=$(mktemp -d) | ||
| trap "rm -rf $tmpdir" EXIT | ||
|
|
||
| mkdir $tmpdir/test-module | ||
| cd $tmpdir/test-module | ||
|
|
||
| cfengine init --policy-module --with-input --non-interactive | ||
| cfengine test | ||
|
|
||
|
|
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.
will work when new version of cfengine-cli is released (with the changes in this pr)