Skip to content

Commit 4c37354

Browse files
authored
Merge pull request #232 from SimonThalvorsen/ENT-14443
ENT-14443: Added `cfengine test`, a one-stop command to lint/build/deploy/run a policy-set
2 parents 7e4fdde + bd62179 commit 4c37354

13 files changed

Lines changed: 171 additions & 15 deletions

File tree

‎.coveragerc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[run]
22
parallel = True
3-
source = src
3+
source = ${PROJECT_ROOT}/src
44
concurrency = multiprocessing
55
sigterm = True

‎Makefile‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ install:
2323
pipx install --force --editable .
2424

2525

26-
export COVERAGE_PROCESS_START = $(PWD)/.coveragerc
26+
coverage: export COVERAGE_PROCESS_START = $(PWD)/.coveragerc
27+
coverage: export COVERAGE_FILE = $(PWD)/.coverage
28+
coverage: export PROJECT_ROOT = $(PWD)
2729
coverage:
2830
uv run coverage erase
2931
uv run coverage run --parallel-mode -m pytest

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ cfengine = "cfengine_cli.main:main"
4343
license-files = [] # Workaround bug in setuptools https://github.com/astral-sh/uv/issues/9513
4444

4545
[tool.setuptools.package-data]
46-
cfengine_cli = ["*.json"] # syntax-description.json
46+
cfengine_cli = ["*.json", "docker/test-agent/Dockerfile"] # syntax-description.json, cfengine test's container
4747

4848
[tool.pyright]
4949
include = ["src"]

‎src/cfengine_cli/cfengine_wrapper/cfengine_commands.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323

