databricks-cli/internal/init_test.go

89 lines
2.2 KiB
Go
Raw Normal View History

package internal
import (
"context"
"os"
"path/filepath"
"strconv"
"testing"
"github.com/databricks/cli/libs/auth"
"github.com/databricks/databricks-sdk-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAccBundleInitErrorOnUnknownFields(t *testing.T) {
t.Log(GetEnvOrSkipTest(t, "CLOUD_ENV"))
tmpDir := t.TempDir()
_, _, err := RequireErrorRun(t, "bundle", "init", "./testdata/init/field-does-not-exist", "--output-dir", tmpDir)
assert.EqualError(t, err, "failed to compute file content for bar.tmpl. variable \"does_not_exist\" not defined")
}
func TestAccBundleInitHelpers(t *testing.T) {
env := GetEnvOrSkipTest(t, "CLOUD_ENV")
t.Log(env)
w, err := databricks.NewWorkspaceClient(&databricks.Config{})
require.NoError(t, err)
me, err := w.CurrentUser.Me(context.Background())
require.NoError(t, err)
var smallestNode string
switch env {
case "azure":
smallestNode = "Standard_D3_v2"
case "gcp":
smallestNode = "n1-standard-4"
default:
smallestNode = "i3.xlarge"
}
tests := []struct {
funcName string
expected string
}{
{
funcName: "{{short_name}}",
expected: auth.GetShortUserName(me.UserName),
},
{
funcName: "{{user_name}}",
expected: me.UserName,
},
{
funcName: "{{workspace_host}}",
expected: w.Config.Host,
},
{
funcName: "{{is_service_principal}}",
expected: strconv.FormatBool(auth.IsServicePrincipal(me.Id)),
},
{
funcName: "{{smallest_node_type}}",
expected: smallestNode,
},
}
for _, test := range tests {
// Setup template to test the helper function.
tmpDir := t.TempDir()
tmpDir2 := t.TempDir()
err := os.Mkdir(filepath.Join(tmpDir, "template"), 0755)
require.NoError(t, err)
err = os.WriteFile(filepath.Join(tmpDir, "template", "foo.txt.tmpl"), []byte(test.funcName), 0644)
require.NoError(t, err)
err = os.WriteFile(filepath.Join(tmpDir, "databricks_template_schema.json"), []byte("{}"), 0644)
require.NoError(t, err)
// Run bundle init.
RequireSuccessfulRun(t, "bundle", "init", tmpDir, "--output-dir", tmpDir2)
// Assert that the helper function was correctly computed.
assertLocalFileContents(t, filepath.Join(tmpDir2, "foo.txt"), test.expected)
}
}