2 * ============LICENSE_START=======================================================
3 * oom-certservice-k8s-external-provider
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
31 cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
32 "github.com/stretchr/testify/assert"
33 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
35 "k8s.io/klog/v2/klogr"
37 x509utils "onap.org/oom-certservice/k8s-external-provider/src/x509"
40 var checkedLogMessages = [7]string{"Property 'duration'", "Property 'usages'", "Property 'ipAddresses'",
41 "Property 'isCA'", "Property 'subject.streetAddress'", "Property 'subject.postalCodes'",
42 "Property 'subject.serialNumber'"}
44 var supportedProperties = [7]string{"Property 'organization'", "Property 'organization unit'", "Property 'country'",
45 "Property 'state'", "Property 'location'", "Property 'dns names'"}
48 func TestMain(m *testing.M) {
50 flag.CommandLine.Set("v", "10")
51 flag.CommandLine.Set("skip_headers", "true")
52 flag.CommandLine.Set("logtostderr", "false")
53 flag.CommandLine.Set("alsologtostderr", "false")
58 func TestLogShouldNotProvideInformationAboutSkippedPropertiesIfNotExistInCSR(t *testing.T) {
61 request := getCertificateRequestWithoutSkippedProperties()
62 tmpWriteBuffer := getLogBuffer()
64 csr, err := x509utils.DecodeCSR(request.Spec.Request)
66 assert.FailNow(t, "Could not parse Certificate Sign Request")
70 LogCertRequestProperties(logger, request, csr)
72 logsArray := convertBufferToStringArray(tmpWriteBuffer)
74 for _, logMsg := range checkedLogMessages {
75 assert.False(t, logsContainExpectedMessage(logsArray, logMsg), "Logs contain: "+logMsg+", but should not")
79 func TestLogShouldProvideInformationAboutSkippedPropertiesIfExistInCSR(t *testing.T) {
82 request := getCertificateRequestWithSkippedProperties()
83 tmpWriteBuffer := getLogBuffer()
85 csr, err := x509utils.DecodeCSR(request.Spec.Request)
87 assert.FailNow(t, "Could not parse Certificate Sign Request")
91 LogCertRequestProperties(logger, request, csr)
93 logsArray := convertBufferToStringArray(tmpWriteBuffer)
96 for _, logMsg := range checkedLogMessages {
97 assert.True(t, logsContainExpectedMessage(logsArray, logMsg), "Logs not contain: "+logMsg)
101 func TestLogShouldListSupportedProperties(t *testing.T) {
103 logger := klogr.New()
104 request := getCertificateRequestWithoutSkippedProperties()
105 tmpWriteBuffer := getLogBuffer()
107 csr, err := x509utils.DecodeCSR(request.Spec.Request)
109 assert.FailNow(t, "Could not parse Certificate Sign Request")
113 LogCertRequestProperties(logger, request, csr)
115 logsArray := convertBufferToStringArray(tmpWriteBuffer)
118 for _, logMsg := range supportedProperties {
119 assert.True(t, logsContainExpectedMessage(logsArray, logMsg), "Logs not contain: "+logMsg)
123 func getCertificateRequestWithoutSkippedProperties() *cmapi.CertificateRequest {
124 request := new(cmapi.CertificateRequest)
125 request.Spec.Request = []byte(csrWithoutSkippedProperties)
129 func getCertificateRequestWithSkippedProperties() *cmapi.CertificateRequest {
130 request := new(cmapi.CertificateRequest)
131 request.Spec.Request = []byte(csrWithSkippedProperties)
132 request.Spec.Duration = &metav1.Duration{Duration: time.Hour}
133 request.Spec.IsCA = true
134 request.Spec.Usages = cmapi.DefaultKeyUsages()
138 func getLogBuffer() *bytes.Buffer {
139 tmpWriteBuffer := bytes.NewBuffer(nil)
140 klog.SetOutput(tmpWriteBuffer)
141 return tmpWriteBuffer
144 func closeLogBuffer() {
148 func convertBufferToStringArray(buffer *bytes.Buffer) []string {
149 return strings.Split(buffer.String(), "\n")
152 func logsContainExpectedMessage(array []string, expectedMsg string) bool {
153 for _, logMsg := range array {
154 if strings.Contains(logMsg, expectedMsg) {