2016-11-10 22:56:29 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 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 parser
|
|
|
|
|
|
|
|
import (
|
2020-02-05 02:06:07 +00:00
|
|
|
"net/url"
|
2016-11-10 22:56:29 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-07-16 19:19:59 +00:00
|
|
|
api "k8s.io/api/core/v1"
|
2021-08-21 20:42:00 +00:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2017-04-01 14:39:42 +00:00
|
|
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2016-11-10 22:56:29 +00:00
|
|
|
)
|
|
|
|
|
2019-06-09 22:49:59 +00:00
|
|
|
func buildIngress() *networking.Ingress {
|
|
|
|
return &networking.Ingress{
|
2017-04-01 14:39:42 +00:00
|
|
|
ObjectMeta: meta_v1.ObjectMeta{
|
2016-11-10 22:56:29 +00:00
|
|
|
Name: "foo",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
2019-06-09 22:49:59 +00:00
|
|
|
Spec: networking.IngressSpec{},
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBoolAnnotation(t *testing.T) {
|
|
|
|
ing := buildIngress()
|
|
|
|
|
2017-11-23 16:46:23 +00:00
|
|
|
_, err := GetBoolAnnotation("", nil)
|
2016-11-10 22:56:29 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected error but retuned nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
field string
|
|
|
|
value string
|
|
|
|
exp bool
|
|
|
|
expErr bool
|
|
|
|
}{
|
|
|
|
{"valid - false", "bool", "false", false, false},
|
|
|
|
{"valid - true", "bool", "true", true, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
data := map[string]string{}
|
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2017-11-23 16:46:23 +00:00
|
|
|
data[GetAnnotationWithPrefix(test.field)] = test.value
|
2016-11-10 22:56:29 +00:00
|
|
|
|
2017-11-23 16:46:23 +00:00
|
|
|
u, err := GetBoolAnnotation(test.field, ing)
|
2016-11-10 22:56:29 +00:00
|
|
|
if test.expErr {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("%v: expected error but retuned nil", test.name)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if u != test.exp {
|
|
|
|
t.Errorf("%v: expected \"%v\" but \"%v\" was returned", test.name, test.exp, u)
|
|
|
|
}
|
2016-12-29 20:02:06 +00:00
|
|
|
|
|
|
|
delete(data, test.field)
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetStringAnnotation(t *testing.T) {
|
|
|
|
ing := buildIngress()
|
|
|
|
|
2017-11-23 16:46:23 +00:00
|
|
|
_, err := GetStringAnnotation("", nil)
|
2016-11-10 22:56:29 +00:00
|
|
|
if err == nil {
|
2018-12-02 18:35:12 +00:00
|
|
|
t.Errorf("expected error but none returned")
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
field string
|
|
|
|
value string
|
|
|
|
exp string
|
|
|
|
expErr bool
|
|
|
|
}{
|
2019-05-07 11:41:53 +00:00
|
|
|
{"valid - A", "string", "A ", "A", false},
|
|
|
|
{"valid - B", "string", " B", "B", false},
|
|
|
|
{"empty", "string", " ", "", true},
|
|
|
|
{"valid multiline", "string", `
|
2020-02-05 02:06:07 +00:00
|
|
|
rewrite (?i)/arcgis/rest/services/Utilities/Geometry/GeometryServer(.*)$ /arcgis/rest/services/Utilities/Geometry/GeometryServer$1 break;
|
|
|
|
rewrite (?i)/arcgis/services/Utilities/Geometry/GeometryServer(.*)$ /arcgis/services/Utilities/Geometry/GeometryServer$1 break;
|
2019-05-07 11:41:53 +00:00
|
|
|
`, `
|
|
|
|
rewrite (?i)/arcgis/rest/services/Utilities/Geometry/GeometryServer(.*)$ /arcgis/rest/services/Utilities/Geometry/GeometryServer$1 break;
|
|
|
|
rewrite (?i)/arcgis/services/Utilities/Geometry/GeometryServer(.*)$ /arcgis/services/Utilities/Geometry/GeometryServer$1 break;
|
|
|
|
`,
|
|
|
|
false},
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data := map[string]string{}
|
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2017-11-23 16:46:23 +00:00
|
|
|
data[GetAnnotationWithPrefix(test.field)] = test.value
|
2016-11-10 22:56:29 +00:00
|
|
|
|
2017-11-23 16:46:23 +00:00
|
|
|
s, err := GetStringAnnotation(test.field, ing)
|
2016-11-10 22:56:29 +00:00
|
|
|
if test.expErr {
|
|
|
|
if err == nil {
|
2018-12-02 18:35:12 +00:00
|
|
|
t.Errorf("%v: expected error but none returned", test.name)
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if s != test.exp {
|
|
|
|
t.Errorf("%v: expected \"%v\" but \"%v\" was returned", test.name, test.exp, s)
|
|
|
|
}
|
2016-12-29 20:02:06 +00:00
|
|
|
|
|
|
|
delete(data, test.field)
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetIntAnnotation(t *testing.T) {
|
|
|
|
ing := buildIngress()
|
|
|
|
|
2017-11-23 16:46:23 +00:00
|
|
|
_, err := GetIntAnnotation("", nil)
|
2016-11-10 22:56:29 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected error but retuned nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
field string
|
|
|
|
value string
|
|
|
|
exp int
|
|
|
|
expErr bool
|
|
|
|
}{
|
|
|
|
{"valid - A", "string", "1", 1, false},
|
|
|
|
{"valid - B", "string", "2", 2, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
data := map[string]string{}
|
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2017-11-23 16:46:23 +00:00
|
|
|
data[GetAnnotationWithPrefix(test.field)] = test.value
|
2016-11-10 22:56:29 +00:00
|
|
|
|
2017-11-23 16:46:23 +00:00
|
|
|
s, err := GetIntAnnotation(test.field, ing)
|
2016-11-10 22:56:29 +00:00
|
|
|
if test.expErr {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("%v: expected error but retuned nil", test.name)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if s != test.exp {
|
|
|
|
t.Errorf("%v: expected \"%v\" but \"%v\" was returned", test.name, test.exp, s)
|
|
|
|
}
|
2016-12-29 20:02:06 +00:00
|
|
|
|
|
|
|
delete(data, test.field)
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-05 02:06:07 +00:00
|
|
|
|
|
|
|
func TestStringToURL(t *testing.T) {
|
|
|
|
validURL := "http://bar.foo.com/external-auth"
|
|
|
|
validParsedURL, _ := url.Parse(validURL)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
title string
|
|
|
|
url string
|
|
|
|
message string
|
|
|
|
parsed *url.URL
|
|
|
|
expErr bool
|
|
|
|
}{
|
|
|
|
{"empty", "", "url scheme is empty", nil, true},
|
|
|
|
{"no scheme", "bar", "url scheme is empty", nil, true},
|
|
|
|
{"invalid host", "http://", "url host is empty", nil, true},
|
|
|
|
{"invalid host (multiple dots)", "http://foo..bar.com", "invalid url host", nil, true},
|
|
|
|
{"valid URL", validURL, "", validParsedURL, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
i, err := StringToURL(test.url)
|
|
|
|
if test.expErr {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("%v: expected error but none returned", test.title)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err.Error() != test.message {
|
|
|
|
t.Errorf("%v: expected error \"%v\" but \"%v\" was returned", test.title, test.message, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.String() != test.parsed.String() {
|
|
|
|
t.Errorf("%v: expected \"%v\" but \"%v\" was returned", test.title, test.parsed, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|