sigs.k8s.io/external-dns@v0.14.1/source/connector_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package source 18 19 import ( 20 "context" 21 "encoding/gob" 22 "net" 23 "testing" 24 25 "github.com/stretchr/testify/assert" 26 "github.com/stretchr/testify/suite" 27 28 "sigs.k8s.io/external-dns/endpoint" 29 ) 30 31 type ConnectorSuite struct { 32 suite.Suite 33 } 34 35 func (suite *ConnectorSuite) SetupTest() { 36 } 37 38 func startServerToServeTargets(t *testing.T, endpoints []*endpoint.Endpoint) net.Listener { 39 ln, err := net.Listen("tcp", "localhost:0") 40 if err != nil { 41 t.Fatal(err) 42 } 43 44 go func() { 45 conn, err := ln.Accept() 46 if err != nil { 47 ln.Close() 48 return 49 } 50 enc := gob.NewEncoder(conn) 51 enc.Encode(endpoints) 52 ln.Close() 53 }() 54 t.Logf("Server listening on %s", ln.Addr().String()) 55 return ln 56 } 57 58 func TestConnectorSource(t *testing.T) { 59 t.Parallel() 60 61 suite.Run(t, new(ConnectorSuite)) 62 t.Run("Interface", testConnectorSourceImplementsSource) 63 t.Run("Endpoints", testConnectorSourceEndpoints) 64 } 65 66 // testConnectorSourceImplementsSource tests that connectorSource is a valid Source. 67 func testConnectorSourceImplementsSource(t *testing.T) { 68 assert.Implements(t, (*Source)(nil), new(connectorSource)) 69 } 70 71 // testConnectorSourceEndpoints tests that NewConnectorSource doesn't return an error. 72 func testConnectorSourceEndpoints(t *testing.T) { 73 for _, ti := range []struct { 74 title string 75 server bool 76 expected []*endpoint.Endpoint 77 expectError bool 78 }{ 79 { 80 title: "invalid remote server", 81 server: false, 82 expectError: true, 83 }, 84 { 85 title: "valid remote server with no endpoints", 86 server: true, 87 expectError: false, 88 }, 89 { 90 title: "valid remote server", 91 server: true, 92 expected: []*endpoint.Endpoint{ 93 { 94 DNSName: "abc.example.org", 95 Targets: endpoint.Targets{"1.2.3.4"}, 96 RecordType: endpoint.RecordTypeA, 97 RecordTTL: 180, 98 }, 99 }, 100 expectError: false, 101 }, 102 { 103 title: "valid remote server with multiple endpoints", 104 server: true, 105 expected: []*endpoint.Endpoint{ 106 { 107 DNSName: "abc.example.org", 108 Targets: endpoint.Targets{"1.2.3.4"}, 109 RecordType: endpoint.RecordTypeA, 110 RecordTTL: 180, 111 }, 112 { 113 DNSName: "xyz.example.org", 114 Targets: endpoint.Targets{"abc.example.org"}, 115 RecordType: endpoint.RecordTypeCNAME, 116 RecordTTL: 180, 117 }, 118 }, 119 expectError: false, 120 }, 121 } { 122 ti := ti 123 t.Run(ti.title, func(t *testing.T) { 124 t.Parallel() 125 126 addr := "localhost:9999" 127 if ti.server { 128 ln := startServerToServeTargets(t, ti.expected) 129 defer ln.Close() 130 addr = ln.Addr().String() 131 } 132 cs, _ := NewConnectorSource(addr) 133 134 endpoints, err := cs.Endpoints(context.Background()) 135 if ti.expectError { 136 assert.Error(t, err) 137 } else { 138 assert.NoError(t, err) 139 } 140 141 // Validate returned endpoints against expected endpoints. 142 validateEndpoints(t, endpoints, ti.expected) 143 }) 144 } 145 }