feat: add isolated bats test suite for main.sh#37
Draft
hllvc wants to merge 8 commits into
Draft
Conversation
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



Summary
Adds an isolated, hermetic bats test suite (unit + smoke) for
main.sh, the minimal seams inmain.shthat make it testable, a lint setup that enforces modern Bash idioms, aMakefile, and a GitHub Actions workflow that runs on pull requests againstmain. The suite runs without root, network, Docker, or a real host, so it can gate every change.Motivation & Context
main.shis the private-runner registration entrypoint, yet it had no automated tests. Its most fragile surface — the proxy/runner-id injection guards, required-argument enforcement, and HTTP-status parsing (including the "a proxy inserts its own 200" case) — could regress silently. This introduces fast, deterministic tests that pin that behavior, mocking every host-affecting command so nothing touches the machine running them.Changes Made
main.shtestability seams (behavior-preserving). Wrappedmain "$@"in a[[ "${BASH_SOURCE[0]}" == "${0}" ]]guard so the script can be sourced without executing; made the hardcoded write paths (LOG_FILE,SG_DIAGNOSTIC_DIR,ECS_CONFIG_DIR,ECS_LOG_DIR,ECS_DATA_DIR,REGISTRATION_DIR) env-overridable with unchanged production defaults; added anSG_SKIP_ROOT_CHECKbypass inis_root()(off by default).[[ ]]inmain.shand the mock stubs.bats-core,bats-support,bats-assertas git submodules undertest/lib/; added a!test/lib/negation to.gitignore(the existinglib/rule was swallowing them).test/helpers/load.bash(single entry point: redirects all paths to a per-test temp dir, prepends the mockPATH, sourcesmain.shon demand),test/helpers/mocks.bashassertions, andtest/mocks/bin/*stubs for every external command (systemctl,docker,curl,iptables,crontab, ...).test/unit/, 104 tests). Arg parsing/validation,api_callstatus/response parsing,patch_json,update_diagnostic, predicates,iptables_ensureidempotency,configure_local_dataecs.config rendering, andcgroupsv2.test/smoke/, 11 tests). Drivesmain.shas a subprocess to assert the CLI contract (help output, command validation, required-argument enforcement) — the surface that exits beforepreflight(), so it needs no systemd, Docker, or root..shellcheckrc(enable=require-double-brackets,enable=deprecate-which,disable=SC2034) plus amake lintidiom gate —shellcheck --include=SC2292,SC2006over every shell source — enforcing[[ ]]over[ ]and$(...)over backticks.Makefile(test,test-unit,test-smoke,lint) and.github/workflows/test.yml(lint+unit-smokejobs,actions/checkout@v7withsubmodules: recursive), triggered on pull requests againstmain.README.md(fixed the table-of-contents anchors, corrected the stale/tmpdiagnostic/log paths to/var/lib/sg-runnerand/var/log, fixed acgroupsv2typo, added a Development section) and addedtest/README.mddocumenting the harness.Testing
make test— 115 tests, 0 failures, 1 intentional documenting skip.make lint—shellcheckcorrectness plus the modern-idiom gate, clean acrossmain.sh, the mock stubs, and the helpers.Risks & Edge Cases
main.shis modified. The seams are behavior-preserving: overridable paths default to the original values, the source guard only affectssource-ing, and the root check is unchanged unlessSG_SKIP_ROOT_CHECK=true. No runtime behavior changes when the script is executed normally.submodules: recursive; a plain clone yields no test runner..batsfiles are not shellchecked — their@testsyntax isn't valid standalone Bash, so the idiom gate covers.sh/.bashsources only.preflight()requires/run/systemd/system, so subcommands that reach preflight aren't exercised end to end by this host-only suite (documented as askip).Deployment Notes
pull_requesttargetingmain; it pinsactions/checkout@v7and installsshellcheckon the runner. No AWS accounts, secrets, or prod wiring involved.git submodule update --init --recursive).