mirror of
https://github.com/kubernetes-sigs/external-dns.git
synced 2025-08-06 01:26:59 +02:00
create internal testutils package (#119)
* create internal testutils package * add boilerplate header to testutils/endpoint.go
This commit is contained in:
parent
7fb84ea2b9
commit
45b01c0b1f
71
internal/testutils/endpoint.go
Normal file
71
internal/testutils/endpoint.go
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
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 testutils
|
||||
|
||||
import (
|
||||
"github.com/kubernetes-incubator/external-dns/endpoint"
|
||||
)
|
||||
|
||||
/** test utility functions for endpoints verifications */
|
||||
|
||||
// SameEndpoint returns true if two endpoint are same
|
||||
// considers example.org. and example.org DNSName/Target as different endpoints
|
||||
// TODO:might need reconsideration regarding trailing dot
|
||||
func SameEndpoint(a, b endpoint.Endpoint) bool {
|
||||
return a.DNSName == b.DNSName && a.Target == b.Target
|
||||
}
|
||||
|
||||
// SameEndpoints compares two slices of endpoints regardless of order
|
||||
// [x,y,z] == [z,x,y]
|
||||
// [x,x,z] == [x,z,x]
|
||||
// [x,y,y] != [x,x,y]
|
||||
// [x,x,x] != [x,x,z]
|
||||
func SameEndpoints(a, b []endpoint.Endpoint) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
|
||||
calculator := map[string]map[string]uint8{} //testutils is not meant for large data sets
|
||||
for _, recordA := range a {
|
||||
if _, exists := calculator[recordA.DNSName]; !exists {
|
||||
calculator[recordA.DNSName] = map[string]uint8{}
|
||||
}
|
||||
if _, exists := calculator[recordA.DNSName][recordA.Target]; !exists {
|
||||
calculator[recordA.DNSName][recordA.Target] = 0
|
||||
}
|
||||
calculator[recordA.DNSName][recordA.Target]++
|
||||
}
|
||||
for _, recordB := range b {
|
||||
if _, exists := calculator[recordB.DNSName]; !exists {
|
||||
return false
|
||||
}
|
||||
if _, exists := calculator[recordB.DNSName][recordB.Target]; !exists {
|
||||
return false
|
||||
}
|
||||
calculator[recordB.DNSName][recordB.Target]--
|
||||
}
|
||||
|
||||
for _, byDNSName := range calculator {
|
||||
for _, byCounter := range byDNSName {
|
||||
if byCounter != 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
@ -16,10 +16,14 @@ limitations under the License.
|
||||
|
||||
package provider
|
||||
|
||||
import "testing"
|
||||
import "reflect"
|
||||
import "github.com/kubernetes-incubator/external-dns/endpoint"
|
||||
import "github.com/kubernetes-incubator/external-dns/plan"
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/kubernetes-incubator/external-dns/endpoint"
|
||||
"github.com/kubernetes-incubator/external-dns/internal/testutils"
|
||||
"github.com/kubernetes-incubator/external-dns/plan"
|
||||
)
|
||||
|
||||
func TestInMemoryProvider(t *testing.T) {
|
||||
t.Run("Records", testInMemoryRecords)
|
||||
@ -243,7 +247,7 @@ func testInMemoryEndpoints(t *testing.T) {
|
||||
} {
|
||||
t.Run(ti.title, func(t *testing.T) {
|
||||
im := &InMemoryProvider{ti.init}
|
||||
if !sameEndpoints(im.endpoints(ti.zone), ti.expected) {
|
||||
if !testutils.SameEndpoints(im.endpoints(ti.zone), ti.expected) {
|
||||
t.Errorf("endpoints returned wrong set")
|
||||
}
|
||||
})
|
||||
@ -329,7 +333,7 @@ func testInMemoryRecords(t *testing.T) {
|
||||
if !ti.expectError && err != nil {
|
||||
t.Errorf("unexpected error")
|
||||
}
|
||||
if !ti.expectError && !sameEndpoints(im.endpoints(ti.zone), records) {
|
||||
if !ti.expectError && !testutils.SameEndpoints(im.endpoints(ti.zone), records) {
|
||||
t.Errorf("endpoints returned wrong set")
|
||||
}
|
||||
})
|
||||
@ -902,37 +906,6 @@ func testNewInMemoryProvider(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// helper functions
|
||||
|
||||
func sameEndpoints(a, b []endpoint.Endpoint) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for _, recordA := range a {
|
||||
found := false
|
||||
for _, recordB := range b {
|
||||
if recordA.DNSName == recordB.DNSName && recordA.Target == recordB.Target {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for _, recordB := range b {
|
||||
found := false
|
||||
for _, recordA := range a {
|
||||
if recordB.DNSName == recordA.DNSName && recordB.Target == recordA.Target {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func testInMemoryCreateZone(t *testing.T) {
|
||||
im := NewInMemoryProvider()
|
||||
if err := im.CreateZone("zone"); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user