Repository: zalando-incubator/kube-ingress-aws-controller
Pull request #617 (2023-06-15 15:07:51)

Move more mocks to fake package
Follow up on https://github.com/zalando-incubator/kube-ingress-aws-controller/pull/587


Patch:
diff --git a/aws/fake/acm.go b/aws/fake/acm.go
new file mode 100644
index 0000000..e50556f
--- /dev/null
+++ b/aws/fake/acm.go
@@ -0,0 +1,32 @@
+package fake
+
+import (
+	"github.com/aws/aws-sdk-go/service/acm"
+	"github.com/aws/aws-sdk-go/service/acm/acmiface"
+)
+
+type ACMClient struct {
+	acmiface.ACMAPI
+	output acm.ListCertificatesOutput
+	cert   acm.GetCertificateOutput
+}
+
+func (m ACMClient) ListCertificates(in *acm.ListCertificatesInput) (*acm.ListCertificatesOutput, error) {
+	return &m.output, nil
+}
+
+func (m ACMClient) ListCertificatesPages(input *acm.ListCertificatesInput, fn func(p *acm.ListCertificatesOutput, lastPage bool) (shouldContinue bool)) error {
+	fn(&m.output, true)
+	return nil
+}
+
+func (m ACMClient) GetCertificate(input *acm.GetCertificateInput) (*acm.GetCertificateOutput, error) {
+	return &m.cert, nil
+}
+
+func NewACMClient(output acm.ListCertificatesOutput, cert acm.GetCertificateOutput) ACMClient {
+	return ACMClient{
+		output: output,
+		cert:   cert,
+	}
+}
diff --git a/aws/fake/iam.go b/aws/fake/iam.go
new file mode 100644
index 0000000..ae6ff3d
--- /dev/null
+++ b/aws/fake/iam.go
@@ -0,0 +1,32 @@
+package fake
+
+import (
+	"github.com/aws/aws-sdk-go/service/iam"
+	"github.com/aws/aws-sdk-go/service/iam/iamiface"
+)
+
+type IAMClient struct {
+	iamiface.IAMAPI
+	list iam.ListServerCertificatesOutput
+	cert iam.GetServerCertificateOutput
+}
+
+func (m IAMClient) ListServerCertificates(*iam.ListServerCertificatesInput) (*iam.ListServerCertificatesOutput, error) {
+	return &m.list, nil
+}
+
+func (m IAMClient) ListServerCertificatesPages(input *iam.ListServerCertificatesInput, fn func(*iam.ListServerCertificatesOutput, bool) bool) error {
+	fn(&m.list, true)
+	return nil
+}
+
+func (m IAMClient) GetServerCertificate(*iam.GetServerCertificateInput) (*iam.GetServerCertificateOutput, error) {
+	return &m.cert, nil
+}
+
+func NewIAMClient(list iam.ListServerCertificatesOutput, cert iam.GetServerCertificateOutput) IAMClient {
+	return IAMClient{
+		list: list,
+		cert: cert,
+	}
+}
diff --git a/aws/helpers.go b/aws/helpers.go
new file mode 100644
index 0000000..9ca0a13
--- /dev/null
+++ b/aws/helpers.go
@@ -0,0 +1,11 @@
+package aws
+
+import "os"
+
+func mustRead(testFile string) string {
+	buf, err := os.ReadFile("testdata/" + testFile)
+	if err != nil {
+		panic(err)
+	}
+	return string(buf)
+}
diff --git a/certs/fake/certificate.go b/certs/fake/certificate.go
index d8af416..2e6e0ba 100644
--- a/certs/fake/certificate.go
+++ b/certs/fake/certificate.go
@@ -124,3 +124,52 @@ func (m *CertificateProvider) GetCertificates() ([]*certs.CertificateSummary, er
 	c := certs.NewCertificate(arn, reparsed, []*x509.Certificate{m.ca.chainCert})
 	return []*certs.CertificateSummary{c.WithRoots(m.ca.roots)}, nil
 }
+
+// certmock implements CertificatesFinder for testing, without validating
+// a real certificate in x509.
+type Cert struct {
+	summaries []*certs.CertificateSummary
+}
+
+func NewCert(summaries []*certs.CertificateSummary) *Cert {
+	return &Cert{
+		summaries: summaries,
+	}
+}
+
+func (m *Cert) CertificateSummaries() []*certs.CertificateSummary {
+	return m.summaries
+}
+
+func (m *Cert) CertificateExists(certificateARN string) bool {
+	for _, c := range m.summaries {
+		if c.ID() == certificateARN {
+			return true
+		}
+	}
+
+	return false
+}
+
+func intersect(a, b []string) bool {
+	for _, ai := range a {
+		for _, bi := range b {
+			if ai == bi {
+				return true
+			}
+		}
+	}
+
+	return false
+}
+
+func (m *Cert) FindMatchingCertificateIDs(hostnames []string) []string {
+	var ids []string
+	for _, c := range m.summaries {
+		if intersect(c.DomainNames(), hostnames) {
+			ids = append(ids, c.ID())
+		}
+	}
+
+	return ids
+}


