Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions acceptance/bin/hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python3
"""Make the testserver send a request of its own when a request of the test arrives.

Usage: hook.py [--clock-offset-ms N] PATTERN OFFSET TIMES REQUEST [BODY]

--clock-offset-ms shift the time the handler of REQUEST sees by N milliseconds, e.g. -30000
for a resource created 30 seconds ago. Only handlers that take the time
from the request honor it (today: the vector search endpoint create).
PATTERN HTTP method and path of the request to wait for, as for fault.py:
* matches within one path segment, ** spans /
OFFSET number of matching requests to let through before the hook runs
TIMES number of times to run the hook
REQUEST HTTP method and path of the request to send, e.g.
"POST /api/2.0/vector-search/endpoints"
BODY optional body of the request to send

The server handles REQUEST after the matching request is handled (or failed by a fault
rule) and before its response is sent. So REQUEST lands between that request and the next
one the CLI sends, whatever the speed of the machine: use it for what another client does
in the middle of a command, instead of a background process and a sleep. REQUEST is sent
with the current DATABRICKS_TOKEN, is recorded like any other request, and fails the test
if it is answered with an error.

The rule is scoped to the current DATABRICKS_TOKEN so it only affects
the test that registers it, even when tests share a server.
"""

import json
import os
import sys
import urllib.request

host = os.environ.get("DATABRICKS_HOST", "")
token = os.environ.get("DATABRICKS_TOKEN", "")

if not host:
print("DATABRICKS_HOST not set", file=sys.stderr)
sys.exit(1)

args = sys.argv[1:]

clock_offset_ms = 0
if "--clock-offset-ms" in args:
i = args.index("--clock-offset-ms")
clock_offset_ms = int(args[i + 1])
del args[i : i + 2]

if len(args) not in (4, 5) or " " not in args[3]:
print(
f"usage: {sys.argv[0]} [--clock-offset-ms N] PATTERN OFFSET TIMES REQUEST [BODY]",
file=sys.stderr,
)
sys.exit(1)

pattern, offset, times = args[0], int(args[1]), int(args[2])
method, path = args[3].split(" ", 1)
body = args[4] if len(args) == 5 else ""

data = json.dumps(
{
"pattern": pattern,
"offset": offset,
"times": times,
"method": method,
"path": path,
"body": body,
"clock_offset_ms": clock_offset_ms,
}
).encode()

req = urllib.request.Request(
f"{host}/__testserver/hook",
data=data,
headers={"Content-Type": "application/json", "Authorization": f"Bearer {token}"},
method="POST",
)
urllib.request.urlopen(req)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bundle:
name: test-bundle

sync:
paths: []

resources:
postgres_projects:
my_project:
project_id: test-pg-proj
display_name: "Test Postgres Project"
pg_version: 16
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Cloud = false
EnvMatrix.DMS = ["", "true"]
EnvMatrix.READPLAN = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

=== Outside the bundle, create a project with the project_id the bundle uses

>>> [CLI] postgres create-project test-pg-proj --json {"spec": {"display_name": "Test Postgres Project", "pg_version": 16}}
{
"name": "projects/test-pg-proj"
}

=== Deploy fails: the project_id is taken

