Skip to content

test: clear DTCTL_* variables in the cmd test binary - #872

Merged
discostu105 merged 8 commits into
Dynatrace:mainfrom
Beehtrix:cursor/cmd-tests-clear-dtctl-env-1798
Dec 4, 2026
Merged

discostu105 merged 8 commits into
Dynatrace:mainfrom
Beehtrix:cursor/cmd-tests-clear-dtctl-env-1798

Conversation

@Beehtrix

@Beehtrix Beehtrix commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Makes the cmd test binary independent of the developer's shell and config.

  • TestMain unsets every DTCTL_* variable, matching the prefix case-insensitively because Windows environment names are case-insensitive. The re-exec guard asserts the child sees none, and its environment includes a mixed-case dtctl_context variable to cover that path.
  • Config-mutating tests in config_test.go and ctx_test.go run from their own temp working directory as well as an isolated XDG config home. A .dtctl.yaml in the repo or a parent directory is therefore never discovered and rewritten.

Fixes #826

Verification

  • go vet ./cmd/ and go test ./cmd/ -count=1 pass locally on Linux.
  • Not run on Windows here. The case-insensitive match is covered by the re-exec child test above.

TestMain now unsets every DTCTL_* variable, so the cmd tests no longer
depend on settings such as DTCTL_OUTPUT exported in the host shell.
The re-exec guard asserts the child sees none, and its marker variable
drops the DTCTL_ prefix so TestMain does not unset it.
@Beehtrix
Beehtrix requested review from a team and discostu105 as code owners December 1, 2026 10:46
Copilot AI lite review requested due to automatic review settings December 1, 2026 10:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The PR description contains unrelated pyfixest content and does not accurately document the DTCTL_* test-environment isolation change or its verification.

1 open issue
What changed in this PR

This PR isolates the cmd test binary from inherited DTCTL_* environment variables, preventing developer-specific settings from affecting test behavior.

Changes:

  • Clears inherited DTCTL_* variables before running the command test suite while preserving the test-only keyring setting.
  • Extends the subprocess regression test to verify that DTCTL_* variables are removed.
  • Uses a non-DTCTL_ child-process marker so it survives environment cleanup.
File Description
cmd/​main_test.go Clears inherited DTCTL_* environment variables before command tests and validates the cleanup in a re-executed test process.

Review effort: Lite

Comment thread cmd/main_test.go Resolved

@dynadev-ai dynadev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

❌ request changes

Request changes: clearing the config override can redirect mutating tests into a developer’s project config, and Windows environment variables are not fully scrubbed. The PR description also describes unrelated Python changes and should be corrected.


Review by DynaDev AI. Think this review is incorrect or incomplete? Report a DynaDev review issue, including this PR URL and the review-comment permalink.

Comment thread cmd/main_test.go Resolved
Comment thread cmd/main_test.go Resolved
Config-mutating tests now run from their own temp working directory as well
as an isolated XDG config home, so a .dtctl.yaml in the repo or a parent
directory is never discovered and rewritten.

TestMain matches DTCTL_ case-insensitively, because Windows environment
names are case-insensitive. The re-exec guard now also sets a mixed-case
dtctl_ variable to cover that path.
Copilot AI lite review requested due to automatic review settings December 1, 2026 11:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Windows-specific environment scrubbing remains insufficiently verified because testing was limited to Linux, leaving mixed-case handling, duplicate-name cleanup, and the child guard unvalidated.

No open issues 🎉

1 resolved since last review

Review effort: Lite

@dynadev-ai dynadev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💬 comment

Request changes: the config isolation fix is incomplete when the temporary directory is beneath a developer-owned .dtctl.yaml. The case-insensitive environment cleanup is fixed.

Follow-up Review — ✅ Fixed (1) · ⚠️ Remaining (0) · 🔄 Partial (1) · 🆕 New (0)

🔄 cmd/config_test.go:26 — partially addressed (Lines 23–26 change into t.TempDir(), but that directory can be under the repository when TMPDIR points there. Discovery still walks to the filesystem root, and TestConfigSetCmd can rewrite an ancestor .dtctl.yaml; pinning DTCTL_CONFIG to the temporary config would prevent discovery.)
✅ cmd/main_test.go:55 — fixed (Lines 55–56 use strings.EqualFold for the prefix, and lines 65–69 unset matches. The re-exec test supplies a mixed-case dtctl_context at line 144 and checks that it is absent.)

isolateConfigHome already chdirs into a temp directory and sets
XDG_CONFIG_HOME, but discovery still walks from that directory to the
filesystem root. When TMPDIR is inside the repository, that walk can
find a developer-owned .dtctl.yaml and a config-mutating test can
rewrite it. Pin DTCTL_CONFIG to the temporary global config file so
Load and Save skip discovery entirely.
Copilot AI lite review requested due to automatic review settings December 1, 2026 12:48
@Beehtrix

