Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize dynamic value for bundle validate output #1499

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bundle/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,9 @@ func (r Root) GetLocation(path string) dyn.Location {
}
return v.Location()
}

// Value returns the dynamic configuration value of the root object. This value
// is the source of truth and is kept in sync with values in the typed configuration.
func (r Root) Value() dyn.Value {
return r.value
}
2 changes: 1 addition & 1 deletion cmd/bundle/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func renderTextOutput(cmd *cobra.Command, b *bundle.Bundle, diags diag.Diagnosti
}

func renderJsonOutput(cmd *cobra.Command, b *bundle.Bundle, diags diag.Diagnostics) error {
buf, err := json.MarshalIndent(b.Config, "", " ")
buf, err := json.MarshalIndent(b.Config.Value().AsAny(), "", " ")
if err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions internal/bundle/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ func writeConfigFile(t *testing.T, config map[string]any) (string, error) {
return filepath, err
}

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
}

func deployBundle(t *testing.T, ctx context.Context, path string) error {
t.Setenv("BUNDLE_ROOT", path)
c := internal.NewCobraTestRunnerWithContext(t, ctx, "bundle", "deploy", "--force-lock")
Expand Down
60 changes: 60 additions & 0 deletions internal/bundle/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package bundle

import (
"context"
"encoding/json"
"testing"

"github.com/databricks/cli/internal/testutil"
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/dyn/convert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAccBundleValidate(t *testing.T) {
testutil.GetEnvOrSkipTest(t, "CLOUD_ENV")

tmpDir := t.TempDir()
testutil.TouchWithContent(t,
`
bundle:
name: "foobar"

resources:
jobs:
outer_loop:
name: outer loop
tasks:
- task_key: my task
run_job_task:
job_id: ${resources.jobs.inner_loop.id}

inner_loop:
name: inner loop

`, tmpDir, "databricks.yml")

ctx := context.Background()
stdout, err := validateBundle(t, ctx, tmpDir)
require.NoError(t, err)

config := make(map[string]any)
err = json.Unmarshal(stdout, &config)

getValue := func(key string) any {
v, err := convert.FromTyped(config, dyn.NilValue)
require.NoError(t, err)
v, err = dyn.GetByPath(v, dyn.MustPathFromString(key))
require.NoError(t, err)
return v.AsAny()
}
require.NoError(t, err)

assert.Equal(t, "foobar", getValue("bundle.name"))
assert.Equal(t, "outer loop", getValue("resources.jobs.outer_loop.name"))
assert.Equal(t, "inner loop", getValue("resources.jobs.inner_loop.name"))
assert.Equal(t, "my task", getValue("resources.jobs.outer_loop.tasks[0].task_key"))
// Assert resource references are retained in the output.
assert.Equal(t, "${resources.jobs.inner_loop.id}", getValue("resources.jobs.outer_loop.tasks[0].run_job_task.job_id"))
}
12 changes: 12 additions & 0 deletions internal/testutil/touch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ func Touch(t *testing.T, elems ...string) string {
f.Close()
return path
}

func TouchWithContent(t *testing.T, content string, elems ...string) string {
path := filepath.Join(elems...)
os.MkdirAll(filepath.Dir(path), 0755)
f, err := os.Create(path)
require.NoError(t, err)
_, err = f.WriteString(content)
require.NoError(t, err)
err = f.Close()
require.NoError(t, err)
return path
}
Loading