2024-01-17 14:26:33 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/internal"
|
2024-02-07 11:18:56 +00:00
|
|
|
"github.com/databricks/cli/internal/acc"
|
2024-01-17 14:26:33 +00:00
|
|
|
"github.com/databricks/cli/internal/testutil"
|
|
|
|
"github.com/databricks/cli/libs/filer"
|
|
|
|
"github.com/databricks/databricks-sdk-go"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/compute"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccGenerateFromExistingJobAndDeploy(t *testing.T) {
|
2024-02-07 11:18:56 +00:00
|
|
|
ctx, wt := acc.WorkspaceTest(t)
|
|
|
|
gt := &generateJobTest{T: t, w: wt.W}
|
2024-01-17 14:26:33 +00:00
|
|
|
|
|
|
|
uniqueId := uuid.New().String()
|
2024-02-07 11:18:56 +00:00
|
|
|
bundleRoot, err := initTestTemplate(t, ctx, "with_includes", map[string]any{
|
2024-01-17 14:26:33 +00:00
|
|
|
"unique_id": uniqueId,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-02-07 11:18:56 +00:00
|
|
|
jobId := gt.createTestJob(ctx)
|
2024-01-17 14:26:33 +00:00
|
|
|
t.Cleanup(func() {
|
2024-02-07 11:18:56 +00:00
|
|
|
gt.destroyJob(ctx, jobId)
|
2024-01-17 14:26:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Setenv("BUNDLE_ROOT", bundleRoot)
|
2024-02-07 11:18:56 +00:00
|
|
|
c := internal.NewCobraTestRunnerWithContext(t, ctx, "bundle", "generate", "job",
|
2024-01-17 14:26:33 +00:00
|
|
|
"--existing-job-id", fmt.Sprint(jobId),
|
|
|
|
"--config-dir", filepath.Join(bundleRoot, "resources"),
|
|
|
|
"--source-dir", filepath.Join(bundleRoot, "src"))
|
|
|
|
_, _, err = c.Run()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = os.Stat(filepath.Join(bundleRoot, "src", "test.py"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-01-31 10:23:35 +00:00
|
|
|
matches, err := filepath.Glob(filepath.Join(bundleRoot, "resources", "generated_job_*.yml"))
|
2024-01-17 14:26:33 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, matches, 1)
|
|
|
|
|
|
|
|
// check the content of generated yaml
|
|
|
|
data, err := os.ReadFile(matches[0])
|
|
|
|
require.NoError(t, err)
|
|
|
|
generatedYaml := string(data)
|
|
|
|
require.Contains(t, generatedYaml, "notebook_task:")
|
2024-01-19 08:05:59 +00:00
|
|
|
require.Contains(t, generatedYaml, fmt.Sprintf("notebook_path: %s", filepath.Join("..", "src", "test.py")))
|
2024-01-17 14:26:33 +00:00
|
|
|
require.Contains(t, generatedYaml, "task_key: test")
|
|
|
|
require.Contains(t, generatedYaml, "new_cluster:")
|
|
|
|
require.Contains(t, generatedYaml, "spark_version: 13.3.x-scala2.12")
|
|
|
|
require.Contains(t, generatedYaml, "num_workers: 1")
|
|
|
|
|
2024-02-07 11:18:56 +00:00
|
|
|
err = deployBundle(t, ctx, bundleRoot)
|
2024-01-17 14:26:33 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-02-07 11:18:56 +00:00
|
|
|
err = destroyBundle(t, ctx, bundleRoot)
|
2024-01-17 14:26:33 +00:00
|
|
|
require.NoError(t, err)
|
2024-02-07 11:18:56 +00:00
|
|
|
}
|
2024-01-17 14:26:33 +00:00
|
|
|
|
2024-02-07 11:18:56 +00:00
|
|
|
type generateJobTest struct {
|
|
|
|
T *testing.T
|
|
|
|
w *databricks.WorkspaceClient
|
2024-01-17 14:26:33 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 11:18:56 +00:00
|
|
|
func (gt *generateJobTest) createTestJob(ctx context.Context) int64 {
|
|
|
|
t := gt.T
|
|
|
|
w := gt.w
|
|
|
|
|
2024-01-17 14:26:33 +00:00
|
|
|
var nodeTypeId string
|
|
|
|
switch testutil.GetCloud(t) {
|
|
|
|
case testutil.AWS:
|
|
|
|
nodeTypeId = "i3.xlarge"
|
|
|
|
case testutil.Azure:
|
|
|
|
nodeTypeId = "Standard_DS4_v2"
|
|
|
|
case testutil.GCP:
|
|
|
|
nodeTypeId = "n1-standard-4"
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpdir := internal.TemporaryWorkspaceDir(t, w)
|
|
|
|
f, err := filer.NewWorkspaceFilesClient(w, tmpdir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = f.Write(ctx, "test.py", strings.NewReader("# Databricks notebook source\nprint('Hello world!'))"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err := w.Jobs.Create(ctx, jobs.CreateJob{
|
|
|
|
Name: internal.RandomName("generated-job-"),
|
|
|
|
Tasks: []jobs.Task{
|
|
|
|
{
|
|
|
|
TaskKey: "test",
|
|
|
|
NewCluster: &compute.ClusterSpec{
|
|
|
|
SparkVersion: "13.3.x-scala2.12",
|
|
|
|
NumWorkers: 1,
|
|
|
|
NodeTypeId: nodeTypeId,
|
2024-02-15 15:03:19 +00:00
|
|
|
SparkConf: map[string]string{
|
|
|
|
"spark.databricks.enableWsfs": "true",
|
|
|
|
"spark.databricks.hive.metastore.glueCatalog.enabled": "true",
|
|
|
|
"spark.databricks.pip.ignoreSSL": "true",
|
|
|
|
},
|
2024-01-17 14:26:33 +00:00
|
|
|
},
|
|
|
|
NotebookTask: &jobs.NotebookTask{
|
|
|
|
NotebookPath: path.Join(tmpdir, "test"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return resp.JobId
|
|
|
|
}
|
|
|
|
|
2024-02-07 11:18:56 +00:00
|
|
|
func (gt *generateJobTest) destroyJob(ctx context.Context, jobId int64) {
|
|
|
|
err := gt.w.Jobs.Delete(ctx, jobs.DeleteJob{
|
2024-01-17 14:26:33 +00:00
|
|
|
JobId: jobId,
|
|
|
|
})
|
2024-02-07 11:18:56 +00:00
|
|
|
require.NoError(gt.T, err)
|
2024-01-17 14:26:33 +00:00
|
|
|
}
|