-
Notifications
You must be signed in to change notification settings - Fork 257
ROSAENG-61180 | test: add tests for link/unlink ocm-role and user-role #3527
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
Draft
olucasfreitas
wants to merge
1
commit into
ROSAENG-61180/iam-6-ocmrole-userrole
from
ROSAENG-61180/iam-7-link-unlink
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package ocmrole | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| func TestLinkOcmRole(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Link OCM role suite") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package ocmrole | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| awssdk "github.com/aws/aws-sdk-go-v2/aws" | ||
| iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types" | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| . "github.com/openshift-online/ocm-sdk-go/testing" | ||
|
|
||
| "github.com/openshift/rosa/pkg/aws" | ||
| "github.com/openshift/rosa/pkg/interactive" | ||
| "github.com/openshift/rosa/pkg/test" | ||
| ) | ||
|
|
||
| const ( | ||
| testOrgID = "org-123" | ||
| testRoleARN = "arn:aws:iam::123456789012:role/ManagedOpenshift-OCM-Role" | ||
| ) | ||
|
|
||
| var currentAccountResponse = `{ | ||
| "kind": "Account", | ||
| "id": "acct-123", | ||
| "username": "testuser", | ||
| "organization": { | ||
| "id": "org-123", | ||
| "kind": "Organization" | ||
| } | ||
| }` | ||
|
|
||
| func mockIAMRole(roleARN string) iamtypes.Role { | ||
| return iamtypes.Role{ | ||
| Arn: awssdk.String(roleARN), | ||
| } | ||
| } | ||
|
|
||
| var _ = Describe("link ocm-role", func() { | ||
| var t *test.TestingRuntime | ||
|
|
||
| BeforeEach(func() { | ||
| t = test.NewTestRuntime() | ||
| args = struct { | ||
| roleArn string | ||
| organizationID string | ||
| }{} | ||
| interactive.SetEnabled(false) | ||
| Expect(Cmd.Flag("yes").Value.Set("false")).To(Succeed()) | ||
| }) | ||
|
|
||
| Context("runWithRuntime", func() { | ||
| It("returns error when role ARN is empty", func() { | ||
| t.ApiServer.AppendHandlers( | ||
| RespondWithJSON(http.StatusOK, currentAccountResponse), | ||
| ) | ||
|
|
||
| err := runWithRuntime(t.RosaRuntime, Cmd) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("expected a valid ocm role ARN to link to a current organization")) | ||
| }) | ||
|
|
||
| It("returns error when role ARN format is invalid", func() { | ||
| args.roleArn = "invalid-arn" | ||
| t.ApiServer.AppendHandlers( | ||
| RespondWithJSON(http.StatusOK, currentAccountResponse), | ||
| ) | ||
|
|
||
| err := runWithRuntime(t.RosaRuntime, Cmd) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("expected a valid ocm role ARN to link to a current organization")) | ||
| }) | ||
|
|
||
| It("returns error when role does not exist in AWS", func() { | ||
| args.roleArn = testRoleARN | ||
| t.ApiServer.AppendHandlers( | ||
| RespondWithJSON(http.StatusOK, currentAccountResponse), | ||
| ) | ||
|
|
||
| mockClient := t.RosaRuntime.AWSClient.(*aws.MockClient) | ||
| mockClient.EXPECT().GetRoleByARN(testRoleARN).Return(iamtypes.Role{}, fmt.Errorf("role not found")) | ||
|
|
||
| err := runWithRuntime(t.RosaRuntime, Cmd) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("there was a problem checking if role")) | ||
| Expect(err.Error()).To(ContainSubstring("role not found")) | ||
| }) | ||
|
|
||
| It("successfully links role when OCM API call succeeds", func() { | ||
| args.roleArn = testRoleARN | ||
| Expect(Cmd.Flag("yes").Value.Set("true")).To(Succeed()) | ||
|
|
||
| t.ApiServer.AppendHandlers( | ||
| RespondWithJSON(http.StatusOK, currentAccountResponse), | ||
| RespondWithJSON(http.StatusOK, `{"key":"sts_ocm_role","value":""}`), | ||
| RespondWithJSON(http.StatusCreated, fmt.Sprintf(`{"key":"sts_ocm_role","value":"%s"}`, testRoleARN)), | ||
| ) | ||
|
|
||
| mockClient := t.RosaRuntime.AWSClient.(*aws.MockClient) | ||
| mockClient.EXPECT().GetRoleByARN(testRoleARN).Return(mockIAMRole(testRoleARN), nil) | ||
|
|
||
| stdout, stderr, err := test.RunWithOutputCapture(runWithRuntime, t.RosaRuntime, Cmd) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(stderr).To(BeEmpty()) | ||
| Expect(stdout).To(ContainSubstring("Successfully linked role-arn")) | ||
| Expect(stdout).To(ContainSubstring(testRoleARN)) | ||
| Expect(stdout).To(ContainSubstring(testOrgID)) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package userrole | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| func TestLinkUserRole(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Link user role suite") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Return the account lookup error instead of continuing with an empty account ID.
When
GetCurrentAccount()returns an error, it returns a nil account.Account.ID()accepts a nil receiver and returns"", so this code does not panic. It can continue toLinkAccountRole("", roleArn)after role validation and confirmation. ReturngetAccountErrinstead.📝 Committable suggestion
🤖 Prompt for AI Agents