airshipctl/pkg/document/plugin/templater/extlib/regexgen_test.go
Alexey Odinokov 47df361761 Implement genCAEx and genSignedCertEx with Subj support
The original sprig-library implementation of
genCA and genSignedCert may accept only CN parameter
which isn't enough for K8s admin certificate.
That certification must have O=system:masters, e.g.
/CN=admin/O=system:masters

This PR introduces the set of functions that
insted of cn accept subj argument that may have
a form compatible with openssl -subj parameter.
If the first symbol isn't '/' subj behaves as
cn argument.

The set of new functions that accept subj arg is:
genCAEx
genCAWithKeyEx
genSignedCertEx
genSignedCertWithKeyEx

Since the implementaion required to copy some
non-exported helper functions from sprig,
the decision was made to separate all go-template
extension functions into a separate package:
extlib. This package can be reused in other
go-applications, it's just necessary to use
GenericFuncMap function to get function-map.

Change-Id: I0ffddee2e597323803bf5f1b54f315ded424b7be
2021-01-29 16:43:32 +00:00

58 lines
1.3 KiB
Go

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package extlib
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRegexGen(t *testing.T) {
tpl := `{{- $regex := "^[a-z]{5,10}$" }}
{{- $nomatchregex := "^[a-z]{0,4}$" }}
true={{- regexMatch $regex (regexGen $regex 10) }},
false={{- regexMatch $nomatchregex (regexGen $regex 10) }}
`
out, err := runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
assert.Equal(t, `
true=true,
false=false
`, out)
}
func TestRegexPanicOnPattern(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
regexGen("[a-z", 1)
}
func TestRegexPanicOnLimit(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
regexGen("[a-z]{0,4}", 0)
}