|
| 1 | +package bundle_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/databricks/cli/integration/internal/acc" |
| 13 | + "github.com/databricks/cli/internal/testcli" |
| 14 | + "github.com/databricks/cli/internal/testutil" |
| 15 | + "github.com/databricks/cli/libs/python" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +var pythonVersions = []string{ |
| 20 | + "3.8", |
| 21 | + "3.9", |
| 22 | + "3.10", |
| 23 | + "3.11", |
| 24 | + "3.12", |
| 25 | + "3.13", |
| 26 | +} |
| 27 | + |
| 28 | +var pythonVersionsShort = []string{ |
| 29 | + "3.9", |
| 30 | + "3.12", |
| 31 | +} |
| 32 | + |
| 33 | +var extraInstalls = map[string][]string{ |
| 34 | + "3.12": {"setuptools"}, |
| 35 | + "3.13": {"setuptools"}, |
| 36 | +} |
| 37 | + |
| 38 | +func TestDefaultPython(t *testing.T) { |
| 39 | + versions := pythonVersions |
| 40 | + if testing.Short() { |
| 41 | + versions = pythonVersionsShort |
| 42 | + } |
| 43 | + |
| 44 | + for _, pythonVersion := range versions { |
| 45 | + t.Run("Python "+pythonVersion, func(t *testing.T) { |
| 46 | + testDefaultPython(t, pythonVersion) |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func setupPython(t testutil.TestingT, ctx context.Context, directory, pythonVersion string) { |
| 52 | + testutil.RunCommand(t, "uv", "venv", ".myenv", "--python", pythonVersion, "--seed") |
| 53 | + |
| 54 | + testutil.InsertVirtualenvInPath(t, filepath.Join(directory, ".myenv")) |
| 55 | + |
| 56 | + pythonExe, err := python.DetectExecutable(ctx) |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + t.Logf("pythonExe=%s", pythonExe) |
| 60 | + |
| 61 | + p, err := exec.LookPath("python3") |
| 62 | + t.Logf("python3 lookup=%s %s", p, err) |
| 63 | + |
| 64 | + p, err = exec.LookPath("python") |
| 65 | + t.Logf("python lookup=%s %s", p, err) |
| 66 | + |
| 67 | + p, err = exec.LookPath("python.exe") |
| 68 | + t.Logf("python.exe lookup=%s %s", p, err) |
| 69 | + |
| 70 | + actualVersion := testutil.CaptureCommandOutput(t, pythonExe, "--version") |
| 71 | + expectVersion := "Python " + pythonVersion |
| 72 | + require.True(t, strings.HasPrefix(actualVersion, expectVersion), "Running %s --version: Expected %v, got %v", pythonExe, expectVersion, actualVersion) |
| 73 | + |
| 74 | + extras, ok := extraInstalls[pythonVersion] |
| 75 | + if ok { |
| 76 | + args := append([]string{"pip", "install"}, extras...) |
| 77 | + testutil.RunCommand(t, "uv", args...) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func testDefaultPython(t *testing.T, pythonVersion string) { |
| 82 | + ctx, wt := acc.WorkspaceTest(t) |
| 83 | + |
| 84 | + uniqueProjectId := testutil.RandomName("") |
| 85 | + ctx, replacements := testcli.WithReplacementsMap(ctx) |
| 86 | + replacements.Set(uniqueProjectId, "$UNIQUE_PRJ") |
| 87 | + |
| 88 | + testcli.PrepareReplacements(t, replacements, wt.W) |
| 89 | + |
| 90 | + user, err := wt.W.CurrentUser.Me(ctx) |
| 91 | + require.NoError(t, err) |
| 92 | + if user != nil { |
| 93 | + testcli.PrepareReplacementsUser(t, replacements, *user) |
| 94 | + } |
| 95 | + |
| 96 | + tmpDir1 := t.TempDir() |
| 97 | + testutil.Chdir(t, tmpDir1) |
| 98 | + |
| 99 | + setupPython(t, ctx, tmpDir1, pythonVersion) |
| 100 | + |
| 101 | + projectName := "project_name_" + uniqueProjectId |
| 102 | + |
| 103 | + initConfig := map[string]string{ |
| 104 | + "project_name": projectName, |
| 105 | + "include_notebook": "yes", |
| 106 | + "include_python": "yes", |
| 107 | + "include_dlt": "yes", |
| 108 | + } |
| 109 | + b, err := json.Marshal(initConfig) |
| 110 | + require.NoError(t, err) |
| 111 | + err = os.WriteFile(filepath.Join(tmpDir1, "config.json"), b, 0o644) |
| 112 | + require.NoError(t, err) |
| 113 | + |
| 114 | + testcli.RequireOutput(t, ctx, []string{"bundle", "init", "default-python", "--config-file", "config.json"}, "testdata/default_python/bundle_init.txt") |
| 115 | + testutil.Chdir(t, projectName) |
| 116 | + |
| 117 | + testcli.RequireOutput(t, ctx, []string{"bundle", "validate"}, "testdata/default_python/bundle_validate.txt") |
| 118 | + |
| 119 | + testcli.RequireOutput(t, ctx, []string{"bundle", "deploy"}, "testdata/default_python/bundle_deploy.txt") |
| 120 | + t.Cleanup(func() { |
| 121 | + // Delete the stack |
| 122 | + testcli.RequireSuccessfulRun(t, ctx, "bundle", "destroy", "--auto-approve") |
| 123 | + }) |
| 124 | + |
| 125 | + ignoredFields := []string{ |
| 126 | + "/resources/jobs/project_name_$UNIQUE_PRJ_job/email_notifications", |
| 127 | + "/resources/jobs/project_name_$UNIQUE_PRJ_job/job_clusters/0/new_cluster/node_type_id", |
| 128 | + "/resources/jobs/project_name_$UNIQUE_PRJ_job/url", |
| 129 | + "/resources/pipelines/project_name_$UNIQUE_PRJ_pipeline/catalog", |
| 130 | + "/resources/pipelines/project_name_$UNIQUE_PRJ_pipeline/url", |
| 131 | + "/workspace/current_user/externalId", |
| 132 | + "/workspace/current_user/groups", |
| 133 | + "/workspace/current_user/name/familyName", |
| 134 | + } |
| 135 | + |
| 136 | + testcli.RequireOutputJQ(t, ctx, []string{"bundle", "summary", "--output", "json"}, "testdata/default_python/bundle_summary.txt", ignoredFields) |
| 137 | +} |
0 commit comments