2023-07-30 07:19:49 +00:00
|
|
|
package mutator
|
2023-07-12 06:51:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-30 07:19:49 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2023-07-12 06:51:54 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/bundle/config/resources"
|
2023-10-02 06:58:51 +00:00
|
|
|
"github.com/databricks/cli/libs/tags"
|
|
|
|
sdkconfig "github.com/databricks/databricks-sdk-go/config"
|
2023-10-16 15:32:49 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/catalog"
|
2023-07-30 07:19:49 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/iam"
|
2023-07-12 06:51:54 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/ml"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/pipelines"
|
2023-09-07 21:54:31 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/serving"
|
2023-07-12 06:51:54 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-07-30 07:19:49 +00:00
|
|
|
func mockBundle(mode config.Mode) *bundle.Bundle {
|
|
|
|
return &bundle.Bundle{
|
2023-07-12 06:51:54 +00:00
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
2023-07-30 07:19:49 +00:00
|
|
|
Mode: mode,
|
2023-07-30 12:44:33 +00:00
|
|
|
Git: config.Git{
|
|
|
|
OriginURL: "http://origin",
|
|
|
|
Branch: "main",
|
|
|
|
},
|
2023-07-30 07:19:49 +00:00
|
|
|
},
|
|
|
|
Workspace: config.Workspace{
|
|
|
|
CurrentUser: &config.User{
|
|
|
|
ShortName: "lennart",
|
|
|
|
User: &iam.User{
|
|
|
|
UserName: "lennart@company.com",
|
|
|
|
Id: "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
StatePath: "/Users/lennart@company.com/.bundle/x/y/state",
|
|
|
|
ArtifactsPath: "/Users/lennart@company.com/.bundle/x/y/artifacts",
|
|
|
|
FilesPath: "/Users/lennart@company.com/.bundle/x/y/files",
|
2023-07-12 06:51:54 +00:00
|
|
|
},
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job1": {JobSettings: &jobs.JobSettings{Name: "job1"}},
|
|
|
|
},
|
|
|
|
Pipelines: map[string]*resources.Pipeline{
|
|
|
|
"pipeline1": {PipelineSpec: &pipelines.PipelineSpec{Name: "pipeline1"}},
|
|
|
|
},
|
|
|
|
Experiments: map[string]*resources.MlflowExperiment{
|
|
|
|
"experiment1": {Experiment: &ml.Experiment{Name: "/Users/lennart.kats@databricks.com/experiment1"}},
|
|
|
|
"experiment2": {Experiment: &ml.Experiment{Name: "experiment2"}},
|
|
|
|
},
|
|
|
|
Models: map[string]*resources.MlflowModel{
|
|
|
|
"model1": {Model: &ml.Model{Name: "model1"}},
|
|
|
|
},
|
2023-09-07 21:54:31 +00:00
|
|
|
ModelServingEndpoints: map[string]*resources.ModelServingEndpoint{
|
|
|
|
"servingendpoint1": {CreateServingEndpoint: &serving.CreateServingEndpoint{Name: "servingendpoint1"}},
|
|
|
|
},
|
2023-10-16 15:32:49 +00:00
|
|
|
RegisteredModels: map[string]*resources.RegisteredModel{
|
|
|
|
"registeredmodel1": {CreateRegisteredModelRequest: &catalog.CreateRegisteredModelRequest{Name: "registeredmodel1"}},
|
|
|
|
},
|
2023-07-12 06:51:54 +00:00
|
|
|
},
|
|
|
|
},
|
2023-10-02 06:58:51 +00:00
|
|
|
// Use AWS implementation for testing.
|
|
|
|
Tagging: tags.ForCloud(&sdkconfig.Config{
|
|
|
|
Host: "https://company.cloud.databricks.com",
|
|
|
|
}),
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
2023-07-30 07:19:49 +00:00
|
|
|
}
|
2023-07-12 06:51:54 +00:00
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
func TestProcessTargetModeDevelopment(t *testing.T) {
|
2023-07-30 07:19:49 +00:00
|
|
|
bundle := mockBundle(config.Development)
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
m := ProcessTargetMode()
|
2023-07-12 06:51:54 +00:00
|
|
|
err := m.Apply(context.Background(), bundle)
|
|
|
|
require.NoError(t, err)
|
2023-10-02 06:58:51 +00:00
|
|
|
|
|
|
|
// Job 1
|
2023-07-30 07:19:49 +00:00
|
|
|
assert.Equal(t, "[dev lennart] job1", bundle.Config.Resources.Jobs["job1"].Name)
|
2023-10-02 06:58:51 +00:00
|
|
|
assert.Equal(t, bundle.Config.Resources.Jobs["job1"].Tags["dev"], "lennart")
|
|
|
|
|
|
|
|
// Pipeline 1
|
2023-07-30 07:19:49 +00:00
|
|
|
assert.Equal(t, "[dev lennart] pipeline1", bundle.Config.Resources.Pipelines["pipeline1"].Name)
|
2023-10-02 06:58:51 +00:00
|
|
|
assert.True(t, bundle.Config.Resources.Pipelines["pipeline1"].PipelineSpec.Development)
|
|
|
|
|
|
|
|
// Experiment 1
|
2023-07-30 07:19:49 +00:00
|
|
|
assert.Equal(t, "/Users/lennart.kats@databricks.com/[dev lennart] experiment1", bundle.Config.Resources.Experiments["experiment1"].Name)
|
2023-10-02 06:58:51 +00:00
|
|
|
assert.Contains(t, bundle.Config.Resources.Experiments["experiment1"].Experiment.Tags, ml.ExperimentTag{Key: "dev", Value: "lennart"})
|
2023-10-16 15:32:49 +00:00
|
|
|
assert.Equal(t, "dev", bundle.Config.Resources.Experiments["experiment1"].Experiment.Tags[0].Key)
|
2023-10-02 06:58:51 +00:00
|
|
|
|
|
|
|
// Experiment 2
|
2023-07-30 07:19:49 +00:00
|
|
|
assert.Equal(t, "[dev lennart] experiment2", bundle.Config.Resources.Experiments["experiment2"].Name)
|
2023-10-02 06:58:51 +00:00
|
|
|
assert.Contains(t, bundle.Config.Resources.Experiments["experiment2"].Experiment.Tags, ml.ExperimentTag{Key: "dev", Value: "lennart"})
|
|
|
|
|
|
|
|
// Model 1
|
2023-07-30 07:19:49 +00:00
|
|
|
assert.Equal(t, "[dev lennart] model1", bundle.Config.Resources.Models["model1"].Name)
|
2023-10-02 06:58:51 +00:00
|
|
|
|
|
|
|
// Model serving endpoint 1
|
2023-09-07 21:54:31 +00:00
|
|
|
assert.Equal(t, "dev_lennart_servingendpoint1", bundle.Config.Resources.ModelServingEndpoints["servingendpoint1"].Name)
|
2023-10-16 15:32:49 +00:00
|
|
|
|
|
|
|
// Registered model 1
|
|
|
|
assert.Equal(t, "dev_lennart_registeredmodel1", bundle.Config.Resources.RegisteredModels["registeredmodel1"].Name)
|
2023-10-02 06:58:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessTargetModeDevelopmentTagNormalizationForAws(t *testing.T) {
|
|
|
|
bundle := mockBundle(config.Development)
|
|
|
|
bundle.Tagging = tags.ForCloud(&sdkconfig.Config{
|
|
|
|
Host: "https://dbc-XXXXXXXX-YYYY.cloud.databricks.com/",
|
|
|
|
})
|
|
|
|
|
|
|
|
bundle.Config.Workspace.CurrentUser.ShortName = "Héllö wörld?!"
|
|
|
|
err := ProcessTargetMode().Apply(context.Background(), bundle)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert that tag normalization took place.
|
|
|
|
assert.Equal(t, "Hello world__", bundle.Config.Resources.Jobs["job1"].Tags["dev"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessTargetModeDevelopmentTagNormalizationForAzure(t *testing.T) {
|
|
|
|
bundle := mockBundle(config.Development)
|
|
|
|
bundle.Tagging = tags.ForCloud(&sdkconfig.Config{
|
|
|
|
Host: "https://adb-xxx.y.azuredatabricks.net/",
|
|
|
|
})
|
|
|
|
|
|
|
|
bundle.Config.Workspace.CurrentUser.ShortName = "Héllö wörld?!"
|
|
|
|
err := ProcessTargetMode().Apply(context.Background(), bundle)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert that tag normalization took place (Azure allows more characters than AWS).
|
|
|
|
assert.Equal(t, "Héllö wörld?!", bundle.Config.Resources.Jobs["job1"].Tags["dev"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProcessTargetModeDevelopmentTagNormalizationForGcp(t *testing.T) {
|
|
|
|
bundle := mockBundle(config.Development)
|
|
|
|
bundle.Tagging = tags.ForCloud(&sdkconfig.Config{
|
|
|
|
Host: "https://123.4.gcp.databricks.com/",
|
|
|
|
})
|
|
|
|
|
|
|
|
bundle.Config.Workspace.CurrentUser.ShortName = "Héllö wörld?!"
|
|
|
|
err := ProcessTargetMode().Apply(context.Background(), bundle)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert that tag normalization took place.
|
|
|
|
assert.Equal(t, "Hello_world", bundle.Config.Resources.Jobs["job1"].Tags["dev"])
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
func TestProcessTargetModeDefault(t *testing.T) {
|
2023-07-30 07:19:49 +00:00
|
|
|
bundle := mockBundle("")
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
m := ProcessTargetMode()
|
2023-07-30 07:19:49 +00:00
|
|
|
err := m.Apply(context.Background(), bundle)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "job1", bundle.Config.Resources.Jobs["job1"].Name)
|
|
|
|
assert.Equal(t, "pipeline1", bundle.Config.Resources.Pipelines["pipeline1"].Name)
|
|
|
|
assert.False(t, bundle.Config.Resources.Pipelines["pipeline1"].PipelineSpec.Development)
|
2023-09-07 21:54:31 +00:00
|
|
|
assert.Equal(t, "servingendpoint1", bundle.Config.Resources.ModelServingEndpoints["servingendpoint1"].Name)
|
2023-10-16 15:32:49 +00:00
|
|
|
assert.Equal(t, "registeredmodel1", bundle.Config.Resources.RegisteredModels["registeredmodel1"].Name)
|
2023-07-30 07:19:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
func TestProcessTargetModeProduction(t *testing.T) {
|
2023-07-30 07:19:49 +00:00
|
|
|
bundle := mockBundle(config.Production)
|
|
|
|
|
|
|
|
err := validateProductionMode(context.Background(), bundle, false)
|
|
|
|
require.ErrorContains(t, err, "state_path")
|
|
|
|
|
|
|
|
bundle.Config.Workspace.StatePath = "/Shared/.bundle/x/y/state"
|
|
|
|
bundle.Config.Workspace.ArtifactsPath = "/Shared/.bundle/x/y/artifacts"
|
|
|
|
bundle.Config.Workspace.FilesPath = "/Shared/.bundle/x/y/files"
|
|
|
|
|
|
|
|
err = validateProductionMode(context.Background(), bundle, false)
|
|
|
|
require.ErrorContains(t, err, "production")
|
|
|
|
|
|
|
|
permissions := []resources.Permission{
|
|
|
|
{
|
|
|
|
Level: "CAN_MANAGE",
|
|
|
|
UserName: "user@company.com",
|
2023-07-12 06:51:54 +00:00
|
|
|
},
|
|
|
|
}
|
2023-07-30 07:19:49 +00:00
|
|
|
bundle.Config.Resources.Jobs["job1"].Permissions = permissions
|
|
|
|
bundle.Config.Resources.Jobs["job1"].RunAs = &jobs.JobRunAs{UserName: "user@company.com"}
|
|
|
|
bundle.Config.Resources.Pipelines["pipeline1"].Permissions = permissions
|
|
|
|
bundle.Config.Resources.Experiments["experiment1"].Permissions = permissions
|
|
|
|
bundle.Config.Resources.Experiments["experiment2"].Permissions = permissions
|
|
|
|
bundle.Config.Resources.Models["model1"].Permissions = permissions
|
2023-09-07 21:54:31 +00:00
|
|
|
bundle.Config.Resources.ModelServingEndpoints["servingendpoint1"].Permissions = permissions
|
2023-07-12 06:51:54 +00:00
|
|
|
|
2023-07-30 07:19:49 +00:00
|
|
|
err = validateProductionMode(context.Background(), bundle, false)
|
2023-07-12 06:51:54 +00:00
|
|
|
require.NoError(t, err)
|
2023-07-30 07:19:49 +00:00
|
|
|
|
2023-07-12 06:51:54 +00:00
|
|
|
assert.Equal(t, "job1", bundle.Config.Resources.Jobs["job1"].Name)
|
|
|
|
assert.Equal(t, "pipeline1", bundle.Config.Resources.Pipelines["pipeline1"].Name)
|
|
|
|
assert.False(t, bundle.Config.Resources.Pipelines["pipeline1"].PipelineSpec.Development)
|
2023-09-07 21:54:31 +00:00
|
|
|
assert.Equal(t, "servingendpoint1", bundle.Config.Resources.ModelServingEndpoints["servingendpoint1"].Name)
|
2023-10-16 15:32:49 +00:00
|
|
|
assert.Equal(t, "registeredmodel1", bundle.Config.Resources.RegisteredModels["registeredmodel1"].Name)
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
2023-07-30 07:19:49 +00:00
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
func TestProcessTargetModeProductionOkForPrincipal(t *testing.T) {
|
2023-07-30 07:19:49 +00:00
|
|
|
bundle := mockBundle(config.Production)
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
// Our target has all kinds of problems when not using service principals ...
|
2023-07-30 07:19:49 +00:00
|
|
|
err := validateProductionMode(context.Background(), bundle, false)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// ... but we're much less strict when a principal is used
|
|
|
|
err = validateProductionMode(context.Background(), bundle, true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that we have test coverage for all resource types
|
|
|
|
func TestAllResourcesMocked(t *testing.T) {
|
|
|
|
bundle := mockBundle(config.Development)
|
|
|
|
resources := reflect.ValueOf(bundle.Config.Resources)
|
|
|
|
|
|
|
|
for i := 0; i < resources.NumField(); i++ {
|
|
|
|
field := resources.Field(i)
|
|
|
|
if field.Kind() == reflect.Map {
|
|
|
|
assert.True(
|
|
|
|
t,
|
|
|
|
!field.IsNil() && field.Len() > 0,
|
2023-08-17 15:22:32 +00:00
|
|
|
"process_target_mode should support '%s' (please add it to process_target_mode.go and extend the test suite)",
|
2023-07-30 07:19:49 +00:00
|
|
|
resources.Type().Field(i).Name,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that we at least rename all resources
|
|
|
|
func TestAllResourcesRenamed(t *testing.T) {
|
|
|
|
bundle := mockBundle(config.Development)
|
|
|
|
resources := reflect.ValueOf(bundle.Config.Resources)
|
|
|
|
|
2023-08-17 15:22:32 +00:00
|
|
|
m := ProcessTargetMode()
|
2023-07-30 07:19:49 +00:00
|
|
|
err := m.Apply(context.Background(), bundle)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for i := 0; i < resources.NumField(); i++ {
|
|
|
|
field := resources.Field(i)
|
|
|
|
|
|
|
|
if field.Kind() == reflect.Map {
|
|
|
|
for _, key := range field.MapKeys() {
|
|
|
|
resource := field.MapIndex(key)
|
|
|
|
nameField := resource.Elem().FieldByName("Name")
|
|
|
|
if nameField.IsValid() && nameField.Kind() == reflect.String {
|
|
|
|
assert.True(
|
|
|
|
t,
|
|
|
|
strings.Contains(nameField.String(), "dev"),
|
2023-08-17 15:22:32 +00:00
|
|
|
"process_target_mode should rename '%s' in '%s'",
|
2023-07-30 07:19:49 +00:00
|
|
|
key,
|
|
|
|
resources.Type().Field(i).Name,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|