2023-09-07 14:08:16 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"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-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)
|
2023-09-07 14:08:16 +00:00
|
|
|
cmd := cmdio.NewIO(flags.OutputJSON, strings.NewReader(""), os.Stdout, os.Stderr, "bundles")
|
|
|
|
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-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-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
|
|
|
|
}
|