Skip to content

test: add coverage for removeComments CSE stripping logic#8489

Merged
cameronmeissner merged 9 commits into
mainfrom
djsly/37912827
May 11, 2026
Merged

test: add coverage for removeComments CSE stripping logic#8489
cameronmeissner merged 9 commits into
mainfrom
djsly/37912827

Conversation

@djsly
Copy link
Copy Markdown
Collaborator

@djsly djsly commented May 11, 2026

Summary

Add Go unit tests that validate all CSE shell scripts survive the removeComments → gzip → base64 → decode → gunzip pipeline without breaking bash syntax.

Background

PR #8475 (DirtyFrag kernel module blacklist) was dead on arrival because removeComments in pkg/agent/utils.go stripped a # description line from inside a printf format string, breaking the script. PR #8486 fixed the immediate issue, but there was no test to prevent this class of regression.

What this PR adds

TestRemoveComments_ShellPatterns — 6 table-driven subtests

  • Pure comment removal
  • Hash inside quoted grep pattern (with # in input)
  • Trailing comment trimming
  • Shebang preservation
  • Hash in variable expansions (${#array[@]})
  • DOA regression pattern: documents that # inside string literals IS stripped (the root cause)

TestCSEScriptRoundTrip — 34 CSE pipeline scripts

  • Script list derived from getBase64EncodedGzippedCustomScript() calls in variables.go
  • Full pipeline: removeComments → gzip → base64 → decode → gunzip
  • Byte-for-byte round-trip integrity check
  • bash -n syntax validation on each decoded script (with extglob enabled)
  • Scripts with Go template directives ({{}}) skip bash -n (not valid bash pre-execution)

Related

Add unit tests and integration test for the removeComments function that
strips comments from shell scripts during CSE assembly. The existing tests
only covered simple comment patterns and did not exercise realistic CSE
script content.

New coverage includes:
- disableVulnerableKernelModule function body survives stripping
- Function call sites are preserved after stripping
- grep patterns with hash characters are not mistaken for comments
- Hash in variable expansions (${#array[@]}) is preserved
- Documents the known limitation where comment-like lines inside string
  literals get stripped (root cause of DirtyFrag DOA bug PR #8475)
- Integration test reads actual cse_main.sh and validates critical
  patterns survive removeComments intact

AB#37912827

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds additional Go test coverage around removeComments (used during Linux CSE assembly) to prevent regressions where comment-stripping heuristics accidentally remove functionally significant shell content (notably around the DirtyFrag/CVE kernel module blacklist mitigation added recently in cse_main.sh).

Changes:

  • Adds table-driven unit tests covering several shell-script patterns that removeComments must handle safely.
  • Adds an integration-style test that loads parts/linux/cloud-init/artifacts/cse_main.sh, runs removeComments, and validates key disableVulnerableKernelModule patterns and call sites survive stripping.
  • Introduces a repoRoot() helper to locate cse_main.sh without hardcoded absolute paths.

Comment thread pkg/agent/utils_test.go Outdated
Comment thread pkg/agent/utils_test.go Outdated
Comment thread pkg/agent/utils_test.go Outdated
Replace the removeComments-only integration test with comprehensive
full-pipeline round-trip tests that exercise the exact same path used
in production: removeComments → gzip → base64 → decode → gunzip.

TestCSEScriptRoundTrip validates all 6 embedded CSE shell scripts:
- Byte-for-byte round-trip integrity (decoded == stripped input)
- bash -n syntax check on decoded output (skipped for cse_cmd.sh
  which contains Go template directives pre-execution)

TestCSEScriptRoundTrip_CSEMainShCriticalContent validates cse_main.sh
critical content survives the full pipeline (function declaration,
call sites, printf patterns, modprobe commands, grep patterns, and
no orphaned install/blacklist lines).

This would have caught the PR #8475 DOA bug where removeComments
broke a printf format string, producing invalid bash that only
failed at runtime on the VM.

AB#37912827

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Dynamically discover all .sh files in artifacts/ (55+ scripts, including subdirs)
- Full pipeline: removeComments → gzip → base64 → decode → gunzip → bash -n
- Remove DirtyFrag-specific content assertions — the bash -n check is sufficient
- Enable extglob for syntax check (scripts use @() patterns)
- Document known removeComments limitation for aks-localdns-hosts-setup.sh

AB#37912827

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 11, 2026 15:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.

Comment thread pkg/agent/utils_test.go Outdated
Comment thread pkg/agent/utils_test.go Outdated
Comment thread pkg/agent/utils_test.go Outdated
Comment thread pkg/agent/utils_test.go Outdated
- Extract cseRoundTrip and cseValidateBashSyntax helpers to reduce
  cognitive complexity (gocognit: 38 → under 20)
- Fix err shadow (govet) by using writeErr variable
- Fix goimports formatting
- Rename 'grep with hash pattern' test to clarify intent

AB#37912827

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 11, 2026 15:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.

Comment thread pkg/agent/utils_test.go
Comment thread pkg/agent/utils_test.go Outdated
Comment thread pkg/agent/utils_test.go
Comment thread pkg/agent/utils_test.go Outdated
The aks-localdns-hosts-setup.sh fix is being addressed in a
separate PR. This test should have zero skip exceptions.

AB#37912827

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use authoritative script list from getBase64EncodedGzippedCustomScript()
calls in variables.go instead of walking all .sh files. Only scripts
that flow through removeComments in production are tested — no skip
lists needed.

AB#37912827

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 11, 2026 16:16
- Add test documenting known limitation: '# ' inside string literals
  gets stripped by removeComments (the PR #8475 DOA root cause)
- Fix grep test to actually include '#' in the pattern

AB#37912827

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.

Comment thread pkg/agent/utils_test.go
Comment thread pkg/agent/utils_test.go
Comment thread pkg/agent/utils_test.go
Comment thread pkg/agent/utils_test.go
Replace hardcoded script list with discoverCSEScripts() that parses
variables.go for getBase64EncodedGzippedCustomScript() calls, resolves
constant names from const.go, and filters to .sh files. New scripts
added to the CSE pipeline are automatically covered.

AB#37912827

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cameronmeissner cameronmeissner merged commit 18560af into main May 11, 2026
23 checks passed
@cameronmeissner cameronmeissner deleted the djsly/37912827 branch May 11, 2026 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants