Skip to content

Commit 4c87e6a

Browse files
denikIsaac
andauthored
acc: add plan_filter.py to keep backend-owned remote_state out of plan goldens (#7764)
## Changes New acceptance helper `acceptance/bin/plan_filter.py`: reads `bundle plan -o json` on stdin, drops `remote_state` from every plan entry and removes the deployment stamp (reusing `nostamp.render`). With a resource key it prints just that entry, and fails if the key isn't in the plan. So ``` $CLI bundle plan -o json | nostamp | jq '(.plan."resources.X.y" // .) | del(.remote_state)' ``` becomes ``` $CLI bundle plan -o json | plan_filter.py resources.X.y ``` It's covered by a selftest (`acceptance/selftest/plan_filter`) and documented in `.agents/rules/testing.md`, along with a rule to use it unless a test checks `remote_state` itself. Applied to the cloud postgres plan tests: `postgres_branches/update_protected`, `postgres_databases/update`, `postgres_roles/update`, `postgres_projects/add_default_endpoint_settings`, plus `postgres_projects/update_display_name` and `postgres_endpoints/update_autoscaling`, which already dropped `remote_state` with jq. The golden updates only remove `remote_state` blocks (3 insertions, all from trailing-comma changes, 193 deletions). ## Why `remote_state` is the backend's GET response copied into the plan as is: status, timestamps, sizes, UIDs, and any field the backend starts returning. If a golden records it, the test fails whenever the backend changes, even when the CLI's decision (`action`, `new_state`, `changes`) is the same, and the failure shows up on every cloud at once. `changes[*].remote` still records the remote value of each field the plan compares. Measured on main before this PR: - 115 goldens record `remote_state`, 42 of them in `Cloud = true` tests. - Over 30 days of main runs, these "Plan differs" errors had every diff line inside `remote_state`, so this filter would have prevented them: [error/2961](http://go/cli/dashboard/error/2961?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d) (13 failures), [error/2893](http://go/cli/dashboard/error/2893?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d) (10), [error/2964](http://go/cli/dashboard/error/2964?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d), [error/3587](http://go/cli/dashboard/error/3587?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d), [error/3526](http://go/cli/dashboard/error/3526?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d). #7745 and #7495 fixed some of these test by test. - Not prevented: the postgres GET change from last week ([error/3534](http://go/cli/dashboard/error/3534?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d), [error/3535](http://go/cli/dashboard/error/3535?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d), [error/3537](http://go/cli/dashboard/error/3537?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d) and siblings, fixed in #7596). Once the backend echoed spec fields back, `changes` changed too (`skip`/`spec:input_only` entries disappeared and `remote` values appeared), so those tests would have failed with or without `remote_state`. That's a real change in the plan, and the goldens should catch it. | error | snippet (inside `remote_state`) | |---|---| | [error/2961](http://go/cli/dashboard/error/2961?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d) | `+ "compute_last_active_time": "[TIMESTAMP]",` | | [error/2893](http://go/cli/dashboard/error/2893?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d) | `Unexpected output "status": "MONITOR_STATUS_PENDING",` | ## Decisions - Tests opt in with a helper; the runner doesn't strip `remote_state` by default. Some tests check `remote_state` on purpose (dashboards' `serialized_dashboard`, grants' `__embed__` principals, `apps/config-env-no-drift`, `quality_monitors`' allow-list), and a filter hidden in the runner would make those goldens misleading. - `changes[*].remote` is kept. It's the input to the CLI's drift decision, and out-of-band tests depend on it. - When the resource key is missing, `plan_filter.py` fails. The old `jq '.plan."KEY" // .'` silently wrote the whole plan instead. None of the converted goldens relied on that fallback, and a missing entry should fail loudly. - For `skip` entries (the `no_change` goldens), only `action` is left, because a skip has no `changes`. That's intended: the plan's decision is the assertion. If a broken read changed what the CLI sees, the action would turn into update or recreate, and that still shows up. - It's a separate script rather than a flag on `nostamp.py`. `nostamp` is also used for states and request bodies, where `remote_state` doesn't apply. `plan_filter.py` reuses `nostamp.render`, so stamp handling stays in one place. - Scope is the postgres family, which has churned the most. The other cloud goldens that record `remote_state` (dashboards, grants, quality_monitors) check it on purpose or already filter it. Local-only tests can adopt the helper when they're next touched. ## Tests - `selftest/plan_filter` (local): whole plan, one entry, missing entry, non-plan and empty input. - All 12 variants (DMS= and DMS=true) of the six postgres tests pass on AWS (`testme-aws`). The last commit message requests the same AWS run in CI. ## Dashboard [error/3534](http://go/cli/dashboard/error/3534?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d), [error/3535](http://go/cli/dashboard/error/3535?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d), [error/3537](http://go/cli/dashboard/error/3537?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d): these motivated the change. They were already fixed on main by #7596, and this PR wouldn't have prevented them (see above). It does prevent the class above: [error/2961](http://go/cli/dashboard/error/2961?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d), [error/3587](http://go/cli/dashboard/error/3587?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d), [error/2964](http://go/cli/dashboard/error/2964?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d), [error/3526](http://go/cli/dashboard/error/3526?main=1&pr=1&end=2026-11-18T11:00:00Z&last=14d). ``` Plan differs (out.plan.update.json): Unexpected output "is_protected": false, "no_expiry": true, Plan differs (out.plan.restore.json): Unexpected output "compute_last_active_time": "[TIMESTAMP]", ``` This pull request and its description were written by Isaac. --------- Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 247b83e commit 4c87e6a

22 files changed

Lines changed: 193 additions & 216 deletions

File tree

‎.agents/rules/testing.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,15 @@ Available on `PATH` during test execution (from `acceptance/bin/`):
180180
- `find.py REGEX [--expect N]`: find files matching regex in current directory. `--expect N` asserts an exact count.
181181
- `diff.py DIR1 DIR2` or `diff.py FILE1 FILE2`: recursive diff with test replacements applied.
182182
- `print_state.py [-t TARGET] [--backup]`: print deployment state (`resources.json`, or a terraform state left by an older CLI).
183+
- `plan_filter.py [RESOURCE_KEY]`: filter `bundle plan -o json` on stdin for a golden: drops every entry's `remote_state` and the deployment stamp (as `nostamp` does); with RESOURCE_KEY, prints only that entry. Errors if the key is not in the plan.
183184
- `edit_resource.py TYPE ID < script.py`: fetch resource by ID, execute Python on it (resource in `r`), then update it. TYPE is `jobs` or `pipelines`.
184185
- `gron.py`: flatten JSON into greppable discrete assignments (simpler than `jq` for searching JSON).
185186
- `jq` is also available for JSON processing.
186187

187188
**RULE: Prefer `gron.py | grep <field>` over inline `jq` paths for single-value lookups.** The gron output prints the JSON path inline, so the test log explains itself.
188189

190+
**RULE: Record whole plan entries with `plan_filter.py` unless the test asserts on `remote_state` itself.** A narrower projection such as `nostamp | jq '.plan[KEY] | {action, changes}'` is fine as is. `remote_state` is the backend's GET payload as is (status, timestamps, sizes, fields the backend adds), so a golden that records it breaks on backend changes that leave the plan's action and changes untouched, on every cloud at once. `changes[*].remote` still records the remote value of each field with a change entry.
191+
189192
**RULE: Don't pass `--keep` to `print_requests.py` if a later `print_requests.py` call follows.** The buffer accumulates, so the second call double-prints the earlier requests.
190193

191194
**RULE: Filter recorded requests with `print_requests.py`, never with a hand-written `jq 'select(...)' out.requests.txt` pipeline** — inline, or hidden inside a local `print_requests()` shell function. The helper already excludes GET, normalizes query strings, optionally sorts, and deletes `out.requests.txt` afterwards. A copy-pasted `jq` wrapper reimplements all of that, drifts from the canonical output format, and is the single most common acceptance-test anti-pattern in this repo. Wrapping `print_requests.py` *itself* in a local function is fine — e.g. to send each variant's output to its own `out.requests.<name>.json`. Reach for `jq` on `out.requests.txt` only for what `print_requests.py` genuinely can't express: filtering on request *body* content, or deleting noisy body fields (prefer a `Repls` entry in `test.toml` even then).

‎acceptance/bin/plan_filter.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
"""Read `bundle plan -o json` on stdin and write the part of it a golden should record.
3+
4+
A plan entry's remote_state is the backend's GET payload, copied as is: it carries
5+
backend-computed fields (status, timestamps, sizes, UIDs) and every field the backend
6+
starts or stops returning. Recording it makes a golden fail on backend changes that
7+
leave the CLI's decision (action, new_state, changes) untouched, often on every cloud
8+
and test at once. This drops remote_state from every entry; changes[*].remote still
9+
records the remote value of each field with a change entry.
10+
11+
Also removes the deployment stamp, as nostamp.py does, so it replaces
12+
`nostamp | jq '.plan["KEY"] | del(.remote_state)'`.
13+
14+
Usage:
15+
$CLI bundle plan -o json | plan_filter.py # whole plan
16+
$CLI bundle plan -o json | plan_filter.py resources.jobs.foo # one entry
17+
18+
A test that asserts on remote_state itself should not use this.
19+
"""
20+
21+
import argparse
22+
import json
23+
import sys
24+
25+
import nostamp
26+
27+
28+
def filter_plan(data, key=None):
29+
entries = data.get("plan") if isinstance(data, dict) else None
30+
if not isinstance(entries, dict):
31+
raise SystemExit("plan_filter.py: input is not a plan: no 'plan' object")
32+
for entry in entries.values():
33+
if isinstance(entry, dict):
34+
entry.pop("remote_state", None)
35+
if key is None:
36+
return data
37+
if key not in entries:
38+
raise SystemExit(f"plan_filter.py: no plan entry {key!r}; entries: {sorted(entries)}")
39+
return entries[key]
40+
41+
42+
def main():
43+
parser = argparse.ArgumentParser()
44+
parser.add_argument("key", nargs="?", help="resource key of the one entry to print, e.g. resources.jobs.foo")
45+
args = parser.parse_args()
46+
text = sys.stdin.read()
47+
try:
48+
data = json.loads(text)
49+
except json.JSONDecodeError as ex:
50+
raise SystemExit(f"plan_filter.py: input is not JSON ({ex}): {text[:200]!r}") from None
51+
data = filter_plan(data, args.key)
52+
sys.stdout.write(nostamp.render(data, 2) + "\n")
53+
54+
55+
if __name__ == "__main__":
56+
main()

‎acceptance/bundle/resources/postgres_branches/update_protected/out.plan.no_change.json‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,5 @@
55
"label": "${resources.postgres_projects.my_project.id}"
66
}
77
],
8-
"action": "skip",
9-
"remote_state": {
10-
"branch_id": "dev-branch",
11-
"create_time": "[TIMESTAMP]",
12-
"is_protected": false,
13-
"name": "[DEV_BRANCH_ID]",
14-
"no_expiry": true,
15-
"parent": "projects/test-pg-proj-[UNIQUE_NAME]",
16-
"status": {
17-
"branch_id": "dev-branch",
18-
"current_state": "READY",
19-
"default": false,
20-
"is_protected": false,
21-
"source_branch": "projects/test-pg-proj-[UNIQUE_NAME]/branches/production",
22-
"source_branch_lsn": "[LSN]",
23-
"source_branch_time": "[TIMESTAMP]",
24-
"state_change_time": "[TIMESTAMP]"
25-
},
26-
"uid": "[BRANCH_UID]",
27-
"update_time": "[TIMESTAMP]"
28-
}
8+
"action": "skip"
299
}