Beehtrix commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up from the last review: isolateConfigHome now also pins DTCTL_CONFIG to the temporary global config file (ae2a5ac). That skips discovery entirely, so a .dtctl.yaml above the temp directory cannot be loaded or rewritten even when TMPDIR sits inside the repo.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Approval is deferred because the change depends on Windows-specific case-insensitive environment and process-isolation behavior that was not validated beyond Linux, requiring Windows CI or a maintainer run.

No open issues 🎉

Review effort: Lite

@dynadev-ai dynadev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💬 comment

Request changes: the original config-mutation risk is only partially addressed. The named config and context tests are isolated, but the command probes can still rewrite an ancestor .dtctl.yaml, so the linked issue’s host-independence scope remains incomplete.

Follow-up Review — ✅ Fixed (0) · ⚠️ Remaining (0) · 🔄 Partial (1) · 🆕 New (0)

🔄 cmd/main_test.go:65 — partially addressed (The helper at cmd/config_test.go:27-31 pins the named tests to a temporary config, including when the temp directory is below the repository. However, isolateProbeHost clears DTCTL_CONFIG and changes into a temp directory (cmd/file_flags_probe_test.go:35-45); TestFileFlagsAreDeclared then runs config-mutating commands through runProbe without blocking them. If that temp directory is below a real .dtctl.yaml, their load and save paths can still discover and rewrite it.)

Copilot AI lite review requested due to automatic review settings December 1, 2026 22:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The changes comprehensively isolate environment, token, configuration, working-directory, and file-probe state across the affected tests, with no active blocking issues identified.

No open issues 🎉

Review effort: Lite

@dynadev-ai dynadev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💬 comment

Request changes: the original config-mutating tests are isolated, but the probe isolation still permits a destructive write to an ancestor config. The delta can replace that config's known fields with an empty test configuration.

Follow-up Review — ✅ Fixed (0) · ⚠️ Remaining (0) · 🔄 Partial (1) · 🆕 New (1)

🔄 cmd/file_flags_probe_test.go:47 — partially addressed (isolateConfigHome at cmd/config_test.go:27-31 pins the config and changes directory for the cited config and context tests. However, the probe helper at cmd/file_flags_probe_test.go:47 only sets the process environment; concurrent session probes scrub that value from their invocation overlay, allowing writes to discover an ancestor config. The new guard at lines 399-405 checks the environment value but does not exercise a write.)

Comment thread cmd/file_flags_probe_test.go Resolved
Session-backed Concurrent probes scrub DTCTL_CONFIG from the invocation
overlay, so saveConfig fell through to FindLocalConfig and could overwrite
an ancestor .dtctl.yaml with an empty config. Re-grant the isolateProbeHost
pin through RunOptions.Env and cover the write path with a regression test.
Copilot AI lite review requested due to automatic review settings December 1, 2026 23:14

@dynadev-ai dynadev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💬 comment

Request changes: both previously identified config-write hazards are fixed, but the linked issue remains incomplete because some tests still load the developer’s config. The new ancestor-preservation test also has a non-blocking coverage gap.

Follow-up Review — ✅ Fixed (2) · ⚠️ Remaining (0) · 🔄 Partial (0) · 🆕 New (2)

✅ cmd/config_test.go:27 — fixed (Lines 27–31 set a temporary XDG config home, change to that directory, and pin DTCTL_CONFIG to its config file. The affected config and ctx tests use this helper; the explicit pin also prevents discovery above a temporary directory.)
✅ cmd/file_flags_probe_test.go:219 — fixed (Lines 219–230 pass the process’s temporary config pin through RunOptions.Env, which is applied after session scrubbing. The config-save path therefore uses that pin rather than ancestor discovery; lines 429–459 add the requested ancestor-config scenario.)

Comment thread cmd/main_test.go Resolved
Comment thread cmd/file_flags_probe_test.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A moderate regression-test issue remains because it ignores the probe’s exit code, allowing a failed configuration write to produce a misleading passing result.

1 open issue

Review effort: Lite

Comment thread cmd/file_flags_probe_test.go Resolved
Clearing DTCTL_* left Run-level tests discovering a host .dtctl.yaml or
global config. Pin a minimal config after the scrub, keep discovery tests
able to clear the pin, and require the ancestor-preservation probe to
succeed with the editor saved on the pinned path.
Copilot AI lite review requested due to automatic review settings December 1, 2026 23:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Windows-specific environment behavior and the re-exec guard remain unverified because the PR did not run on Windows.

No open issues 🎉

1 resolved since last review

