From b14ad7e287958e0dd5fd8a3020b345e009ecfc85 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 | 76 +++++++++++++++++++ .../exec/rosacli/ocm_resource_service.go | 40 ++++++++++ 2 files changed, 116 insertions(+) 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..7b7572eb19 --- /dev/null +++ b/tests/e2e/test_rosacli_list.go @@ -0,0 +1,76 @@ +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 + ocmResourceService rosacli.OCMResourceService + ) + + 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() + ocmResourceService = rosaClient.OCMResource + }) + + AfterEach(func() { + By("Clean remaining resources") + rosaClient.CleanResources(clusterID) + + }) + + It("can create/describe/delete admin user - [id:38816]", + labels.Medium, + func() { + var ( + clusterData []string + ) + + By("List all clusters") + clusterList, _, err := ocmResourceService.ListClusters() + Expect(err).To(BeNil()) + if err != nil { + log.Logger.Errorf("Failed to fetch clusters: %v", err) + return + } + + for _, it := range clusterList.ClusterList { + clusterData = append(clusterData, it.ID) + } + Expect(err).To(BeNil()) + Expect(clusterData).Should(ContainElement(clusterID)) + + clusterData = []string{} + By("List clusters with '--all' flag") + clusterList, _, err = ocmResourceService.ListClusters("--all") + Expect(err).To(BeNil()) + if err != nil { + log.Logger.Errorf("Failed to fetch clusters: %v", err) + return + } + for _, it := range clusterList.ClusterList { + clusterData = append(clusterData, it.ID) + } + Expect(err).To(BeNil()) + Expect(clusterData).Should(ContainElement(clusterID)) + }) + }) diff --git a/tests/utils/exec/rosacli/ocm_resource_service.go b/tests/utils/exec/rosacli/ocm_resource_service.go index 41b34bf1fe..5868392435 100644 --- a/tests/utils/exec/rosacli/ocm_resource_service.go +++ b/tests/utils/exec/rosacli/ocm_resource_service.go @@ -55,6 +55,7 @@ type OCMResourceService interface { ListOIDCConfig() (OIDCConfigList, bytes.Buffer, error) ListInstanceTypes() (InstanceTypesList, bytes.Buffer, error) + ListClusters(flags ...string) (ClusterList, bytes.Buffer, error) DeleteOIDCConfig(flags ...string) (bytes.Buffer, error) CreateOIDCConfig(flags ...string) (bytes.Buffer, error) ReflectOIDCConfigList(result bytes.Buffer) (oidclist OIDCConfigList, err error) @@ -83,6 +84,17 @@ func NewOCMResourceService(client *Client) OCMResourceService { } } +// Struct for the 'rosa list clusters' 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 ClusterList struct { + ClusterList []Clusters `json:"ClusterList,omitempty"` +} + // Struct for the 'rosa list instance-types' output type InstanceTypes struct { ID string `json:"ID,omitempty"` @@ -202,6 +214,34 @@ func (ors *ocmResourceService) ListInstanceTypes() (InstanceTypesList, bytes.Buf return instanceList, output, err } +// Pasrse the result of 'rosa list clusters' to Clusters struct +func (ors *ocmResourceService) ReflectClusterList(result bytes.Buffer) (url ClusterList, err error) { + url = ClusterList{} + theMap := ors.client.Parser.TableData.Input(result).Parse().Output() + for _, instanceTypeItem := range theMap { + ur := &Clusters{} + err = MapStructure(instanceTypeItem, ur) + if err != nil { + return + } + url.ClusterList = append(url.ClusterList, *ur) + } + return +} + +// ListClusters implements OCMResourceService. +func (ors *ocmResourceService) ListClusters(flags ...string) (ClusterList, bytes.Buffer, error) { + ors.client.Runner.cmdArgs = []string{} + listClusters := ors.client.Runner. + Cmd("list", "clusters").CmdFlags(flags...) + output, err := listClusters.Run() + if err != nil { + return ClusterList{}, output, err + } + clusterList, err := ors.ReflectClusterList(output) + return clusterList, output, err +} + // List region func (ors *ocmResourceService) ListRegion(flags ...string) ([]*CloudRegion, bytes.Buffer, error) { listRegion := ors.client.Runner