IT with aws client mock

This commit is contained in:
kkb0318
2024-04-09 21:40:09 +09:00
parent 0f3323b2fc
commit d99f64f148
7 changed files with 60 additions and 240 deletions
@@ -19,12 +19,16 @@ package controller
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/s3"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
awsclient "github.com/kkb0318/irsa-manager/internal/client"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
irsav1alpha1 "github.com/kkb0318/irsa-manager/api/v1alpha1"
@@ -75,10 +79,12 @@ var _ = Describe("IRSASetup Controller", func() {
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
})
It("should successfully reconcile the resource", func() {
awsClient := newMockAwsClient()
By("Reconciling the created resource")
controllerReconciler := &IRSASetupReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Client: k8sClient,
Scheme: k8sClient.Scheme(),
AwsClient: awsClient,
}
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
@@ -90,3 +96,42 @@ var _ = Describe("IRSASetup Controller", func() {
})
})
})
func newMockAwsClient() awsclient.AwsClient {
return &mockAwsClient{}
}
type mockAwsClient struct{}
func (m *mockAwsClient) IamClient() *awsclient.AwsIamClient {
return &awsclient.AwsIamClient{AwsIamAPI: &mockAwsIamAPI{}}
}
func (m *mockAwsClient) S3Client(region, bucketName string) *awsclient.AwsS3Client {
return &awsclient.AwsS3Client{AwsS3API: &mockAwsS3API{}}
}
type (
mockAwsIamAPI struct{}
mockAwsS3API struct{}
)
func (m *mockAwsIamAPI) CreateOpenIDConnectProvider(ctx context.Context, params *iam.CreateOpenIDConnectProviderInput, optFns ...func(*iam.Options)) (*iam.CreateOpenIDConnectProviderOutput, error) {
return &iam.CreateOpenIDConnectProviderOutput{OpenIDConnectProviderArn: aws.String("arn::mock")}, nil
}
func (m *mockAwsS3API) CreateBucket(ctx context.Context, params *s3.CreateBucketInput, optFns ...func(*s3.Options)) (*s3.CreateBucketOutput, error) {
return nil, nil
}
func (m *mockAwsS3API) PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
return nil, nil
}
func (m *mockAwsS3API) DeletePublicAccessBlock(ctx context.Context, params *s3.DeletePublicAccessBlockInput, optFns ...func(*s3.Options)) (*s3.DeletePublicAccessBlockOutput, error) {
return nil, nil
}
func (m *mockAwsS3API) PutBucketOwnershipControls(ctx context.Context, params *s3.PutBucketOwnershipControlsInput, optFns ...func(*s3.Options)) (*s3.PutBucketOwnershipControlsOutput, error) {
return nil, nil
}
+5 -4
View File
@@ -39,9 +39,11 @@ import (
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var (
cfg *rest.Config
k8sClient client.Client
testEnv *envtest.Environment
)
func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
@@ -80,7 +82,6 @@ var _ = BeforeSuite(func() {
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
})
var _ = AfterSuite(func() {