2017-08-10 03:22:54 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 The Kubernetes Authors.
|
|
|
|
|
|
|
|
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 alias
|
|
|
|
|
|
|
|
import (
|
2019-08-26 14:58:44 +00:00
|
|
|
"reflect"
|
2017-08-10 03:22:54 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
api "k8s.io/api/core/v1"
|
2021-08-21 20:42:00 +00:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2017-08-10 03:22:54 +00:00
|
|
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-11-23 16:46:23 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
2017-11-08 20:58:57 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
2017-08-10 03:22:54 +00:00
|
|
|
)
|
|
|
|
|
2023-07-22 03:32:07 +00:00
|
|
|
var annotation = parser.GetAnnotationWithPrefix(serverAliasAnnotation)
|
2017-11-08 20:58:57 +00:00
|
|
|
|
2017-08-10 03:22:54 +00:00
|
|
|
func TestParse(t *testing.T) {
|
2017-11-08 20:58:57 +00:00
|
|
|
ap := NewParser(&resolver.Mock{})
|
2017-08-10 03:22:54 +00:00
|
|
|
if ap == nil {
|
|
|
|
t.Fatalf("expected a parser.IngressAnnotation but returned nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2023-07-22 03:32:07 +00:00
|
|
|
annotations map[string]string
|
|
|
|
expected []string
|
|
|
|
skipValidation bool
|
|
|
|
wantErr bool
|
2017-08-10 03:22:54 +00:00
|
|
|
}{
|
2023-07-22 03:32:07 +00:00
|
|
|
{map[string]string{annotation: "a.com, b.com, , c.com"}, []string{"a.com", "b.com", "c.com"}, false, false},
|
|
|
|
{map[string]string{annotation: "www.example.com"}, []string{"www.example.com"}, false, false},
|
|
|
|
{map[string]string{annotation: "*.example.com,www.example.*"}, []string{"*.example.com", "www.example.*"}, false, false},
|
|
|
|
{map[string]string{annotation: `~^www\d+\.example\.com$`}, []string{`~^www\d+\.example\.com$`}, false, false},
|
|
|
|
{map[string]string{annotation: `www.xpto;lala`}, []string{}, false, true},
|
|
|
|
{map[string]string{annotation: `www.xpto;lala`}, []string{"www.xpto;lala"}, true, false}, // When we skip validation no error should happen
|
|
|
|
{map[string]string{annotation: ""}, []string{}, false, true},
|
|
|
|
{map[string]string{}, []string{}, false, true},
|
|
|
|
{nil, []string{}, false, true},
|
2017-08-10 03:22:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 22:49:59 +00:00
|
|
|
ing := &networking.Ingress{
|
2017-08-10 03:22:54 +00:00
|
|
|
ObjectMeta: meta_v1.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
2019-06-09 22:49:59 +00:00
|
|
|
Spec: networking.IngressSpec{},
|
2017-08-10 03:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
ing.SetAnnotations(testCase.annotations)
|
2023-07-22 03:32:07 +00:00
|
|
|
if testCase.skipValidation {
|
|
|
|
parser.EnableAnnotationValidation = false
|
|
|
|
}
|
2023-08-31 07:36:48 +00:00
|
|
|
t.Cleanup(func() {
|
2023-07-22 03:32:07 +00:00
|
|
|
parser.EnableAnnotationValidation = true
|
2023-08-31 07:36:48 +00:00
|
|
|
})
|
2023-07-22 03:32:07 +00:00
|
|
|
result, err := ap.Parse(ing)
|
|
|
|
if (err != nil) != testCase.wantErr {
|
|
|
|
t.Errorf("ParseAliasAnnotation() annotation: %s, error = %v, wantErr %v", testCase.annotations, err, testCase.wantErr)
|
|
|
|
}
|
2019-08-26 14:58:44 +00:00
|
|
|
if !reflect.DeepEqual(result, testCase.expected) {
|
2017-08-10 03:22:54 +00:00
|
|
|
t.Errorf("expected %v but returned %v, annotations: %s", testCase.expected, result, testCase.annotations)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|