2023-09-07 14:08:16 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2024-03-18 14:41:58 +00:00
|
|
|
"fmt"
|
2023-09-07 14:08:16 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/cmd/root"
|
|
|
|
"github.com/databricks/cli/internal"
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
|
|
|
"github.com/databricks/cli/libs/flags"
|
|
|
|
"github.com/databricks/cli/libs/template"
|
2024-03-18 14:41:58 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-07 14:08:16 +00:00
|
|
|
)
|
|
|
|
|
2024-04-19 11:31:54 +00:00
|
|
|
const defaultSparkVersion = "13.3.x-snapshot-scala2.12"
|
|
|
|
|
2024-02-07 11:18:56 +00:00
|
|
|
func initTestTemplate(t *testing.T, ctx context.Context, templateName string, config map[string]any) (string, error) {
|
2023-09-07 14:08:16 +00:00
|
|
|
templateRoot := filepath.Join("bundles", templateName)
|
|
|
|
|
|
|
|
bundleRoot := t.TempDir()
|
|
|
|
configFilePath, err := writeConfigFile(t, config)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-02-07 11:18:56 +00:00
|
|
|
ctx = root.SetWorkspaceClient(ctx, nil)
|
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, "", "bundles")
|
2023-09-07 14:08:16 +00:00
|
|
|
ctx = cmdio.InContext(ctx, cmd)
|
|
|
|
|
|
|
|
err = template.Materialize(ctx, configFilePath, templateRoot, bundleRoot)
|
|
|
|
return bundleRoot, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeConfigFile(t *testing.T, config map[string]any) (string, error) {
|
|
|
|
bytes, err := json.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
filepath := filepath.Join(dir, "config.json")
|
|
|
|
t.Log("Configuration for template: ", string(bytes))
|
|
|
|
|
|
|
|
err = os.WriteFile(filepath, bytes, 0644)
|
|
|
|
return filepath, err
|
|
|
|
}
|
|
|
|
|
2024-06-18 15:04:20 +00:00
|
|
|
func validateBundle(t *testing.T, ctx context.Context, path string) ([]byte, error) {
|
|
|
|
t.Setenv("BUNDLE_ROOT", path)
|
|
|
|
c := internal.NewCobraTestRunnerWithContext(t, ctx, "bundle", "validate", "--output", "json")
|
|
|
|
stdout, _, err := c.Run()
|
|
|
|
return stdout.Bytes(), err
|
|
|
|
}
|
|
|
|
|
2024-02-07 11:18:56 +00:00
|
|
|
func deployBundle(t *testing.T, ctx context.Context, path string) error {
|
2023-09-07 14:08:16 +00:00
|
|
|
t.Setenv("BUNDLE_ROOT", path)
|
2024-02-07 11:18:56 +00:00
|
|
|
c := internal.NewCobraTestRunnerWithContext(t, ctx, "bundle", "deploy", "--force-lock")
|
2023-09-07 14:08:16 +00:00
|
|
|
_, _, err := c.Run()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-18 15:39:18 +00:00
|
|
|
func deployBundleWithFlags(t *testing.T, ctx context.Context, path string, flags []string) error {
|
|
|
|
t.Setenv("BUNDLE_ROOT", path)
|
|
|
|
args := []string{"bundle", "deploy", "--force-lock"}
|
|
|
|
args = append(args, flags...)
|
|
|
|
c := internal.NewCobraTestRunnerWithContext(t, ctx, args...)
|
|
|
|
_, _, err := c.Run()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-07 11:18:56 +00:00
|
|
|
func runResource(t *testing.T, ctx context.Context, path string, key string) (string, error) {
|
2023-09-07 14:08:16 +00:00
|
|
|
ctx = cmdio.NewContext(ctx, cmdio.Default())
|
|
|
|
|
|
|
|
c := internal.NewCobraTestRunnerWithContext(t, ctx, "bundle", "run", key)
|
|
|
|
stdout, _, err := c.Run()
|
|
|
|
return stdout.String(), err
|
|
|
|
}
|
|
|
|
|
2024-02-07 11:18:56 +00:00
|
|
|
func runResourceWithParams(t *testing.T, ctx context.Context, path string, key string, params ...string) (string, error) {
|
2023-12-01 10:35:20 +00:00
|
|
|
ctx = cmdio.NewContext(ctx, cmdio.Default())
|
|
|
|
|
|
|
|
args := make([]string, 0)
|
|
|
|
args = append(args, "bundle", "run", key)
|
|
|
|
args = append(args, params...)
|
|
|
|
c := internal.NewCobraTestRunnerWithContext(t, ctx, args...)
|
|
|
|
stdout, _, err := c.Run()
|
|
|
|
return stdout.String(), err
|
|
|
|
}
|
|
|
|
|
2024-02-07 11:18:56 +00:00
|
|
|
func destroyBundle(t *testing.T, ctx context.Context, path string) error {
|
2023-09-07 14:08:16 +00:00
|
|
|
t.Setenv("BUNDLE_ROOT", path)
|
2024-02-07 11:18:56 +00:00
|
|
|
c := internal.NewCobraTestRunnerWithContext(t, ctx, "bundle", "destroy", "--auto-approve")
|
2023-09-07 14:08:16 +00:00
|
|
|
_, _, err := c.Run()
|
|
|
|
return err
|
|
|
|
}
|
2024-03-18 14:41:58 +00:00
|
|
|
|
|
|
|
func getBundleRemoteRootPath(w *databricks.WorkspaceClient, t *testing.T, uniqueId string) string {
|
|
|
|
// Compute root path for the bundle deployment
|
|
|
|
me, err := w.CurrentUser.Me(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
root := fmt.Sprintf("/Users/%s/.bundle/%s", me.UserName, uniqueId)
|
|
|
|
return root
|
|
|
|
}
|