2424
from cfengine_cli.utils import UserError
25+
from cfengine_cli.container import run_in_container
2526
from cfengine_cli.cfengine_wrapper.cfengine_objects import (
2627
Executable,
2728
ensure_default_agent_flags,
@@ -271,10 +272,7 @@ def deploy(
271272

272273
# TODO/WOULD be nice: Deploy without run (CFE-4704: https://northerntech.atlassian.net/browse/CFE-4704)
273274
if hubs:
274-
# cf-remote functions use "localhost" (not "local" as it is here)
275-
deploy_targets = [
276-
"localhost" if location == "local" else location for location in hubs
277-
]
275+
deploy_targets = [location for location in hubs]
278276
error = deploy_command(deploy_targets, masterfiles)
279277
else:
280278
return deploy_command(hubs, masterfiles)
@@ -287,6 +285,14 @@ def deploy(
287285
return error
288286

289287

288+
def test() -> int:
289+
rc = build_command()
290+
if rc != 0:
291+
return rc
292+
293+
return run_in_container("out/masterfiles")
294+
295+
290296
def show(target: list[str] | None = None) -> int:
291297
if target == [] or target is None:
292298
return show_command(False)

‎src/cfengine_cli/cfengine_wrapper/cfengine_objects.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ def ensure_default_agent_flags(command: str) -> str:
2727
class Executable:
2828
"""
2929
A single binary (cf-agent or cf-hub) at a known location -- either
30-
"local" or a remote host identifier ("user@ip"). Knows its own path
30+
localhost or a remote host identifier ("user@ip"). Knows its own path
3131
and how to run a command against itself, whether that means a local
3232
subprocess or an SSH call via cf-remote.
3333
"""
3434

3535
def __init__(self, name: str, location: str, path: str, aliases=None) -> None:
3636
self.name = name # "cf-agent" / "cf-hub"
37-
self.location = location # "local" or "user@ip"
37+
self.location = location # "localhost" or "user@ip"
3838
self.path = path # absolute path to the binary at that location
3939
self.aliases = (
4040
aliases or []
4141
) # friendly names from cf-remote's saved state, e.g. "hub", "local", "remote"
4242

4343
@property
4444
def is_local(self) -> bool:
45-
return self.location == "local"
45+
return self.location == "localhost"
4646

4747
@property
4848
def label(self) -> str:
@@ -102,7 +102,7 @@ class Installation:
102102

103103
@property
104104
def is_local(self) -> bool:
105-
return self.location == "local"
105+
return self.location == "localhost"
106106

107107
@property
108108
def aliases(self) -> list:

‎src/cfengine_cli/cfengine_wrapper/cfengine_utils.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,16 @@ def _hosts_with_info(role_filter=None):
111111

112112
def _identities(binary_name: str) -> Iterator[_Id]:
113113
"""local + every known host as (location, aliases), without connecting."""
114-
yield _Id("local", ["localhost"])
114+
yield _Id("localhost", ["localhost", "local"])
115115
for host, aliases in _known_hosts(None if binary_name == "cf-agent" else "hub"):
116116
yield _Id(host, aliases)
117117

118118

119119
def _resolve(binary_name: str, ident: _Id) -> Executable | None:
120120
"""Connect (if remote) and build an Executable, or None if unavailable."""
121-
if ident.location == "local":
121+
if ident.location == "localhost":
122122
path = _find_local_path(binary_name)
123-
return Executable(binary_name, "local", path) if path else None
123+
return Executable(binary_name, "localhost", path) if path else None
124124
data = _host_info(ident.location)
125125
if not data:
126126
return None

‎src/cfengine_cli/commands.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from cfengine_cli.initialize_project import init_policy_module, init_promise_type
1313
from cf_remote.paths import cf_remote_dir
1414
from cfbs.commands import init_command
15+
from cfengine_cli.cfengine_wrapper.cfengine_commands import test as _cfengine_test
1516
from pydantic import ValidationError
1617

1718

@@ -47,6 +48,17 @@ def lint(files, strict, syntax_path) -> int:
4748
return errors
4849

4950

51+
def test(files, strict) -> int:
52+
errors = _lint(files, strict, None)
53+
if errors != 0:
54+
plural = "error" if errors == 1 else "errors"
55+
print(f"Lint failed, {errors} {plural} in total. Skipping build/deploy/run.")
56+
return errors
57+
58+
print("Lint passed, no errors found.")
59+
return _cfengine_test()
60+
61+
5062
def dev(subcommand, args) -> int:
5163
return dispatch_dev_subcommand(subcommand, args)
5264

‎src/cfengine_cli/container.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
import shutil
3+
import subprocess
4+
5+
from cfengine_cli.utils import UserError
6+
7+
_DOCKERFILE_DIR = os.path.join(os.path.dirname(__file__), "docker", "test-agent")
8+
_IMAGE_TAG = "cfengine-cli-test-agent:latest"
9+
10+
11+
def require_docker() -> None:
12+
if shutil.which("docker") is None:
13+
raise UserError(
14+
"`cfengine test` runs the built policy set inside a Docker container -- install Docker to use it."
15+
)
16+
17+
18+
def _ensure_image_built() -> None:
19+
result = subprocess.run(
20+
["docker", "build", "-q", "-t", _IMAGE_TAG, _DOCKERFILE_DIR]
21+
)
22+
if result.returncode != 0:
23+
raise UserError("Failed to build the cfengine-test Docker image.")
24+
25+
26+
def run_in_container(masterfiles_dir: str) -> int:
27+
"""
28+
Runs cf-agent against the given built masterfiles directory inside a container
29+
"""
30+
require_docker()
31+
_ensure_image_built()
32+
33+
abs_masterfiles = os.path.abspath(masterfiles_dir)
34+
result = subprocess.run(
35+
[
36+
"docker",
37+
"run",
38+
"--rm",
39+
"-v",
40+
f"{abs_masterfiles}:/mnt/masterfiles:ro",
41+
_IMAGE_TAG,
42+
"sh",
43+
"-c",
44+
"rm -rf /var/cfengine/inputs "
45+
"&& cp -r /mnt/masterfiles /var/cfengine/inputs "
46+
"&& /var/cfengine/bin/cf-agent -KIf update.cf "
47+
"&& /var/cfengine/bin/cf-agent -KI",
48+
]
49+
)
50+
return result.returncode
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Install cfengine community (client)
2+
# `cfengine test` never needs a real hub, given that it is only supposed
3+
# to check and execute a policy set.
4+
FROM debian:12-slim
5+
ENV PATH="/root/.local/bin:${PATH}"
6+
7+
RUN apt-get update \
8+
&& apt-get install -y --no-install-recommends \
9+
python3 pipx sudo
10+
RUN pipx install cfengine --force
11+
# RUN cfengine install --hub localhost --edition community
12+
# Does not work since localhost resolves to `local` in cfengine cli
13+
RUN cfengine install --clients localhost --edition community
14+
15+
# The package promise module wrapper (and other Python-backed modules) look
16+
# for an interpreter here
17+
RUN ln -s "$(command -v python3)" /var/cfengine/bin/cfengine-selected-python

‎src/cfengine_cli/main.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ def _get_arg_parser():
8888
)
8989
lnt.add_argument("files", nargs="*", help="Files to lint")
9090

91+
tst = subp.add_parser(
92+
"test",
93+
help="Lint, then build/run a cfbs project in a throwaway container as one non-interactive check",
94+
description="A convenience wrapper chaining `cfengine lint`, `cfengine build`, `cfengine deploy` and `cfengine run` "
95+
"to quickly test a module or policy-set end-to-end.",
96+
)
97+
tst.add_argument(
98+
"--strict",
99+
type=str,
100+
default="yes",
101+
help="Strict mode for linting. Default=yes, checks for undefined promise types, bundles, bodies, functions",
102+
)
103+
tst.add_argument(
104+
"files",
105+
nargs="*",
106+
help="Files/Folder to lint (default is . (the entire project) )",
107+
)
108+
91109
dev_parser = subp.add_parser(
92110
"dev", help="Utilities intended for developers / maintainers of CFEngine"
93111
)
@@ -245,6 +263,11 @@ def run_command_with_args(args) -> int:
245263
(args.strict.lower() in ("y", "ye", "yes")),
246264
args.syntax_description,
247265
)
266+
if args.command == "test":
267+
return commands.test(
268+
args.files,
269+
(args.strict.lower() in ("y", "ye", "yes")),
270+
)
248271
if args.command == "report":
249272
return cfengine_commands.report(
250273
target=args.hub,
@@ -376,7 +399,7 @@ def validate_args(args):
376399
if "hub" in args and args.hub:
377400
log.debug(f"validate_args, hubs in args, args.hub='{args.hub}'")
378401
if args.hub in ["local", "localhost"]:
379-
args.hub = ["local"]
402+
args.hub = ["localhost"]
380403
else:
381404
args.hub = resolve_hosts(args.hub)
382405

0 commit comments

Comments
 (0)