From 34c490f80359e06df6ffd225b02ae95cc122eb64 Mon Sep 17 00:00:00 2001 From: Dharmit Shah Date: Fri, 15 May 2020 12:28:37 +0530 Subject: [PATCH 1/2] JSON output for service list in experimental mode --- pkg/odo/cli/service/list.go | 17 +++++++++++------ .../integration/operatorhub/cmd_service_test.go | 11 ++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pkg/odo/cli/service/list.go b/pkg/odo/cli/service/list.go index cd1560d0498..efdef93a026 100644 --- a/pkg/odo/cli/service/list.go +++ b/pkg/odo/cli/service/list.go @@ -74,9 +74,16 @@ func (o *ServiceListOptions) Run() (err error) { return err } - w := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent) + if len(list) == 0 { + return fmt.Errorf("No operator backed services found in the namesapce") + } + + if log.IsJSON() { + machineoutput.OutputSuccess(list) + return + } else { + w := tabwriter.NewWriter(os.Stdout, 5, 2, 3, ' ', tabwriter.TabIndent) - if len(list) > 0 { fmt.Fprintln(w, "NAME", "\t", "TYPE", "\t", "AGE") for _, item := range list { @@ -84,11 +91,9 @@ func (o *ServiceListOptions) Run() (err error) { fmt.Fprintln(w, item.GetName(), "\t", item.GetKind(), "\t", duration) } - } else { - fmt.Fprintln(w, "No operator backed services found in the namesapce") - } + w.Flush() - w.Flush() + } return err } diff --git a/tests/integration/operatorhub/cmd_service_test.go b/tests/integration/operatorhub/cmd_service_test.go index e21a2dd9b55..dc8c0855e77 100644 --- a/tests/integration/operatorhub/cmd_service_test.go +++ b/tests/integration/operatorhub/cmd_service_test.go @@ -207,14 +207,23 @@ spec: Expect(stdOut).To(ContainSubstring("example")) Expect(stdOut).To(ContainSubstring("EtcdCluster")) + // now check for json output + jsonOut := helper.CmdShouldPass("odo", "service", "list", "-o", "json") + Expect(jsonOut).To(ContainSubstring("\"apiVersion\": \"etcd.database.coreos.com/v1beta2\"")) + Expect(jsonOut).To(ContainSubstring("\"kind\": \"EtcdCluster\"")) + Expect(jsonOut).To(ContainSubstring("\"name\": \"example3\"")) + // Delete the pods created. This should idealy be done by `odo // service delete` but that's not implemented for operator backed // services yet. helper.CmdShouldPass("oc", "delete", "EtcdCluster", "example3") // Now let's check the output again to ensure expected behaviour - stdOut = helper.CmdShouldPass("odo", "service", "list") + stdOut = helper.CmdShouldFail("odo", "service", "list") + jsonOut = helper.CmdShouldFail("odo", "service", "list", "-o", "json") Expect(stdOut).To(ContainSubstring("No operator backed services found in the namesapce")) + Expect(jsonOut).To(ContainSubstring("No operator backed services found in the namesapce")) + Expect(jsonOut).To(ContainSubstring("\"message\": \"No operator backed services found in the namesapce\"")) }) }) }) From 9a78b7399511dec0abccb08b4463682e3cf4b845 Mon Sep 17 00:00:00 2001 From: Dharmit Shah Date: Fri, 15 May 2020 14:14:56 +0530 Subject: [PATCH 2/2] Changes to tests as per PR feedback https://github.com/openshift/odo/pull/3186#pullrequestreview-412446173 --- tests/integration/operatorhub/cmd_service_test.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/integration/operatorhub/cmd_service_test.go b/tests/integration/operatorhub/cmd_service_test.go index dc8c0855e77..2341f457388 100644 --- a/tests/integration/operatorhub/cmd_service_test.go +++ b/tests/integration/operatorhub/cmd_service_test.go @@ -209,9 +209,7 @@ spec: // now check for json output jsonOut := helper.CmdShouldPass("odo", "service", "list", "-o", "json") - Expect(jsonOut).To(ContainSubstring("\"apiVersion\": \"etcd.database.coreos.com/v1beta2\"")) - Expect(jsonOut).To(ContainSubstring("\"kind\": \"EtcdCluster\"")) - Expect(jsonOut).To(ContainSubstring("\"name\": \"example3\"")) + helper.MatchAllInOutput(jsonOut, []string{"\"apiVersion\": \"etcd.database.coreos.com/v1beta2\"", "\"kind\": \"EtcdCluster\"", "\"name\": \"example3\""}) // Delete the pods created. This should idealy be done by `odo // service delete` but that's not implemented for operator backed @@ -222,8 +220,7 @@ spec: stdOut = helper.CmdShouldFail("odo", "service", "list") jsonOut = helper.CmdShouldFail("odo", "service", "list", "-o", "json") Expect(stdOut).To(ContainSubstring("No operator backed services found in the namesapce")) - Expect(jsonOut).To(ContainSubstring("No operator backed services found in the namesapce")) - Expect(jsonOut).To(ContainSubstring("\"message\": \"No operator backed services found in the namesapce\"")) + helper.MatchAllInOutput(jsonOut, []string{"No operator backed services found in the namesapce", "\"message\": \"No operator backed services found in the namesapce\""}) }) }) })