2024-01-04 21:04:42 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/bundle/config/variable"
|
2024-06-19 08:03:06 +00:00
|
|
|
"github.com/databricks/cli/libs/env"
|
2024-01-19 14:12:58 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2024-01-04 21:04:42 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2024-01-19 14:12:58 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/experimental/mocks"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/compute"
|
2024-03-04 16:12:10 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/service/iam"
|
2024-01-19 14:12:58 +00:00
|
|
|
)
|
2024-01-04 21:04:42 +00:00
|
|
|
|
|
|
|
func TestResolveClusterReference(t *testing.T) {
|
|
|
|
clusterRef1 := "Some Custom Cluster"
|
|
|
|
clusterRef2 := "Some Other Name"
|
|
|
|
justString := "random string"
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Variables: map[string]*variable.Variable{
|
|
|
|
"my-cluster-id-1": {
|
|
|
|
Lookup: &variable.Lookup{
|
|
|
|
Cluster: clusterRef1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"my-cluster-id-2": {
|
|
|
|
Lookup: &variable.Lookup{
|
|
|
|
Cluster: clusterRef2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"some-variable": {
|
2024-06-26 10:25:32 +00:00
|
|
|
Value: justString,
|
2024-01-04 21:04:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-19 14:12:58 +00:00
|
|
|
m := mocks.NewMockWorkspaceClient(t)
|
|
|
|
b.SetWorkpaceClient(m.WorkspaceClient)
|
|
|
|
clusterApi := m.GetMockClustersAPI()
|
2024-09-06 11:34:57 +00:00
|
|
|
clusterApi.EXPECT().ListAll(mock.Anything, compute.ListClustersRequest{
|
|
|
|
FilterBy: &compute.ListClustersFilterBy{
|
|
|
|
ClusterSources: []compute.ClusterSource{compute.ClusterSourceApi, compute.ClusterSourceUi},
|
|
|
|
},
|
|
|
|
}).Return([]compute.ClusterDetails{
|
|
|
|
{ClusterId: "1234-5678-abcd", ClusterName: clusterRef1},
|
|
|
|
{ClusterId: "9876-5432-xywz", ClusterName: clusterRef2},
|
2024-01-19 14:12:58 +00:00
|
|
|
}, nil)
|
2024-01-04 21:04:42 +00:00
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, ResolveResourceReferences())
|
|
|
|
require.NoError(t, diags.Error())
|
2024-06-26 10:25:32 +00:00
|
|
|
require.Equal(t, "1234-5678-abcd", b.Config.Variables["my-cluster-id-1"].Value)
|
|
|
|
require.Equal(t, "9876-5432-xywz", b.Config.Variables["my-cluster-id-2"].Value)
|
2024-01-04 21:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveNonExistentClusterReference(t *testing.T) {
|
|
|
|
clusterRef := "Random"
|
|
|
|
justString := "random string"
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Variables: map[string]*variable.Variable{
|
|
|
|
"my-cluster-id": {
|
|
|
|
Lookup: &variable.Lookup{
|
|
|
|
Cluster: clusterRef,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"some-variable": {
|
2024-06-26 10:25:32 +00:00
|
|
|
Value: justString,
|
2024-01-04 21:04:42 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-19 14:12:58 +00:00
|
|
|
m := mocks.NewMockWorkspaceClient(t)
|
|
|
|
b.SetWorkpaceClient(m.WorkspaceClient)
|
|
|
|
clusterApi := m.GetMockClustersAPI()
|
2024-09-06 11:34:57 +00:00
|
|
|
clusterApi.EXPECT().ListAll(mock.Anything, compute.ListClustersRequest{
|
|
|
|
FilterBy: &compute.ListClustersFilterBy{
|
|
|
|
ClusterSources: []compute.ClusterSource{compute.ClusterSourceApi, compute.ClusterSourceUi},
|
|
|
|
},
|
|
|
|
}).Return([]compute.ClusterDetails{
|
|
|
|
{ClusterId: "1234-5678-abcd", ClusterName: "some other cluster"},
|
|
|
|
}, nil)
|
2024-01-04 21:04:42 +00:00
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, ResolveResourceReferences())
|
2024-09-06 11:34:57 +00:00
|
|
|
require.ErrorContains(t, diags.Error(), "failed to resolve cluster: Random, err: cluster named 'Random' does not exist")
|
2024-01-04 21:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoLookupIfVariableIsSet(t *testing.T) {
|
|
|
|
clusterRef := "donotexist"
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Variables: map[string]*variable.Variable{
|
|
|
|
"my-cluster-id": {
|
|
|
|
Lookup: &variable.Lookup{
|
|
|
|
Cluster: clusterRef,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-19 14:12:58 +00:00
|
|
|
m := mocks.NewMockWorkspaceClient(t)
|
|
|
|
b.SetWorkpaceClient(m.WorkspaceClient)
|
|
|
|
|
2024-12-09 12:56:41 +00:00
|
|
|
err := b.Config.Variables["my-cluster-id"].Set("random value")
|
|
|
|
require.NoError(t, err)
|
2024-01-04 21:04:42 +00:00
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, ResolveResourceReferences())
|
|
|
|
require.NoError(t, diags.Error())
|
2024-06-26 10:25:32 +00:00
|
|
|
require.Equal(t, "random value", b.Config.Variables["my-cluster-id"].Value)
|
2024-01-04 21:04:42 +00:00
|
|
|
}
|
2024-03-04 16:12:10 +00:00
|
|
|
|
|
|
|
func TestResolveServicePrincipal(t *testing.T) {
|
|
|
|
spName := "Some SP name"
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Variables: map[string]*variable.Variable{
|
|
|
|
"my-sp": {
|
|
|
|
Lookup: &variable.Lookup{
|
|
|
|
ServicePrincipal: spName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mocks.NewMockWorkspaceClient(t)
|
|
|
|
b.SetWorkpaceClient(m.WorkspaceClient)
|
|
|
|
spApi := m.GetMockServicePrincipalsAPI()
|
|
|
|
spApi.EXPECT().GetByDisplayName(mock.Anything, spName).Return(&iam.ServicePrincipal{
|
|
|
|
Id: "1234",
|
|
|
|
ApplicationId: "app-1234",
|
|
|
|
}, nil)
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, ResolveResourceReferences())
|
|
|
|
require.NoError(t, diags.Error())
|
2024-06-26 10:25:32 +00:00
|
|
|
require.Equal(t, "app-1234", b.Config.Variables["my-sp"].Value)
|
2024-03-04 16:12:10 +00:00
|
|
|
}
|
2024-04-18 09:56:16 +00:00
|
|
|
|
|
|
|
func TestResolveVariableReferencesInVariableLookups(t *testing.T) {
|
2024-06-26 10:25:32 +00:00
|
|
|
s := "bar"
|
2024-04-18 09:56:16 +00:00
|
|
|
b := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Target: "dev",
|
|
|
|
},
|
|
|
|
Variables: map[string]*variable.Variable{
|
|
|
|
"foo": {
|
2024-06-26 10:25:32 +00:00
|
|
|
Value: s,
|
2024-04-18 09:56:16 +00:00
|
|
|
},
|
|
|
|
"lookup": {
|
|
|
|
Lookup: &variable.Lookup{
|
|
|
|
Cluster: "cluster-${var.foo}-${bundle.target}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mocks.NewMockWorkspaceClient(t)
|
|
|
|
b.SetWorkpaceClient(m.WorkspaceClient)
|
|
|
|
clusterApi := m.GetMockClustersAPI()
|
2024-09-06 11:34:57 +00:00
|
|
|
|
|
|
|
clusterApi.EXPECT().ListAll(mock.Anything, compute.ListClustersRequest{
|
|
|
|
FilterBy: &compute.ListClustersFilterBy{
|
|
|
|
ClusterSources: []compute.ClusterSource{compute.ClusterSourceApi, compute.ClusterSourceUi},
|
|
|
|
},
|
|
|
|
}).Return([]compute.ClusterDetails{
|
|
|
|
{ClusterId: "1234-5678-abcd", ClusterName: "cluster-bar-dev"},
|
|
|
|
{ClusterId: "9876-5432-xywz", ClusterName: "some other cluster"},
|
2024-04-18 09:56:16 +00:00
|
|
|
}, nil)
|
|
|
|
|
|
|
|
diags := bundle.Apply(context.Background(), b, bundle.Seq(ResolveVariableReferencesInLookup(), ResolveResourceReferences()))
|
|
|
|
require.NoError(t, diags.Error())
|
|
|
|
require.Equal(t, "cluster-bar-dev", b.Config.Variables["lookup"].Lookup.Cluster)
|
2024-06-26 10:25:32 +00:00
|
|
|
require.Equal(t, "1234-5678-abcd", b.Config.Variables["lookup"].Value)
|
2024-04-18 09:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveLookupVariableReferencesInVariableLookups(t *testing.T) {
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Variables: map[string]*variable.Variable{
|
|
|
|
"another_lookup": {
|
|
|
|
Lookup: &variable.Lookup{
|
|
|
|
Cluster: "cluster",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"lookup": {
|
|
|
|
Lookup: &variable.Lookup{
|
|
|
|
Cluster: "cluster-${var.another_lookup}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mocks.NewMockWorkspaceClient(t)
|
|
|
|
b.SetWorkpaceClient(m.WorkspaceClient)
|
|
|
|
|
|
|
|
diags := bundle.Apply(context.Background(), b, bundle.Seq(ResolveVariableReferencesInLookup(), ResolveResourceReferences()))
|
|
|
|
require.ErrorContains(t, diags.Error(), "lookup variables cannot contain references to another lookup variables")
|
|
|
|
}
|
2024-06-19 08:03:06 +00:00
|
|
|
|
|
|
|
func TestNoResolveLookupIfVariableSetWithEnvVariable(t *testing.T) {
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Target: "dev",
|
|
|
|
},
|
|
|
|
Variables: map[string]*variable.Variable{
|
|
|
|
"lookup": {
|
|
|
|
Lookup: &variable.Lookup{
|
2024-06-26 10:25:32 +00:00
|
|
|
Cluster: "cluster-${bundle.target}",
|
2024-06-19 08:03:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mocks.NewMockWorkspaceClient(t)
|
|
|
|
b.SetWorkpaceClient(m.WorkspaceClient)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = env.Set(ctx, "BUNDLE_VAR_lookup", "1234-5678-abcd")
|
|
|
|
|
|
|
|
diags := bundle.Apply(ctx, b, bundle.Seq(SetVariables(), ResolveVariableReferencesInLookup(), ResolveResourceReferences()))
|
|
|
|
require.NoError(t, diags.Error())
|
2024-06-26 10:25:32 +00:00
|
|
|
require.Equal(t, "1234-5678-abcd", b.Config.Variables["lookup"].Value)
|
2024-06-19 08:03:06 +00:00
|
|
|
}
|