>>> musterr [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
Error: cannot create resources.postgres_projects.my_project: A project with this resource name already exists in the workspace. (400 BAD_REQUEST)

Endpoint: POST [DATABRICKS_URL]/api/2.0/postgres/projects?project_id=test-pg-proj
HTTP Status: 400 Bad Request
API error_code: BAD_REQUEST
API message: A project with this resource name already exists in the workspace.

Files: 0 uploaded, 0 deleted

=== The same deploy, but its create request fails with a 504 that the backend did not apply: deploy takes the project

>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
Created postgres_projects.my_project
Files: 0 uploaded, 0 deleted
Resources: 1 created, 0 changed, 0 deleted, 0 unchanged

>>> print_requests.py --get --summary //postgres/projects
POST /api/2.0/postgres/projects project_id=test-pg-proj
GET /api/2.0/postgres/projects/test-pg-proj
GET /api/2.0/postgres/projects/test-pg-proj/branches/production

>>> [CLI] bundle plan
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged

=== Destroy deletes the project the bundle did not create

>>> [CLI] bundle destroy --auto-approve
The following resources will be deleted:
delete resources.postgres_projects.my_project

This action will result in the deletion of the following Lakebase projects along with
all their branches, databases, and endpoints. All data stored in them will be permanently lost:
delete resources.postgres_projects.my_project

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

Destroy: 1 deleted

>>> print_requests.py --summary //postgres/projects
DELETE /api/2.0/postgres/projects/test-pg-proj

>>> musterr [CLI] postgres get-project projects/test-pg-proj
Error: project id not found [TraceId: [UUID]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cleanup() {
# Whatever destroy left behind.
errcode $CLI bundle destroy --auto-approve > LOG.cleanup 2>&1
errcode $CLI postgres delete-project projects/test-pg-proj >> LOG.cleanup 2>&1
rm -f "$OUT_REQUESTS"
}
trap cleanup EXIT

title "Outside the bundle, create a project with the project_id the bundle uses"
trace $CLI postgres create-project test-pg-proj --json '{"spec": {"display_name": "Test Postgres Project", "pg_version": 16}}' | jq '{name}'

title "Deploy fails: the project_id is taken"
trace musterr $CLI bundle deploy
rm -f "$OUT_REQUESTS"

title "The same deploy, but its create request fails with a 504 that the backend did not apply: deploy takes the project"
fault.py "POST /api/2.0/postgres/projects" 504 0 1 DEADLINE_EXCEEDED
trace $CLI bundle deploy
trace print_requests.py --get --summary //postgres/projects
trace $CLI bundle plan

title "Destroy deletes the project the bundle did not create"
trace $CLI bundle destroy --auto-approve
trace print_requests.py --summary //postgres/projects
trace musterr $CLI postgres get-project projects/test-pg-proj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Badness = "After a create request fails with a 504, deploy looks the project up by name and takes any project created less than a minute before the request. The read response has status.owner, which is not checked. Here the project was created outside the bundle, and the 504 was returned before the backend handled the request, so the bundle created nothing; without the 504 the same deploy fails with 'already exists'. Destroy then deletes the project and its data. Deploy should take a project only when it can tell that its own request created it, and otherwise fail as it does without the 504."

Cloud = false
EnvMatrix.READPLAN = []
Env.DATABRICKS_BUNDLE_ENGINE = "direct"
Env.DATABRICKS_BUNDLE_RETRY_INTERVAL_MS = "100"
SuppressLines.warn_retry = ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
bundle:
name: schema-504-foreign-$UNIQUE_NAME

sync:
paths: []

resources:
schemas:
my_schema:
catalog_name: main
name: test-schema-$UNIQUE_NAME
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Cloud = false
EnvMatrix.DMS = ["", "true"]
EnvMatrix.READPLAN = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

=== Outside the bundle, the deploying user creates a schema with the name the bundle uses

>>> [CLI] schemas create test-schema-[UNIQUE_NAME] main
{
"full_name": "main.test-schema-[UNIQUE_NAME]",
"created_by": "[USERNAME]"
}

=== Deploy fails: the name is taken

>>> musterr [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/schema-504-foreign-[UNIQUE_NAME]/default/files...
Error: cannot create resources.schemas.my_schema: Schema 'test-schema-[UNIQUE_NAME]' already exists (400 SCHEMA_ALREADY_EXISTS)

Endpoint: POST [DATABRICKS_URL]/api/2.1/unity-catalog/schemas
HTTP Status: 400 Bad Request
API error_code: SCHEMA_ALREADY_EXISTS
API message: Schema 'test-schema-[UNIQUE_NAME]' already exists

Files: 0 uploaded, 0 deleted

=== The same deploy, but its create request fails with a 504 that the backend did not apply: deploy takes the schema

>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/schema-504-foreign-[UNIQUE_NAME]/default/files...
Created schemas.my_schema
Files: 0 uploaded, 0 deleted
Resources: 1 created, 0 changed, 0 deleted, 0 unchanged

>>> print_requests.py --get --summary //unity-catalog/schemas //scim/v2/Me
POST /api/2.1/unity-catalog/schemas
GET /api/2.1/unity-catalog/schemas/main.test-schema-[UNIQUE_NAME]
GET /api/2.0/preview/scim/v2/Me

>>> [CLI] bundle plan
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged

=== Destroy deletes the schema the bundle did not create

>>> [CLI] bundle destroy --auto-approve
The following resources will be deleted:
delete resources.schemas.my_schema

This action will result in the deletion of the following UC schemas. Any underlying data may be lost:
delete resources.schemas.my_schema

All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/schema-504-foreign-[UNIQUE_NAME]/default

Destroy: 1 deleted

>>> print_requests.py --summary //unity-catalog/schemas
DELETE /api/2.1/unity-catalog/schemas/main.test-schema-[UNIQUE_NAME] force=true

>>> musterr [CLI] schemas get main.test-schema-[UNIQUE_NAME]
Error: Schema 'main.test-schema-[UNIQUE_NAME]' does not exist.
29 changes: 29 additions & 0 deletions acceptance/bundle/resources/schemas/create_504_foreign/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
envsubst < databricks.yml.tmpl > databricks.yml

SCHEMA="main.test-schema-$UNIQUE_NAME"

cleanup() {
# Whatever destroy left behind.
errcode $CLI bundle destroy --auto-approve > LOG.cleanup 2>&1
errcode $CLI schemas delete "$SCHEMA" --force >> LOG.cleanup 2>&1
rm -f "$OUT_REQUESTS"
}
trap cleanup EXIT

title "Outside the bundle, the deploying user creates a schema with the name the bundle uses"
trace $CLI schemas create "test-schema-$UNIQUE_NAME" main | jq '{full_name, created_by}'

title "Deploy fails: the name is taken"
trace musterr $CLI bundle deploy
rm -f "$OUT_REQUESTS"

title "The same deploy, but its create request fails with a 504 that the backend did not apply: deploy takes the schema"
fault.py "POST /api/2.1/unity-catalog/schemas" 504 0 1 DEADLINE_EXCEEDED
trace $CLI bundle deploy
trace print_requests.py --get --summary //unity-catalog/schemas //scim/v2/Me
trace $CLI bundle plan

title "Destroy deletes the schema the bundle did not create"
trace $CLI bundle destroy --auto-approve
trace print_requests.py --summary //unity-catalog/schemas
trace musterr $CLI schemas get "$SCHEMA"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Badness = "After a create request fails with a 504, deploy looks the schema up by name and takes a schema that the deploying user created less than a minute before the request. That does not tell the bundle's own schema from one the same user created another way: another bundle or target, a script, or the same service principal in another CI job. Here the 504 was returned before the backend handled the request, so the bundle created nothing; without the 504 the same deploy fails with 'already exists'. Destroy then deletes the schema. Deploy should take a schema only when it can tell that its own request created it, and otherwise fail as it does without the 504."

# Faults are injected through the testserver.
Cloud = false
EnvMatrix.READPLAN = []
Env.DATABRICKS_BUNDLE_ENGINE = "direct"
Env.DATABRICKS_BUNDLE_RETRY_INTERVAL_MS = "100"
SuppressLines.warn_retry = ""

Ignore = ["databricks.yml"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
bundle:
name: vs-endpoint-503-replayed-$UNIQUE_NAME

sync:
paths: []

resources:
vector_search_endpoints:
my_endpoint:
name: vs-endpoint-$UNIQUE_NAME
endpoint_type: STANDARD
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
EnvMatrix.DMS = [""]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

=== The create request is applied but answered with a 503: the SDK repeats it, and the repeat fails because the endpoint exists

>>> musterr [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/vs-endpoint-503-replayed-[UNIQUE_NAME]/default/files...
Error: cannot create resources.vector_search_endpoints.my_endpoint: AI Search endpoint vs-endpoint-[UNIQUE_NAME] already exists. Please try again with a different endpoint name. (409 ALREADY_EXISTS)

Endpoint: POST [DATABRICKS_URL]/api/2.0/vector-search/endpoints
HTTP Status: 409 Conflict
API error_code: ALREADY_EXISTS
API message: AI Search endpoint vs-endpoint-[UNIQUE_NAME] already exists. Please try again with a different endpoint name.

Files: 0 uploaded, 0 deleted

>>> print_requests.py --get --summary //vector-search/endpoints
POST /api/2.0/vector-search/endpoints
POST /api/2.0/vector-search/endpoints

=== The endpoint exists, but it is not in the bundle's state

>>> [CLI] vector-search-endpoints get-endpoint vs-endpoint-[UNIQUE_NAME]
{
"name": "vs-endpoint-[UNIQUE_NAME]"
}

>>> [CLI] bundle plan
create vector_search_endpoints.my_endpoint

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

=== The next deploy fails the same way

>>> musterr [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/vs-endpoint-503-replayed-[UNIQUE_NAME]/default/files...
Error: cannot create resources.vector_search_endpoints.my_endpoint: AI Search endpoint vs-endpoint-[UNIQUE_NAME] already exists. Please try again with a different endpoint name. (409 ALREADY_EXISTS)

Endpoint: POST [DATABRICKS_URL]/api/2.0/vector-search/endpoints
HTTP Status: 409 Conflict
API error_code: ALREADY_EXISTS
API message: AI Search endpoint vs-endpoint-[UNIQUE_NAME] already exists. Please try again with a different endpoint name.

Files: 0 uploaded, 0 deleted
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
envsubst < databricks.yml.tmpl > databricks.yml

ENDPOINT="vs-endpoint-$UNIQUE_NAME"

cleanup() {
# Whatever destroy left behind.
errcode $CLI bundle destroy --auto-approve > LOG.cleanup 2>&1
errcode $CLI vector-search-endpoints delete-endpoint "$ENDPOINT" >> LOG.cleanup 2>&1
rm -f "$OUT_REQUESTS"
}
trap cleanup EXIT

# The SDK repeats a request that is answered with a 503 on its own, so deploy never sees the 503.
title "The create request is applied but answered with a 503: the SDK repeats it, and the repeat fails because the endpoint exists"
fault.py --after-handler "POST /api/2.0/vector-search/endpoints" 503 0 1
trace musterr $CLI bundle deploy
trace print_requests.py --get --summary //vector-search/endpoints

title "The endpoint exists, but it is not in the bundle's state"
trace $CLI vector-search-endpoints get-endpoint "$ENDPOINT" | jq '{name}'
trace $CLI bundle plan

title "The next deploy fails the same way"
trace musterr $CLI bundle deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Badness = "A create request that the backend applied but answered with a 503 is repeated by the SDK. The repeat fails with 'already exists', and deploy returns that error without looking the endpoint up: it only does that when the error it sees itself leaves the outcome unknown, as a 504 does (see create-504). The endpoint the bundle created is left outside its state, and every later deploy fails with 'already exists' until the endpoint is deleted or bound by hand. Deploy should end up with the endpoint in its state, as it does after a 504."

# Faults are injected through the testserver.
Cloud = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
bundle:
name: vs-endpoint-504-foreign-during-$UNIQUE_NAME

sync:
paths: []

resources:
vector_search_endpoints:
my_endpoint:
name: vs-endpoint-$UNIQUE_NAME
endpoint_type: STANDARD
Loading
Loading