From 333a7cf1ea1a229bb06a8a5b497a34a902e28fc1 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Tue, 11 Mar 2025 15:36:57 +0100 Subject: [PATCH] convert TestBindExperimentToExistingExperiment to an acceptance (cloud-only) test --- acceptance/acceptance_test.go | 1 + .../deployment/bind/experiment/databricks.yml | 8 +++ .../deployment/bind/experiment/output.txt | 31 ++++++++++ .../bundle/deployment/bind/experiment/script | 41 ++++++++++++ .../deployment/bind/experiment/test.toml | 6 ++ integration/bundle/bind_resource_test.go | 62 ------------------- 6 files changed, 87 insertions(+), 62 deletions(-) create mode 100644 acceptance/bundle/deployment/bind/experiment/databricks.yml create mode 100644 acceptance/bundle/deployment/bind/experiment/output.txt create mode 100644 acceptance/bundle/deployment/bind/experiment/script create mode 100644 acceptance/bundle/deployment/bind/experiment/test.toml diff --git a/acceptance/acceptance_test.go b/acceptance/acceptance_test.go index f0feb7b39..5bcc43c9f 100644 --- a/acceptance/acceptance_test.go +++ b/acceptance/acceptance_test.go @@ -367,6 +367,7 @@ func runTest(t *testing.T, dir, coverDir string, repls testdiff.ReplacementsCont require.NoError(t, err) cmd.Env = append(cmd.Env, "TESTDIR="+absDir) cmd.Env = append(cmd.Env, "CLOUD_ENV="+cloudEnv) + cmd.Env = append(cmd.Env, "CURRENT_USER_NAME="+user.UserName) cmd.Dir = tmpDir outputPath := filepath.Join(tmpDir, "output.txt") diff --git a/acceptance/bundle/deployment/bind/experiment/databricks.yml b/acceptance/bundle/deployment/bind/experiment/databricks.yml new file mode 100644 index 000000000..c77da2d88 --- /dev/null +++ b/acceptance/bundle/deployment/bind/experiment/databricks.yml @@ -0,0 +1,8 @@ +bundle: + name: bind-ml-experiment-test-$BUNDLE_NAME_SUFFIX + +resources: + experiments: + experiment1: + name: $EXPERIMENT_NAME + diff --git a/acceptance/bundle/deployment/bind/experiment/output.txt b/acceptance/bundle/deployment/bind/experiment/output.txt new file mode 100644 index 000000000..e9c96a198 --- /dev/null +++ b/acceptance/bundle/deployment/bind/experiment/output.txt @@ -0,0 +1,31 @@ + +=== Bind experiment test: +=== Substitute variables in the template +=== Create a pre-defined experiment +=== Bind experiment: Updating deployment state... +Successfully bound databricks_mlflow_experiment with an id '[NUMID]'. Run 'bundle deploy' to deploy changes to your workspace + +=== Deploy bundle: Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/bind-ml-experiment-test-[UUID]/default/files... +Deploying resources... +Updating deployment state... +Deployment complete! + +=== Read the pre-defined experiment: { + "name": "/Users/[USERNAME]/test-experiment[UUID]", + "lifecycle_stage": "active" +} + +=== Unbind the experiment: Updating deployment state... + +=== Destroy the bundle: All files and directories at the following location will be deleted: /Workspace/Users/[USERNAME]/.bundle/bind-ml-experiment-test-[UUID]/default + +Deleting files... +Destroy complete! + +=== Read the pre-defined experiment again (expecting it still exists and is not deleted): { + "name": "/Users/[USERNAME]/test-experiment[UUID]", + "lifecycle_stage": "active" +} + +=== Test cleanup: +=== Delete the pre-defined experiment: 0 diff --git a/acceptance/bundle/deployment/bind/experiment/script b/acceptance/bundle/deployment/bind/experiment/script new file mode 100644 index 000000000..90d04471e --- /dev/null +++ b/acceptance/bundle/deployment/bind/experiment/script @@ -0,0 +1,41 @@ +title "Bind experiment test: " + +title "Substitute variables in the template" +BUNDLE_NAME_SUFFIX=$(uuid) +export BUNDLE_NAME_SUFFIX + +EXPERIMENT_NAME="/Workspace/Users/${CURRENT_USER_NAME}/test-experiment$(uuid)" +if [ -z "$CLOUD_ENV" ]; then + EXPERIMENT_NAME="/Workspace/Users/${CURRENT_USER_NAME}/test-experiment6260d50f-e8ff-4905-8f28-812345678903" # use hard-coded uuid when running locally +fi +export EXPERIMENT_NAME +envsubst < databricks.yml > out.yml && mv out.yml databricks.yml + +title "Create a pre-defined experiment" +EXPERIMENT_ID=$($CLI experiments create-experiment ${EXPERIMENT_NAME} | jq -r '.experiment_id') + +cleanupRemoveExperiment() { + title "Test cleanup: " + title "Delete the pre-defined experiment: " + $CLI experiments delete-experiment ${EXPERIMENT_ID} + echo $? +} +trap cleanupRemoveExperiment EXIT + +title "Bind experiment: " +$CLI bundle deployment bind experiment1 ${EXPERIMENT_ID} --auto-approve + +title "Deploy bundle: " +$CLI bundle deploy --force-lock --auto-approve + +title "Read the pre-defined experiment: " +$CLI experiments get-experiment ${EXPERIMENT_ID} | jq '{name: .experiment.name, lifecycle_stage: .experiment.lifecycle_stage}' + +title "Unbind the experiment: " +$CLI bundle deployment unbind experiment1 + +title "Destroy the bundle: " +$CLI bundle destroy --auto-approve + +title "Read the pre-defined experiment again (expecting it still exists and is not deleted): " +$CLI experiments get-experiment ${EXPERIMENT_ID} | jq '{name: .experiment.name, lifecycle_stage: .experiment.lifecycle_stage}' diff --git a/acceptance/bundle/deployment/bind/experiment/test.toml b/acceptance/bundle/deployment/bind/experiment/test.toml new file mode 100644 index 000000000..726c4c55d --- /dev/null +++ b/acceptance/bundle/deployment/bind/experiment/test.toml @@ -0,0 +1,6 @@ +Local = false +Cloud = true + +[[Repls]] +Old = "[0-9]{3,}" +New = "[NUMID]" \ No newline at end of file diff --git a/integration/bundle/bind_resource_test.go b/integration/bundle/bind_resource_test.go index 639826cd1..b182150ad 100644 --- a/integration/bundle/bind_resource_test.go +++ b/integration/bundle/bind_resource_test.go @@ -17,70 +17,8 @@ import ( "github.com/stretchr/testify/require" "github.com/databricks/databricks-sdk-go/service/catalog" - "github.com/databricks/databricks-sdk-go/service/ml" ) -func TestBindExperimentToExistingExperiment(t *testing.T) { - ctx, wt := acc.UcWorkspaceTest(t) - - currentUser, err := wt.W.CurrentUser.Me(ctx) - require.NoError(t, err) - - // create a pre-defined experiment: - uniqueId := uuid.New().String() - experimentName := "/Workspace/Users/" + currentUser.UserName + "/test-experiment" + uniqueId - predefinedExperiment, err := wt.W.Experiments.CreateExperiment(ctx, ml.CreateExperiment{ - Name: experimentName, - ArtifactLocation: "s3://test-location", - }) - require.NoError(t, err) - t.Cleanup(func() { - err := wt.W.Experiments.DeleteExperiment(ctx, ml.DeleteExperiment{ExperimentId: predefinedExperiment.ExperimentId}) - require.NoError(t, err) - }) - - // setup the bundle: - bundleRoot := initTestTemplate(t, ctx, "ml_experiment", map[string]any{ - "unique_id": uniqueId, - "experiment_name": experimentName, - }) - ctx = env.Set(ctx, "BUNDLE_ROOT", bundleRoot) - - // run the bind command: - c := testcli.NewRunner(t, ctx, "bundle", "deployment", "bind", "experiment1", predefinedExperiment.ExperimentId, "--auto-approve") - _, _, err = c.Run() - require.NoError(t, err) - - // deploy the bundle: - deployBundle(t, ctx, bundleRoot) - - // check that the predefinedExperiment was not re-created / deleted (it is still active): - w, err := databricks.NewWorkspaceClient() - require.NoError(t, err) - - updatedExperiment, err := w.Experiments.GetExperiment(ctx, ml.GetExperimentRequest{ - ExperimentId: predefinedExperiment.ExperimentId, - }) - require.NoError(t, err) - - require.Equal(t, "active", updatedExperiment.Experiment.LifecycleStage) - - // unbind the experiment: - c = testcli.NewRunner(t, ctx, "bundle", "deployment", "unbind", "experiment1") - _, _, err = c.Run() - require.NoError(t, err) - - // destroy the bundle: - destroyBundle(t, ctx, bundleRoot) - - // Check that experiment is unbound and exists after bundle is destroyed - postDestroyExperiment, err := w.Experiments.GetExperiment(ctx, ml.GetExperimentRequest{ - ExperimentId: predefinedExperiment.ExperimentId, - }) - require.NoError(t, err) - require.Equal(t, "active", postDestroyExperiment.Experiment.LifecycleStage) -} - func TestBindSchemaToExistingSchema(t *testing.T) { ctx, wt := acc.UcWorkspaceTest(t)