mirror of
https://github.com/kubernetes-sigs/external-dns.git
synced 2025-11-30 09:21:29 +01:00
feat(source): promote MockSource to source package (#78)
This commit is contained in:
parent
15f45fcacd
commit
cf9c5e39ed
@ -26,25 +26,6 @@ import (
|
|||||||
"github.com/kubernetes-incubator/external-dns/source"
|
"github.com/kubernetes-incubator/external-dns/source"
|
||||||
)
|
)
|
||||||
|
|
||||||
// mockSource returns mock endpoints.
|
|
||||||
type mockSource struct {
|
|
||||||
RecordsStore []endpoint.Endpoint
|
|
||||||
}
|
|
||||||
|
|
||||||
// Endpoints returns the desired mock endpoints.
|
|
||||||
func (s *mockSource) Endpoints() ([]endpoint.Endpoint, error) {
|
|
||||||
return s.RecordsStore, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// newMockSource creates a new mockSource returning the given endpoints.
|
|
||||||
func newMockSource(endpoints []endpoint.Endpoint) source.Source {
|
|
||||||
source := &mockSource{
|
|
||||||
RecordsStore: endpoints,
|
|
||||||
}
|
|
||||||
|
|
||||||
return source
|
|
||||||
}
|
|
||||||
|
|
||||||
// mockDNSProvider returns mock endpoints and validates changes.
|
// mockDNSProvider returns mock endpoints and validates changes.
|
||||||
type mockDNSProvider struct {
|
type mockDNSProvider struct {
|
||||||
RecordsStore []endpoint.Endpoint
|
RecordsStore []endpoint.Endpoint
|
||||||
@ -108,7 +89,7 @@ func newMockDNSProvider(endpoints []endpoint.Endpoint, zone string, changes *pla
|
|||||||
// TestRunOnce tests that RunOnce correctly orchestrates the different components.
|
// TestRunOnce tests that RunOnce correctly orchestrates the different components.
|
||||||
func TestRunOnce(t *testing.T) {
|
func TestRunOnce(t *testing.T) {
|
||||||
// Fake some desired endpoints coming from our source.
|
// Fake some desired endpoints coming from our source.
|
||||||
source := newMockSource(
|
source := source.NewMockSource(
|
||||||
[]endpoint.Endpoint{
|
[]endpoint.Endpoint{
|
||||||
{
|
{
|
||||||
DNSName: "create-record",
|
DNSName: "create-record",
|
||||||
|
|||||||
34
source/mock_source.go
Normal file
34
source/mock_source.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
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"
|
||||||
|
|
||||||
|
// mockSource returns mock endpoints.
|
||||||
|
type mockSource struct {
|
||||||
|
store []endpoint.Endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMockSource creates a new mockSource returning the given endpoints.
|
||||||
|
func NewMockSource(endpoints []endpoint.Endpoint) Source {
|
||||||
|
return &mockSource{store: endpoints}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Endpoints returns the desired mock endpoints.
|
||||||
|
func (s *mockSource) Endpoints() ([]endpoint.Endpoint, error) {
|
||||||
|
return s.store, nil
|
||||||
|
}
|
||||||
62
source/mock_source_test.go
Normal file
62
source/mock_source_test.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
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 mockSource is a Source
|
||||||
|
var _ Source = &mockSource{}
|
||||||
|
|
||||||
|
func TestMockSource(t *testing.T) {
|
||||||
|
t.Run("Endpoints", testMockSourceEndpoints)
|
||||||
|
}
|
||||||
|
|
||||||
|
// testMockSourceEndpoints tests that endpoints are returned as given.
|
||||||
|
func testMockSourceEndpoints(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
title string
|
||||||
|
givenAndExpected []endpoint.Endpoint
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"no endpoints given return no endpoints",
|
||||||
|
[]endpoint.Endpoint{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"single endpoint given returns single endpoint",
|
||||||
|
[]endpoint.Endpoint{
|
||||||
|
{DNSName: "foo", Target: "8.8.8.8"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(tc.title, func(t *testing.T) {
|
||||||
|
// Create our object under test and get the endpoints.
|
||||||
|
source := NewMockSource(tc.givenAndExpected)
|
||||||
|
|
||||||
|
endpoints, err := source.Endpoints()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate returned endpoints against desired endpoints.
|
||||||
|
validateEndpoints(t, endpoints, tc.givenAndExpected)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user