Skip to content

Commit 042111a

Browse files
denikIsaac
andcommitted
acceptance: rework state_present to replay a terraform fixture
state_present verified engine resolution by deploying on the terraform engine, which is now rejected. Replay a captured schema fixture (reused from the schema migrate coverage) to reconstruct the terraform state locally instead, then exercise the same resolution paths: the direct default migrates the terraform state, a lower-serial resources.json still migrates, and a higher-serial one wins as the authoritative direct state. A .gitignore keeps the fixtures out of the deploy upload. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 5e1594e commit 042111a

6 files changed

Lines changed: 110 additions & 104 deletions

File tree

‎acceptance/bundle/state/state_present/databricks.yml‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ bundle:
33

44
resources:
55
schemas:
6-
foo:
7-
catalog_name: bar
8-
name: foo
6+
my_schema:
7+
name: myschema
8+
catalog_name: main
9+
storage_root: s3://mybucket/init
Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,52 @@
11

2-
=== Terraform state is present, empty env var uses state engine
2+
=== Terraform state present: the direct default migrates it to the direct engine
33

4-
>>> DATABRICKS_BUNDLE_ENGINE=terraform [CLI] bundle deploy
5-
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
6-
Created schemas.foo
7-
Files: 1 uploaded, 0 deleted
8-
Resources: 1 created, 0 changed, 0 deleted, 0 unchanged
4+
>>> replay_tfstate.py terraform-requests.json terraform.tfstate
95

