Skip to content

Commit 80a10b8

Browse files
authored
acc: Fix schemas CRUD and add test for schema deployment (#2664)
## Changes - Fix testserver's schemas CRUD to merge PATCH requests properly. - Add an acceptance test for modifying schema resource. - Add a script to read selected state from terraform in tests. ## Why This test helps validate testserver. I'm also using it in terraform removal branch to validate my prototype. ## Tests Existing tests.
1 parent affa020 commit 80a10b8

File tree

6 files changed

+127
-1
lines changed

6 files changed

+127
-1
lines changed

acceptance/bin/read_state.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Print selected attributes from terraform state.
4+
5+
Usage: <section> <name> [attr...]
6+
"""
7+
8+
import sys
9+
import os
10+
import json
11+
12+
13+
def print_resource_terraform(section, name, *attrs):
14+
resource_type = "databricks_" + section[:-1]
15+
filename = ".databricks/bundle/default/terraform/terraform.tfstate"
16+
data = json.load(open(filename))
17+
available = []
18+
found = 0
19+
for r in data["resources"]:
20+
r_type = r["type"]
21+
r_name = r["name"]
22+
if r_type != resource_type:
23+
available.append((r_type, r_name))
24+
continue
25+
if r_name != name:
26+
available.append((r_type, r_name))
27+
continue
28+
for inst in r["instances"]:
29+
attribute_values = inst.get("attributes")
30+
if attribute_values:
31+
values = [f"{x}={attribute_values.get(x)!r}" for x in attrs]
32+
print(section, name, " ".join(values))
33+
found += 1
34+
if not found:
35+
print(f"Resource {(resource_type, name)} not found. Available: {available}")
36+
37+
38+
print_resource_terraform(*sys.argv[1:])
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
bundle:
2+
name: schema-test
3+
4+
resources:
5+
schemas:
6+
schema1:
7+
name: myschema
8+
catalog_name: main
9+
comment: COMMENT1
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
>>> [CLI] bundle deploy
3+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/schema-test/default/files...
4+
Deploying resources...
5+
Updating deployment state...
6+
Deployment complete!
7+
8+
>>> print_requests
9+
{
10+
"method": "POST",
11+
"path": "/api/2.1/unity-catalog/schemas",
12+
"body": {
13+
"catalog_name": "main",
14+
"comment": "COMMENT1",
15+
"name": "myschema"
16+
}
17+
}
18+
schemas schema1 id='main.myschema' name='myschema' catalog_name='main' comment='COMMENT1'
19+
20+
=== Update comment and re-deploy
21+
>>> update_file.py databricks.yml COMMENT1 COMMENT2
22+
23+
>>> [CLI] bundle deploy
24+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/schema-test/default/files...
25+
Deploying resources...
26+
Updating deployment state...
27+
Deployment complete!
28+
29+
>>> print_requests
30+
{
31+
"method": "PATCH",
32+
"path": "/api/2.1/unity-catalog/schemas/main.myschema",
33+
"body": {
34+
"comment": "COMMENT2"
35+
}
36+
}
37+
schemas schema1 id='main.myschema' name='myschema' catalog_name='main' comment='COMMENT2'
38+
39+
=== Restore comment to original value and re-deploy
40+
>>> update_file.py databricks.yml COMMENT2 COMMENT1
41+
42+
>>> [CLI] bundle deploy
43+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/schema-test/default/files...
44+
Deploying resources...
45+
Updating deployment state...
46+
Deployment complete!
47+
48+
>>> print_requests
49+
{
50+
"method": "PATCH",
51+
"path": "/api/2.1/unity-catalog/schemas/main.myschema",
52+
"body": {
53+
"comment": "COMMENT1"
54+
}
55+
}
56+
schemas schema1 id='main.myschema' name='myschema' catalog_name='main' comment='COMMENT1'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
echo "*" > .gitignore
2+
trace $CLI bundle deploy
3+
4+
print_requests() {
5+
jq 'select(.method != "GET" and (.path | contains("/unity")))' < out.requests.txt
6+
rm out.requests.txt
7+
}
8+
9+
trace print_requests
10+
read_state.py schemas schema1 id name catalog_name comment
11+
12+
title "Update comment and re-deploy"
13+
trace update_file.py databricks.yml COMMENT1 COMMENT2
14+
trace $CLI bundle deploy
15+
trace print_requests
16+
read_state.py schemas schema1 id name catalog_name comment
17+
18+
title "Restore comment to original value and re-deploy"
19+
trace update_file.py databricks.yml COMMENT2 COMMENT1
20+
trace $CLI bundle deploy
21+
trace print_requests
22+
read_state.py schemas schema1 id name catalog_name comment
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
RecordRequests = true

libs/testserver/schemas.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (s *FakeWorkspace) SchemasUpdate(req Request, name string) Response {
4343
}
4444
}
4545

46-
err := mergo.Merge(&existing, schemaUpdate)
46+
err := mergo.Merge(&existing, schemaUpdate, mergo.WithOverride)
4747
if err != nil {
4848
return Response{
4949
Body: fmt.Sprintf("mergo error: %s", err),

0 commit comments

Comments
 (0)