From 36e09f18bb665aadc0cb088936180f9de84d2008 Mon Sep 17 00:00:00 2001 From: Ashley Cui Date: Thu, 17 Aug 2023 09:38:17 -0400 Subject: [PATCH] Update machine list test check if --format json returns valid json Signed-off-by: Ashley Cui --- pkg/machine/e2e/config_test.go | 31 +++++++++++++++++++++++++++++++ pkg/machine/e2e/list_test.go | 1 + 2 files changed, 32 insertions(+) diff --git a/pkg/machine/e2e/config_test.go b/pkg/machine/e2e/config_test.go index 157bfc57f7..06f9ca9f0b 100644 --- a/pkg/machine/e2e/config_test.go +++ b/pkg/machine/e2e/config_test.go @@ -14,7 +14,9 @@ import ( "github.com/containers/storage/pkg/stringid" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "github.com/onsi/gomega/format" . "github.com/onsi/gomega/gexec" + "github.com/onsi/gomega/types" ) var originalHomeDir = os.Getenv("HOME") @@ -170,3 +172,32 @@ func runWrapper(podmanBinary string, cmdArgs []string, timeout time.Duration, wa func randomString() string { return stringid.GenerateRandomID()[0:12] } + +type ValidJSONMatcher struct { + types.GomegaMatcher +} + +func BeValidJSON() *ValidJSONMatcher { + return &ValidJSONMatcher{} +} + +func (matcher *ValidJSONMatcher) Match(actual interface{}) (success bool, err error) { + s, ok := actual.(string) + if !ok { + return false, fmt.Errorf("ValidJSONMatcher expects a string, not %q", actual) + } + + var i interface{} + if err := json.Unmarshal([]byte(s), &i); err != nil { + return false, err + } + return true, nil +} + +func (matcher *ValidJSONMatcher) FailureMessage(actual interface{}) (message string) { + return format.Message(actual, "to be valid JSON") +} + +func (matcher *ValidJSONMatcher) NegatedFailureMessage(actual interface{}) (message string) { + return format.Message(actual, "to _not_ be valid JSON") +} diff --git a/pkg/machine/e2e/list_test.go b/pkg/machine/e2e/list_test.go index 4343fd3556..272ddf692c 100644 --- a/pkg/machine/e2e/list_test.go +++ b/pkg/machine/e2e/list_test.go @@ -133,6 +133,7 @@ var _ = Describe("podman machine list", func() { listSession2, err := mb.setCmd(list2).run() Expect(err).ToNot(HaveOccurred()) Expect(listSession2).To(Exit(0)) + Expect(listSession2.outputToString()).To(BeValidJSON()) var listResponse []*entities.ListReporter err = jsoniter.Unmarshal(listSession2.Bytes(), &listResponse)