Remove global variable from clusters integration test (#1972)

## Changes

I saw this test fail on rerun because the global wasn't set.

Fix by removing the global and using a different approach to acquire a
valid cluster ID.

## Tests

Integration tests.
This commit is contained in:
Pieter Noordhuis 2024-12-09 15:25:06 +01:00 committed by GitHub
parent 1b2be1b2cb
commit e9fa7b7c0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 25 additions and 3 deletions

View File

@ -5,11 +5,13 @@ import (
"regexp" "regexp"
"testing" "testing"
"github.com/databricks/cli/internal/acc"
"github.com/databricks/databricks-sdk-go/listing"
"github.com/databricks/databricks-sdk-go/service/compute"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
var clusterId string
func TestAccClustersList(t *testing.T) { func TestAccClustersList(t *testing.T) {
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV")) t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
@ -21,13 +23,14 @@ func TestAccClustersList(t *testing.T) {
assert.Equal(t, "", stderr.String()) assert.Equal(t, "", stderr.String())
idRegExp := regexp.MustCompile(`[0-9]{4}\-[0-9]{6}-[a-z0-9]{8}`) idRegExp := regexp.MustCompile(`[0-9]{4}\-[0-9]{6}-[a-z0-9]{8}`)
clusterId = idRegExp.FindString(outStr) clusterId := idRegExp.FindString(outStr)
assert.NotEmpty(t, clusterId) assert.NotEmpty(t, clusterId)
} }
func TestAccClustersGet(t *testing.T) { func TestAccClustersGet(t *testing.T) {
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV")) t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
clusterId := findValidClusterID(t)
stdout, stderr := RequireSuccessfulRun(t, "clusters", "get", clusterId) stdout, stderr := RequireSuccessfulRun(t, "clusters", "get", clusterId)
outStr := stdout.String() outStr := stdout.String()
assert.Contains(t, outStr, fmt.Sprintf(`"cluster_id":"%s"`, clusterId)) assert.Contains(t, outStr, fmt.Sprintf(`"cluster_id":"%s"`, clusterId))
@ -38,3 +41,22 @@ func TestClusterCreateErrorWhenNoArguments(t *testing.T) {
_, _, err := RequireErrorRun(t, "clusters", "create") _, _, err := RequireErrorRun(t, "clusters", "create")
assert.Contains(t, err.Error(), "accepts 1 arg(s), received 0") assert.Contains(t, err.Error(), "accepts 1 arg(s), received 0")
} }
// findValidClusterID lists clusters in the workspace to find a valid cluster ID.
func findValidClusterID(t *testing.T) string {
ctx, wt := acc.WorkspaceTest(t)
it := wt.W.Clusters.List(ctx, compute.ListClustersRequest{
FilterBy: &compute.ListClustersFilterBy{
ClusterSources: []compute.ClusterSource{
compute.ClusterSourceApi,
compute.ClusterSourceUi,
},
},
})
clusterIDs, err := listing.ToSliceN(ctx, it, 1)
require.NoError(t, err)
require.Len(t, clusterIDs, 1)
return clusterIDs[0].ClusterId
}