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=========================================================
33 cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
34 "github.com/stretchr/testify/assert"
35 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37 x509utils "onap.org/oom-certservice/k8s-external-provider/src/x509"
38 "onap.org/oom-certservice/k8s-external-provider/src/leveledlogger"
41 var checkedLogMessages = [7]string{"Property 'duration'", "Property 'usages'", "Property 'ipAddresses'",
42 "Property 'isCA'", "Property 'subject.streetAddress'", "Property 'subject.postalCodes'",
43 "Property 'subject.serialNumber'"}
45 var supportedProperties = [7]string{"Property 'organization'", "Property 'organization unit'", "Property 'country'",
46 "Property 'state'", "Property 'location'", "Property 'dns names'"}
48 const RESULT_LOG = "testdata/test_result.log"
50 func TestMain(m *testing.M) {
51 leveledlogger.SetConfigFileName("testdata/test_logger_config.json")
55 func TestLogShouldNotProvideInformationAboutSkippedPropertiesIfNotExistInCSR(t *testing.T) {
57 logger := leveledlogger.GetLoggerWithName("test")
58 request := getCertificateRequestWithoutSkippedProperties()
60 csr, err := x509utils.DecodeCSR(request.Spec.Request)
62 assert.FailNow(t, "Could not parse Certificate Sign Request")
66 LogCertRequestProperties(logger, request, csr)
67 logsArray := convertLogFileToStringArray(RESULT_LOG)
70 for _, logMsg := range checkedLogMessages {
71 assert.False(t, logsContainExpectedMessage(logsArray, logMsg), "Logs contain: "+logMsg+", but should not")
73 removeTemporaryFile(RESULT_LOG)
76 func TestLogShouldProvideInformationAboutSkippedPropertiesIfExistInCSR(t *testing.T) {
78 logger := leveledlogger.GetLoggerWithName("test")
79 request := getCertificateRequestWithSkippedProperties()
81 csr, err := x509utils.DecodeCSR(request.Spec.Request)
83 assert.FailNow(t, "Could not parse Certificate Sign Request")
87 LogCertRequestProperties(logger, request, csr)
88 logsArray := convertLogFileToStringArray(RESULT_LOG)
91 for _, logMsg := range checkedLogMessages {
92 assert.True(t, logsContainExpectedMessage(logsArray, logMsg), "Logs not contain: "+logMsg)
94 removeTemporaryFile(RESULT_LOG)
97 func TestLogShouldListSupportedProperties(t *testing.T) {
99 logger := leveledlogger.GetLoggerWithName("test")
100 request := getCertificateRequestWithoutSkippedProperties()
102 csr, err := x509utils.DecodeCSR(request.Spec.Request)
104 assert.FailNow(t, "Could not parse Certificate Sign Request")
108 LogCertRequestProperties(logger, request, csr)
109 logsArray := convertLogFileToStringArray(RESULT_LOG)
112 for _, logMsg := range supportedProperties {
113 assert.True(t, logsContainExpectedMessage(logsArray, logMsg), "Logs not contain: "+logMsg)
115 removeTemporaryFile(RESULT_LOG)
118 func getCertificateRequestWithoutSkippedProperties() *cmapi.CertificateRequest {
119 request := new(cmapi.CertificateRequest)
120 request.Spec.Request = []byte(csrWithoutSkippedProperties)
124 func getCertificateRequestWithSkippedProperties() *cmapi.CertificateRequest {
125 request := new(cmapi.CertificateRequest)
126 request.Spec.Request = []byte(csrWithSkippedProperties)
127 request.Spec.Duration = &metav1.Duration{Duration: time.Hour}
128 request.Spec.IsCA = true
129 request.Spec.Usages = cmapi.DefaultKeyUsages()
133 func convertBufferToStringArray(buffer *bytes.Buffer) []string {
134 return strings.Split(buffer.String(), "\n")
137 func convertLogFileToStringArray(filename string) []string {
138 buffer := bytes.NewBuffer(make([]byte, 0))
139 buffer.Write(readFile(filename))
140 return convertBufferToStringArray(buffer)
143 func readFile(filename string) []byte {
144 certRequest, err := ioutil.ReadFile(filename)
151 func removeTemporaryFile(fileName string) {
152 if _, err := os.Stat(fileName); err == nil {
153 e := os.Remove(fileName)
160 func logsContainExpectedMessage(array []string, expectedMsg string) bool {
161 for _, logMsg := range array {
162 if strings.Contains(logMsg, expectedMsg) {