mirror of
https://github.com/kubernetes-sigs/external-dns.git
synced 2025-08-06 09:36:58 +02:00
161 lines
3.7 KiB
Go
161 lines
3.7 KiB
Go
/*
|
|
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 source
|
|
|
|
import (
|
|
"context"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestParseTemplate(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
annotationFilter string
|
|
fqdnTemplate string
|
|
combineFQDNAndAnnotation bool
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "invalid template",
|
|
expectError: true,
|
|
fqdnTemplate: "{{.Name",
|
|
},
|
|
{
|
|
name: "valid empty template",
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "valid template",
|
|
expectError: false,
|
|
fqdnTemplate: "{{.Name}}-{{.Namespace}}.ext-dns.test.com",
|
|
},
|
|
{
|
|
name: "valid template",
|
|
expectError: false,
|
|
fqdnTemplate: "{{.Name}}-{{.Namespace}}.ext-dns.test.com, {{.Name}}-{{.Namespace}}.ext-dna.test.com",
|
|
},
|
|
{
|
|
name: "valid template",
|
|
expectError: false,
|
|
fqdnTemplate: "{{.Name}}-{{.Namespace}}.ext-dns.test.com, {{.Name}}-{{.Namespace}}.ext-dna.test.com",
|
|
combineFQDNAndAnnotation: true,
|
|
},
|
|
{
|
|
name: "non-empty annotation filter label",
|
|
expectError: false,
|
|
annotationFilter: "kubernetes.io/ingress.class=nginx",
|
|
},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := parseTemplate(tt.fqdnTemplate)
|
|
if tt.expectError {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFqdnTemplate(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
fqdnTemplate string
|
|
expectedError bool
|
|
}{
|
|
{
|
|
name: "empty template",
|
|
fqdnTemplate: "",
|
|
expectedError: false,
|
|
},
|
|
{
|
|
name: "valid template",
|
|
fqdnTemplate: "{{ .Name }}.example.com",
|
|
expectedError: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
tmpl, err := parseTemplate(tt.fqdnTemplate)
|
|
if tt.expectedError {
|
|
assert.Error(t, err)
|
|
assert.Nil(t, tmpl)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
if tt.fqdnTemplate == "" {
|
|
assert.Nil(t, tmpl)
|
|
} else {
|
|
assert.NotNil(t, tmpl)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type mockInformerFactory struct {
|
|
syncResults map[reflect.Type]bool
|
|
}
|
|
|
|
func (m *mockInformerFactory) WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool {
|
|
return m.syncResults
|
|
}
|
|
|
|
func TestWaitForCacheSync(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
syncResults map[reflect.Type]bool
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "all caches synced",
|
|
syncResults: map[reflect.Type]bool{reflect.TypeOf(""): true},
|
|
expectError: false,
|
|
},
|
|
{
|
|
name: "some caches not synced",
|
|
syncResults: map[reflect.Type]bool{reflect.TypeOf(""): false},
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "context timeout",
|
|
syncResults: map[reflect.Type]bool{reflect.TypeOf(""): false},
|
|
expectError: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
|
defer cancel()
|
|
|
|
factory := &mockInformerFactory{syncResults: tt.syncResults}
|
|
err := waitForCacheSync(ctx, factory)
|
|
|
|
if tt.expectError {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|