2 * ============LICENSE_START=======================================================
3 * oom-certservice-k8s-external-provider
4 * ================================================================================
5 * Copyright (C) 2021 Nokia. All rights reserved.
6 * ================================================================================
7 * This source code was copied from the following git repository:
8 * https://github.com/smallstep/step-issuer
9 * The source code was modified for usage in the ONAP project.
10 * ================================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 * ============LICENSE_END=========================================================
31 cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
32 "github.com/stretchr/testify/assert"
33 "onap.org/oom-certservice/k8s-external-provider/src/testdata"
34 "sigs.k8s.io/controller-runtime/pkg/client/fake"
38 testPrivateKeyData = "test-private-key"
39 testCertificateData = "test-certificate"
42 func Test_CheckIfCertificateUpdateAndRetrieveOldCertificateAndPk_revisionOne(t *testing.T) {
43 request := new(cmapi.CertificateRequest)
44 request.ObjectMeta.Annotations = map[string]string{
45 revisionAnnotation: "2",
47 certificate, privateKey := RetrieveOldCertificateAndPkForCertificateUpdate(nil, request, nil)
48 assert.Equal(t, []byte{}, certificate)
49 assert.Equal(t, []byte{}, privateKey)
52 func Test_CheckIfCertificateUpdateAndRetrieveOldCertificateAndPk_revisionTwoSecretPresent(t *testing.T) {
53 request := new(cmapi.CertificateRequest)
54 request.ObjectMeta.Annotations = map[string]string{
55 revisionAnnotation: "2",
56 certificateConfigurationAnnotation: testdata.OldCertificateConfig,
58 fakeClient := fake.NewFakeClientWithScheme(testdata.GetScheme(), testdata.GetValidCertificateSecret())
59 certificate, privateKey := RetrieveOldCertificateAndPkForCertificateUpdate(fakeClient, request, nil)
60 assert.Equal(t, []byte(testCertificateData), certificate)
61 assert.Equal(t, []byte(testPrivateKeyData), privateKey)
64 func Test_CheckIfCertificateUpdateAndRetrieveOldCertificateAndPk_revisionTwoSecretNotPresent(t *testing.T) {
65 request := new(cmapi.CertificateRequest)
66 request.ObjectMeta.Annotations = map[string]string{
67 revisionAnnotation: "2",
68 certificateConfigurationAnnotation: testdata.OldCertificateConfig,
70 fakeClient := fake.NewFakeClientWithScheme(testdata.GetScheme())
71 certificate, privateKey := RetrieveOldCertificateAndPkForCertificateUpdate(fakeClient, request, nil)
72 assert.Equal(t, []byte{}, certificate)
73 assert.Equal(t, []byte{}, privateKey)
76 func Test_IsUpdateCertificateRevision(t *testing.T) {
77 parameters := []struct {
86 for _, parameter := range parameters {
87 testName := fmt.Sprintf("Expected:%v for revision=%v", parameter.expected, parameter.revision)
88 t.Run(testName, func(t *testing.T) {
89 testIsUpdateCertificateRevision(t, parameter.revision, parameter.expected)
94 func testIsUpdateCertificateRevision(t *testing.T, revision string, expected bool) {
95 request := new(cmapi.CertificateRequest)
96 request.ObjectMeta.Annotations = map[string]string{
97 revisionAnnotation: revision,
99 assert.Equal(t, expected, IsUpdateCertificateRevision(request))
102 func Test_RetrieveOldCertificateAndPk_shouldSucceedWhenSecretPresent(t *testing.T) {
103 request := new(cmapi.CertificateRequest)
104 request.ObjectMeta.Annotations = map[string]string{
105 certificateConfigurationAnnotation: testdata.OldCertificateConfig,
107 fakeClient := fake.NewFakeClientWithScheme(testdata.GetScheme(), testdata.GetValidCertificateSecret())
108 certificate, privateKey := RetrieveOldCertificateAndPk(fakeClient, request, nil)
109 assert.Equal(t, []byte(testCertificateData), certificate)
110 assert.Equal(t, []byte(testPrivateKeyData), privateKey)
113 func Test_RetrieveOldCertificateAndPk_shouldBeEmptyWhenSecretNotPresent(t *testing.T) {
114 request := new(cmapi.CertificateRequest)
115 request.ObjectMeta.Annotations = map[string]string{
116 certificateConfigurationAnnotation: testdata.OldCertificateConfig,
118 fakeClient := fake.NewFakeClientWithScheme(testdata.GetScheme())
119 certificate, privateKey := RetrieveOldCertificateAndPk(fakeClient, request, nil)
120 assert.Equal(t, []byte{}, certificate)
121 assert.Equal(t, []byte{}, privateKey)
124 func Test_RetrieveOldCertificateAndPk_shouldBeEmptyWhenOldCertificateCannotBeUnmarshalled(t *testing.T) {
125 request := new(cmapi.CertificateRequest)
126 fakeClient := fake.NewFakeClientWithScheme(testdata.GetScheme())
127 certificate, privateKey := RetrieveOldCertificateAndPk(fakeClient, request, nil)
128 assert.Equal(t, []byte{}, certificate)
129 assert.Equal(t, []byte{}, privateKey)