From 143c5ce38d2e3661ababb0aefe3186891f08d8be Mon Sep 17 00:00:00 2001 From: irbekrm Date: Mon, 26 Jul 2021 13:42:05 +0100 Subject: [PATCH] Adds a test for copying the annotations from Certificate Signed-off-by: irbekrm --- pkg/controller/certificates/BUILD.bazel | 1 + pkg/controller/certificates/util_test.go | 48 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/pkg/controller/certificates/BUILD.bazel b/pkg/controller/certificates/BUILD.bazel index 381c70b09..6b075f5c3 100644 --- a/pkg/controller/certificates/BUILD.bazel +++ b/pkg/controller/certificates/BUILD.bazel @@ -60,6 +60,7 @@ go_test( deps = [ "//pkg/apis/certmanager/v1:go_default_library", "//pkg/util/pki:go_default_library", + "//test/unit/gen:go_default_library", "@com_github_stretchr_testify//assert:go_default_library", "@io_k8s_api//core/v1:go_default_library", "@io_k8s_apimachinery//pkg/apis/meta/v1:go_default_library", diff --git a/pkg/controller/certificates/util_test.go b/pkg/controller/certificates/util_test.go index 44d4de429..121e19374 100644 --- a/pkg/controller/certificates/util_test.go +++ b/pkg/controller/certificates/util_test.go @@ -29,6 +29,7 @@ import ( cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1" "github.com/jetstack/cert-manager/pkg/util/pki" + "github.com/jetstack/cert-manager/test/unit/gen" ) func mustGenerateRSA(t *testing.T, keySize int) crypto.PrivateKey { @@ -352,3 +353,50 @@ func TestRenewalTime(t *testing.T) { }) } } +func TestBuildAnnotationsToCopy(t *testing.T) { + testCert := gen.Certificate("test") + tests := map[string]struct { + cert *cmapi.Certificate + copiedAnnotations []string + want map[string]string + }{ + "no annotations should be copied": { + cert: gen.CertificateFrom(testCert, + gen.AddCertificateAnnotations(map[string]string{"foo": "bar", "bar": "bat"})), + copiedAnnotations: []string{}, + want: make(map[string]string), + }, + "all annotations should be copied": { + cert: gen.CertificateFrom(testCert, + gen.AddCertificateAnnotations(map[string]string{"foo": "bar", "bar": "bat"})), + copiedAnnotations: []string{"*"}, + want: map[string]string{"foo": "bar", "bar": "bat"}, + }, + "all except some should be copied": { + cert: gen.CertificateFrom(testCert, + gen.AddCertificateAnnotations(map[string]string{"foo": "bar", "foo.io/thing": "bar", "foo.io/anotherthing": "bat", "bar": "bat"})), + copiedAnnotations: []string{"*", "-foo.io/"}, + want: map[string]string{"foo": "bar", "bar": "bat"}, + }, + "only some should be copied": { + cert: gen.CertificateFrom(testCert, + gen.AddCertificateAnnotations(map[string]string{ + "foo": "bar", "foo.io/thing": "bar", "foo.io/anotherthing": "bat", "bar": "bat", + })), + copiedAnnotations: []string{"foo.io/"}, + want: map[string]string{"foo.io/thing": "bar", "foo.io/anotherthing": "bat"}, + }, + "some annotations have been specified, but none found on the cert": { + cert: gen.CertificateFrom(testCert), + copiedAnnotations: []string{"*", "-foo.io/"}, + want: map[string]string{}, + }, + } + for name, test := range tests { + t.Run(name, func(t *testing.T) { + if got := BuildAnnotationsToCopy(test.cert, test.copiedAnnotations); !reflect.DeepEqual(got, test.want) { + t.Errorf("BuildAnnotationsToCopy() = %+#v, want %+#v", got, test.want) + } + }) + } +}