Skip to content

Commit

Permalink
Add acceptance tests for builtin templates (#2135)
Browse files Browse the repository at this point in the history
## Changes

To accommodate:
* Add the server URL to the set of output replacements
* Include a call to the permissions API to the dummy server
* Run the main script in a subshell to isolate working directory changes
  • Loading branch information
pietern authored Jan 14, 2025
1 parent 72e677d commit 82e3553
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 6 deletions.
7 changes: 6 additions & 1 deletion acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestAccept(t *testing.T) {
server := StartServer(t)
AddHandlers(server)
// Redirect API access to local server:
t.Setenv("DATABRICKS_HOST", fmt.Sprintf("http://127.0.0.1:%d", server.Port))
t.Setenv("DATABRICKS_HOST", server.URL)
t.Setenv("DATABRICKS_TOKEN", "dapi1234")

homeDir := t.TempDir()
Expand Down Expand Up @@ -217,6 +217,11 @@ func doComparison(t *testing.T, pathExpected, pathNew, valueNew string) {
// Note, cleanups are not executed if main script fails; that's not a huge issue, since it runs it temp dir.
func readMergedScriptContents(t *testing.T, dir string) string {
scriptContents := testutil.ReadFile(t, filepath.Join(dir, EntryPointScript))

// Wrap script contents in a subshell such that changing the working
// directory only affects the main script and not cleanup.
scriptContents = "(\n" + scriptContents + ")\n"

prepares := []string{}
cleanups := []string{}

Expand Down
6 changes: 6 additions & 0 deletions acceptance/bundle/init/dbt-sql/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"project_name": "my_dbt_sql",
"http_path": "/sql/2.0/warehouses/f00dcafe",
"default_catalog": "main",
"personal_schemas": "yes, use a schema based on the current user name during development"
}
32 changes: 32 additions & 0 deletions acceptance/bundle/init/dbt-sql/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

>>> $CLI bundle init dbt-sql --config-file ./input.json

Welcome to the dbt template for Databricks Asset Bundles!

A workspace was selected based on your current profile. For information about how to change this, see https://docs.databricks.com/dev-tools/cli/profiles.html.
workspace_host: http://$DATABRICKS_HOST

📊 Your new project has been created in the 'my_dbt_sql' directory!
If you already have dbt installed, just type 'cd my_dbt_sql; dbt init' to get started.
Refer to the README.md file for full "getting started" guide and production setup instructions.


>>> $CLI bundle validate -t dev
Name: my_dbt_sql
Target: dev
Workspace:
Host: http://$DATABRICKS_HOST
User: $USERNAME
Path: /Workspace/Users/$USERNAME/.bundle/my_dbt_sql/dev

Validation OK!

>>> $CLI bundle validate -t prod
Name: my_dbt_sql
Target: prod
Workspace:
Host: http://$DATABRICKS_HOST
User: $USERNAME
Path: /Workspace/Users/$USERNAME/.bundle/my_dbt_sql/prod

Validation OK!
5 changes: 5 additions & 0 deletions acceptance/bundle/init/dbt-sql/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trace $CLI bundle init dbt-sql --config-file ./input.json

cd my_dbt_sql
trace $CLI bundle validate -t dev
trace $CLI bundle validate -t prod
1 change: 1 addition & 0 deletions acceptance/bundle/init/dbt-sql/script.cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -fr my_dbt_sql
6 changes: 6 additions & 0 deletions acceptance/bundle/init/default-python/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"project_name": "my_default_python",
"include_notebook": "yes",
"include_dlt": "yes",
"include_python": "yes"
}
30 changes: 30 additions & 0 deletions acceptance/bundle/init/default-python/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

>>> $CLI bundle init default-python --config-file ./input.json

Welcome to the default Python template for Databricks Asset Bundles!
Workspace to use (auto-detected, edit in 'my_default_python/databricks.yml'): http://$DATABRICKS_HOST

✨ Your new project has been created in the 'my_default_python' directory!

Please refer to the README.md file for "getting started" instructions.
See also the documentation at https://docs.databricks.com/dev-tools/bundles/index.html.

>>> $CLI bundle validate -t dev
Name: my_default_python
Target: dev
Workspace:
Host: http://$DATABRICKS_HOST
User: $USERNAME
Path: /Workspace/Users/$USERNAME/.bundle/my_default_python/dev

Validation OK!

>>> $CLI bundle validate -t prod
Name: my_default_python
Target: prod
Workspace:
Host: http://$DATABRICKS_HOST
User: $USERNAME
Path: /Workspace/Users/$USERNAME/.bundle/my_default_python/prod

Validation OK!
5 changes: 5 additions & 0 deletions acceptance/bundle/init/default-python/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trace $CLI bundle init default-python --config-file ./input.json

cd my_default_python
trace $CLI bundle validate -t dev
trace $CLI bundle validate -t prod
1 change: 1 addition & 0 deletions acceptance/bundle/init/default-python/script.cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -fr my_default_python
6 changes: 6 additions & 0 deletions acceptance/bundle/init/default-sql/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"project_name": "my_default_sql",
"http_path": "/sql/2.0/warehouses/f00dcafe",
"default_catalog": "main",
"personal_schemas": "yes, automatically use a schema based on the current user name during development"
}
32 changes: 32 additions & 0 deletions acceptance/bundle/init/default-sql/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

>>> $CLI bundle init default-sql --config-file ./input.json

Welcome to the default SQL template for Databricks Asset Bundles!

A workspace was selected based on your current profile. For information about how to change this, see https://docs.databricks.com/dev-tools/cli/profiles.html.
workspace_host: http://$DATABRICKS_HOST

✨ Your new project has been created in the 'my_default_sql' directory!

Please refer to the README.md file for "getting started" instructions.
See also the documentation at https://docs.databricks.com/dev-tools/bundles/index.html.

>>> $CLI bundle validate -t dev
Name: my_default_sql
Target: dev
Workspace:
Host: http://$DATABRICKS_HOST
User: $USERNAME
Path: /Workspace/Users/$USERNAME/.bundle/my_default_sql/dev

Validation OK!

>>> $CLI bundle validate -t prod
Name: my_default_sql
Target: prod
Workspace:
Host: http://$DATABRICKS_HOST
User: $USERNAME
Path: /Workspace/Users/$USERNAME/.bundle/my_default_sql/prod

Validation OK!
5 changes: 5 additions & 0 deletions acceptance/bundle/init/default-sql/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trace $CLI bundle init default-sql --config-file ./input.json

cd my_default_sql
trace $CLI bundle validate -t dev
trace $CLI bundle validate -t prod
1 change: 1 addition & 0 deletions acceptance/bundle/init/default-sql/script.cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -fr my_default_sql
30 changes: 25 additions & 5 deletions acceptance/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,30 @@ package acceptance_test

import (
"encoding/json"
"net"
"net/http"
"net/http/httptest"
"testing"

"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/databricks-sdk-go/service/compute"
"github.com/databricks/databricks-sdk-go/service/iam"
"github.com/databricks/databricks-sdk-go/service/workspace"
)

type TestServer struct {
*httptest.Server
Mux *http.ServeMux
Port int
Mux *http.ServeMux
}

type HandlerFunc func(r *http.Request) (any, error)

func NewTestServer() *TestServer {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
port := server.Listener.Addr().(*net.TCPAddr).Port

return &TestServer{
Server: server,
Mux: mux,
Port: port,
}
}

Expand Down Expand Up @@ -126,4 +123,27 @@ func AddHandlers(server *TestServer) {
ResourceId: "1001",
}, nil
})

server.Handle("/api/2.1/unity-catalog/current-metastore-assignment", func(r *http.Request) (any, error) {
return catalog.MetastoreAssignment{
DefaultCatalogName: "main",
}, nil
})

server.Handle("/api/2.0/permissions/directories/1001", func(r *http.Request) (any, error) {
return workspace.WorkspaceObjectPermissions{
ObjectId: "1001",
ObjectType: "DIRECTORY",
AccessControlList: []workspace.WorkspaceObjectAccessControlResponse{
{
UserName: "[email protected]",
AllPermissions: []workspace.WorkspaceObjectPermission{
{
PermissionLevel: "CAN_MANAGE",
},
},
},
},
}, nil
})
}

0 comments on commit 82e3553

Please sign in to comment.