2024-12-13 14:38:58 +00:00
|
|
|
package clusters_test
|
2023-05-26 12:46:08 +00:00
|
|
|
|
|
|
|
import (
|
2024-12-16 11:34:37 +00:00
|
|
|
"context"
|
2023-05-26 12:46:08 +00:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
2024-12-17 07:45:58 +00:00
|
|
|
"github.com/databricks/cli/integration/internal/acc"
|
2024-12-12 16:48:51 +00:00
|
|
|
"github.com/databricks/cli/internal/testcli"
|
2024-12-09 14:25:06 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/listing"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/compute"
|
2023-05-26 12:46:08 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-12-09 14:25:06 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-05-26 12:46:08 +00:00
|
|
|
)
|
|
|
|
|
2024-12-13 14:47:50 +00:00
|
|
|
func TestClustersList(t *testing.T) {
|
2024-12-16 11:34:37 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
stdout, stderr := testcli.RequireSuccessfulRun(t, ctx, "clusters", "list")
|
2023-05-26 12:46:08 +00:00
|
|
|
outStr := stdout.String()
|
|
|
|
assert.Contains(t, outStr, "ID")
|
|
|
|
assert.Contains(t, outStr, "Name")
|
|
|
|
assert.Contains(t, outStr, "State")
|
|
|
|
assert.Equal(t, "", stderr.String())
|
|
|
|
|
|
|
|
idRegExp := regexp.MustCompile(`[0-9]{4}\-[0-9]{6}-[a-z0-9]{8}`)
|
2024-12-09 14:25:06 +00:00
|
|
|
clusterId := idRegExp.FindString(outStr)
|
2023-05-26 12:46:08 +00:00
|
|
|
assert.NotEmpty(t, clusterId)
|
|
|
|
}
|
|
|
|
|
2024-12-13 14:47:50 +00:00
|
|
|
func TestClustersGet(t *testing.T) {
|
2024-12-16 11:34:37 +00:00
|
|
|
ctx := context.Background()
|
2024-12-09 14:25:06 +00:00
|
|
|
clusterId := findValidClusterID(t)
|
2024-12-16 11:34:37 +00:00
|
|
|
stdout, stderr := testcli.RequireSuccessfulRun(t, ctx, "clusters", "get", clusterId)
|
2023-05-26 12:46:08 +00:00
|
|
|
outStr := stdout.String()
|
|
|
|
assert.Contains(t, outStr, fmt.Sprintf(`"cluster_id":"%s"`, clusterId))
|
|
|
|
assert.Equal(t, "", stderr.String())
|
|
|
|
}
|
2023-06-05 17:38:45 +00:00
|
|
|
|
|
|
|
func TestClusterCreateErrorWhenNoArguments(t *testing.T) {
|
2024-12-16 11:34:37 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
_, _, err := testcli.RequireErrorRun(t, ctx, "clusters", "create")
|
2024-03-12 14:12:34 +00:00
|
|
|
assert.Contains(t, err.Error(), "accepts 1 arg(s), received 0")
|
2023-06-05 17:38:45 +00:00
|
|
|
}
|
2024-12-09 14:25:06 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|