Test patch:
diff --git a/aws/acm_test.go b/aws/acm_test.go
index de4baec..3dc206e 100644
--- a/aws/acm_test.go
+++ b/aws/acm_test.go
@@ -7,27 +7,9 @@ import (
 	"github.com/aws/aws-sdk-go/service/acm"
 	"github.com/aws/aws-sdk-go/service/acm/acmiface"
 	"github.com/stretchr/testify/require"
+	"github.com/zalando-incubator/kube-ingress-aws-controller/aws/fake"
 )
 
-type mockedACMClient struct {
-	acmiface.ACMAPI
-	output acm.ListCertificatesOutput
-	cert   acm.GetCertificateOutput
-}
-
-func (m mockedACMClient) ListCertificates(in *acm.ListCertificatesInput) (*acm.ListCertificatesOutput, error) {
-	return &m.output, nil
-}
-
-func (m mockedACMClient) ListCertificatesPages(input *acm.ListCertificatesInput, fn func(p *acm.ListCertificatesOutput, lastPage bool) (shouldContinue bool)) error {
-	fn(&m.output, true)
-	return nil
-}
-
-func (m mockedACMClient) GetCertificate(input *acm.GetCertificateInput) (*acm.GetCertificateOutput, error) {
-	return &m.cert, nil
-}
-
 type acmExpect struct {
 	ARN         string
 	DomainNames []string
@@ -46,8 +28,8 @@ func TestACM(t *testing.T) {
 	}{
 		{
 			msg: "Found ACM Cert foobar and a chain",
-			api: mockedACMClient{
-				output: acm.ListCertificatesOutput{
+			api: fake.NewACMClient(
+				acm.ListCertificatesOutput{
 					CertificateSummaryList: []*acm.CertificateSummary{
 						{
 							CertificateArn: aws.String("foobar"),
@@ -55,11 +37,11 @@ func TestACM(t *testing.T) {
 						},
 					},
 				},
-				cert: acm.GetCertificateOutput{
+				acm.GetCertificateOutput{
 					Certificate:      aws.String(cert),
 					CertificateChain: aws.String(chain),
 				},
-			},
+			),
 			expect: acmExpect{
 				ARN:         "foobar",
 				DomainNames: []string{"foobar.de"},
@@ -68,8 +50,8 @@ func TestACM(t *testing.T) {
 		},
 		{
 			msg: "Found ACM Cert foobar and no chain",
-			api: mockedACMClient{
-				output: acm.ListCertificatesOutput{
+			api: fake.NewACMClient(
+				acm.ListCertificatesOutput{
 					CertificateSummaryList: []*acm.CertificateSummary{
 						{
 							CertificateArn: aws.String("foobar"),
@@ -77,10 +59,10 @@ func TestACM(t *testing.T) {
 						},
 					},
 				},
-				cert: acm.GetCertificateOutput{
+				acm.GetCertificateOutput{
 					Certificate: aws.String(cert),
 				},
-			},
+			),
 			expect: acmExpect{
 				ARN:         "foobar",
 				DomainNames: []string{"foobar.de"},
diff --git a/aws/iam_test.go b/aws/iam_test.go
index 369d4fb..86b68ca 100644
--- a/aws/iam_test.go
+++ b/aws/iam_test.go
@@ -1,43 +1,15 @@
 package aws
 
 import (
-	"os"
 	"testing"
 	"time"
 
 	"github.com/aws/aws-sdk-go/aws"
 	"github.com/aws/aws-sdk-go/service/iam"
-	"github.com/aws/aws-sdk-go/service/iam/iamiface"
 	"github.com/stretchr/testify/require"
+	"github.com/zalando-incubator/kube-ingress-aws-controller/aws/fake"
 )
 
-type mockedIAMClient struct {
-	iamiface.IAMAPI
-	list iam.ListServerCertificatesOutput
-	cert iam.GetServerCertificateOutput
-}
-
-func (m mockedIAMClient) ListServerCertificates(*iam.ListServerCertificatesInput) (*iam.ListServerCertificatesOutput, error) {
-	return &m.list, nil
-}
-
-func (m mockedIAMClient) ListServerCertificatesPages(input *iam.ListServerCertificatesInput, fn func(*iam.ListServerCertificatesOutput, bool) bool) error {
-	fn(&m.list, true)
-	return nil
-}
-
-func (m mockedIAMClient) GetServerCertificate(*iam.GetServerCertificateInput) (*iam.GetServerCertificateOutput, error) {
-	return &m.cert, nil
-}
-
-func mustRead(testFile string) string {
-	buf, err := os.ReadFile("testdata/" + testFile)
-	if err != nil {
-		panic(err)
-	}
-	return string(buf)
-}
-
 func TestIAM(t *testing.T) {
 	foobarNotBefore := time.Date(2017, 3, 29, 16, 11, 32, 0, time.UTC)
 	foobarNotAfter := time.Date(2027, 3, 27, 16, 11, 32, 0, time.UTC)
@@ -201,8 +173,8 @@ func TestIAM(t *testing.T) {
 
 func TestIAMParseError(t *testing.T) {
 	provider := iamCertificateProvider{
-		api: mockedIAMClient{
-			list: iam.ListServerCertificatesOutput{
+		api: fake.NewIAMClient(
+			iam.ListServerCertificatesOutput{
 				ServerCertificateMetadataList: []*iam.ServerCertificateMetadata{
 					{
 						Arn:                   aws.String("foobar-arn"),
@@ -211,12 +183,12 @@ func TestIAMParseError(t *testing.T) {
 					},
 				},
 			},
-			cert: iam.GetServerCertificateOutput{
+			iam.GetServerCertificateOutput{
 				ServerCertificate: &iam.ServerCertificate{
 					CertificateBody: aws.String("..."),
 				},
 			},
-		},
+		),
 	}
 	_, err := provider.GetCertificates()
 	require.Equal(t, ErrNoCertificates, err)
diff --git a/certmock_test.go b/certmock_test.go
deleted file mode 100644
index bb6d1f1..0000000
--- a/certmock_test.go
+++ /dev/null
@@ -1,46 +0,0 @@
-package main
-
-import "github.com/zalando-incubator/kube-ingress-aws-controller/certs"
-
-// certmock implements CertificatesFinder for testing, without validating
-// a real certificate in x509.
-type certmock struct {
-	summaries []*certs.CertificateSummary
-}
-
-func (m *certmock) CertificateSummaries() []*certs.CertificateSummary {
-	return m.summaries
-}
-
-func (m *certmock) CertificateExists(certificateARN string) bool {
-	for _, c := range m.summaries {
-		if c.ID() == certificateARN {
-			return true
-		}
-	}
-
-	return false
-}
-
-func intersect(a, b []string) bool {
-	for _, ai := range a {
-		for _, bi := range b {
-			if ai == bi {
-				return true
-			}
-		}
-	}
-
-	return false
-}
-
-func (m *certmock) FindMatchingCertificateIDs(hostnames []string) []string {
-	var ids []string
-	for _, c := range m.summaries {
-		if intersect(c.DomainNames(), hostnames) {
-			ids = append(ids, c.ID())
-		}
-	}
-
-	return ids
-}
diff --git a/worker_test.go b/worker_test.go
index 55af560..dccda0f 100644
--- a/worker_test.go
+++ b/worker_test.go
@@ -814,8 +814,8 @@ func TestIsLBInSync(t *testing.T) {
 
 func TestMatchIngressesToLoadbalancers(t *testing.T) {
 	defaultMaxCertsPerLB := 3
-	defaultCerts := &certmock{
-		summaries: []*certs.CertificateSummary{
+	defaultCerts := certsfake.NewCert(
+		[]*certs.CertificateSummary{
 			certs.NewCertificate(
 				"foo",
 				&x509.Certificate{
@@ -824,7 +824,7 @@ func TestMatchIngressesToLoadbalancers(t *testing.T) {
 				nil,
 			),
 		},
-	}
+	)
 
 	for _, test := range []struct {
 		title         string
@@ -1034,8 +1034,8 @@ func TestMatchIngressesToLoadbalancers(t *testing.T) {
 
 func TestBuildModel(t *testing.T) {
 	defaultMaxCertsPerLB := 3
-	defaultCerts := &certmock{
-		summaries: []*certs.CertificateSummary{
+	defaultCerts := certsfake.NewCert(
+		[]*certs.CertificateSummary{
 			certs.NewCertificate(
 				"foo",
 				&x509.Certificate{
@@ -1044,7 +1044,7 @@ func TestBuildModel(t *testing.T) {
 				nil,
 			),
 		},
-	}
+	)
 
 	const certTTL = time.Hour