[OOM-K8S-CERT-EXTERNAL-PROVIDER] Refactor provider code
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / cmpv2controller / util / certificate_update_util_test.go
1 /*
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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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=========================================================
23  */
24
25 package util
26
27 import (
28         "fmt"
29         "testing"
30
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"
35 )
36
37 const (
38         testPrivateKeyData   = "test-private-key"
39         testCertificateData  = "test-certificate"
40 )
41
42 func Test_CheckIfCertificateUpdateAndRetrieveOldCertificateAndPk_revisionOne(t *testing.T) {
43         request := new(cmapi.CertificateRequest)
44         request.ObjectMeta.Annotations = map[string]string{
45                 revisionAnnotation: "2",
46         }
47         certificate, privateKey := RetrieveOldCertificateAndPkForCertificateUpdate(nil, request, nil)
48         assert.Equal(t, []byte{}, certificate)
49         assert.Equal(t, []byte{}, privateKey)
50 }
51
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,
57         }
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)
62 }
63
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,
69         }
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)
74 }
75
76 func Test_IsUpdateCertificateRevision(t *testing.T) {
77         parameters := []struct {
78                 revision string
79                 expected bool
80         }{
81                 {"1", false},
82                 {"2", true},
83                 {"invalid", false},
84         }
85
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)
90                 })
91         }
92 }
93
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,
98         }
99         assert.Equal(t, expected, IsUpdateCertificateRevision(request))
100 }
101
102 func Test_RetrieveOldCertificateAndPk_shouldSucceedWhenSecretPresent(t *testing.T) {
103         request := new(cmapi.CertificateRequest)
104         request.ObjectMeta.Annotations = map[string]string{
105                 certificateConfigurationAnnotation: testdata.OldCertificateConfig,
106         }
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)
111 }
112
113 func Test_RetrieveOldCertificateAndPk_shouldBeEmptyWhenSecretNotPresent(t *testing.T) {
114         request := new(cmapi.CertificateRequest)
115         request.ObjectMeta.Annotations = map[string]string{
116                 certificateConfigurationAnnotation: testdata.OldCertificateConfig,
117         }
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)
122 }
123
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)
130 }
131