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=========================================================
21 package cmpv2controller
26 "github.com/go-logr/logr"
27 "github.com/stretchr/testify/assert"
28 "github.com/stretchr/testify/mock"
30 "onap.org/oom-certservice/k8s-external-provider/src/cmpv2api"
33 func Test_shouldBeValidCMPv2IssuerSpec_whenAllFieldsAreSet(t *testing.T) {
34 spec := getValidCMPv2IssuerSpec()
36 err := validateCMPv2IssuerSpec(spec, &MockLogger{})
40 func Test_shouldBeInvalidCMPv2IssuerSpec_whenSpecIsEmpty(t *testing.T) {
41 spec := cmpv2api.CMPv2IssuerSpec{}
42 err := validateCMPv2IssuerSpec(spec, nil)
46 func Test_shouldBeInvalidCMPv2IssuerSpec_whenNotAllFieldsAreSet(t *testing.T) {
47 setEmptyFieldFunctions := map[string]func(spec *cmpv2api.CMPv2IssuerSpec){
48 "emptyUrl": func(spec *cmpv2api.CMPv2IssuerSpec) { spec.URL = "" },
49 "empryCaName": func(spec *cmpv2api.CMPv2IssuerSpec) { spec.CaName = "" },
50 "emptySecretName": func(spec *cmpv2api.CMPv2IssuerSpec) { spec.CertSecretRef.Name = "" },
51 "emptySecretKeyRef": func(spec *cmpv2api.CMPv2IssuerSpec) { spec.CertSecretRef.KeyRef = "" },
52 "emptySecretCertRef": func(spec *cmpv2api.CMPv2IssuerSpec) { spec.CertSecretRef.CertRef = "" },
53 "emptySecretCaertRef": func(spec *cmpv2api.CMPv2IssuerSpec) { spec.CertSecretRef.CacertRef = "" },
56 for caseName, setEmptyFieldFunction := range setEmptyFieldFunctions {
57 t.Run(caseName, func(t *testing.T) {
58 test_shouldBeInvalidCMPv2IssuerSpec_whenFunctionApplied(t, setEmptyFieldFunction)
63 func test_shouldBeInvalidCMPv2IssuerSpec_whenFunctionApplied(t *testing.T, transformSpec func(spec *cmpv2api.CMPv2IssuerSpec)) {
64 spec := getValidCMPv2IssuerSpec()
66 err := validateCMPv2IssuerSpec(spec, nil)
70 func getValidCMPv2IssuerSpec() cmpv2api.CMPv2IssuerSpec {
71 issuerSpec := cmpv2api.CMPv2IssuerSpec{
72 URL: "https://oom-cert-service:8443/v1/certificate/",
74 CertSecretRef: cmpv2api.SecretKeySelector{
75 Name: "issuer-cert-secret",
76 KeyRef: "cmpv2Issuer-key.pem",
77 CertRef: "cmpv2Issuer-cert.pem",
78 CacertRef: "cacert.pem",
84 type MockLogger struct {
88 func (m *MockLogger) Info(msg string, keysAndValues ...interface{}) {}
89 func (m *MockLogger) Error(err error, msg string, keysAndValues ...interface{}) {}
90 func (m *MockLogger) Enabled() bool { return false }
91 func (m *MockLogger) V(level int) logr.Logger { return m }
92 func (m *MockLogger) WithValues(keysAndValues ...interface{}) logr.Logger { return m }
93 func (m *MockLogger) WithName(name string) logr.Logger { return m }