From 76dd5631113ec0773a4aea945401cecaf71751d9 Mon Sep 17 00:00:00 2001 From: Akash Kanni Date: Mon, 20 May 2024 18:49:17 +0530 Subject: [PATCH] OCM-8152 | test: automated id:38816 List cluster via ROSA cli will work well --- tests/e2e/test_rosacli_list.go | 75 +++++++++++++++++++++ tests/utils/exec/rosacli/cluster_service.go | 45 ++++++++++++- 2 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 tests/e2e/test_rosacli_list.go diff --git a/tests/e2e/test_rosacli_list.go b/tests/e2e/test_rosacli_list.go new file mode 100644 index 0000000000..e2fd69392f --- /dev/null +++ b/tests/e2e/test_rosacli_list.go @@ -0,0 +1,75 @@ +package e2e + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/openshift/rosa/tests/ci/labels" + "github.com/openshift/rosa/tests/utils/config" + "github.com/openshift/rosa/tests/utils/exec/rosacli" + "github.com/openshift/rosa/tests/utils/log" +) + +var _ = Describe("List resources", + labels.Day2, + labels.FeatureMachinepool, + labels.NonHCPCluster, + func() { + defer GinkgoRecover() + var ( + clusterID string + rosaClient *rosacli.Client + ) + + BeforeEach(func() { + By("Get the cluster") + clusterID = config.GetClusterID() + Expect(clusterID).ToNot(Equal(""), "ClusterID is required. Please export CLUSTER_ID") + + By("Init the client") + rosaClient = rosacli.NewClient() + }) + + AfterEach(func() { + By("Clean remaining resources") + rosaClient.CleanResources(clusterID) + + }) + + It("List cluster via ROSA cli will work well - [id:38816]", + labels.Medium, + func() { + var ( + clusterData []string + ClusterService = rosaClient.Cluster + ) + + By("List all clusters") + clusterList, _, err := ClusterService.ListCluster() + Expect(err).To(BeNil()) + if err != nil { + log.Logger.Errorf("Failed to fetch clusters: %v", err) + return + } + + for _, it := range clusterList.ListCluster { + clusterData = append(clusterData, it.ID) + } + Expect(err).To(BeNil()) + Expect(clusterData).Should(ContainElement(clusterID)) + + clusterData = []string{} + By("List clusters with '--all' flag") + clusterList, _, err = ClusterService.ListCluster("--all") + Expect(err).To(BeNil()) + if err != nil { + log.Logger.Errorf("Failed to fetch clusters: %v", err) + return + } + for _, it := range clusterList.ListCluster { + clusterData = append(clusterData, it.ID) + } + Expect(err).To(BeNil()) + Expect(clusterData).Should(ContainElement(clusterID)) + }) + }) diff --git a/tests/utils/exec/rosacli/cluster_service.go b/tests/utils/exec/rosacli/cluster_service.go index 00fd3dd3c5..155324ce9e 100644 --- a/tests/utils/exec/rosacli/cluster_service.go +++ b/tests/utils/exec/rosacli/cluster_service.go @@ -18,7 +18,7 @@ type ClusterService interface { DescribeCluster(clusterID string) (bytes.Buffer, error) ReflectClusterDescription(result bytes.Buffer) (*ClusterDescription, error) DescribeClusterAndReflect(clusterID string) (*ClusterDescription, error) - List() (bytes.Buffer, error) + ListCluster(flags ...string) (ListCluster, bytes.Buffer, error) Create(clusterName string, flags ...string) (bytes.Buffer, error, string) DeleteCluster(clusterID string, flags ...string) (bytes.Buffer, error) CreateDryRun(clusterName string, flags ...string) (bytes.Buffer, error) @@ -49,6 +49,17 @@ func NewClusterService(client *Client) ClusterService { } } +// Struct for the 'rosa list cluster' output +type Clusters struct { + ID string `json:"ID,omitempty"` + NAME string `json:"NAME,omitempty"` + STATE string `json:"STATE,omitempty"` + TOPOLOGY string `json:"TOPOLOGY,omitempty"` +} +type ListCluster struct { + ListCluster []Clusters `json:"ListCluster,omitempty"` +} + // Struct for the 'rosa describe cluster' output type ClusterDescription struct { Name string `yaml:"Name,omitempty"` @@ -95,6 +106,34 @@ type ClusterDescription struct { ExternalAuthentication string `yaml:"External Authentication,omitempty"` } +// Pasrse the result of 'rosa list clusters' to Clusters struct +func (c *clusterService) ReflectClusterList(result bytes.Buffer) (url ListCluster, err error) { + url = ListCluster{} + theMap := c.client.Parser.TableData.Input(result).Parse().Output() + for _, ClusterItem := range theMap { + ur := &Clusters{} + err = MapStructure(ClusterItem, ur) + if err != nil { + return + } + url.ListCluster = append(url.ListCluster, *ur) + } + return +} + +// ListClusters implements OCMResourceService. +func (c *clusterService) ListCluster(flags ...string) (ListCluster, bytes.Buffer, error) { + c.client.Runner.cmdArgs = []string{} + listClusters := c.client.Runner. + Cmd("list", "clusters").CmdFlags(flags...) + output, err := listClusters.Run() + if err != nil { + return ListCluster{}, output, err + } + clusterList, err := c.ReflectClusterList(output) + return clusterList, output, err +} + func (c *clusterService) DescribeCluster(clusterID string) (bytes.Buffer, error) { describe := c.client.Runner. Cmd("describe", "cluster"). @@ -139,8 +178,8 @@ func (c *clusterService) ReflectClusterDescription(result bytes.Buffer) (res *Cl return res, err } -func (c *clusterService) List() (bytes.Buffer, error) { - list := c.client.Runner.Cmd("list", "cluster") +func (c *clusterService) List(flags ...string) (bytes.Buffer, error) { + list := c.client.Runner.Cmd("list", "cluster").CmdFlags(flags...) return list.Run() }