‎acceptance/bundle/resources/postgres_branches/update_protected/out.plan.restore.json‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,6 @@
1414
"parent": "projects/test-pg-proj-[UNIQUE_NAME]"
1515
}
1616
},
17-
"remote_state": {
18-
"branch_id": "dev-branch",
19-
"create_time": "[TIMESTAMP]",
20-
"is_protected": true,
21-
"name": "[DEV_BRANCH_ID]",
22-
"no_expiry": true,
23-
"parent": "projects/test-pg-proj-[UNIQUE_NAME]",
24-
"status": {
25-
"branch_id": "dev-branch",
26-
"current_state": "READY",
27-
"default": false,
28-
"is_protected": true,
29-
"source_branch": "projects/test-pg-proj-[UNIQUE_NAME]/branches/production",
30-
"source_branch_lsn": "[LSN]",
31-
"source_branch_time": "[TIMESTAMP]",
32-
"state_change_time": "[TIMESTAMP]"
33-
},
34-
"uid": "[BRANCH_UID]",
35-
"update_time": "[TIMESTAMP]"
36-
},
3717
"changes": {
3818
"is_protected": {
3919
"action": "update",

‎acceptance/bundle/resources/postgres_branches/update_protected/out.plan.update.json‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,6 @@
1414
"parent": "projects/test-pg-proj-[UNIQUE_NAME]"
1515
}
1616
},
17-
"remote_state": {
18-
"branch_id": "dev-branch",
19-
"create_time": "[TIMESTAMP]",
20-
"is_protected": false,
21-
"name": "[DEV_BRANCH_ID]",
22-
"no_expiry": true,
23-
"parent": "projects/test-pg-proj-[UNIQUE_NAME]",
24-
"status": {
25-
"branch_id": "dev-branch",
26-
"current_state": "READY",
27-
"default": false,
28-
"is_protected": false,
29-
"source_branch": "projects/test-pg-proj-[UNIQUE_NAME]/branches/production",
30-
"source_branch_lsn": "[LSN]",
31-
"source_branch_time": "[TIMESTAMP]",
32-
"state_change_time": "[TIMESTAMP]"
33-
},
34-
"uid": "[BRANCH_UID]",
35-
"update_time": "[TIMESTAMP]"
36-
},
3717
"changes": {
3818
"is_protected": {
3919
"action": "update",

‎acceptance/bundle/resources/postgres_branches/update_protected/script‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ print_requests() {
1515
title "Initial deployment"
1616
trace $CLI bundle validate
1717
trace $CLI bundle plan
18-
trace $CLI bundle plan -o json | nostamp | jq '.plan."resources.postgres_branches.dev_branch" // .' > out.plan.create.json
18+
trace $CLI bundle plan -o json | plan_filter.py resources.postgres_branches.dev_branch > out.plan.create.json
1919
rm -f "$OUT_REQUESTS"
2020
trace $CLI bundle deploy
2121

@@ -28,7 +28,7 @@ trace $CLI postgres get-branch "${branch_name}" | branch_fields
2828

2929
title "Verify no_change (no changes)"
3030
trace $CLI bundle plan
31-
trace $CLI bundle plan -o json | nostamp | jq '(.plan."resources.postgres_branches.dev_branch" // .) | del(.remote_state.status.logical_size_bytes)' > out.plan.no_change.json
31+
trace $CLI bundle plan -o json | plan_filter.py resources.postgres_branches.dev_branch > out.plan.no_change.json
3232
rm -f "$OUT_REQUESTS"
3333
trace $CLI bundle deploy
3434

@@ -39,7 +39,7 @@ trace update_file.py databricks.yml "is_protected: false" "is_protected: true"
3939
trace cat databricks.yml
4040

4141
trace $CLI bundle plan
42-
trace $CLI bundle plan -o json | nostamp | jq '(.plan."resources.postgres_branches.dev_branch" // .) | del(.remote_state.status.logical_size_bytes)' > out.plan.update.json
42+
trace $CLI bundle plan -o json | plan_filter.py resources.postgres_branches.dev_branch > out.plan.update.json
4343
rm -f "$OUT_REQUESTS"
4444
trace $CLI bundle deploy
4545

@@ -51,7 +51,7 @@ trace $CLI postgres get-branch "${branch_name}" | branch_fields
5151
title "Restore is_protected to false"
5252
trace update_file.py databricks.yml "is_protected: true" "is_protected: false"
5353
trace $CLI bundle plan
54-
trace $CLI bundle plan -o json | nostamp | jq '(.plan."resources.postgres_branches.dev_branch" // .) | del(.remote_state.status.logical_size_bytes)' > out.plan.restore.json
54+
trace $CLI bundle plan -o json | plan_filter.py resources.postgres_branches.dev_branch > out.plan.restore.json
5555
rm -f "$OUT_REQUESTS"
5656
trace $CLI bundle deploy
5757

‎acceptance/bundle/resources/postgres_databases/update/out.plan.no_change.json‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,5 @@
99
"label": "${resources.postgres_roles.owner.id}"
1010
}
1111
],
12-
"action": "skip",
13-
"remote_state": {
14-
"create_time": "[TIMESTAMP]",
15-
"database_id": "my-database",
16-
"name": "[MY_DATABASE_ID]",
17-
"parent": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main",
18-
"postgres_database": "initial_db_name",
19-
"role": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main/roles/app-owner",
20-
"status": {
21-
"database_id": "my-database",
22-
"postgres_database": "initial_db_name",
23-
"role": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main/roles/app-owner"
24-
},
25-
"update_time": "[TIMESTAMP]"
26-
}
12+
"action": "skip"
2713
}

