Skip to content

Commit

Permalink
OCM-10021 | test: fix ids:57408,43053,74761,60956,60971
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwang-RH committed Aug 2, 2024
1 parent add477a commit 235bcb9
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 14 deletions.
19 changes: 17 additions & 2 deletions tests/e2e/test_rosacli_account_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package e2e

import (
"fmt"
"os"
"strings"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -21,11 +22,20 @@ var _ = Describe("Edit account roles", labels.Feature.AccountRoles, func() {
rosaClient *rosacli.Client
ocmResourceService rosacli.OCMResourceService
permissionsBoundaryArn string = "arn:aws:iam::aws:policy/AdministratorAccess"
defaultDir string
dirToClean string
)
BeforeEach(func() {
By("Init the client")
rosaClient = rosacli.NewClient()
ocmResourceService = rosaClient.OCMResource

By("Get the default dir")
defaultDir = rosaClient.Runner.GetDir()
})
AfterEach(func() {
By("Go back original by setting runner dir")
rosaClient.Runner.SetDir(defaultDir)
})

It("can create/list/delete account-roles - [id:43070]",
Expand Down Expand Up @@ -548,7 +558,12 @@ var _ = Describe("Edit account roles", labels.Feature.AccountRoles, func() {
_, _, roleVersion, err = version.MajorMinor()
Expect(err).To(BeNil())

By("Create a temp dir to execute the create commands")
dirToClean, err = os.MkdirTemp("", "*")
Expect(err).To(BeNil())

By("Create classic account-roles with managed policies in manual mode")
rosaClient.Runner.SetDir(dirToClean)
output, err := ocmResourceService.CreateAccountRole("--mode", "manual",
"--prefix", rolePrefixManual,
"--path", path,
Expand All @@ -574,12 +589,12 @@ var _ = Describe("Edit account roles", labels.Feature.AccountRoles, func() {
}

By("Delete the account-roles in manual mode")
output, err = ocmResourceService.DeleteAccountRole("--mode", "auto",
output, err = ocmResourceService.DeleteAccountRole("--mode", "manual",
"--prefix", rolePrefixManual,
"-y")

Expect(err).To(BeNil())
commands = common.ExtractCommandsToCreateAWSResoueces(output)
commands = common.ExtractCommandsToDeleteAccountRoles(output)

for _, command := range commands {
_, err := rosaClient.Runner.RunCMD(strings.Split(command, " "))
Expand Down
12 changes: 12 additions & 0 deletions tests/e2e/test_rosacli_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1595,16 +1595,23 @@ var _ = Describe("create/delete operator-roles and oidc-provider to cluster",
clusterNameToClean string
clusterService rosacli.ClusterService
clusterID string
defaultDir string
dirToClean string
)

BeforeEach(func() {
By("Init the client")
rosaClient = rosacli.NewClient()
ocmResourceService = rosaClient.OCMResource
clusterService = rosaClient.Cluster

By("Get the default dir")
defaultDir = rosaClient.Runner.GetDir()
})

AfterEach(func() {
By("Go back original by setting runner dir")
rosaClient.Runner.SetDir(defaultDir)

By("Delete cluster")
rosaClient.Runner.UnsetArgs()
Expand Down Expand Up @@ -1658,7 +1665,12 @@ var _ = Describe("create/delete operator-roles and oidc-provider to cluster",
Expect(err).To(BeNil())
ar := arl.DigAccountRoles(accountRolePrefix, false)

By("Create a temp dir to execute the create commands")
dirToClean, err = os.MkdirTemp("", "*")
Expect(err).To(BeNil())

By("Create one sts cluster in manual mode")
rosaClient.Runner.SetDir(dirToClean)
clusterNameToClean = "test-43053"
operatorRolePreifx := "opPrefix43053"
_, err, _ = clusterService.Create(
Expand Down
11 changes: 5 additions & 6 deletions tests/e2e/test_rosacli_oidc_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ var _ = Describe("Register ummanaged oidc config testing",
ocmResourceService = rosaClient.OCMResource

By("Get the default dir")
defaultDir, _ = os.Getwd()
defaultDir = rosaClient.Runner.GetDir()
})
AfterEach(func() {

By("Go back original by setting runner dir")
rosaClient.Runner.Dir(defaultDir)
rosaClient.Runner.SetDir(defaultDir)

if oidcConfigID != "" {
By("Delete oidc config")
Expand Down Expand Up @@ -258,16 +258,15 @@ var _ = Describe("Register ummanaged oidc config testing",
roleArn := installerRole.RoleArn

By("Create a temp dir to execute the create commands")
output, err := rosaClient.Runner.RunCMD([]string{"mktemp", "-d"})
dirToClean, err = os.MkdirTemp("", "*")
Expect(err).To(BeNil())
dirToClean = strings.TrimSpace(output.String())

By("Go to the temp dir by setting Dir")
rosaClient.Runner.Dir(dirToClean)
rosaClient.Runner.SetDir(dirToClean)

By("Create unmanaged oidc config")
oidcConfigPrefix := "ocp64620oc"
output, err = ocmResourceService.CreateOIDCConfig(
output, err := ocmResourceService.CreateOIDCConfig(
"--mode", "manual",
"--prefix", oidcConfigPrefix,
"--role-arn", roleArn,
Expand Down
32 changes: 30 additions & 2 deletions tests/e2e/test_rosacli_operator_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"os"
"strings"
"time"

Expand Down Expand Up @@ -38,15 +39,23 @@ var _ = Describe("Edit operator roles", labels.Feature.OperatorRoles, func() {

Describe("on cluster", func() {
var (
clusterID string
clusterID string
defaultDir string
dirToClean string
)
BeforeEach(func() {
By("Get the cluster id")
clusterID = config.GetClusterID()
Expect(clusterID).ToNot(Equal(""), "ClusterID is required. Please export CLUSTER_ID")

By("Get the default dir")
defaultDir = rosaClient.Runner.GetDir()
})

AfterEach(func() {
By("Go back original by setting runner dir")
rosaClient.Runner.SetDir(defaultDir)

By("Clean remaining resources")
err := rosaClient.CleanResources(clusterID)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -78,7 +87,12 @@ var _ = Describe("Edit operator roles", labels.Feature.OperatorRoles, func() {
Expect(err).To(HaveOccurred())
Expect(output.String()).To(ContainSubstring("There are clusters using Operator Roles Prefix"))

By("Create a temp dir to execute the create commands")
dirToClean, err = os.MkdirTemp("", "*")
Expect(err).To(BeNil())

By("Delete in-used operator roles by prefix in manual mode")
rosaClient.Runner.SetDir(dirToClean)
output, err = ocmResourceService.DeleteOperatorRoles(
"--prefix", operatorRolePrefix,
"-y",
Expand Down Expand Up @@ -387,7 +401,7 @@ var _ = Describe("Edit operator roles", labels.Feature.OperatorRoles, func() {
)
Expect(err).To(HaveOccurred())
textData = rosaClient.Parser.TextData.Input(output).Parse().Tip()
Expect(textData).Should(ContainSubstring("Invalid ARN"))
Expect(textData).Should(ContainSubstring("to be a valid IAM role ARN"))

By("Create operator roles with not-existed oidc id")
output, err = ocmResourceService.CreateOperatorRoles(
Expand Down Expand Up @@ -790,6 +804,9 @@ var _ = Describe("Detele operator roles with byo oidc", labels.Feature.OperatorR

installerRoleArnC string
installerRoleArnH string

defaultDir string
dirToClean string
)
BeforeEach(func() {
By("Init the client")
Expand All @@ -799,6 +816,9 @@ var _ = Describe("Detele operator roles with byo oidc", labels.Feature.OperatorR
awsClient, err = aws_client.CreateAWSClient("", "")
Expect(err).To(BeNil())

By("Get the default dir")
defaultDir = rosaClient.Runner.GetDir()

})
AfterEach(func() {

Expand Down Expand Up @@ -834,6 +854,9 @@ var _ = Describe("Detele operator roles with byo oidc", labels.Feature.OperatorR
"-y",
)
Expect(err).To(BeNil())

By("Go back original by setting runner dir")
rosaClient.Runner.SetDir(defaultDir)
})
It("to delete operator-roles and byo oidc-config in manual mode - [id:60956]",
labels.Critical, labels.Runtime.OCMResources, func() {
Expand Down Expand Up @@ -876,7 +899,12 @@ var _ = Describe("Detele operator roles with byo oidc", labels.Feature.OperatorR
Expect(err).ToNot(HaveOccurred())
Expect(output.String()).Should(ContainSubstring("Created role"))

By("Create a temp dir to execute the create commands")
dirToClean, err = os.MkdirTemp("", "*")
Expect(err).To(BeNil())

By("Delete the hosted-cp operator-roles by prefix in manual mode")
rosaClient.Runner.SetDir(dirToClean)
output, err = ocmResourceService.DeleteOperatorRoles("--prefix", operatorRolePrefixH, "-y", "--mode", "manual")
Expect(err).NotTo(HaveOccurred())
commands := common.ExtractCommandsToDeleteAWSResoueces(output)
Expand Down
32 changes: 29 additions & 3 deletions tests/utils/common/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func ExtractCommandsToCreateAWSResoueces(bf bytes.Buffer) []string {
return newCommands
}

// Extract aws commands to create AWS resource promted by rosacli, this function supports to parse bellow commands
// `rosa create operator-roles --mode manual`
// `rosa create oidc-provider --mode manual`
// Extract aws commands to delete AWS resource promted by rosacli, this function supports to parse bellow commands
// `rosa delete operator-roles --mode manual`
// `rosa delete oidc-provider --mode manual`
func ExtractCommandsToDeleteAWSResoueces(bf bytes.Buffer) []string {
var commands []string
output := strings.Split(bf.String(), "\naws")
Expand All @@ -58,3 +58,29 @@ func ExtractCommandsToDeleteAWSResoueces(bf bytes.Buffer) []string {
}
return newCommands
}

// Extract aws command to delete account roles in manual mode
func ExtractCommandsToDeleteAccountRoles(bf bytes.Buffer) []string {
var commands []string
output := strings.Split(bf.String(), "\n\n")
for _, message := range output {
if strings.HasPrefix(message, "aws iam") {
commands = append(commands, message)
}
}
var newCommands []string
for _, command := range commands {
if strings.Contains(command, "WARN") {
awscommand := strings.Split(command, "\nWARN")
command = awscommand[0]
}
command = strings.ReplaceAll(command, "\\", "")
command = strings.ReplaceAll(command, "\n", " ")
spaceRegex := regexp.MustCompile(`\s+`)
command = spaceRegex.ReplaceAllString(command, " ")
command = strings.ReplaceAll(command, "'", "")
newCommands = append(newCommands, command)

}
return newCommands
}
6 changes: 5 additions & 1 deletion tests/utils/exec/rosacli/cmd_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ func (r *runner) Color(color string) *runner {
return r
}

func (r *runner) Dir(dir string) *runner {
func (r *runner) SetDir(dir string) *runner {
r.dir = dir
return r
}

func (r *runner) GetDir() string {
return r.dir
}

func (r *runner) JsonFormat() *runner {
return r.format(jsonRunnerFormat)
}
Expand Down

0 comments on commit 235bcb9

Please sign in to comment.