2022-12-12 10:31:28 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/bricks/bundle/config"
|
|
|
|
"github.com/databricks/bricks/bundle/config/resources"
|
2023-04-21 08:30:20 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/compute"
|
2022-12-12 10:31:28 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
2023-04-21 08:30:20 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/ml"
|
2023-04-05 14:29:42 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/pipelines"
|
2022-12-12 10:31:28 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-12-12 15:36:59 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-12-12 10:31:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConvertJob(t *testing.T) {
|
|
|
|
var src = resources.Job{
|
|
|
|
JobSettings: &jobs.JobSettings{
|
|
|
|
Name: "my job",
|
|
|
|
JobClusters: []jobs.JobCluster{
|
|
|
|
{
|
|
|
|
JobClusterKey: "key",
|
2023-04-21 08:30:20 +00:00
|
|
|
NewCluster: &compute.BaseClusterInfo{
|
2022-12-12 10:31:28 +00:00
|
|
|
SparkVersion: "10.4.x-scala2.12",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
GitSource: &jobs.GitSource{
|
|
|
|
GitProvider: jobs.GitSourceGitProviderGithub,
|
|
|
|
GitUrl: "https://github.com/foo/bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = config.Root{
|
|
|
|
Resources: config.Resources{
|
2022-12-15 12:00:41 +00:00
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"my_job": &src,
|
2022-12-12 10:31:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := BundleToTerraform(&config)
|
|
|
|
assert.Equal(t, "my job", out.Resource.Job["my_job"].Name)
|
|
|
|
assert.Len(t, out.Resource.Job["my_job"].JobCluster, 1)
|
|
|
|
assert.Equal(t, "https://github.com/foo/bar", out.Resource.Job["my_job"].GitSource.Url)
|
|
|
|
assert.Nil(t, out.Data)
|
|
|
|
}
|
2022-12-12 15:36:59 +00:00
|
|
|
|
2023-03-21 09:58:16 +00:00
|
|
|
func TestConvertJobPermissions(t *testing.T) {
|
|
|
|
var src = resources.Job{
|
|
|
|
Permissions: []resources.Permission{
|
|
|
|
{
|
|
|
|
Level: "CAN_VIEW",
|
|
|
|
UserName: "jane@doe.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = config.Root{
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"my_job": &src,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := BundleToTerraform(&config)
|
|
|
|
assert.NotEmpty(t, out.Resource.Permissions["job_my_job"].JobId)
|
|
|
|
assert.Len(t, out.Resource.Permissions["job_my_job"].AccessControl, 1)
|
|
|
|
|
|
|
|
p := out.Resource.Permissions["job_my_job"].AccessControl[0]
|
|
|
|
assert.Equal(t, "jane@doe.com", p.UserName)
|
|
|
|
assert.Equal(t, "CAN_VIEW", p.PermissionLevel)
|
|
|
|
}
|
|
|
|
|
2022-12-12 15:36:59 +00:00
|
|
|
func TestConvertJobTaskLibraries(t *testing.T) {
|
|
|
|
var src = resources.Job{
|
|
|
|
JobSettings: &jobs.JobSettings{
|
|
|
|
Name: "my job",
|
|
|
|
Tasks: []jobs.JobTaskSettings{
|
|
|
|
{
|
|
|
|
TaskKey: "key",
|
2023-04-21 08:30:20 +00:00
|
|
|
Libraries: []compute.Library{
|
2022-12-12 15:36:59 +00:00
|
|
|
{
|
2023-04-21 08:30:20 +00:00
|
|
|
Pypi: &compute.PythonPyPiLibrary{
|
2022-12-12 15:36:59 +00:00
|
|
|
Package: "mlflow",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = config.Root{
|
|
|
|
Resources: config.Resources{
|
2022-12-15 12:00:41 +00:00
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"my_job": &src,
|
2022-12-12 15:36:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := BundleToTerraform(&config)
|
|
|
|
assert.Equal(t, "my job", out.Resource.Job["my_job"].Name)
|
|
|
|
require.Len(t, out.Resource.Job["my_job"].Task, 1)
|
|
|
|
require.Len(t, out.Resource.Job["my_job"].Task[0].Library, 1)
|
|
|
|
assert.Equal(t, "mlflow", out.Resource.Job["my_job"].Task[0].Library[0].Pypi.Package)
|
|
|
|
}
|
2023-03-20 20:28:43 +00:00
|
|
|
|
2023-04-05 14:29:42 +00:00
|
|
|
func TestConvertPipeline(t *testing.T) {
|
|
|
|
var src = resources.Pipeline{
|
|
|
|
PipelineSpec: &pipelines.PipelineSpec{
|
|
|
|
Name: "my pipeline",
|
|
|
|
Libraries: []pipelines.PipelineLibrary{
|
|
|
|
{
|
|
|
|
Notebook: &pipelines.NotebookLibrary{
|
|
|
|
Path: "notebook path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
File: &pipelines.FileLibrary{
|
|
|
|
Path: "file path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = config.Root{
|
|
|
|
Resources: config.Resources{
|
|
|
|
Pipelines: map[string]*resources.Pipeline{
|
|
|
|
"my_pipeline": &src,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := BundleToTerraform(&config)
|
|
|
|
assert.Equal(t, "my pipeline", out.Resource.Pipeline["my_pipeline"].Name)
|
|
|
|
assert.Len(t, out.Resource.Pipeline["my_pipeline"].Library, 2)
|
|
|
|
assert.Nil(t, out.Data)
|
|
|
|
}
|
|
|
|
|
2023-03-21 09:58:16 +00:00
|
|
|
func TestConvertPipelinePermissions(t *testing.T) {
|
|
|
|
var src = resources.Pipeline{
|
|
|
|
Permissions: []resources.Permission{
|
|
|
|
{
|
|
|
|
Level: "CAN_VIEW",
|
|
|
|
UserName: "jane@doe.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = config.Root{
|
|
|
|
Resources: config.Resources{
|
|
|
|
Pipelines: map[string]*resources.Pipeline{
|
|
|
|
"my_pipeline": &src,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := BundleToTerraform(&config)
|
|
|
|
assert.NotEmpty(t, out.Resource.Permissions["pipeline_my_pipeline"].PipelineId)
|
|
|
|
assert.Len(t, out.Resource.Permissions["pipeline_my_pipeline"].AccessControl, 1)
|
|
|
|
|
|
|
|
p := out.Resource.Permissions["pipeline_my_pipeline"].AccessControl[0]
|
|
|
|
assert.Equal(t, "jane@doe.com", p.UserName)
|
|
|
|
assert.Equal(t, "CAN_VIEW", p.PermissionLevel)
|
|
|
|
}
|
|
|
|
|
2023-03-20 20:28:43 +00:00
|
|
|
func TestConvertModel(t *testing.T) {
|
|
|
|
var src = resources.MlflowModel{
|
2023-04-21 08:30:20 +00:00
|
|
|
Model: &ml.Model{
|
2023-03-20 20:28:43 +00:00
|
|
|
Name: "name",
|
|
|
|
Description: "description",
|
2023-04-21 08:30:20 +00:00
|
|
|
Tags: []ml.ModelTag{
|
2023-03-20 20:28:43 +00:00
|
|
|
{
|
|
|
|
Key: "k1",
|
|
|
|
Value: "v1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "k2",
|
|
|
|
Value: "v2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = config.Root{
|
|
|
|
Resources: config.Resources{
|
|
|
|
Models: map[string]*resources.MlflowModel{
|
|
|
|
"my_model": &src,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := BundleToTerraform(&config)
|
|
|
|
assert.Equal(t, "name", out.Resource.MlflowModel["my_model"].Name)
|
|
|
|
assert.Equal(t, "description", out.Resource.MlflowModel["my_model"].Description)
|
|
|
|
assert.Len(t, out.Resource.MlflowModel["my_model"].Tags, 2)
|
|
|
|
assert.Equal(t, "k1", out.Resource.MlflowModel["my_model"].Tags[0].Key)
|
|
|
|
assert.Equal(t, "v1", out.Resource.MlflowModel["my_model"].Tags[0].Value)
|
|
|
|
assert.Equal(t, "k2", out.Resource.MlflowModel["my_model"].Tags[1].Key)
|
|
|
|
assert.Equal(t, "v2", out.Resource.MlflowModel["my_model"].Tags[1].Value)
|
|
|
|
assert.Nil(t, out.Data)
|
|
|
|
}
|
|
|
|
|
2023-03-21 09:58:16 +00:00
|
|
|
func TestConvertModelPermissions(t *testing.T) {
|
|
|
|
var src = resources.MlflowModel{
|
|
|
|
Permissions: []resources.Permission{
|
|
|
|
{
|
|
|
|
Level: "CAN_READ",
|
|
|
|
UserName: "jane@doe.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = config.Root{
|
|
|
|
Resources: config.Resources{
|
|
|
|
Models: map[string]*resources.MlflowModel{
|
|
|
|
"my_model": &src,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := BundleToTerraform(&config)
|
|
|
|
assert.NotEmpty(t, out.Resource.Permissions["mlflow_model_my_model"].RegisteredModelId)
|
|
|
|
assert.Len(t, out.Resource.Permissions["mlflow_model_my_model"].AccessControl, 1)
|
|
|
|
|
|
|
|
p := out.Resource.Permissions["mlflow_model_my_model"].AccessControl[0]
|
|
|
|
assert.Equal(t, "jane@doe.com", p.UserName)
|
|
|
|
assert.Equal(t, "CAN_READ", p.PermissionLevel)
|
|
|
|
}
|
|
|
|
|
2023-03-20 20:28:43 +00:00
|
|
|
func TestConvertExperiment(t *testing.T) {
|
|
|
|
var src = resources.MlflowExperiment{
|
2023-04-21 08:30:20 +00:00
|
|
|
Experiment: &ml.Experiment{
|
2023-03-20 20:28:43 +00:00
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = config.Root{
|
|
|
|
Resources: config.Resources{
|
|
|
|
Experiments: map[string]*resources.MlflowExperiment{
|
|
|
|
"my_experiment": &src,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := BundleToTerraform(&config)
|
|
|
|
assert.Equal(t, "name", out.Resource.MlflowExperiment["my_experiment"].Name)
|
|
|
|
assert.Nil(t, out.Data)
|
|
|
|
}
|
2023-03-21 09:58:16 +00:00
|
|
|
|
|
|
|
func TestConvertExperimentPermissions(t *testing.T) {
|
|
|
|
var src = resources.MlflowExperiment{
|
|
|
|
Permissions: []resources.Permission{
|
|
|
|
{
|
|
|
|
Level: "CAN_READ",
|
|
|
|
UserName: "jane@doe.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var config = config.Root{
|
|
|
|
Resources: config.Resources{
|
|
|
|
Experiments: map[string]*resources.MlflowExperiment{
|
|
|
|
"my_experiment": &src,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out := BundleToTerraform(&config)
|
|
|
|
assert.NotEmpty(t, out.Resource.Permissions["mlflow_experiment_my_experiment"].ExperimentId)
|
|
|
|
assert.Len(t, out.Resource.Permissions["mlflow_experiment_my_experiment"].AccessControl, 1)
|
|
|
|
|
|
|
|
p := out.Resource.Permissions["mlflow_experiment_my_experiment"].AccessControl[0]
|
|
|
|
assert.Equal(t, "jane@doe.com", p.UserName)
|
|
|
|
assert.Equal(t, "CAN_READ", p.PermissionLevel)
|
|
|
|
|
|
|
|
}
|