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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOverrideDevelopment(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,
|
|
|
|
ComputeID: "newClusterID",
|
|
|
|
},
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job1": {JobSettings: &jobs.JobSettings{
|
|
|
|
Name: "job1",
|
|
|
|
Tasks: []jobs.Task{
|
|
|
|
{
|
|
|
|
NewCluster: &compute.ClusterSpec{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ExistingClusterId: "cluster2",
|
|
|
|
},
|
2023-08-28 07:51:35 +00:00
|
|
|
{
|
|
|
|
ComputeKey: "compute_key",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
JobClusterKey: "cluster_key",
|
|
|
|
},
|
2023-07-12 06:51:54 +00:00
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mutator.OverrideCompute()
|
2023-11-15 14:19:18 +00:00
|
|
|
err := bundle.Apply(context.Background(), b, m)
|
2023-07-12 06:51:54 +00:00
|
|
|
require.NoError(t, err)
|
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)
|
|
|
|
assert.Empty(t, b.Config.Resources.Jobs["job1"].Tasks[2].ComputeKey)
|
|
|
|
assert.Empty(t, b.Config.Resources.Jobs["job1"].Tasks[3].JobClusterKey)
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverrideDevelopmentEnv(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()
|
2023-11-15 14:19:18 +00:00
|
|
|
err := bundle.Apply(context.Background(), b, m)
|
2023-07-12 06:51:54 +00:00
|
|
|
require.NoError(t, err)
|
2023-11-15 14:03:36 +00:00
|
|
|
assert.Equal(t, "cluster2", b.Config.Resources.Jobs["job1"].Tasks[1].ExistingClusterId)
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 07:51:35 +00:00
|
|
|
func TestOverridePipelineTask(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()
|
2023-11-15 14:19:18 +00:00
|
|
|
err := bundle.Apply(context.Background(), b, m)
|
2023-08-28 07:51:35 +00:00
|
|
|
require.NoError(t, err)
|
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
|
|
|
}
|
|
|
|
|
2023-07-12 06:51:54 +00:00
|
|
|
func TestOverrideProduction(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{
|
|
|
|
ComputeID: "newClusterID",
|
|
|
|
},
|
|
|
|
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()
|
2023-11-15 14:19:18 +00:00
|
|
|
err := bundle.Apply(context.Background(), b, m)
|
2023-07-12 06:51:54 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2023-11-15 14:19:18 +00:00
|
|
|
err := bundle.Apply(context.Background(), b, m)
|
2023-07-12 06:51:54 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|