‎acceptance/bundle/resources/postgres_databases/update/out.plan.restore.json‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@
1818
"role": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main/roles/app-owner"
1919
}
2020
},
21-
"remote_state": {
22-
"create_time": "[TIMESTAMP]",
23-
"database_id": "my-database",
24-
"name": "[MY_DATABASE_ID]",
25-
"parent": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main",
26-
"postgres_database": "renamed_db_name",
27-
"role": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main/roles/app-owner",
28-
"status": {
29-
"database_id": "my-database",
30-
"postgres_database": "renamed_db_name",
31-
"role": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main/roles/app-owner"
32-
},
33-
"update_time": "[TIMESTAMP]"
34-
},
3521
"changes": {
3622
"postgres_database": {
3723
"action": "update",

‎acceptance/bundle/resources/postgres_databases/update/out.plan.update.json‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@
1818
"role": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main/roles/app-owner"
1919
}
2020
},
21-
"remote_state": {
22-
"create_time": "[TIMESTAMP]",
23-
"database_id": "my-database",
24-
"name": "[MY_DATABASE_ID]",
25-
"parent": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main",
26-
"postgres_database": "initial_db_name",
27-
"role": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main/roles/app-owner",
28-
"status": {
29-
"database_id": "my-database",
30-
"postgres_database": "initial_db_name",
31-
"role": "projects/test-pg-proj-[UNIQUE_NAME]/branches/main/roles/app-owner"
32-
},
33-
"update_time": "[TIMESTAMP]"
34-
},
3521
"changes": {
3622
"postgres_database": {
3723
"action": "update",

‎acceptance/bundle/resources/postgres_databases/update/script‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ print_requests() {
1515
title "Initial deployment"
1616
trace $CLI bundle validate
1717
trace $CLI bundle plan
18-
trace $CLI bundle plan -o json | nostamp | jq '.plan."resources.postgres_databases.my_database" // .' > out.plan.create.json
18+
trace $CLI bundle plan -o json | plan_filter.py resources.postgres_databases.my_database > out.plan.create.json
1919
rm -f "$OUT_REQUESTS"
2020
trace $CLI bundle deploy
2121

@@ -29,7 +29,7 @@ trace $CLI postgres get-database "${database_name}" | database_fields
2929

3030
title "Verify no changes"
3131
trace $CLI bundle plan
32-
trace $CLI bundle plan -o json | nostamp | jq '.plan."resources.postgres_databases.my_database" // .' > out.plan.no_change.json
32+
trace $CLI bundle plan -o json | plan_filter.py resources.postgres_databases.my_database > out.plan.no_change.json
3333
rm -f "$OUT_REQUESTS"
3434
trace $CLI bundle deploy
3535

@@ -40,7 +40,7 @@ trace update_file.py databricks.yml "postgres_database: initial_db_name" "postgr
4040
trace cat databricks.yml
4141

4242
trace $CLI bundle plan
43-
trace $CLI bundle plan -o json | nostamp | jq '.plan."resources.postgres_databases.my_database" // .' > out.plan.update.json
43+
trace $CLI bundle plan -o json | plan_filter.py resources.postgres_databases.my_database > out.plan.update.json
4444
rm -f "$OUT_REQUESTS"
4545
trace $CLI bundle deploy
4646

@@ -52,7 +52,7 @@ trace $CLI postgres get-database "${database_name}" | database_fields
5252
title "Restore postgres_database to original value"
5353
trace update_file.py databricks.yml "postgres_database: renamed_db_name" "postgres_database: initial_db_name"
5454
trace $CLI bundle plan
55-
trace $CLI bundle plan -o json | nostamp | jq '.plan."resources.postgres_databases.my_database" // .' > out.plan.restore.json
55+
trace $CLI bundle plan -o json | plan_filter.py resources.postgres_databases.my_database > out.plan.restore.json
5656
rm -f "$OUT_REQUESTS"
5757
trace $CLI bundle deploy
5858

0 commit comments

Comments
 (0)