-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Berk Dehrioglu
committed
Sep 19, 2023
1 parent
aea360d
commit 2f4b4e1
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 iam_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestIam(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Iam Suite") | ||
} |
This file contains 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,105 @@ | ||
package iam_test | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
awsclientgo "github.com/aws/aws-sdk-go/aws/client" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
awsIAM "github.com/aws/aws-sdk-go/service/iam" | ||
"github.com/aws/aws-sdk-go/service/iam/iamiface" | ||
"github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
"github.com/giantswarm/capa-iam-operator/pkg/iam" | ||
"github.com/giantswarm/capa-iam-operator/pkg/test/mocks" | ||
) | ||
|
||
var _ = Describe("ReconcileRole", func() { | ||
|
||
var ( | ||
mockCtrl *gomock.Controller | ||
mockIAMClient *mocks.MockIAMAPI | ||
iamService *iam.IAMService | ||
err error | ||
sess awsclientgo.ConfigProvider | ||
) | ||
|
||
BeforeEach(func() { | ||
//create me new iam service config with mocks | ||
sess, err = session.NewSession(&aws.Config{ | ||
Region: aws.String("eu-west-1")}, | ||
) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
mockCtrl = gomock.NewController(GinkgoT()) | ||
mockIAMClient = mocks.NewMockIAMAPI(mockCtrl) | ||
|
||
iamConfig := iam.IAMServiceConfig{ | ||
ClusterName: "test-cluster", | ||
MainRoleName: "test-role", | ||
Region: "test-region", | ||
RoleType: "control-plane", | ||
PrincipalRoleARN: "test-principal-role-arn", | ||
Log: ctrl.Log, | ||
AWSSession: sess, | ||
IAMClientFactory: func(session awsclientgo.ConfigProvider) iamiface.IAMAPI { | ||
return mockIAMClient | ||
}, | ||
} | ||
iamService, err = iam.New(iamConfig) | ||
Expect(err).To(BeNil()) | ||
}) | ||
|
||
When("role is present", func() { | ||
BeforeEach(func() { | ||
mockIAMClient.EXPECT().GetRole(gomock.Any()).Return(&awsIAM.GetRoleOutput{Role: &awsIAM.Role{ | ||
Tags: []*awsIAM.Tag{{Key: aws.String("capi-iam-controller/owned"), Value: aws.String("test-cluster")}}, | ||
}}, nil).AnyTimes() | ||
}) | ||
When("inline policy is already attached", func() { | ||
BeforeEach(func() { | ||
mockIAMClient.EXPECT().ListRolePolicies(gomock.Any()).Return(&awsIAM.ListRolePoliciesOutput{PolicyNames: aws.StringSlice([]string{"control-plane-test-cluster-policy"})}, nil) | ||
}) | ||
It("should return nil", func() { | ||
err := iamService.ReconcileRole() | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
When("could not attach InlinePolicy", func() { | ||
JustBeforeEach(func() { | ||
mockIAMClient.EXPECT().ListRolePolicies(gomock.Any()).Return(&awsIAM.ListRolePoliciesOutput{}, nil).AnyTimes() | ||
mockIAMClient.EXPECT().PutRolePolicy(gomock.Any()).Return(&awsIAM.PutRolePolicyOutput{}, errors.New("test error")).AnyTimes() | ||
}) | ||
It("should return error", func() { | ||
err := iamService.ReconcileRole() | ||
Expect(err).NotTo(BeNil()) | ||
}) | ||
}) | ||
}) | ||
|
||
When("role is not present", func() { | ||
BeforeEach(func() { | ||
mockIAMClient.EXPECT().GetRole(gomock.Any()).Return(&awsIAM.GetRoleOutput{}, awserr.New(awsIAM.ErrCodeNoSuchEntityException, "test", nil)).Times(1) | ||
mockIAMClient.EXPECT().CreateRole(gomock.Any()).Return(&awsIAM.CreateRoleOutput{}, nil) | ||
mockIAMClient.EXPECT().CreateInstanceProfile(gomock.Any()).Return(&awsIAM.CreateInstanceProfileOutput{}, nil) | ||
mockIAMClient.EXPECT().AddRoleToInstanceProfile(gomock.Any()).Return(&awsIAM.AddRoleToInstanceProfileOutput{}, nil) | ||
mockIAMClient.EXPECT().GetRole(gomock.Any()).Return(&awsIAM.GetRoleOutput{Role: &awsIAM.Role{ | ||
Tags: []*awsIAM.Tag{{Key: aws.String("capi-iam-controller/owned"), Value: aws.String("test-cluster")}}, | ||
}}, nil).AnyTimes() | ||
mockIAMClient.EXPECT().ListRolePolicies(gomock.Any()).Return(&awsIAM.ListRolePoliciesOutput{}, nil).AnyTimes() | ||
mockIAMClient.EXPECT().PutRolePolicy(gomock.Any()).Return(&awsIAM.PutRolePolicyOutput{}, nil).AnyTimes() | ||
}) | ||
It("should create the role", func() { | ||
err := iamService.ReconcileRole() | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
|
||
AfterEach(func() { | ||
mockCtrl.Finish() | ||
}) | ||
}) |
This file contains 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
//go:generate ../../../tools/mockgen -destination aws_iam_mock.go -package mocks github.com/aws/aws-sdk-go/service/iam/iamiface IAMAPI | ||
//go:generate ../../../tools/mockgen -destination awsclient_mock.go -package mocks -source ../../awsclient/awsclient.go AWSClient | ||
//go:generate ../../../tools/mockgen -destination eks_mock.go -package mocks github.com/aws/aws-sdk-go/service/eks/eksiface EKSAPI | ||
|
||
package mocks |