mirror of
				https://github.com/kubernetes-sigs/external-dns.git
				synced 2025-11-04 12:41:00 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			293 lines
		
	
	
		
			5.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			293 lines
		
	
	
		
			5.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
/*
 | 
						|
Copyright 2023 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 source
 | 
						|
 | 
						|
import (
 | 
						|
	"strings"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"sigs.k8s.io/external-dns/endpoint"
 | 
						|
	v1 "sigs.k8s.io/gateway-api/apis/v1"
 | 
						|
)
 | 
						|
 | 
						|
func TestGatewayMatchingHost(t *testing.T) {
 | 
						|
	tests := []struct {
 | 
						|
		desc string
 | 
						|
		a, b string
 | 
						|
		host string
 | 
						|
		ok   bool
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			desc: "ipv4-rejected",
 | 
						|
			a:    "1.2.3.4",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "ipv6-rejected",
 | 
						|
			a:    "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "empty-matches-empty",
 | 
						|
			ok:   true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "empty-matches-nonempty",
 | 
						|
			a:    "example.net",
 | 
						|
			host: "example.net",
 | 
						|
			ok:   true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "simple-match",
 | 
						|
			a:    "example.net",
 | 
						|
			b:    "example.net",
 | 
						|
			host: "example.net",
 | 
						|
			ok:   true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "wildcard-matches-longer",
 | 
						|
			a:    "*.example.net",
 | 
						|
			b:    "test.example.net",
 | 
						|
			host: "test.example.net",
 | 
						|
			ok:   true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "wildcard-matches-equal-length",
 | 
						|
			a:    "*.example.net",
 | 
						|
			b:    "a.example.net",
 | 
						|
			host: "a.example.net",
 | 
						|
			ok:   true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "wildcard-matches-multiple-subdomains",
 | 
						|
			a:    "*.example.net",
 | 
						|
			b:    "foo.bar.test.example.net",
 | 
						|
			host: "foo.bar.test.example.net",
 | 
						|
			ok:   true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "wildcard-doesnt-match-parent",
 | 
						|
			a:    "*.example.net",
 | 
						|
			b:    "example.net",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "wildcard-must-be-complete-label",
 | 
						|
			a:    "*example.net",
 | 
						|
			b:    "test.example.net",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
	}
 | 
						|
	for _, tt := range tests {
 | 
						|
		t.Run(tt.desc, func(t *testing.T) {
 | 
						|
			for i := 0; i < 2; i++ {
 | 
						|
				if host, ok := gwMatchingHost(tt.a, tt.b); host != tt.host || ok != tt.ok {
 | 
						|
					t.Errorf(
 | 
						|
						"gwMatchingHost(%q, %q); got: %q, %v; want: %q, %v",
 | 
						|
						tt.a, tt.b, host, ok, tt.host, tt.ok,
 | 
						|
					)
 | 
						|
				}
 | 
						|
				tt.a, tt.b = tt.b, tt.a
 | 
						|
			}
 | 
						|
		})
 | 
						|
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestGatewayMatchingProtocol(t *testing.T) {
 | 
						|
	tests := []struct {
 | 
						|
		route, lis string
 | 
						|
		desc       string
 | 
						|
		ok         bool
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			desc:  "protocol-matches-lis-https-route-http",
 | 
						|
			route: "HTTP",
 | 
						|
			lis:   "HTTPS",
 | 
						|
			ok:    true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc:  "protocol-match-invalid-list-https-route-tcp",
 | 
						|
			route: "TCP",
 | 
						|
			lis:   "HTTPS",
 | 
						|
			ok:    false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc:  "protocol-match-valid-lis-tls-route-tls",
 | 
						|
			route: "TLS",
 | 
						|
			lis:   "TLS",
 | 
						|
			ok:    true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc:  "protocol-match-valid-lis-TLS-route-TCP",
 | 
						|
			route: "TCP",
 | 
						|
			lis:   "TLS",
 | 
						|
			ok:    true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc:  "protocol-match-valid-lis-TLS-route-TCP",
 | 
						|
			route: "TLS",
 | 
						|
			lis:   "TCP",
 | 
						|
			ok:    false,
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	for _, tt := range tests {
 | 
						|
		t.Run(tt.desc, func(t *testing.T) {
 | 
						|
			for i := 0; i < 2; i++ {
 | 
						|
				if ok := gwProtocolMatches(v1.ProtocolType(tt.route), v1.ProtocolType(tt.lis)); ok != tt.ok {
 | 
						|
					t.Errorf(
 | 
						|
						"gwProtocolMatches(%q, %q); got: %v; want: %v",
 | 
						|
						tt.route, tt.lis, ok, tt.ok,
 | 
						|
					)
 | 
						|
				}
 | 
						|
				//tt.a, tt.b = tt.b, tt.a
 | 
						|
			}
 | 
						|
		})
 | 
						|
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestIsDNS1123Domain(t *testing.T) {
 | 
						|
	tests := []struct {
 | 
						|
		desc string
 | 
						|
		in   string
 | 
						|
		ok   bool
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			desc: "empty",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "label-too-long",
 | 
						|
			in:   strings.Repeat("x", 64) + ".example.net",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "domain-too-long",
 | 
						|
			in:   strings.Repeat("testing.", 256/(len("testing."))) + "example.net",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "hostname",
 | 
						|
			in:   "example",
 | 
						|
			ok:   true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "domain",
 | 
						|
			in:   "example.net",
 | 
						|
			ok:   true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "subdomain",
 | 
						|
			in:   "test.example.net",
 | 
						|
			ok:   true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "dashes",
 | 
						|
			in:   "test-with-dash.example.net",
 | 
						|
			ok:   true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "dash-prefix",
 | 
						|
			in:   "-dash-prefix.example.net",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "dash-suffix",
 | 
						|
			in:   "dash-suffix-.example.net",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "underscore",
 | 
						|
			in:   "under_score.example.net",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "plus",
 | 
						|
			in:   "pl+us.example.net",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "brackets",
 | 
						|
			in:   "bra[k]ets.example.net",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "parens",
 | 
						|
			in:   "pa[re]ns.example.net",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc: "wild",
 | 
						|
			in:   "*.example.net",
 | 
						|
			ok:   false,
 | 
						|
		},
 | 
						|
	}
 | 
						|
	for _, tt := range tests {
 | 
						|
		t.Run(tt.desc, func(t *testing.T) {
 | 
						|
			if ok := isDNS1123Domain(tt.in); ok != tt.ok {
 | 
						|
				t.Errorf("isDNS1123Domain(%q); got: %v; want: %v", tt.in, ok, tt.ok)
 | 
						|
			}
 | 
						|
		})
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestDualStackLabel(t *testing.T) {
 | 
						|
	tests := []struct {
 | 
						|
		desc      string
 | 
						|
		in        map[string](string)
 | 
						|
		setsLabel bool
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			desc:      "empty-annotation",
 | 
						|
			setsLabel: false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc:      "correct-annotation-key-and-value",
 | 
						|
			in:        map[string]string{gatewayAPIDualstackAnnotationKey: gatewayAPIDualstackAnnotationValue},
 | 
						|
			setsLabel: true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc:      "correct-annotation-key-incorrect-value",
 | 
						|
			in:        map[string]string{gatewayAPIDualstackAnnotationKey: "foo"},
 | 
						|
			setsLabel: false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			desc:      "incorrect-annotation-key-correct-value",
 | 
						|
			in:        map[string]string{"FOO": gatewayAPIDualstackAnnotationValue},
 | 
						|
			setsLabel: false,
 | 
						|
		},
 | 
						|
	}
 | 
						|
	for _, tt := range tests {
 | 
						|
		t.Run(tt.desc, func(t *testing.T) {
 | 
						|
			endpoints := make([]*endpoint.Endpoint, 0)
 | 
						|
			endpoints = append(endpoints, endpoint.NewEndpoint("www.example.com", endpoint.RecordTypeA, "10.0.0.2", "10.0.0.3"))
 | 
						|
 | 
						|
			rt := &gatewayHTTPRoute{}
 | 
						|
			rt.Metadata().Annotations = tt.in
 | 
						|
 | 
						|
			setDualstackLabel(rt, endpoints)
 | 
						|
			got := endpoints[0].Labels[endpoint.DualstackLabelKey] == "true"
 | 
						|
 | 
						|
			if got != tt.setsLabel {
 | 
						|
				t.Errorf("setDualstackLabel(%q); got: %v; want: %v", tt.in, got, tt.setsLabel)
 | 
						|
			}
 | 
						|
		})
 | 
						|
	}
 | 
						|
}
 |