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=========================================================
32 cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
33 "github.com/stretchr/testify/assert"
34 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36 "onap.org/oom-certservice/k8s-external-provider/src/leveledlogger"
37 x509utils "onap.org/oom-certservice/k8s-external-provider/src/x509"
40 var unsupportedProperties = []string{
41 "* property 'duration'",
42 "* property 'usages'",
44 "- property 'subject.streetAddress'",
45 "- property 'subject.postalCodes'",
46 "- property 'subject.serialNumber'"}
48 var supportedProperties = []string{
49 "+ property 'common name'",
50 "+ property 'organization'",
51 "+ property 'organization unit'",
52 "+ property 'country'",
54 "+ property 'location'",
55 "+ property 'dns names'",
56 "+ property 'ipAddresses'",
58 "+ property 'email addresses'",
61 const RESULT_LOG = "testdata/test_result.log"
63 func TestMain(m *testing.M) {
64 leveledlogger.SetConfigFileName("testdata/test_logger_config.json")
68 func TestLogShouldNotProvideInformationAboutSkippedPropertiesIfNotExistInCSR(t *testing.T) {
70 logger := leveledlogger.GetLoggerWithName("test")
71 request := getCertificateRequestWithoutSkippedProperties()
73 csr, err := x509utils.DecodeCSR(request.Spec.Request)
75 assert.FailNow(t, "Could not parse Certificate Sign Request")
79 LogCertRequestProperties(logger, request, csr)
80 logsArray := convertLogFileToStringArray(RESULT_LOG)
83 for _, logMsg := range unsupportedProperties {
84 assert.False(t, logsContainExpectedMessage(logsArray, logMsg), "Logs should not contain: ["+logMsg+"]")
86 removeTemporaryFile(RESULT_LOG)
89 func TestLogShouldProvideInformationAboutSkippedPropertiesIfExistInCSR(t *testing.T) {
91 logger := leveledlogger.GetLoggerWithName("test")
92 request := getCertificateRequestWithSkippedProperties()
94 csr, err := x509utils.DecodeCSR(request.Spec.Request)
96 assert.FailNow(t, "Could not parse Certificate Sign Request")
100 LogCertRequestProperties(logger, request, csr)
101 logsArray := convertLogFileToStringArray(RESULT_LOG)
104 for _, logMsg := range unsupportedProperties {
105 assert.True(t, logsContainExpectedMessage(logsArray, logMsg), "Logs should contain: ["+logMsg+"]")
107 removeTemporaryFile(RESULT_LOG)
110 func TestLogShouldListSupportedProperties(t *testing.T) {
112 logger := leveledlogger.GetLoggerWithName("test")
113 request := getCertificateRequestWithoutSkippedProperties()
115 csr, err := x509utils.DecodeCSR(request.Spec.Request)
117 assert.FailNow(t, "Could not parse Certificate Sign Request")
121 LogCertRequestProperties(logger, request, csr)
122 logsArray := convertLogFileToStringArray(RESULT_LOG)
125 for _, logMsg := range supportedProperties {
126 assert.True(t, logsContainExpectedMessage(logsArray, logMsg), "Logs should contain: ["+logMsg+"]")
128 removeTemporaryFile(RESULT_LOG)
131 func getCertificateRequestWithoutSkippedProperties() *cmapi.CertificateRequest {
132 request := new(cmapi.CertificateRequest)
133 request.Spec.Request = []byte(csrWithoutSkippedProperties)
137 func getCertificateRequestWithSkippedProperties() *cmapi.CertificateRequest {
138 request := new(cmapi.CertificateRequest)
139 request.Spec.Request = []byte(csrWithSkippedProperties)
140 request.Spec.Duration = &metav1.Duration{Duration: time.Hour}
141 request.Spec.IsCA = true
142 request.Spec.Usages = cmapi.DefaultKeyUsages()
146 func convertBufferToStringArray(buffer *bytes.Buffer) []string {
147 return strings.Split(buffer.String(), "\n")
150 func convertLogFileToStringArray(filename string) []string {
151 buffer := bytes.NewBuffer(make([]byte, 0))
152 buffer.Write(readFile(filename))
153 return convertBufferToStringArray(buffer)
156 func readFile(filename string) []byte {
157 certRequest, err := ioutil.ReadFile(filename)
164 func removeTemporaryFile(fileName string) {
165 if _, err := os.Stat(fileName); err == nil {
166 e := os.Remove(fileName)
173 func logsContainExpectedMessage(array []string, expectedMsg string) bool {
174 for _, logMsg := range array {
175 if strings.Contains(logMsg, expectedMsg) {