Adds a test for copying the annotations from Certificate

Signed-off-by: irbekrm <irbekrm@gmail.com>
This commit is contained in:
irbekrm 2021-07-26 13:42:05 +01:00
parent ddf7e130b7
commit 143c5ce38d
2 changed files with 49 additions and 0 deletions

View File

@ -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",

View File

@ -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)
}
})
}
}