10-
>>> print_requests.py //api/2.1/unity-catalog/schemas
11-
"databricks-tf-provider/[TF_PROVIDER_VERSION] databricks-sdk-go/[SDK_VERSION] go/1.25.8 os/[OS] cli/[CLI_VERSION] terraform/1.5.5 sdk/sdkv2 resource/schema auth/pat"
12-
13-
>>> DATABRICKS_BUNDLE_ENGINE= [CLI] bundle plan
6+
>>> [CLI] bundle plan
147
Notice: migrating your bundle to direct deployment engine (https://github.com/databricks/cli/issues/6765).
158
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged
169

1710
>>> print_requests.py --get //api/2.1/unity-catalog/schemas
11+
"replay_tfstate.py"
1812
"cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none auth/pat"
1913
"cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_plan cmd-exec-id/[UUID] interactive/none engine/direct auth/pat"
2014

21-
=== Adding resources.json with lower serial does not change anything
22-
>>> DATABRICKS_BUNDLE_ENGINE=direct [CLI] bundle plan
15+
=== A resources.json with a lower serial does not win: the terraform state still migrates
16+
>>> [CLI] bundle plan
2317
Notice: migrating your bundle to direct deployment engine (https://github.com/databricks/cli/issues/6765).
2418
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged
2519

26-
>>> DATABRICKS_BUNDLE_ENGINE=terraform [CLI] bundle plan
27-
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged
28-
29-
>>> DATABRICKS_BUNDLE_ENGINE= [CLI] bundle plan
30-
Notice: migrating your bundle to direct deployment engine (https://github.com/databricks/cli/issues/6765).
31-
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged
32-
33-
=== Adding resources.json with higher serial, env var mismatch warns but uses state engine
34-
35-
>>> DATABRICKS_BUNDLE_ENGINE=direct [CLI] bundle plan
36-
create schemas.foo
37-
38-
Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged
20+
=== A resources.json with a higher serial wins: the direct state is authoritative, no migration
3921

40-
>>> DATABRICKS_BUNDLE_ENGINE=terraform [CLI] bundle plan
41-
create schemas.foo
22+
>>> [CLI] bundle plan
23+
create schemas.my_schema
4224

4325
Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged
4426

45-
>>> DATABRICKS_BUNDLE_ENGINE=terraform [CLI] bundle deploy
27+
>>> [CLI] bundle deploy
4628
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
47-
Created schemas.foo
48-
Files: 0 uploaded, 0 deleted
29+
Created schemas.my_schema
30+
Files: 1 uploaded, 0 deleted
4931
Resources: 1 created, 0 changed, 0 deleted, 0 unchanged
5032

5133
>>> print_requests.py //api/2.1/unity-catalog/schemas
5234
"cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID] interactive/none engine/direct auth/pat"
5335

54-
>>> DATABRICKS_BUNDLE_ENGINE=terraform [CLI] bundle destroy --auto-approve
36+
>>> [CLI] bundle destroy --auto-approve
5537
The following resources will be deleted:
56-
delete resources.schemas.foo
38+
delete resources.schemas.my_schema
5739

5840
This action will result in the deletion of the following UC schemas. Any underlying data may be lost:
59-
delete resources.schemas.foo
41+
delete resources.schemas.my_schema
6042

6143
All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/test-bundle/default
6244

6345
Destroy: 1 deleted
6446

65-
>>> DATABRICKS_BUNDLE_ENGINE=direct [CLI] bundle deploy
47+
>>> [CLI] bundle deploy
6648
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
67-
Created schemas.foo
49+
Created schemas.my_schema
6850
Files: 1 uploaded, 0 deleted
6951
Resources: 1 created, 0 changed, 0 deleted, 0 unchanged
7052

@@ -74,26 +56,9 @@ Resources: 1 created, 0 changed, 0 deleted, 0 unchanged
7456
>>> print_state.py
7557
1
7658

77-
>>> DATABRICKS_BUNDLE_ENGINE=terraform [CLI] bundle deploy
78-
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
79-
Files: 0 uploaded, 0 deleted
80-
Resources: 0 created, 0 changed, 0 deleted, 1 unchanged
81-
82-
>>> DATABRICKS_BUNDLE_ENGINE= [CLI] bundle deploy
83-
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
84-
Files: 0 uploaded, 0 deleted
85-
Resources: 0 created, 0 changed, 0 deleted, 1 unchanged
86-
87-
>>> print_requests.py --get //api/2.1/unity-catalog/schemas
88-
"cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID] interactive/none engine/direct auth/pat"
89-
"cli/[CLI_VERSION] databricks-sdk-go/[SDK_VERSION] go/[GO_VERSION] os/[OS] cmd/bundle_deploy cmd-exec-id/[UUID] interactive/none engine/direct auth/pat"
90-
91-
>>> print_state.py
92-
1
93-
94-
>>> DATABRICKS_BUNDLE_ENGINE= [CLI] bundle debug states
59+
>>> [CLI] bundle debug states
9560
[TEST_TMP_DIR]/.databricks/bundle/default/resources.json: local direct state serial=1 lineage="[UUID]"
9661

97-
>>> DATABRICKS_BUNDLE_ENGINE= [CLI] bundle debug states --force-pull
62+
>>> [CLI] bundle debug states --force-pull
9863
resources.json: remote direct state serial=1 lineage="[UUID]"
9964
[TEST_TMP_DIR]/.databricks/bundle/default/resources.json: local direct state serial=1 lineage="[UUID]"
Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,44 @@
1-
title 'Terraform state is present, empty env var uses state engine\n'
2-
mkdir -p .databricks/bundle/default/terraform
3-
echo '{"version": 4, "serial": 1, "lineage": "test-lineage"}' > .databricks/bundle/default/terraform/terraform.tfstate
4-
5-
# Deploy on terraform explicitly: with an empty env var direct is the default, so
6-
# the deploy would migrate the state away from terraform (that path is covered by
7-
# bundle/migrate/auto-migrate-default) and the rest of this test needs it intact.
8-
trace DATABRICKS_BUNDLE_ENGINE=terraform $CLI bundle deploy
9-
trace print_requests.py //api/2.1/unity-catalog/schemas | jq '.headers["User-Agent"][0]' | contains.py 'terraform' 'tf-provider' '!direct'
10-
11-
# An empty env var defaults to direct, which migrates the terraform state in memory (no commit), so plan runs on direct.
12-
trace DATABRICKS_BUNDLE_ENGINE= $CLI bundle plan
1+
title 'Terraform state present: the direct default migrates it to the direct engine\n'
2+
# Keep the committed fixtures (and generated files) out of `bundle deploy`'s upload.
3+
printf '%s\n' '.gitignore' 'out.*' 'terraform*.tfstate' 'terraform*-requests.json' > .gitignore
4+
5+
# The terraform engine was removed in v1.19.0, so replay a prior terraform deployment (captured once
6+
# from a real terraform run) to reconstruct the terraform-shaped state locally, standing in for a
7+
# `DATABRICKS_BUNDLE_ENGINE=terraform bundle deploy`.
8+
trace replay_tfstate.py terraform-requests.json terraform.tfstate
9+
10+
# The serial-resolution cases below add a direct state (resources.json); it must share the terraform
11+
# state's lineage, or the two are rejected as unrelated ("lineage mismatch") before serials matter.
12+
lineage=$(jq -r .lineage .databricks/bundle/default/terraform/terraform.tfstate)
13+
14+
# An empty env var defaults to direct, which migrates the terraform state in memory (no commit), so
15+
# plan runs on direct and finds the schema unchanged.
16+
trace $CLI bundle plan
1317
trace print_requests.py --get //api/2.1/unity-catalog/schemas | jq '.headers["User-Agent"][0]' | contains.py 'engine/direct' '!tf-provider'
1418

15-
title 'Adding resources.json with lower serial does not change anything'
16-
rm .databricks/bundle/default/terraform/terraform.tfstate
17-
echo '{"serial": 0, "lineage": "test-lineage"}' > .databricks/bundle/default/resources.json
18-
trace DATABRICKS_BUNDLE_ENGINE=direct $CLI bundle plan
19-
trace DATABRICKS_BUNDLE_ENGINE=terraform $CLI bundle plan
20-
trace DATABRICKS_BUNDLE_ENGINE= $CLI bundle plan
19+
title 'A resources.json with a lower serial does not win: the terraform state still migrates'
20+
echo "{\"serial\": 0, \"lineage\": \"$lineage\"}" > .databricks/bundle/default/resources.json
21+
trace $CLI bundle plan
2122

2223
rm "$OUT_REQUESTS"
23-
title 'Adding resources.json with higher serial, env var mismatch warns but uses state engine\n'
24-
echo '{"serial": 10, "lineage": "test-lineage"}' > .databricks/bundle/default/resources.json
25-
trace DATABRICKS_BUNDLE_ENGINE=direct $CLI bundle plan
26-
27-
trace DATABRICKS_BUNDLE_ENGINE=terraform $CLI bundle plan
28-
# env=terraform, but the higher-serial direct state (serial 10) wins engine resolution, so this
29-
# plan and deploy run on direct (with a mismatch warning), NOT terraform.
30-
trace DATABRICKS_BUNDLE_ENGINE=terraform $CLI bundle deploy
31-
# Verify it: every schemas request from the =terraform plan+deploy above carries the direct-engine
32-
# User-Agent and none the terraform provider.
24+
title 'A resources.json with a higher serial wins: the direct state is authoritative, no migration\n'
25+
echo "{\"serial\": 10, \"lineage\": \"$lineage\"}" > .databricks/bundle/default/resources.json
26+
# serial 10 outranks the terraform state (serial 1), so direct wins; its (empty) state plans to
27+
# create the schema, and every schemas request carries the direct-engine User-Agent, not terraform.
28+
trace $CLI bundle plan
29+
trace $CLI bundle deploy
3330
trace print_requests.py //api/2.1/unity-catalog/schemas | jq '.headers["User-Agent"][0]' | contains.py 'engine/direct' '!tf-provider'
34-
trace DATABRICKS_BUNDLE_ENGINE=terraform $CLI bundle destroy --auto-approve
31+
trace $CLI bundle destroy --auto-approve
3532

3633
rm "$OUT_REQUESTS"
37-
# The three =terraform commands above all ran on the higher-serial direct state (serial 10), not
38-
# terraform. The destroy tore everything down: it removed all local state (both engines) and the
39-
# remote deployment - a destroy does not preserve a superseded terraform state for a later deploy to
40-
# resurrect. So this deploy starts fresh on the direct engine: there is nothing to migrate, it just
41-
# recreates the resource.
42-
trace DATABRICKS_BUNDLE_ENGINE=direct $CLI bundle deploy
34+
# The destroy tore everything down: it removed all local state (both engines) and the remote
35+
# deployment - a destroy does not preserve a superseded terraform state for a later deploy to
36+
# resurrect. So this deploy starts fresh on the direct engine: nothing to migrate, serial 1.
37+
trace $CLI bundle deploy
4338
trace print_requests.py //api/2.1/unity-catalog/schemas | jq '.headers["User-Agent"][0]' | contains.py 'engine/direct' '!tf-provider'
4439
trace print_state.py | jq .serial | contains.py "1"
4540

46-
trace DATABRICKS_BUNDLE_ENGINE=terraform $CLI bundle deploy
47-
#trace print_requests.py --get //api/2.1/unity-catalog/schemas | jq '.headers["User-Agent"][0]' | contains.py 'engine/direct' '!terraform' '!tf-provider'
48-
49-
trace DATABRICKS_BUNDLE_ENGINE= $CLI bundle deploy
50-
trace print_requests.py --get //api/2.1/unity-catalog/schemas | jq '.headers["User-Agent"][0]' | contains.py 'engine/direct' '!terraform' '!tf-provider'
51-
trace print_state.py | jq .serial | contains.py "1"
52-
53-
trace DATABRICKS_BUNDLE_ENGINE= $CLI bundle debug states
54-
trace DATABRICKS_BUNDLE_ENGINE= $CLI bundle debug states --force-pull
41+
trace $CLI bundle debug states
42+
trace $CLI bundle debug states --force-pull
5543

5644
rm "$OUT_REQUESTS"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"method": "POST",
4+
"path": "/api/2.1/unity-catalog/schemas",
5+
"body": {
6+
"catalog_name": "main",
7+
"name": "myschema",
8+
"storage_root": "s3://mybucket/init"
9+
}
10+
}
11+
]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": 4,
3+
"terraform_version": "1.5.5",
4+
"serial": 1,
5+
"lineage": "ed6ea4b1-1168-2291-5f1b-45c0c5698c28",
6+
"outputs": {},
7+
"resources": [
8+
{
9+
"mode": "managed",
10+
"type": "databricks_schema",
11+
"name": "my_schema",
12+
"provider": "provider[\"registry.terraform.io/databricks/databricks\"]",
13+
"instances": [
14+
{
15+
"schema_version": 0,
16+
"attributes": {
17+
"catalog_name": "main",
18+
"comment": null,
19+
"enable_predictive_optimization": "INHERIT",
20+
"force_destroy": true,
21+
"id": "main.myschema",
22+
"metastore_id": "120efa64-9b68-46ba-be38-f319458430d2",
23+
"name": "myschema",
24+
"owner": "tester@databricks.com",
25+
"properties": null,
26+
"provider_config": [
27+
{
28+
"workspace_id": "900800700600"
29+
}
30+
],
31+
"schema_id": "79fe88b1-3e30-0048-79fe-88b13e300049",
32+
"storage_root": "s3://mybucket/init"
33+
},
34+
"sensitive_attributes": [],
35+
"private": "bnVsbA=="
36+
}
37+
]
38+
}
39+
],
40+
"check_results": null
41+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# This test deploys on terraform and migrates to direct. Terraform deployments are not supported
2-
# with deployment-history recording, so it only runs without it.
1+
# This test replays a terraform-state fixture and migrates it to direct. Migrating a terraform
2+
# state is not supported with deployment-history recording, so it only runs without it.
33
EnvMatrix.DMS = [""]
44

55
RecordRequests = true
66
IncludeRequestHeaders = ["User-Agent"]
77

8-
Ignore = [".databricks"]
8+
Ignore = [".databricks", ".gitignore"]

0 commit comments

Comments
 (0)