databricks-cli/libs/template/resolver_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
2.9 KiB
Go
Raw Normal View History

Refactor `bundle init` (#2074) ## Summary of changes This PR introduces three new abstractions: 1. `Resolver`: Resolves which reader and writer to use for a template. 2. `Writer`: Writes a template project to disk. Prompts the user if necessary. 3. `Reader`: Reads a template specification from disk, built into the CLI or from GitHub. Introducing these abstractions helps decouple reading a template from writing it. When I tried adding telemetry for the `bundle init` command, I noticed that the code in `cmd/init.go` was getting convoluted and hard to test. A future change could have accidentally logged PII when a user initialised a custom template. Hedging against that risk is important here because we use a generic untyped `map<string, string>` representation in the backend to log telemetry for the `databricks bundle init`. Otherwise, we risk accidentally breaking our compliance with our centralization requirements. ### Details After this PR there are two classes of templates that can be initialized: 1. A `databricks` template: This could be a builtin template or a template outside the CLI like mlops-stacks, which is still owned and managed by Databricks. These templates log their telemetry arguments and template name. 2. A `custom` template: These are templates created by and managed by the end user. In these templates we do not log the template name and args. Instead a generic placeholder string of "custom" is logged in our telemetry system. NOTE: The functionality of the `databricks bundle init` command remains the same after this PR. Only the internal abstractions used are changed. ## Tests New unit tests. Existing golden and unit tests. Also a fair bit of manual testing.
2025-01-20 12:09:28 +00:00
package template
import (
"context"
"testing"
"github.com/databricks/cli/libs/cmdio"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTemplateResolverBothTagAndBranch(t *testing.T) {
r := Resolver{
Tag: "tag",
Branch: "branch",
}
_, err := r.Resolve(context.Background())
assert.EqualError(t, err, "only one of tag or branch can be specified")
}
func TestTemplateResolverErrorsWhenPromptingIsNotSupported(t *testing.T) {
r := Resolver{}
ctx := cmdio.MockDiscard(context.Background())
_, err := r.Resolve(ctx)
assert.EqualError(t, err, "prompting is not supported. Please specify the path, name or URL of the template to use")
}
func TestTemplateResolverForDefaultTemplates(t *testing.T) {
for _, name := range []string{
"default-python",
"default-sql",
"dbt-sql",
} {
t.Run(name, func(t *testing.T) {
r := Resolver{
TemplatePathOrUrl: name,
}
tmpl, err := r.Resolve(context.Background())
require.NoError(t, err)
assert.Equal(t, &builtinReader{name: name}, tmpl.Reader)
assert.IsType(t, &writerWithFullTelemetry{}, tmpl.Writer)
})
}
t.Run("mlops-stacks", func(t *testing.T) {
r := Resolver{
TemplatePathOrUrl: "mlops-stacks",
ConfigFile: "/config/file",
}
tmpl, err := r.Resolve(context.Background())
require.NoError(t, err)
// Assert reader and writer configuration
assert.Equal(t, "https://github.com/databricks/mlops-stacks", tmpl.Reader.(*gitReader).gitUrl)
assert.Equal(t, "/config/file", tmpl.Writer.(*writerWithFullTelemetry).configPath)
})
}
func TestTemplateResolverForCustomUrl(t *testing.T) {
r := Resolver{
TemplatePathOrUrl: "https://www.example.com/abc",
Tag: "tag",
TemplateDir: "/template/dir",
ConfigFile: "/config/file",
}
tmpl, err := r.Resolve(context.Background())
require.NoError(t, err)
assert.Equal(t, Custom, tmpl.name)
// Assert reader configuration
assert.Equal(t, "https://www.example.com/abc", tmpl.Reader.(*gitReader).gitUrl)
assert.Equal(t, "tag", tmpl.Reader.(*gitReader).ref)
assert.Equal(t, "/template/dir", tmpl.Reader.(*gitReader).templateDir)
// Assert writer configuration
assert.Equal(t, "/config/file", tmpl.Writer.(*defaultWriter).configPath)
}
func TestTemplateResolverForCustomPath(t *testing.T) {
r := Resolver{
TemplatePathOrUrl: "/custom/path",
ConfigFile: "/config/file",
}
tmpl, err := r.Resolve(context.Background())
require.NoError(t, err)
assert.Equal(t, Custom, tmpl.name)
// Assert reader configuration
assert.Equal(t, "/custom/path", tmpl.Reader.(*localReader).path)
// Assert writer configuration
assert.Equal(t, "/config/file", tmpl.Writer.(*defaultWriter).configPath)
}
func TestBundleInitIsRepoUrl(t *testing.T) {
assert.True(t, isRepoUrl("git@github.com:databricks/cli.git"))
assert.True(t, isRepoUrl("https://github.com/databricks/cli.git"))
assert.False(t, isRepoUrl("./local"))
assert.False(t, isRepoUrl("foo"))
}