2019-10-30 03:30:01 +00:00
|
|
|
/*
|
2019-11-01 05:22:04 +00:00
|
|
|
Copyright 2019 The Kubernetes Authors.
|
2019-10-30 03:30:01 +00:00
|
|
|
|
|
|
|
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 opentracing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
api "k8s.io/api/core/v1"
|
2021-08-21 20:42:00 +00:00
|
|
|
networking "k8s.io/api/networking/v1"
|
2019-10-30 03:30:01 +00:00
|
|
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
|
|
|
|
"k8s.io/ingress-nginx/internal/ingress/resolver"
|
|
|
|
)
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
const enableAnnotation = "true"
|
|
|
|
|
2019-10-30 03:30:01 +00:00
|
|
|
func buildIngress() *networking.Ingress {
|
|
|
|
defaultBackend := networking.IngressBackend{
|
2021-08-21 20:42:00 +00:00
|
|
|
Service: &networking.IngressServiceBackend{
|
|
|
|
Name: "default-backend",
|
|
|
|
Port: networking.ServiceBackendPort{
|
|
|
|
Number: 80,
|
|
|
|
},
|
|
|
|
},
|
2019-10-30 03:30:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &networking.Ingress{
|
|
|
|
ObjectMeta: meta_v1.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Spec: networking.IngressSpec{
|
2021-08-21 20:42:00 +00:00
|
|
|
DefaultBackend: &networking.IngressBackend{
|
|
|
|
Service: &networking.IngressServiceBackend{
|
|
|
|
Name: "default-backend",
|
|
|
|
Port: networking.ServiceBackendPort{
|
|
|
|
Number: 80,
|
|
|
|
},
|
|
|
|
},
|
2019-10-30 03:30:01 +00:00
|
|
|
},
|
|
|
|
Rules: []networking.IngressRule{
|
|
|
|
{
|
|
|
|
Host: "foo.bar.com",
|
|
|
|
IngressRuleValue: networking.IngressRuleValue{
|
|
|
|
HTTP: &networking.HTTPIngressRuleValue{
|
|
|
|
Paths: []networking.HTTPIngressPath{
|
|
|
|
{
|
|
|
|
Path: "/foo",
|
|
|
|
Backend: defaultBackend,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIngressAnnotationOpentracingSetTrue(t *testing.T) {
|
|
|
|
ing := buildIngress()
|
|
|
|
|
|
|
|
data := map[string]string{}
|
2023-08-31 07:36:48 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix(enableOpentracingAnnotation)] = enableAnnotation
|
2019-10-30 03:30:01 +00:00
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
val, err := NewParser(&resolver.Mock{}).Parse(ing)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error %v", err)
|
|
|
|
}
|
2019-10-30 03:30:01 +00:00
|
|
|
openTracing, ok := val.(*Config)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("expected a Config type")
|
|
|
|
}
|
2020-01-26 00:39:20 +00:00
|
|
|
|
2019-10-30 03:30:01 +00:00
|
|
|
if !openTracing.Enabled {
|
|
|
|
t.Errorf("expected annotation value to be true, got false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIngressAnnotationOpentracingSetFalse(t *testing.T) {
|
|
|
|
ing := buildIngress()
|
|
|
|
|
|
|
|
// Test with explicitly set to false
|
|
|
|
data := map[string]string{}
|
2023-07-22 03:32:07 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix(enableOpentracingAnnotation)] = "false"
|
2019-10-30 03:30:01 +00:00
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
val, err := NewParser(&resolver.Mock{}).Parse(ing)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error %v", err)
|
|
|
|
}
|
2019-10-30 03:30:01 +00:00
|
|
|
openTracing, ok := val.(*Config)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("expected a Config type")
|
|
|
|
}
|
2020-01-26 00:39:20 +00:00
|
|
|
|
2019-10-30 03:30:01 +00:00
|
|
|
if openTracing.Enabled {
|
|
|
|
t.Errorf("expected annotation value to be false, got true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 21:36:21 +00:00
|
|
|
func TestIngressAnnotationOpentracingTrustSetTrue(t *testing.T) {
|
|
|
|
ing := buildIngress()
|
|
|
|
|
|
|
|
data := map[string]string{}
|
2023-08-31 07:36:48 +00:00
|
|
|
data[parser.GetAnnotationWithPrefix(enableOpentracingAnnotation)] = enableAnnotation
|
|
|
|
data[parser.GetAnnotationWithPrefix(opentracingTrustSpanAnnotation)] = enableAnnotation
|
2021-10-24 21:36:21 +00:00
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
val, err := NewParser(&resolver.Mock{}).Parse(ing)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error %v", err)
|
|
|
|
}
|
2021-10-24 21:36:21 +00:00
|
|
|
openTracing, ok := val.(*Config)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("expected a Config type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !openTracing.Enabled {
|
|
|
|
t.Errorf("expected annotation value to be true, got false")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !openTracing.TrustEnabled {
|
|
|
|
t.Errorf("expected annotation value to be true, got false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 03:30:01 +00:00
|
|
|
func TestIngressAnnotationOpentracingUnset(t *testing.T) {
|
|
|
|
ing := buildIngress()
|
|
|
|
|
|
|
|
// Test with no annotation specified
|
|
|
|
data := map[string]string{}
|
|
|
|
ing.SetAnnotations(data)
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
val, err := NewParser(&resolver.Mock{}).Parse(ing)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-01-26 00:39:20 +00:00
|
|
|
_, ok := val.(*Config)
|
2019-10-30 03:30:01 +00:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("expected a Config type")
|
|
|
|
}
|
|
|
|
}
|