Skip to content

Commit

Permalink
OCM-8152 | test: automated id:38816 List cluster via ROSA cli will wo…
Browse files Browse the repository at this point in the history
…rk well
  • Loading branch information
AkashKanni committed May 20, 2024
1 parent 3596c43 commit 208d3c4
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
76 changes: 76 additions & 0 deletions tests/e2e/test_rosacli_list.go
Original file line number Diff line number Diff line change
@@ -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))
})
})
41 changes: 41 additions & 0 deletions tests/utils/exec/rosacli/ocm_resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -202,6 +214,35 @@ func (ors *ocmResourceService) ListInstanceTypes() (InstanceTypesList, bytes.Buf
return instanceList, output, err
}


// Pasrse the result of 'rosa list instance-types' to InstanceTypes 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
Expand Down

0 comments on commit 208d3c4

Please sign in to comment.