This strips any symbols at the end of a shortened domain name in ComputeCertificateRequestName. It also adds tests for the specific util function Signed-off-by: Maartje Eyskens <maartje@eyskens.me>
114 lines
3.2 KiB
Go
114 lines
3.2 KiB
Go
/*
|
|
Copyright 2019 The Jetstack cert-manager contributors.
|
|
|
|
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
|
|
|
|
http://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 util
|
|
|
|
import (
|
|
"testing"
|
|
|
|
cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha2"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/util/validation"
|
|
)
|
|
|
|
func TestComputeCertificateRequestName(t *testing.T) {
|
|
type args struct {
|
|
crt *cmapi.Certificate
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Name generation short domains",
|
|
args: args{
|
|
crt: &cmapi.Certificate{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "unit.test.jetstack.io",
|
|
},
|
|
Spec: cmapi.CertificateSpec{
|
|
CommonName: "unit.test.jetstack.io",
|
|
},
|
|
},
|
|
},
|
|
wantErr: false,
|
|
want: "unit.test.jetstack.io-1683025094",
|
|
},
|
|
{
|
|
name: "Name generation too long domains",
|
|
args: args{
|
|
crt: &cmapi.Certificate{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab.jetstack.io",
|
|
},
|
|
Spec: cmapi.CertificateSpec{
|
|
CommonName: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab.jetstack.io",
|
|
},
|
|
},
|
|
},
|
|
wantErr: false,
|
|
want: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-108802726",
|
|
},
|
|
{
|
|
name: "Name generation for dot as 52nd char",
|
|
args: args{
|
|
crt: &cmapi.Certificate{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.jetstack.io",
|
|
},
|
|
Spec: cmapi.CertificateSpec{
|
|
CommonName: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.jetstack.io",
|
|
},
|
|
},
|
|
},
|
|
wantErr: false,
|
|
want: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-225297437",
|
|
},
|
|
{
|
|
name: "Name generation for dot as 54td char",
|
|
args: args{
|
|
crt: &cmapi.Certificate{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.jetstack.io",
|
|
},
|
|
Spec: cmapi.CertificateSpec{
|
|
CommonName: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.jetstack.io",
|
|
},
|
|
},
|
|
},
|
|
wantErr: false,
|
|
want: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-1448584771",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := ComputeCertificateRequestName(tt.args.crt)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("ComputeCertificateRequestName() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("ComputeCertificateRequestName() = %v, want %v", got, tt.want)
|
|
}
|
|
if len(validation.IsQualifiedName(got)) != 0 {
|
|
t.Errorf("ComputeCertificateRequestName() = %v is not DNS-1123 valid", got)
|
|
}
|
|
})
|
|
}
|
|
}
|