Skip to content

Commit e095249

Browse files
authored
Add tests for default-python template on different Python versions (#2025)
## Changes Add new type of test helpers that run the command and compare full output (golden files approach). In case of JSON, there is also an option to ignore certain paths. Add test for different versions of Python to go through bundle init default-python / validate / deploy / summary. ## Tests New integration tests.
1 parent dd9f598 commit e095249

File tree

13 files changed

+724
-0
lines changed

13 files changed

+724
-0
lines changed

NOTICE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,11 @@ License - https://github.com/stretchr/testify/blob/master/LICENSE
9797
whilp/git-urls - https://github.com/whilp/git-urls
9898
Copyright (c) 2020 Will Maier
9999
License - https://github.com/whilp/git-urls/blob/master/LICENSE
100+
101+
github.com/wI2L/jsondiff v0.6.1
102+
Copyright (c) 2020-2024 William Poussier <william.poussier@gmail.com>
103+
License - https://github.com/wI2L/jsondiff/blob/master/LICENSE
104+
105+
https://github.com/hexops/gotextdiff
106+
Copyright (c) 2009 The Go Authors. All rights reserved.
107+
License - https://github.com/hexops/gotextdiff/blob/main/LICENSE

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/hashicorp/hc-install v0.9.0 // MPL 2.0
1515
github.com/hashicorp/terraform-exec v0.21.0 // MPL 2.0
1616
github.com/hashicorp/terraform-json v0.23.0 // MPL 2.0
17+
github.com/hexops/gotextdiff v1.0.3 // BSD 3-Clause "New" or "Revised" License
1718
github.com/manifoldco/promptui v0.9.0 // BSD-3-Clause
1819
github.com/mattn/go-isatty v0.0.20 // MIT
1920
github.com/nwidger/jsoncolor v0.3.2 // MIT
@@ -22,6 +23,7 @@ require (
2223
github.com/spf13/cobra v1.8.1 // Apache 2.0
2324
github.com/spf13/pflag v1.0.5 // BSD-3-Clause
2425
github.com/stretchr/testify v1.10.0 // MIT
26+
github.com/wI2L/jsondiff v0.6.1 // MIT
2527
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
2628
golang.org/x/mod v0.22.0
2729
golang.org/x/oauth2 v0.24.0
@@ -55,6 +57,10 @@ require (
5557
github.com/mattn/go-colorable v0.1.13 // indirect
5658
github.com/pmezard/go-difflib v1.0.0 // indirect
5759
github.com/stretchr/objx v0.5.2 // indirect
60+
github.com/tidwall/gjson v1.18.0 // indirect
61+
github.com/tidwall/match v1.1.1 // indirect
62+
github.com/tidwall/pretty v1.2.1 // indirect
63+
github.com/tidwall/sjson v1.2.5 // indirect
5864
github.com/zclconf/go-cty v1.15.0 // indirect
5965
go.opencensus.io v0.24.0 // indirect
6066
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect

go.sum

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package bundle_test
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/databricks/cli/integration/internal/acc"
11+
"github.com/databricks/cli/internal/testcli"
12+
"github.com/databricks/cli/internal/testutil"
13+
"github.com/databricks/cli/libs/python/pythontest"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
var pythonVersions = []string{
18+
"3.8",
19+
"3.9",
20+
"3.10",
21+
"3.11",
22+
"3.12",
23+
"3.13",
24+
}
25+
26+
var pythonVersionsShort = []string{
27+
"3.9",
28+
"3.12",
29+
}
30+
31+
var extraInstalls = map[string][]string{
32+
"3.12": {"setuptools"},
33+
"3.13": {"setuptools"},
34+
}
35+
36+
func TestDefaultPython(t *testing.T) {
37+
versions := pythonVersions
38+
if testing.Short() {
39+
versions = pythonVersionsShort
40+
}
41+
42+
for _, pythonVersion := range versions {
43+
t.Run(pythonVersion, func(t *testing.T) {
44+
testDefaultPython(t, pythonVersion)
45+
})
46+
}
47+
}
48+
49+
func testDefaultPython(t *testing.T, pythonVersion string) {
50+
ctx, wt := acc.WorkspaceTest(t)
51+
52+
uniqueProjectId := testutil.RandomName("")
53+
ctx, replacements := testcli.WithReplacementsMap(ctx)
54+
replacements.Set(uniqueProjectId, "$UNIQUE_PRJ")
55+
56+
user, err := wt.W.CurrentUser.Me(ctx)
57+
require.NoError(t, err)
58+
require.NotNil(t, user)
59+
testcli.PrepareReplacementsUser(t, replacements, *user)
60+
testcli.PrepareReplacements(t, replacements, wt.W)
61+
62+
tmpDir := t.TempDir()
63+
testutil.Chdir(t, tmpDir)
64+
65+
opts := pythontest.VenvOpts{
66+
PythonVersion: pythonVersion,
67+
Dir: tmpDir,
68+
}
69+
70+
pythontest.RequireActivatedPythonEnv(t, ctx, &opts)
71+
extras, ok := extraInstalls[pythonVersion]
72+
if ok {
73+
args := append([]string{"pip", "install", "--python", opts.PythonExe}, extras...)
74+
cmd := exec.Command("uv", args...)
75+
require.NoError(t, cmd.Run())
76+
}
77+
78+
projectName := "project_name_" + uniqueProjectId
79+
80+
initConfig := map[string]string{
81+
"project_name": projectName,
82+
"include_notebook": "yes",
83+
"include_python": "yes",
84+
"include_dlt": "yes",
85+
}
86+
b, err := json.Marshal(initConfig)
87+
require.NoError(t, err)
88+
err = os.WriteFile(filepath.Join(tmpDir, "config.json"), b, 0o644)
89+
require.NoError(t, err)
90+
91+
testcli.AssertOutput(
92+
t,
93+
ctx,
94+
[]string{"bundle", "init", "default-python", "--config-file", "config.json"},
95+
testutil.TestData("testdata/default_python/bundle_init.txt"),
96+
)
97+
testutil.Chdir(t, projectName)
98+
99+
t.Cleanup(func() {
100+
// Delete the stack
101+
testcli.RequireSuccessfulRun(t, ctx, "bundle", "destroy", "--auto-approve")
102+
})
103+
104+
testcli.AssertOutput(
105+
t,
106+
ctx,
107+
[]string{"bundle", "validate"},
108+
testutil.TestData("testdata/default_python/bundle_validate.txt"),
109+
)
110+
testcli.AssertOutput(
111+
t,
112+
ctx,
113+
[]string{"bundle", "deploy"},
114+
testutil.TestData("testdata/default_python/bundle_deploy.txt"),
115+
)
116+
117+
testcli.AssertOutputJQ(
118+
t,
119+
ctx,
120+
[]string{"bundle", "summary", "--output", "json"},
121+
testutil.TestData("testdata/default_python/bundle_summary.txt"),
122+
[]string{
123+
"/bundle/terraform/exec_path",
124+
"/resources/jobs/project_name_$UNIQUE_PRJ_job/email_notifications",
125+
"/resources/jobs/project_name_$UNIQUE_PRJ_job/job_clusters/0/new_cluster/node_type_id",
126+
"/resources/jobs/project_name_$UNIQUE_PRJ_job/url",
127+
"/resources/pipelines/project_name_$UNIQUE_PRJ_pipeline/catalog",
128+
"/resources/pipelines/project_name_$UNIQUE_PRJ_pipeline/url",
129+
"/workspace/current_user",
130+
},
131+
)
132+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Building project_name_$UNIQUE_PRJ...
2+
Uploading project_name_$UNIQUE_PRJ-0.0.1+<NUMID>.<NUMID>-py3-none-any.whl...
3+
Uploading bundle files to /Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev/files...
4+
Deploying resources...
5+
Updating deployment state...
6+
Deployment complete!
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
Welcome to the default Python template for Databricks Asset Bundles!
3+
Workspace to use (auto-detected, edit in 'project_name_$UNIQUE_PRJ/databricks.yml'): https://$DATABRICKS_HOST
4+
5+
✨ Your new project has been created in the 'project_name_$UNIQUE_PRJ' directory!
6+
7+
Please refer to the README.md file for "getting started" instructions.
8+
See also the documentation at https://docs.databricks.com/dev-tools/bundles/index.html.

0 commit comments

Comments
 (0)