2016-07-27 10:01:24 -07:00
// +build k8s
2016-07-14 14:50:14 -07:00
package test
import (
"io/ioutil"
"log"
"testing"
2016-08-22 23:15:21 -07:00
"time"
2016-07-14 14:50:14 -07:00
2016-08-22 23:15:21 -07:00
"github.com/mholt/caddy"
2016-07-14 14:50:14 -07:00
"github.com/miekg/dns"
)
// Test data for A records
var testdataLookupA = [ ] struct {
Query string
TotalAnswerCount int
ARecordCount int
} {
// Matching queries
{ "mynginx.demo.coredns.local." , 1 , 1 } , // One A record, should exist
// Failure queries
{ "mynginx.test.coredns.local." , 0 , 0 } , // One A record, is not exposed
{ "someservicethatdoesnotexist.demo.coredns.local." , 0 , 0 } , // Record does not exist
// Namespace wildcards
{ "mynginx.*.coredns.local." , 1 , 1 } , // One A record, via wildcard namespace
{ "mynginx.any.coredns.local." , 1 , 1 } , // One A record, via wildcard namespace
{ "someservicethatdoesnotexist.*.coredns.local." , 0 , 0 } , // Record does not exist with wildcard for namespace
{ "someservicethatdoesnotexist.any.coredns.local." , 0 , 0 } , // Record does not exist with wildcard for namespace
{ "*.demo.coredns.local." , 2 , 2 } , // Two A records, via wildcard
{ "any.demo.coredns.local." , 2 , 2 } , // Two A records, via wildcard
{ "*.test.coredns.local." , 0 , 0 } , // Two A record, via wildcard that is not exposed
{ "any.test.coredns.local." , 0 , 0 } , // Two A record, via wildcard that is not exposed
{ "*.*.coredns.local." , 2 , 2 } , // Two A records, via namespace and service wildcard
}
// Test data for SRV records
var testdataLookupSRV = [ ] struct {
Query string
TotalAnswerCount int
// ARecordCount int
SRVRecordCount int
} {
// Matching queries
{ "mynginx.demo.coredns.local." , 1 , 1 } , // One SRV record, should exist
// Failure queries
{ "mynginx.test.coredns.local." , 0 , 0 } , // One SRV record, is not exposed
{ "someservicethatdoesnotexist.demo.coredns.local." , 0 , 0 } , // Record does not exist
// Namespace wildcards
{ "mynginx.*.coredns.local." , 1 , 1 } , // One SRV record, via wildcard namespace
{ "mynginx.any.coredns.local." , 1 , 1 } , // One SRV record, via wildcard namespace
{ "someservicethatdoesnotexist.*.coredns.local." , 0 , 0 } , // Record does not exist with wildcard for namespace
{ "someservicethatdoesnotexist.any.coredns.local." , 0 , 0 } , // Record does not exist with wildcard for namespace
{ "*.demo.coredns.local." , 1 , 1 } , // One SRV record, via wildcard
{ "any.demo.coredns.local." , 1 , 1 } , // One SRV record, via wildcard
{ "*.test.coredns.local." , 0 , 0 } , // One SRV record, via wildcard that is not exposed
{ "any.test.coredns.local." , 0 , 0 } , // One SRV record, via wildcard that is not exposed
{ "*.*.coredns.local." , 1 , 1 } , // One SRV record, via namespace and service wildcard
}
2016-09-27 07:57:04 +08:00
func TestKubernetesIntegration ( t * testing . T ) {
2016-08-22 23:15:21 -07:00
// t.Skip("Skip Kubernetes Integration tests")
2016-08-19 17:14:17 -07:00
// subtests here (Go 1.7 feature).
2016-07-14 14:50:14 -07:00
testLookupA ( t )
testLookupSRV ( t )
}
2016-08-22 23:15:21 -07:00
func createTestServer ( t * testing . T , corefile string ) ( * caddy . Instance , string ) {
2016-08-19 17:14:17 -07:00
server , err := CoreDNSServer ( corefile )
2016-07-14 14:50:14 -07:00
if err != nil {
2016-10-02 08:31:44 +01:00
t . Fatalf ( "Could not get CoreDNS serving instance: %s" , err )
2016-08-19 17:14:17 -07:00
}
udp , _ := CoreDNSServerPorts ( server , 0 )
if udp == "" {
2016-10-02 08:31:44 +01:00
t . Fatalf ( "Could not get UDP listening port" )
2016-07-14 14:50:14 -07:00
}
2016-08-22 23:15:21 -07:00
return server , udp
}
func testLookupA ( t * testing . T ) {
corefile :=
` . : 0 {
kubernetes coredns . local {
endpoint http : //localhost:8080
namespaces demo
}
`
server , udp := createTestServer ( t , corefile )
2016-07-14 14:50:14 -07:00
defer server . Stop ( )
log . SetOutput ( ioutil . Discard )
2016-08-22 23:15:21 -07:00
// Work-around for timing condition that results in no-data being returned in
// test environment.
time . Sleep ( 5 * time . Second )
2016-07-14 14:50:14 -07:00
for _ , testData := range testdataLookupA {
dnsClient := new ( dns . Client )
dnsMessage := new ( dns . Msg )
dnsMessage . SetQuestion ( testData . Query , dns . TypeA )
dnsMessage . SetEdns0 ( 4096 , true )
res , _ , err := dnsClient . Exchange ( dnsMessage , udp )
if err != nil {
2016-08-20 23:03:36 +01:00
t . Fatalf ( "Could not send query: %s" , err )
2016-07-14 14:50:14 -07:00
}
// Count A records in the answer section
ARecordCount := 0
for _ , a := range res . Answer {
if a . Header ( ) . Rrtype == dns . TypeA {
ARecordCount ++
}
}
if ARecordCount != testData . ARecordCount {
t . Errorf ( "Expected '%v' A records in response. Instead got '%v' A records. Test query string: '%v'" , testData . ARecordCount , ARecordCount , testData . Query )
}
if len ( res . Answer ) != testData . TotalAnswerCount {
t . Errorf ( "Expected '%v' records in answer section. Instead got '%v' records in answer section. Test query string: '%v'" , testData . TotalAnswerCount , len ( res . Answer ) , testData . Query )
}
}
}
func testLookupSRV ( t * testing . T ) {
2016-08-19 17:14:17 -07:00
corefile :=
2016-07-22 16:07:27 -07:00
` . : 0 {
2016-07-14 14:50:14 -07:00
kubernetes coredns . local {
endpoint http : //localhost:8080
namespaces demo
}
`
2016-08-22 23:15:21 -07:00
server , udp := createTestServer ( t , corefile )
2016-07-14 14:50:14 -07:00
defer server . Stop ( )
log . SetOutput ( ioutil . Discard )
2016-08-22 23:15:21 -07:00
// Work-around for timing condition that results in no-data being returned in
// test environment.
time . Sleep ( 5 * time . Second )
2016-07-14 14:50:14 -07:00
// TODO: Add checks for A records in additional section
for _ , testData := range testdataLookupSRV {
dnsClient := new ( dns . Client )
dnsMessage := new ( dns . Msg )
dnsMessage . SetQuestion ( testData . Query , dns . TypeSRV )
dnsMessage . SetEdns0 ( 4096 , true )
res , _ , err := dnsClient . Exchange ( dnsMessage , udp )
if err != nil {
2016-08-20 23:03:36 +01:00
t . Fatalf ( "Could not send query: %s" , err )
2016-07-14 14:50:14 -07:00
}
// Count SRV records in the answer section
srvRecordCount := 0
for _ , a := range res . Answer {
if a . Header ( ) . Rrtype == dns . TypeSRV {
srvRecordCount ++
}
}
if srvRecordCount != testData . SRVRecordCount {
t . Errorf ( "Expected '%v' SRV records in response. Instead got '%v' SRV records. Test query string: '%v'" , testData . SRVRecordCount , srvRecordCount , testData . Query )
}
if len ( res . Answer ) != testData . TotalAnswerCount {
t . Errorf ( "Expected '%v' records in answer section. Instead got '%v' records in answer section. Test query string: '%v'" , testData . TotalAnswerCount , len ( res . Answer ) , testData . Query )
}
}
}