convert TestBindExperimentToExistingExperiment to an acceptance (cloud-only) test

This commit is contained in:
Anton Nekipelov 2025-03-11 15:36:57 +01:00
parent e563a0b0a3
commit 333a7cf1ea
6 changed files with 87 additions and 62 deletions

View File

@ -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")

View File

@ -0,0 +1,8 @@
bundle:
name: bind-ml-experiment-test-$BUNDLE_NAME_SUFFIX
resources:
experiments:
experiment1:
name: $EXPERIMENT_NAME

View File

@ -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

View File

@ -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}'

View File

@ -0,0 +1,6 @@
Local = false
Cloud = true
[[Repls]]
Old = "[0-9]{3,}"
New = "[NUMID]"

View File

@ -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)