2024-01-04 21:04:42 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-19 14:12:58 +00:00
|
|
|
"fmt"
|
2024-01-04 21:04:42 +00:00
|
|
|
"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": {
|
|
|
|
Value: &justString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-19 14:12:58 +00:00
|
|
|
m := mocks.NewMockWorkspaceClient(t)
|
|
|
|
b.SetWorkpaceClient(m.WorkspaceClient)
|
|
|
|
clusterApi := m.GetMockClustersAPI()
|
|
|
|
clusterApi.EXPECT().GetByClusterName(mock.Anything, clusterRef1).Return(&compute.ClusterDetails{
|
|
|
|
ClusterId: "1234-5678-abcd",
|
|
|
|
}, nil)
|
|
|
|
clusterApi.EXPECT().GetByClusterName(mock.Anything, clusterRef2).Return(&compute.ClusterDetails{
|
|
|
|
ClusterId: "9876-5432-xywz",
|
|
|
|
}, 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-01-04 21:04:42 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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": {
|
|
|
|
Value: &justString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-19 14:12:58 +00:00
|
|
|
m := mocks.NewMockWorkspaceClient(t)
|
|
|
|
b.SetWorkpaceClient(m.WorkspaceClient)
|
|
|
|
clusterApi := m.GetMockClustersAPI()
|
|
|
|
clusterApi.EXPECT().GetByClusterName(mock.Anything, clusterRef).Return(nil, fmt.Errorf("ClusterDetails named '%s' does not exist", clusterRef))
|
2024-01-04 21:04:42 +00:00
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, ResolveResourceReferences())
|
|
|
|
require.ErrorContains(t, diags.Error(), "failed to resolve cluster: Random, err: ClusterDetails 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-01-04 21:04:42 +00:00
|
|
|
b.Config.Variables["my-cluster-id"].Set("random value")
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := bundle.Apply(context.Background(), b, ResolveResourceReferences())
|
|
|
|
require.NoError(t, diags.Error())
|
2024-01-04 21:04:42 +00:00
|
|
|
require.Equal(t, "random value", *b.Config.Variables["my-cluster-id"].Value)
|
|
|
|
}
|
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-03-04 16:12:10 +00:00
|
|
|
require.Equal(t, "app-1234", *b.Config.Variables["my-sp"].Value)
|
|
|
|
}
|
2024-04-18 09:56:16 +00:00
|
|
|
|
|
|
|
func TestResolveVariableReferencesInVariableLookups(t *testing.T) {
|
|
|
|
s := func(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Target: "dev",
|
|
|
|
},
|
|
|
|
Variables: map[string]*variable.Variable{
|
|
|
|
"foo": {
|
|
|
|
Value: s("bar"),
|
|
|
|
},
|
|
|
|
"lookup": {
|
|
|
|
Lookup: &variable.Lookup{
|
|
|
|
Cluster: "cluster-${var.foo}-${bundle.target}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mocks.NewMockWorkspaceClient(t)
|
|
|
|
b.SetWorkpaceClient(m.WorkspaceClient)
|
|
|
|
clusterApi := m.GetMockClustersAPI()
|
|
|
|
clusterApi.EXPECT().GetByClusterName(mock.Anything, "cluster-bar-dev").Return(&compute.ClusterDetails{
|
|
|
|
ClusterId: "1234-5678-abcd",
|
|
|
|
}, 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)
|
|
|
|
require.Equal(t, "1234-5678-abcd", *b.Config.Variables["lookup"].Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
s := func(s string) *string {
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &bundle.Bundle{
|
|
|
|
Config: config.Root{
|
|
|
|
Bundle: config.Bundle{
|
|
|
|
Target: "dev",
|
|
|
|
},
|
|
|
|
Variables: map[string]*variable.Variable{
|
|
|
|
"foo": {
|
|
|
|
Value: s("bar"),
|
|
|
|
},
|
|
|
|
"lookup": {
|
|
|
|
Lookup: &variable.Lookup{
|
|
|
|
Cluster: "cluster-${var.foo}-${bundle.target}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
require.Equal(t, "1234-5678-abcd", *b.Config.Variables["lookup"].Value)
|
|
|
|
}
|