Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCM-8152 | test: automated id:38816 List cluster ROSA cli will work #2076

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions tests/e2e/test_rosacli_list.go
Original file line number Diff line number Diff line change
@@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this label ? This should work also for HCP or ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AkashKanni Please answer to this ?

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))
})
})
45 changes: 42 additions & 3 deletions tests/utils/exec/rosacli/cluster_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not add yet another return type. It is better to have a ReflectClusterList like we have for others

Copy link
Contributor Author

@AkashKanni AkashKanni Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @radtriste
All others are returning output in form of byte and their responses don't looks like in form of columns, But for rosa list cluster it would be efficient to return the data the structure (column wise) format in this way we can easily use the values ( ID, NAME, STATE, etc) easily in automation code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AkashKanni I was meaning to have a ReflectClusterList method similar to ReflectClusterDescription which is taking the bugger result and parsing it into struct

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update following this ?

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)
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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").
Expand Down Expand Up @@ -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()
}

Expand Down