2022-12-16 13:49:23 +00:00
|
|
|
package mutator_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"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"
|
2022-12-16 13:49:23 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/pipelines"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-04-05 14:02:17 +00:00
|
|
|
func touchNotebookFile(t *testing.T, path string) {
|
2022-12-16 13:49:23 +00:00
|
|
|
f, err := os.Create(path)
|
|
|
|
require.NoError(t, err)
|
2023-02-20 18:42:55 +00:00
|
|
|
f.WriteString("# Databricks notebook source\n")
|
2022-12-16 13:49:23 +00:00
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
2023-04-05 14:02:17 +00:00
|
|
|
func touchEmptyFile(t *testing.T, path string) {
|
2023-04-12 14:17:13 +00:00
|
|
|
err := os.MkdirAll(filepath.Dir(path), 0700)
|
|
|
|
require.NoError(t, err)
|
2023-04-05 14:02:17 +00:00
|
|
|
f, err := os.Create(path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
2023-06-07 10:34:59 +00:00
|
|
|
func TestTranslatePathsSkippedWithGitSource(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: dir,
|
|
|
|
Workspace: config.Workspace{
|
|
|
|
FilesPath: "/bundle",
|
|
|
|
},
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job": {
|
|
|
|
|
|
|
|
Paths: resources.Paths{
|
|
|
|
ConfigFilePath: filepath.Join(dir, "resource.yml"),
|
|
|
|
},
|
|
|
|
JobSettings: &jobs.JobSettings{
|
|
|
|
GitSource: &jobs.GitSource{
|
|
|
|
GitBranch: "somebranch",
|
|
|
|
GitCommit: "somecommit",
|
|
|
|
GitProvider: "github",
|
|
|
|
GitTag: "sometag",
|
|
|
|
GitUrl: "https://github.com/someuser/somerepo",
|
|
|
|
},
|
2023-07-03 09:46:45 +00:00
|
|
|
Tasks: []jobs.Task{
|
2023-06-07 10:34:59 +00:00
|
|
|
{
|
|
|
|
NotebookTask: &jobs.NotebookTask{
|
|
|
|
NotebookPath: "my_job_notebook.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PythonWheelTask: &jobs.PythonWheelTask{
|
|
|
|
PackageName: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SparkPythonTask: &jobs.SparkPythonTask{
|
|
|
|
PythonFile: "my_python_file.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := mutator.TranslatePaths().Apply(context.Background(), bundle)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"my_job_notebook.py",
|
|
|
|
bundle.Config.Resources.Jobs["job"].Tasks[0].NotebookTask.NotebookPath,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"foo",
|
|
|
|
bundle.Config.Resources.Jobs["job"].Tasks[1].PythonWheelTask.PackageName,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"my_python_file.py",
|
|
|
|
bundle.Config.Resources.Jobs["job"].Tasks[2].SparkPythonTask.PythonFile,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-05 14:02:17 +00:00
|
|
|
func TestTranslatePaths(t *testing.T) {
|
2022-12-16 13:49:23 +00:00
|
|
|
dir := t.TempDir()
|
2023-04-05 14:02:17 +00:00
|
|
|
touchNotebookFile(t, filepath.Join(dir, "my_job_notebook.py"))
|
|
|
|
touchNotebookFile(t, filepath.Join(dir, "my_pipeline_notebook.py"))
|
|
|
|
touchEmptyFile(t, filepath.Join(dir, "my_python_file.py"))
|
2022-12-16 13:49:23 +00:00
|
|
|
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: dir,
|
2023-04-05 14:02:17 +00:00
|
|
|
Workspace: config.Workspace{
|
2023-04-12 14:54:36 +00:00
|
|
|
FilesPath: "/bundle",
|
2023-04-05 14:02:17 +00:00
|
|
|
},
|
2022-12-16 13:49:23 +00:00
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job": {
|
2023-04-12 14:17:13 +00:00
|
|
|
Paths: resources.Paths{
|
|
|
|
ConfigFilePath: filepath.Join(dir, "resource.yml"),
|
|
|
|
},
|
2022-12-16 13:49:23 +00:00
|
|
|
JobSettings: &jobs.JobSettings{
|
2023-07-03 09:46:45 +00:00
|
|
|
Tasks: []jobs.Task{
|
2022-12-16 13:49:23 +00:00
|
|
|
{
|
|
|
|
NotebookTask: &jobs.NotebookTask{
|
|
|
|
NotebookPath: "./my_job_notebook.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NotebookTask: &jobs.NotebookTask{
|
2023-03-21 17:13:16 +00:00
|
|
|
NotebookPath: "/Users/jane.doe@databricks.com/doesnt_exist.py",
|
2022-12-16 13:49:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NotebookTask: &jobs.NotebookTask{
|
|
|
|
NotebookPath: "./my_job_notebook.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PythonWheelTask: &jobs.PythonWheelTask{
|
|
|
|
PackageName: "foo",
|
|
|
|
},
|
|
|
|
},
|
2023-04-05 14:02:17 +00:00
|
|
|
{
|
|
|
|
SparkPythonTask: &jobs.SparkPythonTask{
|
|
|
|
PythonFile: "./my_python_file.py",
|
|
|
|
},
|
|
|
|
},
|
2022-12-16 13:49:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Pipelines: map[string]*resources.Pipeline{
|
|
|
|
"pipeline": {
|
2023-04-12 14:17:13 +00:00
|
|
|
Paths: resources.Paths{
|
|
|
|
ConfigFilePath: filepath.Join(dir, "resource.yml"),
|
|
|
|
},
|
2022-12-16 13:49:23 +00:00
|
|
|
PipelineSpec: &pipelines.PipelineSpec{
|
|
|
|
Libraries: []pipelines.PipelineLibrary{
|
|
|
|
{
|
|
|
|
Notebook: &pipelines.NotebookLibrary{
|
|
|
|
Path: "./my_pipeline_notebook.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Notebook: &pipelines.NotebookLibrary{
|
2023-03-21 17:13:16 +00:00
|
|
|
Path: "/Users/jane.doe@databricks.com/doesnt_exist.py",
|
2022-12-16 13:49:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Notebook: &pipelines.NotebookLibrary{
|
|
|
|
Path: "./my_pipeline_notebook.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Jar: "foo",
|
|
|
|
},
|
2023-04-05 14:29:42 +00:00
|
|
|
{
|
|
|
|
File: &pipelines.FileLibrary{
|
|
|
|
Path: "./my_python_file.py",
|
|
|
|
},
|
|
|
|
},
|
2022-12-16 13:49:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.TranslatePaths().Apply(context.Background(), bundle)
|
2022-12-16 13:49:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Assert that the path in the tasks now refer to the artifact.
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2023-04-05 14:02:17 +00:00
|
|
|
"/bundle/my_job_notebook",
|
2022-12-16 13:49:23 +00:00
|
|
|
bundle.Config.Resources.Jobs["job"].Tasks[0].NotebookTask.NotebookPath,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2023-03-21 17:13:16 +00:00
|
|
|
"/Users/jane.doe@databricks.com/doesnt_exist.py",
|
2022-12-16 13:49:23 +00:00
|
|
|
bundle.Config.Resources.Jobs["job"].Tasks[1].NotebookTask.NotebookPath,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2023-04-05 14:02:17 +00:00
|
|
|
"/bundle/my_job_notebook",
|
2022-12-16 13:49:23 +00:00
|
|
|
bundle.Config.Resources.Jobs["job"].Tasks[2].NotebookTask.NotebookPath,
|
|
|
|
)
|
2023-04-05 14:02:17 +00:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"/bundle/my_python_file.py",
|
|
|
|
bundle.Config.Resources.Jobs["job"].Tasks[4].SparkPythonTask.PythonFile,
|
|
|
|
)
|
2022-12-16 13:49:23 +00:00
|
|
|
|
|
|
|
// Assert that the path in the libraries now refer to the artifact.
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2023-04-05 14:02:17 +00:00
|
|
|
"/bundle/my_pipeline_notebook",
|
2022-12-16 13:49:23 +00:00
|
|
|
bundle.Config.Resources.Pipelines["pipeline"].Libraries[0].Notebook.Path,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2023-03-21 17:13:16 +00:00
|
|
|
"/Users/jane.doe@databricks.com/doesnt_exist.py",
|
2022-12-16 13:49:23 +00:00
|
|
|
bundle.Config.Resources.Pipelines["pipeline"].Libraries[1].Notebook.Path,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2023-04-05 14:02:17 +00:00
|
|
|
"/bundle/my_pipeline_notebook",
|
2022-12-16 13:49:23 +00:00
|
|
|
bundle.Config.Resources.Pipelines["pipeline"].Libraries[2].Notebook.Path,
|
|
|
|
)
|
2023-04-05 14:29:42 +00:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"/bundle/my_python_file.py",
|
|
|
|
bundle.Config.Resources.Pipelines["pipeline"].Libraries[4].File.Path,
|
|
|
|
)
|
2022-12-16 13:49:23 +00:00
|
|
|
}
|
2023-03-21 17:13:16 +00:00
|
|
|
|
2023-04-12 14:17:13 +00:00
|
|
|
func TestTranslatePathsInSubdirectories(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
touchEmptyFile(t, filepath.Join(dir, "job", "my_python_file.py"))
|
|
|
|
touchEmptyFile(t, filepath.Join(dir, "pipeline", "my_python_file.py"))
|
|
|
|
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: dir,
|
|
|
|
Workspace: config.Workspace{
|
2023-04-12 14:54:36 +00:00
|
|
|
FilesPath: "/bundle",
|
2023-04-12 14:17:13 +00:00
|
|
|
},
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job": {
|
|
|
|
Paths: resources.Paths{
|
|
|
|
ConfigFilePath: filepath.Join(dir, "job/resource.yml"),
|
|
|
|
},
|
|
|
|
JobSettings: &jobs.JobSettings{
|
2023-07-03 09:46:45 +00:00
|
|
|
Tasks: []jobs.Task{
|
2023-04-12 14:17:13 +00:00
|
|
|
{
|
|
|
|
SparkPythonTask: &jobs.SparkPythonTask{
|
|
|
|
PythonFile: "./my_python_file.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Pipelines: map[string]*resources.Pipeline{
|
|
|
|
"pipeline": {
|
|
|
|
Paths: resources.Paths{
|
|
|
|
ConfigFilePath: filepath.Join(dir, "pipeline/resource.yml"),
|
|
|
|
},
|
|
|
|
|
|
|
|
PipelineSpec: &pipelines.PipelineSpec{
|
|
|
|
Libraries: []pipelines.PipelineLibrary{
|
|
|
|
{
|
|
|
|
File: &pipelines.FileLibrary{
|
|
|
|
Path: "./my_python_file.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.TranslatePaths().Apply(context.Background(), bundle)
|
2023-04-12 14:17:13 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"/bundle/job/my_python_file.py",
|
|
|
|
bundle.Config.Resources.Jobs["job"].Tasks[0].SparkPythonTask.PythonFile,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"/bundle/pipeline/my_python_file.py",
|
|
|
|
bundle.Config.Resources.Pipelines["pipeline"].Libraries[0].File.Path,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTranslatePathsOutsideBundleRoot(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: dir,
|
|
|
|
Workspace: config.Workspace{
|
2023-04-12 14:54:36 +00:00
|
|
|
FilesPath: "/bundle",
|
2023-04-12 14:17:13 +00:00
|
|
|
},
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job": {
|
|
|
|
Paths: resources.Paths{
|
|
|
|
ConfigFilePath: filepath.Join(dir, "../resource.yml"),
|
|
|
|
},
|
|
|
|
JobSettings: &jobs.JobSettings{
|
2023-07-03 09:46:45 +00:00
|
|
|
Tasks: []jobs.Task{
|
2023-04-12 14:17:13 +00:00
|
|
|
{
|
|
|
|
SparkPythonTask: &jobs.SparkPythonTask{
|
|
|
|
PythonFile: "./my_python_file.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.TranslatePaths().Apply(context.Background(), bundle)
|
2023-04-12 14:17:13 +00:00
|
|
|
assert.ErrorContains(t, err, "is not contained in bundle root")
|
|
|
|
}
|
|
|
|
|
2023-03-21 17:13:16 +00:00
|
|
|
func TestJobNotebookDoesNotExistError(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: dir,
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job": {
|
2023-04-12 14:17:13 +00:00
|
|
|
Paths: resources.Paths{
|
|
|
|
ConfigFilePath: filepath.Join(dir, "fake.yml"),
|
|
|
|
},
|
2023-03-21 17:13:16 +00:00
|
|
|
JobSettings: &jobs.JobSettings{
|
2023-07-03 09:46:45 +00:00
|
|
|
Tasks: []jobs.Task{
|
2023-03-21 17:13:16 +00:00
|
|
|
{
|
|
|
|
NotebookTask: &jobs.NotebookTask{
|
|
|
|
NotebookPath: "./doesnt_exist.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.TranslatePaths().Apply(context.Background(), bundle)
|
2023-04-05 14:02:17 +00:00
|
|
|
assert.EqualError(t, err, "notebook ./doesnt_exist.py not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJobFileDoesNotExistError(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: dir,
|
|
|
|
Resources: config.Resources{
|
|
|
|
Jobs: map[string]*resources.Job{
|
|
|
|
"job": {
|
2023-04-12 14:17:13 +00:00
|
|
|
Paths: resources.Paths{
|
|
|
|
ConfigFilePath: filepath.Join(dir, "fake.yml"),
|
|
|
|
},
|
2023-04-05 14:02:17 +00:00
|
|
|
JobSettings: &jobs.JobSettings{
|
2023-07-03 09:46:45 +00:00
|
|
|
Tasks: []jobs.Task{
|
2023-04-05 14:02:17 +00:00
|
|
|
{
|
|
|
|
SparkPythonTask: &jobs.SparkPythonTask{
|
|
|
|
PythonFile: "./doesnt_exist.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.TranslatePaths().Apply(context.Background(), bundle)
|
2023-04-05 14:02:17 +00:00
|
|
|
assert.EqualError(t, err, "file ./doesnt_exist.py not found")
|
2023-03-21 17:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPipelineNotebookDoesNotExistError(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: dir,
|
|
|
|
Resources: config.Resources{
|
|
|
|
Pipelines: map[string]*resources.Pipeline{
|
|
|
|
"pipeline": {
|
2023-04-12 14:17:13 +00:00
|
|
|
Paths: resources.Paths{
|
|
|
|
ConfigFilePath: filepath.Join(dir, "fake.yml"),
|
|
|
|
},
|
2023-03-21 17:13:16 +00:00
|
|
|
PipelineSpec: &pipelines.PipelineSpec{
|
|
|
|
Libraries: []pipelines.PipelineLibrary{
|
|
|
|
{
|
|
|
|
Notebook: &pipelines.NotebookLibrary{
|
|
|
|
Path: "./doesnt_exist.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.TranslatePaths().Apply(context.Background(), bundle)
|
2023-04-05 14:02:17 +00:00
|
|
|
assert.EqualError(t, err, "notebook ./doesnt_exist.py not found")
|
2023-03-21 17:13:16 +00:00
|
|
|
}
|
2023-04-12 14:17:13 +00:00
|
|
|
|
|
|
|
func TestPipelineFileDoesNotExistError(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
bundle := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Path: dir,
|
|
|
|
Resources: config.Resources{
|
|
|
|
Pipelines: map[string]*resources.Pipeline{
|
|
|
|
"pipeline": {
|
|
|
|
Paths: resources.Paths{
|
|
|
|
ConfigFilePath: filepath.Join(dir, "fake.yml"),
|
|
|
|
},
|
|
|
|
PipelineSpec: &pipelines.PipelineSpec{
|
|
|
|
Libraries: []pipelines.PipelineLibrary{
|
|
|
|
{
|
|
|
|
File: &pipelines.FileLibrary{
|
|
|
|
Path: "./doesnt_exist.py",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
err := mutator.TranslatePaths().Apply(context.Background(), bundle)
|
2023-04-12 14:17:13 +00:00
|
|
|
assert.EqualError(t, err, "file ./doesnt_exist.py not found")
|
|
|
|
}
|