2023-07-12 06:51:54 +00:00
|
|
|
package mutator_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/bundle/config/mutator"
|
|
|
|
"github.com/databricks/cli/bundle/config/resources"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/compute"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2024-11-13 08:43:31 +00:00
|
|
|
func TestOverrideComputeModeDevelopment(t *testing.T) {
|
2023-09-04 07:07:17 +00:00
|
|
|
t.Setenv("DATABRICKS_CLUSTER_ID", "")
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-07-12 06:51:54 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Mode: config.Development,
|
2024-09-23 10:42:34 +00:00
|
|
|
ClusterId: "newClusterID",
|
2023-07-12 06:51:54 +00:00
|
|
|
},
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job1": {JobSettings: &jobs.JobSettings{
|
|
|
|
Name: "job1",
|
|
|
|
Tasks: []jobs.Task{
|
|
|
|
{
|
Use dynamic configuration model in bundles (#1098)
## Changes
This is a fundamental change to how we load and process bundle
configuration. We now depend on the configuration being represented as a
`dyn.Value`. This representation is functionally equivalent to Go's
`any` (it is variadic) and allows us to capture metadata associated with
a value, such as where it was defined (e.g. file, line, and column). It
also allows us to represent Go's zero values properly (e.g. empty
string, integer equal to 0, or boolean false).
Using this representation allows us to let the configuration model
deviate from the typed structure we have been relying on so far
(`config.Root`). We need to deviate from these types when using
variables for fields that are not a string themselves. For example,
using `${var.num_workers}` for an integer `workers` field was impossible
until now (though not implemented in this change).
The loader for a `dyn.Value` includes functionality to capture any and
all type mismatches between the user-defined configuration and the
expected types. These mismatches can be surfaced as validation errors in
future PRs.
Given that many mutators expect the typed struct to be the source of
truth, this change converts between the dynamic representation and the
typed representation on mutator entry and exit. Existing mutators can
continue to modify the typed representation and these modifications are
reflected in the dynamic representation (see `MarkMutatorEntry` and
`MarkMutatorExit` in `bundle/config/root.go`).
Required changes included in this change:
* The existing interpolation package is removed in favor of
`libs/dyn/dynvar`.
* Functionality to merge job clusters, job tasks, and pipeline clusters
are now all broken out into their own mutators.
To be implemented later:
* Allow variable references for non-string types.
* Surface diagnostics about the configuration provided by the user in
the validation output.
* Some mutators use a resource's configuration file path to resolve
related relative paths. These depend on `bundle/config/paths.Path` being
set and populated through `ConfigureConfigFilePath`. Instead, they
should interact with the dynamically typed configuration directly. Doing
this also unlocks being able to differentiate different base paths used
within a job (e.g. a task override with a relative path defined in a
directory other than the base job).
## Tests
* Existing unit tests pass (some have been modified to accommodate)
* Integration tests pass
2024-02-16 19:41:58 +00:00
|
|
|
NewCluster: &compute.ClusterSpec{
|
|
|
|
SparkVersion: "14.2.x-scala2.12",
|
|
|
|
},
|
2023-07-12 06:51:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ExistingClusterId: "cluster2",
|
|
|
|
},
|
2023-08-28 07:51:35 +00:00
|
|
|
{
|
2024-04-16 12:03:21 +00:00
|
|
|
EnvironmentKey: "environment_key",
|
2023-08-28 07:51:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
JobClusterKey: "cluster_key",
|
|
|
|
},
|
2023-07-12 06:51:54 +00:00
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mutator.OverrideCompute()
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, m)
|
|
|
|
require.NoError(t, diags.Error())
|
2023-11-15 14:03:36 +00:00
|
|
|
assert.Nil(t, b.Config.Resources.Jobs["job1"].Tasks[0].NewCluster)
|
|
|
|
assert.Equal(t, "newClusterID", b.Config.Resources.Jobs["job1"].Tasks[0].ExistingClusterId)
|
|
|
|
assert.Equal(t, "newClusterID", b.Config.Resources.Jobs["job1"].Tasks[1].ExistingClusterId)
|
|
|
|
assert.Equal(t, "newClusterID", b.Config.Resources.Jobs["job1"].Tasks[2].ExistingClusterId)
|
|
|
|
assert.Equal(t, "newClusterID", b.Config.Resources.Jobs["job1"].Tasks[3].ExistingClusterId)
|
2023-08-28 07:51:35 +00:00
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
assert.Nil(t, b.Config.Resources.Jobs["job1"].Tasks[0].NewCluster)
|
2024-04-16 12:03:21 +00:00
|
|
|
assert.Empty(t, b.Config.Resources.Jobs["job1"].Tasks[2].EnvironmentKey)
|
2023-11-15 14:03:36 +00:00
|
|
|
assert.Empty(t, b.Config.Resources.Jobs["job1"].Tasks[3].JobClusterKey)
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
2024-11-13 08:43:31 +00:00
|
|
|
func TestOverrideComputeModeDefault(t *testing.T) {
|
2023-09-04 07:07:17 +00:00
|
|
|
t.Setenv("DATABRICKS_CLUSTER_ID", "newClusterId")
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-07-12 06:51:54 +00:00
|
|
|
Config: config.Root{
|
2024-11-13 08:43:31 +00:00
|
|
|
Bundle: config.Bundle{
|
|
|
|
Mode: "",
|
|
|
|
},
|
2023-07-12 06:51:54 +00:00
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job1": {JobSettings: &jobs.JobSettings{
|
|
|
|
Name: "job1",
|
|
|
|
Tasks: []jobs.Task{
|
|
|
|
{
|
|
|
|
NewCluster: &compute.ClusterSpec{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ExistingClusterId: "cluster2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mutator.OverrideCompute()
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, m)
|
2024-11-13 08:43:31 +00:00
|
|
|
require.Empty(t, diags)
|
|
|
|
assert.Equal(t, "newClusterId", b.Config.Resources.Jobs["job1"].Tasks[0].ExistingClusterId)
|
|
|
|
assert.Equal(t, "newClusterId", b.Config.Resources.Jobs["job1"].Tasks[1].ExistingClusterId)
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
2024-11-13 08:43:31 +00:00
|
|
|
func TestOverrideComputePipelineTask(t *testing.T) {
|
2023-09-04 07:07:17 +00:00
|
|
|
t.Setenv("DATABRICKS_CLUSTER_ID", "newClusterId")
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-08-28 07:51:35 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job1": {JobSettings: &jobs.JobSettings{
|
|
|
|
Name: "job1",
|
|
|
|
Tasks: []jobs.Task{
|
|
|
|
{
|
|
|
|
PipelineTask: &jobs.PipelineTask{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mutator.OverrideCompute()
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, m)
|
|
|
|
require.NoError(t, diags.Error())
|
2023-11-15 14:03:36 +00:00
|
|
|
assert.Empty(t, b.Config.Resources.Jobs["job1"].Tasks[0].ExistingClusterId)
|
2023-08-28 07:51:35 +00:00
|
|
|
}
|
|
|
|
|
2024-11-13 08:43:31 +00:00
|
|
|
func TestOverrideComputeForEachTask(t *testing.T) {
|
2024-04-12 09:53:29 +00:00
|
|
|
t.Setenv("DATABRICKS_CLUSTER_ID", "newClusterId")
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job1": {JobSettings: &jobs.JobSettings{
|
|
|
|
Name: "job1",
|
|
|
|
Tasks: []jobs.Task{
|
|
|
|
{
|
|
|
|
ForEachTask: &jobs.ForEachTask{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mutator.OverrideCompute()
|
|
|
|
diags := bundle.Apply(context.Background(), b, m)
|
|
|
|
require.NoError(t, diags.Error())
|
|
|
|
assert.Empty(t, b.Config.Resources.Jobs["job1"].Tasks[0].ForEachTask.Task)
|
|
|
|
}
|
|
|
|
|
2024-11-13 08:43:31 +00:00
|
|
|
func TesOverrideComputeProductionMode(t *testing.T) {
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-07-12 06:51:54 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
2024-11-13 08:43:31 +00:00
|
|
|
Mode: config.Production,
|
2024-09-23 10:42:34 +00:00
|
|
|
ClusterId: "newClusterID",
|
2023-07-12 06:51:54 +00:00
|
|
|
},
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job1": {JobSettings: &jobs.JobSettings{
|
|
|
|
Name: "job1",
|
|
|
|
Tasks: []jobs.Task{
|
|
|
|
{
|
|
|
|
NewCluster: &compute.ClusterSpec{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ExistingClusterId: "cluster2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mutator.OverrideCompute()
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, m)
|
|
|
|
require.True(t, diags.HasError())
|
2024-11-13 08:43:31 +00:00
|
|
|
assert.ErrorContains(t, diags.Error(), "overriding compute is not allowed in production mode")
|
|
|
|
assert.Equal(t, "cluster2", b.Config.Resources.Jobs["job1"].Tasks[0].ExistingClusterId)
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverrideProductionEnv(t *testing.T) {
|
2023-09-04 07:07:17 +00:00
|
|
|
t.Setenv("DATABRICKS_CLUSTER_ID", "newClusterId")
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &bundle.Bundle{
|
2023-07-12 06:51:54 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job1": {JobSettings: &jobs.JobSettings{
|
|
|
|
Name: "job1",
|
|
|
|
Tasks: []jobs.Task{
|
|
|
|
{
|
|
|
|
NewCluster: &compute.ClusterSpec{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ExistingClusterId: "cluster2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mutator.OverrideCompute()
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, m)
|
|
|
|
require.NoError(t, diags.Error())
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|