Review effort: Lite

@dynadev-ai dynadev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💬 comment

Request changes: both previous findings are fixed, but the linked issue remains partially unresolved. A discovery test still reads the developer’s global config, including any inline credentials.

Follow-up Review — ✅ Fixed (2) · ⚠️ Remaining (0) · 🔄 Partial (0) · 🆕 New (1)

✅ cmd/file_flags_probe_test.go:453 — fixed (Lines 453–466 now require a zero exit code, load the pinned config, verify the editor is vim, and then compare the ancestor file.)
✅ cmd/main_test.go:88 — fixed (Lines 88–99 create a minimal test-owned config and pin DTCTL_CONFIG before the suite runs; config.Load prioritizes that path over discovery. Discovery-specific helpers clear the pin, although the alias discovery test has the separate global-config issue reported above.)

Comment thread cmd/alias_resolve_test.go Resolved
…dentials

Clearing the DTCTL_CONFIG pin lets markLocal load the global config for host
bindings. Point XDG_CONFIG_HOME at a test-owned directory in TestMain (and
again before enabling discovery in TestResolveAlias_LocalConfigIgnored) so
issue Dynatrace#826's no-host-credential-read requirement holds.
Copilot AI lite review requested due to automatic review settings December 2, 2026 00:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The changes isolate inherited environment, configuration, credentials, and working-directory state while adding regression coverage for concurrent writes and ancestor preservation, with no active issues identified.

No open issues 🎉

Review effort: Lite

dynadev-ai[bot]
dynadev-ai Bot previously approved these changes Dec 2, 2026

@dynadev-ai dynadev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ approve

No critical code-review findings remain open. 2 warnings or nits are still open and don't block approval; their threads stay unresolved.

Approve: the previous host-config credential-read finding is fixed. Two non-blocking issues remain in the new test isolation and its coverage.

Follow-up Review — ✅ Fixed (1) · ⚠️ Remaining (0) · 🔄 Partial (0) · 🆕 New (2)

✅ cmd/alias_resolve_test.go:221 — fixed (TestMain sets XDG_CONFIG_HOME to its temporary config directory and reloads XDG at cmd/main_test.go:94-108. The alias test independently sets and reloads an isolated XDG home before clearing the pin and calling Load at lines 219-231, so markLocal resolves the global config under that temporary home.)

Comment thread cmd/alias_resolve_test.go Resolved
Comment thread cmd/main_test.go Resolved
With a relative GOTMPDIR, TempDir after chdir can fail and a relative
XDG_CONFIG_HOME stops resolving. Create temps first and use absolute
paths. Assert ConfigDir in a decoy-host child without Reload so a
missing xdg.Reload in TestMain is not masked.

Signed-off-by: Beehtrix <walkin_danger@hotmail.com>
Copilot AI lite review requested due to automatic review settings December 2, 2026 00:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Windows-specific environment scrubbing and child-process behavior remain unverified because validation was Linux-only, so Windows CI is needed.

No open issues 🎉

Review effort: Lite

@dynadev-ai dynadev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ approve

Approve. Both previous findings are fixed, and no new regressions were identified in the delta.

Follow-up Review — ✅ Fixed (2) · ⚠️ Remaining (0) · 🔄 Partial (0) · 🆕 New (0)

✅ cmd/alias_resolve_test.go:214 — fixed (Lines 169–219 create all three temporary directories before changing directories and convert the paths used afterward to absolute paths; the chdir occurs at line 225.)
✅ cmd/main_test.go:185 — fixed (Lines 194–210 start a child with a decoy XDG_CONFIG_HOME. The child checks ConfigDir and DefaultConfigPath at lines 185–189 without reloading first, so it verifies the reload in TestMain.)

@discostu105 discostu105 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. The iterations with the bots converged on the right design: scrub DTCTL_* case-insensitively, pin DTCTL_CONFIG to a file the test binary owns, isolate XDG_CONFIG_HOME and reload xdg, and make discovery tests opt out of the pin explicitly. I ran go test ./cmd/ on the head with DTCTL_OUTPUT=json and a lowercase dtctl_plain=1 exported, and it passes, which is the scenario from #826. Nothing in CI depends on DTCTL_* reaching cmd tests. No product code changes, so the risk is low.

One nit: in cmd/main_test.go the big TestMain doc comment (scrub, pin, XDG, data dir) now ends directly above isDTCTLVar, because the helpers were inserted between the comment and func TestMain. Godoc attaches all of it to isDTCTLVar. Move the two helpers above the comment, or below TestMain.

@discostu105
discostu105 merged commit 4aeb859 into Dynatrace:main Dec 4, 2026
18 checks passed
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.

The cmd tests read the developer's token store and output environment variables

3 participants