Skip to content

Commit

Permalink
Convert some resolve variables tests to acceptance test (#2100)
Browse files Browse the repository at this point in the history
  • Loading branch information
denik authored Jan 8, 2025
1 parent 42b34c7 commit df17e4b
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 114 deletions.
6 changes: 6 additions & 0 deletions acceptance/bundle/variables/resolve-builtin/databricks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bundle:
name: TestResolveVariableReferences

workspace:
root_path: "${bundle.name}/bar"
file_path: "${workspace.root_path}/baz"
11 changes: 11 additions & 0 deletions acceptance/bundle/variables/resolve-builtin/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"artifact_path": "TestResolveVariableReferences/bar/artifacts",
"current_user": {
"short_name": "tester",
"userName": "tester@databricks.com"
},
"file_path": "TestResolveVariableReferences/bar/baz",
"resource_path": "TestResolveVariableReferences/bar/resources",
"root_path": "TestResolveVariableReferences/bar",
"state_path": "TestResolveVariableReferences/bar/state"
}
1 change: 1 addition & 0 deletions acceptance/bundle/variables/resolve-builtin/script
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$CLI bundle validate -o json | jq .workspace
10 changes: 10 additions & 0 deletions acceptance/bundle/variables/resolve-empty/databricks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bundle:
name: TestResolveVariableReferencesToEmptyFields
git:
branch: ""

resources:
jobs:
job1:
tags:
git_branch: "${bundle.git.branch}"
3 changes: 3 additions & 0 deletions acceptance/bundle/variables/resolve-empty/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"git_branch": ""
}
1 change: 1 addition & 0 deletions acceptance/bundle/variables/resolve-empty/script
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$CLI bundle validate -o json | jq .resources.jobs.job1.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
bundle:
name: TestResolveComplexVariableReferencesToFields

variables:
cluster:
type: "complex"
default:
node_type_id: "Standard_DS3_v2"
num_workers: 2

resources:
jobs:
job1:
job_clusters:
- new_cluster:
node_type_id: "${var.cluster.node_type_id}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"node_type_id": "Standard_DS3_v2"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$CLI bundle validate -o json | jq .resources.jobs.job1.job_clusters[0].new_cluster
114 changes: 0 additions & 114 deletions bundle/config/mutator/resolve_variable_references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestResolveVariableReferences(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{
Name: "example",
},
Workspace: config.Workspace{
RootPath: "${bundle.name}/bar",
FilePath: "${workspace.root_path}/baz",
},
},
}

// Apply with an invalid prefix. This should not change the workspace root path.
diags := bundle.Apply(context.Background(), b, ResolveVariableReferences("doesntexist"))
require.NoError(t, diags.Error())
require.Equal(t, "${bundle.name}/bar", b.Config.Workspace.RootPath)
require.Equal(t, "${workspace.root_path}/baz", b.Config.Workspace.FilePath)

// Apply with a valid prefix. This should change the workspace root path.
diags = bundle.Apply(context.Background(), b, ResolveVariableReferences("bundle", "workspace"))
require.NoError(t, diags.Error())
require.Equal(t, "example/bar", b.Config.Workspace.RootPath)
require.Equal(t, "example/bar/baz", b.Config.Workspace.FilePath)
}

func TestResolveVariableReferencesToBundleVariables(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Expand All @@ -66,37 +40,6 @@ func TestResolveVariableReferencesToBundleVariables(t *testing.T) {
require.Equal(t, "example/bar", b.Config.Workspace.RootPath)
}

func TestResolveVariableReferencesToEmptyFields(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{
Name: "example",
Git: config.Git{
Branch: "",
},
},
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"job1": {
JobSettings: &jobs.JobSettings{
Tags: map[string]string{
"git_branch": "${bundle.git.branch}",
},
},
},
},
},
},
}

// Apply for the bundle prefix.
diags := bundle.Apply(context.Background(), b, ResolveVariableReferences("bundle"))
require.NoError(t, diags.Error())

// The job settings should have been interpolated to an empty string.
require.Equal(t, "", b.Config.Resources.Jobs["job1"].JobSettings.Tags["git_branch"])
}

func TestResolveVariableReferencesForPrimitiveNonStringFields(t *testing.T) {
var diags diag.Diagnostics

Expand Down Expand Up @@ -251,63 +194,6 @@ func TestResolveComplexVariable(t *testing.T) {
require.Equal(t, 2, b.Config.Resources.Jobs["job1"].JobSettings.JobClusters[0].NewCluster.NumWorkers)
}

func TestResolveComplexVariableReferencesToFields(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{
Name: "example",
},
Variables: map[string]*variable.Variable{
"cluster": {
Value: map[string]any{
"node_type_id": "Standard_DS3_v2",
"num_workers": 2,
},
Type: variable.VariableTypeComplex,
},
},

Resources: config.Resources{
Jobs: map[string]*resources.Job{
"job1": {
JobSettings: &jobs.JobSettings{
JobClusters: []jobs.JobCluster{
{
NewCluster: compute.ClusterSpec{
NodeTypeId: "random",
},
},
},
},
},
},
},
},
}

ctx := context.Background()

// Assign the variables to the dynamic configuration.
diags := bundle.ApplyFunc(ctx, b, func(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) {
var p dyn.Path
var err error

p = dyn.MustPathFromString("resources.jobs.job1.job_clusters[0].new_cluster")
v, err = dyn.SetByPath(v, p.Append(dyn.Key("node_type_id")), dyn.V("${var.cluster.node_type_id}"))
require.NoError(t, err)

return v, nil
})
return diag.FromErr(err)
})
require.NoError(t, diags.Error())

diags = bundle.Apply(ctx, b, ResolveVariableReferences("bundle", "workspace", "variables"))
require.NoError(t, diags.Error())
require.Equal(t, "Standard_DS3_v2", b.Config.Resources.Jobs["job1"].JobSettings.JobClusters[0].NewCluster.NodeTypeId)
}

func TestResolveComplexVariableReferencesWithComplexVariablesError(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Expand Down

0 comments on commit df17e4b

Please sign in to comment.