2023-07-25 14:42:53 +00:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-25 09:03:42 +00:00
|
|
|
"os"
|
2024-06-06 07:11:23 +00:00
|
|
|
"strconv"
|
2023-07-25 15:18:43 +00:00
|
|
|
"strings"
|
2023-07-25 14:42:53 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-08-25 09:03:42 +00:00
|
|
|
"github.com/databricks/cli/cmd/root"
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
|
|
|
"github.com/databricks/cli/libs/flags"
|
|
|
|
"github.com/databricks/databricks-sdk-go"
|
|
|
|
workspaceConfig "github.com/databricks/databricks-sdk-go/config"
|
2023-07-25 14:42:53 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-07-25 15:18:43 +00:00
|
|
|
func TestTemplatePrintStringWithoutProcessing(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-08-25 09:03:42 +00:00
|
|
|
ctx = root.SetWorkspaceClient(ctx, nil)
|
|
|
|
helpers := loadHelpers(ctx)
|
2024-11-20 10:11:31 +00:00
|
|
|
r, err := newRenderer(ctx, nil, helpers, os.DirFS("."), "./testdata/print-without-processing/template", "./testdata/print-without-processing/library")
|
2023-07-25 15:18:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, r.files, 1)
|
2023-08-01 13:43:27 +00:00
|
|
|
cleanContent := strings.Trim(string(r.files[0].(*inMemoryFile).content), "\n\r")
|
2023-07-25 15:18:43 +00:00
|
|
|
assert.Equal(t, `{{ fail "abc" }}`, cleanContent)
|
|
|
|
}
|
|
|
|
|
2024-12-02 10:29:29 +00:00
|
|
|
func TestTemplateBundleUuidFunction(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
ctx = root.SetWorkspaceClient(ctx, nil)
|
|
|
|
helpers := loadHelpers(ctx)
|
|
|
|
r, err := newRenderer(ctx, nil, helpers, os.DirFS("."), "./testdata/bundle-uuid/template", "./testdata/bundle-uuid/library")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-12-02 14:40:57 +00:00
|
|
|
// We test the content of two generated files to ensure that the same UUID
|
|
|
|
// is used across all files generated by the template. Each file content is
|
|
|
|
// generated by a separate (*template.Template).Execute call, so testing different
|
|
|
|
// files ensures that the UUID is stable across all template execution calls.
|
|
|
|
copy := strings.Clone(bundleUuid)
|
2024-12-02 10:29:29 +00:00
|
|
|
assert.Len(t, r.files, 2)
|
|
|
|
c1 := strings.Trim(string(r.files[0].(*inMemoryFile).content), "\n\r")
|
2024-12-02 14:40:57 +00:00
|
|
|
assert.Equal(t, strings.Repeat(copy, 3), c1)
|
2024-12-02 10:29:29 +00:00
|
|
|
c2 := strings.Trim(string(r.files[1].(*inMemoryFile).content), "\n\r")
|
2024-12-02 14:40:57 +00:00
|
|
|
assert.Equal(t, strings.Repeat(copy, 5), c2)
|
2024-12-02 10:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 14:42:53 +00:00
|
|
|
func TestTemplateRegexpCompileFunction(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-08-25 09:03:42 +00:00
|
|
|
ctx = root.SetWorkspaceClient(ctx, nil)
|
|
|
|
helpers := loadHelpers(ctx)
|
2024-11-20 10:11:31 +00:00
|
|
|
r, err := newRenderer(ctx, nil, helpers, os.DirFS("."), "./testdata/regexp-compile/template", "./testdata/regexp-compile/library")
|
2023-07-25 14:42:53 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, r.files, 1)
|
2023-08-01 13:43:27 +00:00
|
|
|
content := string(r.files[0].(*inMemoryFile).content)
|
2023-07-25 14:42:53 +00:00
|
|
|
assert.Contains(t, content, "0:food")
|
|
|
|
assert.Contains(t, content, "1:fool")
|
|
|
|
}
|
2023-07-27 09:51:31 +00:00
|
|
|
|
2024-06-06 07:11:23 +00:00
|
|
|
func TestTemplateRandIntFunction(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
ctx = root.SetWorkspaceClient(ctx, nil)
|
|
|
|
helpers := loadHelpers(ctx)
|
2024-11-20 10:11:31 +00:00
|
|
|
r, err := newRenderer(ctx, nil, helpers, os.DirFS("."), "./testdata/random-int/template", "./testdata/random-int/library")
|
2024-06-06 07:11:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, r.files, 1)
|
|
|
|
randInt, err := strconv.Atoi(strings.TrimSpace(string(r.files[0].(*inMemoryFile).content)))
|
|
|
|
assert.Less(t, randInt, 10)
|
|
|
|
assert.Empty(t, err)
|
|
|
|
}
|
|
|
|
|
2024-07-19 11:38:20 +00:00
|
|
|
func TestTemplateUuidFunction(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
ctx = root.SetWorkspaceClient(ctx, nil)
|
|
|
|
helpers := loadHelpers(ctx)
|
2024-11-20 10:11:31 +00:00
|
|
|
r, err := newRenderer(ctx, nil, helpers, os.DirFS("."), "./testdata/uuid/template", "./testdata/uuid/library")
|
2024-07-19 11:38:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, r.files, 1)
|
|
|
|
uuid := strings.TrimSpace(string(r.files[0].(*inMemoryFile).content))
|
|
|
|
assert.Regexp(t, "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", uuid)
|
|
|
|
}
|
|
|
|
|
2023-07-27 09:51:31 +00:00
|
|
|
func TestTemplateUrlFunction(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-08-25 09:03:42 +00:00
|
|
|
ctx = root.SetWorkspaceClient(ctx, nil)
|
|
|
|
helpers := loadHelpers(ctx)
|
2024-11-20 10:11:31 +00:00
|
|
|
r, err := newRenderer(ctx, nil, helpers, os.DirFS("."), "./testdata/urlparse-function/template", "./testdata/urlparse-function/library")
|
2023-07-27 09:51:31 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, r.files, 1)
|
2023-08-01 13:43:27 +00:00
|
|
|
assert.Equal(t, "https://www.databricks.com", string(r.files[0].(*inMemoryFile).content))
|
2023-07-27 09:51:31 +00:00
|
|
|
}
|
2023-08-15 16:07:22 +00:00
|
|
|
|
|
|
|
func TestTemplateMapPairFunction(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-08-25 09:03:42 +00:00
|
|
|
ctx = root.SetWorkspaceClient(ctx, nil)
|
|
|
|
helpers := loadHelpers(ctx)
|
2024-11-20 10:11:31 +00:00
|
|
|
r, err := newRenderer(ctx, nil, helpers, os.DirFS("."), "./testdata/map-pair/template", "./testdata/map-pair/library")
|
2023-08-15 16:07:22 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, r.files, 1)
|
|
|
|
assert.Equal(t, "false 123 hello 12.3", string(r.files[0].(*inMemoryFile).content))
|
|
|
|
}
|
2023-08-25 09:03:42 +00:00
|
|
|
|
|
|
|
func TestWorkspaceHost(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
w := &databricks.WorkspaceClient{
|
|
|
|
Config: &workspaceConfig.Config{
|
|
|
|
Host: "https://myhost.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ctx = root.SetWorkspaceClient(ctx, w)
|
|
|
|
|
|
|
|
helpers := loadHelpers(ctx)
|
2024-11-20 10:11:31 +00:00
|
|
|
r, err := newRenderer(ctx, nil, helpers, os.DirFS("."), "./testdata/workspace-host/template", "./testdata/map-pair/library")
|
2023-08-25 09:03:42 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, r.files, 1)
|
|
|
|
assert.Contains(t, string(r.files[0].(*inMemoryFile).content), "https://myhost.com")
|
|
|
|
assert.Contains(t, string(r.files[0].(*inMemoryFile).content), "i3.xlarge")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWorkspaceHostNotConfigured(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
Use Go SDK Iterators when listing resources with the CLI (#1202)
## Changes
Currently, when the CLI run a list API call (like list jobs), it uses
the `List*All` methods from the SDK, which list all resources in the
collection. This is very slow for large collections: if you need to list
all jobs from a workspace that has 10,000+ jobs, you'll be waiting for
at least 100 RPCs to complete before seeing any output.
Instead of using List*All() methods, the SDK recently added an iterator
data structure that allows traversing the collection without needing to
completely list it first. New pages are fetched lazily if the next
requested item belongs to the next page. Using the List() methods that
return these iterators, the CLI can proactively print out some of the
response before the complete collection has been fetched.
This involves a pretty major rewrite of the rendering logic in `cmdio`.
The idea there is to define custom rendering logic based on the type of
the provided resource. There are three renderer interfaces:
1. textRenderer: supports printing something in a textual format (i.e.
not JSON, and not templated).
2. jsonRenderer: supports printing something in a pretty-printed JSON
format.
3. templateRenderer: supports printing something using a text template.
There are also three renderer implementations:
1. readerRenderer: supports printing a reader. This only implements the
textRenderer interface.
2. iteratorRenderer: supports printing a `listing.Iterator` from the Go
SDK. This implements jsonRenderer and templateRenderer, buffering 20
resources at a time before writing them to the output.
3. defaultRenderer: supports printing arbitrary resources (the previous
implementation).
Callers will either use `cmdio.Render()` for rendering individual
resources or `io.Reader` or `cmdio.RenderIterator()` for rendering an
iterator. This separate method is needed to safely be able to match on
the type of the iterator, since Go does not allow runtime type matches
on generic types with an existential type parameter.
One other change that needs to happen is to split the templates used for
text representation of list resources into a header template and a row
template. The template is now executed multiple times for List API
calls, but the header should only be printed once. To support this, I
have added `headerTemplate` to `cmdIO`, and I have also changed
`RenderWithTemplate` to include a `headerTemplate` parameter everywhere.
## Tests
- [x] Unit tests for text rendering logic
- [x] Unit test for reflection-based iterator construction.
---------
Co-authored-by: Andrew Nester <andrew.nester@databricks.com>
2024-02-21 14:16:36 +00:00
|
|
|
cmd := cmdio.NewIO(flags.OutputJSON, strings.NewReader(""), os.Stdout, os.Stderr, "", "template")
|
2023-08-25 09:03:42 +00:00
|
|
|
ctx = cmdio.InContext(ctx, cmd)
|
|
|
|
|
|
|
|
w := &databricks.WorkspaceClient{
|
|
|
|
Config: &workspaceConfig.Config{},
|
|
|
|
}
|
|
|
|
ctx = root.SetWorkspaceClient(ctx, w)
|
|
|
|
|
|
|
|
helpers := loadHelpers(ctx)
|
2024-11-20 10:11:31 +00:00
|
|
|
r, err := newRenderer(ctx, nil, helpers, os.DirFS("."), "./testdata/workspace-host/template", "./testdata/map-pair/library")
|
2023-08-25 09:03:42 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = r.walk()
|
|
|
|
require.ErrorContains(t, err, "cannot determine target workspace")
|
|
|
|
|
|
|
|
}
|