Commit f326f65c authored by Martin Linkhorst's avatar Martin Linkhorst Committed by GitHub
Browse files

MultiSource: a Source that merges results from other Sources (#79)

* feat(source): promote MockSource to source package

* feat(source): add MultiSource to merge other Sources

* feat(source): make structs and members of multiSource private
parent c3378f7f
Showing with 128 additions and 0 deletions
+128 -0
/*
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 "github.com/kubernetes-incubator/external-dns/endpoint"
// multiSource is a Source that merges the endpoints of its nested Sources.
type multiSource struct {
children []Source
}
// Endpoints collects endpoints of all nested Sources and returns them in a single slice.
func (ms *multiSource) Endpoints() ([]endpoint.Endpoint, error) {
result := []endpoint.Endpoint{}
for _, s := range ms.children {
endpoints, err := s.Endpoints()
if err != nil {
return nil, err
}
result = append(result, endpoints...)
}
return result, nil
}
// NewMultiSource creates a new multiSource.
func NewMultiSource(children ...Source) Source {
return &multiSource{children: children}
}
/*
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 (
"testing"
"github.com/kubernetes-incubator/external-dns/endpoint"
)
// Validates that multiSource is a Source
var _ Source = &multiSource{}
func TestMultiSource(t *testing.T) {
t.Run("Endpoints", testMultiSourceEndpoints)
}
// testMultiSourceEndpoints tests merged endpoints from children are returned.
func testMultiSourceEndpoints(t *testing.T) {
foo := endpoint.Endpoint{DNSName: "foo", Target: "8.8.8.8"}
bar := endpoint.Endpoint{DNSName: "bar", Target: "8.8.4.4"}
for _, tc := range []struct {
title string
nestedEndpoints [][]endpoint.Endpoint
expected []endpoint.Endpoint
}{
{
"no child sources return no endpoints",
nil,
[]endpoint.Endpoint{},
},
{
"single empty child source returns no endpoints",
[][]endpoint.Endpoint{{}},
[]endpoint.Endpoint{},
},
{
"single non-empty child source returns child's endpoints",
[][]endpoint.Endpoint{{foo}},
[]endpoint.Endpoint{foo},
},
{
"multiple non-empty child sources returns merged children's endpoints",
[][]endpoint.Endpoint{{foo}, {bar}},
[]endpoint.Endpoint{foo, bar},
},
} {
t.Run(tc.title, func(t *testing.T) {
// Prepare the nested mock sources
sources := make([]Source, 0, len(tc.nestedEndpoints))
for _, endpoints := range tc.nestedEndpoints {
sources = append(sources, NewMockSource(endpoints))
}
// Create our object under test and get the endpoints.
source := NewMultiSource(sources...)
endpoints, err := source.Endpoints()
if err != nil {
t.Fatal(err)
}
// Validate returned endpoints against desired endpoints.
validateEndpoints(t, endpoints, tc.expected)
})
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment