diff --git a/e2e/framework/exec.go b/e2e/framework/exec.go index 26915547a5..45642c116a 100644 --- a/e2e/framework/exec.go +++ b/e2e/framework/exec.go @@ -3,6 +3,7 @@ package framework import ( "bytes" "context" + "encoding/json" "fmt" "io" "os" @@ -95,3 +96,18 @@ func (f *Framework) ExecCommandCapture(ctx context.Context, args []string) (stri err := cmd.Run() return execOut.String(), execErr.String(), err } + +// ExecCommandJSON runs the devsy test binary and decodes its stdout as a JSON object. +func (f *Framework) ExecCommandJSON(ctx context.Context, args []string) (map[string]any, error) { + stdout, stderr, err := f.ExecCommandCapture(ctx, args) + if err != nil { + return nil, fmt.Errorf("devsy %s: %w (stderr: %s)", strings.Join(args, " "), err, stderr) + } + + var result map[string]any + if err := json.Unmarshal([]byte(stdout), &result); err != nil { + return nil, fmt.Errorf("output should be valid JSON: %w", err) + } + + return result, nil +} diff --git a/e2e/tests/configread/configread.go b/e2e/tests/configread/configread.go index 7f3ee6458e..8259dff18a 100644 --- a/e2e/tests/configread/configread.go +++ b/e2e/tests/configread/configread.go @@ -30,16 +30,12 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func f := framework.NewDefaultFramework(initialDir + "/bin") tempDir := framework.CopyTestdataNoChdir("tests/configread/testdata") - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--workspace-folder", tempDir, }) framework.ExpectNoError(err) - var result map[string]any - err = json.Unmarshal([]byte(stdout), &result) - framework.ExpectNoError(err, "output should be valid JSON") - gomega.Expect(result).To(gomega.HaveKey("configuration")) gomega.Expect(result).To(gomega.HaveKey("workspace")) @@ -68,17 +64,13 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func f := framework.NewDefaultFramework(initialDir + "/bin") tempDir := framework.CopyTestdataNoChdir("tests/configread/testdata") - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--workspace-folder", tempDir, "--include-features-configuration", }) framework.ExpectNoError(err) - var result map[string]any - err = json.Unmarshal([]byte(stdout), &result) - framework.ExpectNoError(err) - gomega.Expect(result).To(gomega.HaveKey("features")) features, ok := result["features"].(map[string]any) gomega.Expect(ok).To(gomega.BeTrue(), "features should be an object") @@ -92,17 +84,13 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func f := framework.NewDefaultFramework(initialDir + "/bin") tempDir := framework.CopyTestdataNoChdir("tests/configread/testdata") - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--workspace-folder", tempDir, "--include-merged-configuration", }) framework.ExpectNoError(err) - var result map[string]any - err = json.Unmarshal([]byte(stdout), &result) - framework.ExpectNoError(err) - gomega.Expect(result).To(gomega.HaveKey("mergedConfiguration")) merged, ok := result["mergedConfiguration"].(map[string]any) gomega.Expect(ok).To(gomega.BeTrue(), "mergedConfiguration should be an object") @@ -138,17 +126,13 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func f := framework.NewDefaultFramework(initialDir + "/bin") tempDir := framework.CopyTestdataNoChdir("tests/configread/testdata-port-range") - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--workspace-folder", tempDir, "--include-merged-configuration", }) framework.ExpectNoError(err) - var result map[string]any - err = json.Unmarshal([]byte(stdout), &result) - framework.ExpectNoError(err) - merged, ok := result["mergedConfiguration"].(map[string]any) gomega.Expect(ok).To(gomega.BeTrue()) @@ -176,17 +160,13 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func f := framework.NewDefaultFramework(initialDir + "/bin") tempDir := framework.CopyTestdataNoChdir("tests/configread/testdata-port-attributes") - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--workspace-folder", tempDir, "--include-merged-configuration", }) framework.ExpectNoError(err) - var result map[string]any - err = json.Unmarshal([]byte(stdout), &result) - framework.ExpectNoError(err) - merged, ok := result["mergedConfiguration"].(map[string]any) gomega.Expect(ok).To(gomega.BeTrue()) @@ -212,16 +192,12 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func f := framework.NewDefaultFramework(initialDir + "/bin") tempDir := framework.CopyTestdataNoChdir("tests/configread/testdata-container-env") - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--workspace-folder", tempDir, }) framework.ExpectNoError(err) - var result map[string]any - err = json.Unmarshal([]byte(stdout), &result) - framework.ExpectNoError(err, "output should be valid JSON") - config, ok := result["configuration"].(map[string]any) gomega.Expect(ok).To(gomega.BeTrue(), "configuration should be an object") @@ -235,17 +211,13 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func f := framework.NewDefaultFramework(initialDir + "/bin") tempDir := framework.CopyTestdataNoChdir("tests/configread/testdata-container-env") - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--workspace-folder", tempDir, "--include-merged-configuration", }) framework.ExpectNoError(err) - var result map[string]any - err = json.Unmarshal([]byte(stdout), &result) - framework.ExpectNoError(err) - merged, ok := result["mergedConfiguration"].(map[string]any) gomega.Expect(ok).To(gomega.BeTrue(), "mergedConfiguration should be an object") @@ -259,16 +231,12 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func f := framework.NewDefaultFramework(initialDir + "/bin") tempDir := framework.CopyTestdataNoChdir("tests/configread/testdata-host-requirements") - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--workspace-folder", tempDir, }) framework.ExpectNoError(err) - var result map[string]any - err = json.Unmarshal([]byte(stdout), &result) - framework.ExpectNoError(err, "output should be valid JSON") - config, ok := result["configuration"].(map[string]any) gomega.Expect(ok).To(gomega.BeTrue(), "configuration should be an object") @@ -291,17 +259,13 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func "--id-label", "devsy.readconfig.test=custom") framework.ExpectNoError(err) - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--id-label", "devsy.readconfig.test=custom", "--include-merged-configuration", }) framework.ExpectNoError(err) - var result map[string]any - err = json.Unmarshal([]byte(stdout), &result) - framework.ExpectNoError(err, "output should be valid JSON") - gomega.Expect(result).To(gomega.HaveKey("configuration")) gomega.Expect(result).To(gomega.HaveKey("mergedConfiguration")) @@ -328,16 +292,12 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func _ = cmd.Run() }) - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--container-id", containerID, }) framework.ExpectNoError(err) - var result map[string]any - err = json.Unmarshal([]byte(stdout), &result) - framework.ExpectNoError(err, "output should be valid JSON") - gomega.Expect(result).To(gomega.HaveKey("configuration")) gomega.Expect(result).To(gomega.HaveKey("workspace")) @@ -414,17 +374,13 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func _ = cmd.Run() }) - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--container-id", containerID, "--docker-path", "docker", }) framework.ExpectNoError(err) - var result map[string]any - err = json.Unmarshal([]byte(stdout), &result) - framework.ExpectNoError(err, "output should be valid JSON") - config, ok := result["configuration"].(map[string]any) gomega.Expect(ok).To(gomega.BeTrue()) gomega.Expect(config).To( @@ -450,17 +406,13 @@ var _ = ginkgo.Describe("config read command", ginkgo.Label("config-read"), func f := framework.NewDefaultFramework(initialDir + "/bin") tempDir := framework.CopyTestdataNoChdir(testdataDir) - stdout, _, err := f.ExecCommandCapture(ctx, []string{ + result, err := f.ExecCommandJSON(ctx, []string{ cmdConfig, cmdRead, "--workspace-folder", tempDir, "--include-merged-configuration", }) framework.ExpectNoError(err) - var result map[string]any - err = json.Unmarshal([]byte(stdout), &result) - framework.ExpectNoError(err) - merged, ok := result["mergedConfiguration"].(map[string]any) gomega.Expect(ok).To(gomega.BeTrue(), "mergedConfiguration should be an object") gomega.Expect(merged).To(