| 
									
										
										
										
											2016-07-27 10:01:24 -07:00
										 |  |  | // +build k8s
 | 
					
						
							| 
									
										
										
										
											2016-07-14 14:50:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | package test
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							| 
									
										
										
										
											2016-08-22 23:15:21 -07:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2016-07-14 14:50:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 16:24:06 -05:00
										 |  |  | 	"github.com/miekg/coredns/middleware/test"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-22 23:15:21 -07:00
										 |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							| 
									
										
										
										
											2016-07-14 14:50:14 -07:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 16:24:06 -05:00
										 |  |  | // Test data
 | 
					
						
							|  |  |  | // TODO: Fix the actual RR values
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var dnsTestCases = []test.Case{
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "mynginx.demo.svc.coredns.local.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("mynginx.demo.svc.coredns.local.      1800    IN      A       10.3.0.10"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "bogusservice.demo.svc.coredns.local.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Rcode:  dns.RcodeNameError,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "mynginx.*.svc.coredns.local.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("mynginx.demo.svc.coredns.local.      1800    IN      A       10.3.0.10"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "mynginx.any.svc.coredns.local.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("mynginx.demo.svc.coredns.local.      1800    IN      A       10.3.0.10"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "bogusservice.*.svc.coredns.local.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Rcode:  dns.RcodeNameError,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "bogusservice.any.svc.coredns.local.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Rcode:  dns.RcodeNameError,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "*.demo.svc.coredns.local.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("mynginx.demo.svc.coredns.local.      1800    IN      A       10.3.0.10"),
 | 
					
						
							|  |  |  | 			test.A("webserver.demo.svc.coredns.local.      1800    IN      A       10.3.0.20"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "any.demo.svc.coredns.local.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("mynginx.demo.svc.coredns.local.      1800    IN      A       10.3.0.10"),
 | 
					
						
							|  |  |  | 			test.A("webserver.demo.svc.coredns.local.      1800    IN      A       10.3.0.20"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "any.test.svc.coredns.local.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Rcode:  dns.RcodeNameError,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "*.test.svc.coredns.local.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Rcode:  dns.RcodeNameError,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "*.*.svc.coredns.local.", Qtype: dns.TypeA,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("mynginx.demo.svc.coredns.local.      1800    IN      A       10.3.0.10"),
 | 
					
						
							|  |  |  | 			test.A("webserver.demo.svc.coredns.local.      1800    IN      A       10.3.0.20"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	//TODO: Fix below to all use test.SRV not test.A!
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "mynginx.demo.svc.coredns.local.", Qtype: dns.TypeSRV,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("mynginx.demo.svc.coredns.local.      1800    IN      A       10.3.0.10"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "bogusservice.demo.svc.coredns.local.", Qtype: dns.TypeSRV,
 | 
					
						
							|  |  |  | 		Rcode:  dns.RcodeNameError,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "mynginx.*.svc.coredns.local.", Qtype: dns.TypeSRV,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("mynginx.demo.svc.coredns.local.      1800    IN      A       10.3.0.10"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "mynginx.any.svc.coredns.local.", Qtype: dns.TypeSRV,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("mynginx.demo.svc.coredns.local.      1800    IN      A       10.3.0.10"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "bogusservice.*.svc.coredns.local.", Qtype: dns.TypeSRV,
 | 
					
						
							|  |  |  | 		Rcode:  dns.RcodeNameError,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "bogusservice.any.svc.coredns.local.", Qtype: dns.TypeSRV,
 | 
					
						
							|  |  |  | 		Rcode:  dns.RcodeNameError,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "*.demo.svc.coredns.local.", Qtype: dns.TypeSRV,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("mynginx.demo.svc.coredns.local.      1800    IN      A       10.3.0.10"),
 | 
					
						
							|  |  |  | 			test.A("webserver.demo.svc.coredns.local.      1800    IN      A       10.3.0.20"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "any.demo.svc.coredns.local.", Qtype: dns.TypeSRV,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("mynginx.demo.svc.coredns.local.      1800    IN      A       10.3.0.10"),
 | 
					
						
							|  |  |  | 			test.A("webserver.demo.svc.coredns.local.      1800    IN      A       10.3.0.20"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "any.test.svc.coredns.local.", Qtype: dns.TypeSRV,
 | 
					
						
							|  |  |  | 		Rcode:  dns.RcodeNameError,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "*.test.svc.coredns.local.", Qtype: dns.TypeSRV,
 | 
					
						
							|  |  |  | 		Rcode:  dns.RcodeNameError,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							|  |  |  | 	{
 | 
					
						
							|  |  |  | 		Qname: "*.*.svc.coredns.local.", Qtype: dns.TypeSRV,
 | 
					
						
							|  |  |  | 		Rcode: dns.RcodeSuccess,
 | 
					
						
							|  |  |  | 		Answer: []dns.RR{
 | 
					
						
							|  |  |  | 			test.A("mynginx.demo.svc.coredns.local.      1800    IN      A       10.3.0.10"),
 | 
					
						
							|  |  |  | 			test.A("webserver.demo.svc.coredns.local.      1800    IN      A       10.3.0.20"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	},
 | 
					
						
							| 
									
										
										
										
											2016-07-14 14:50:14 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 16:24:06 -05:00
										 |  |  | func TestKubernetesIntegration(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2016-08-22 23:15:21 -07:00
										 |  |  | 	corefile :=
 | 
					
						
							|  |  |  | 		`.:0 {
 | 
					
						
							|  |  |  |     kubernetes coredns.local {
 | 
					
						
							| 
									
										
										
										
											2016-11-05 07:57:08 -04:00
										 |  |  |                 endpoint http://localhost:8080
 | 
					
						
							| 
									
										
										
										
											2016-11-10 16:24:06 -05:00
										 |  |  | 		#endpoint https://kubernetes/ admin.pem admin-key.pem ca.pem
 | 
					
						
							|  |  |  | 		#endpoint https://kubernetes/ 
 | 
					
						
							|  |  |  | 		#tls k8s_auth/client2.crt k8s_auth/client2.key k8s_auth/ca2.crt
 | 
					
						
							| 
									
										
										
										
											2016-08-22 23:15:21 -07:00
										 |  |  | 		namespaces demo
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | `
 | 
					
						
							|  |  |  | 	server, udp := createTestServer(t, corefile)
 | 
					
						
							| 
									
										
										
										
											2016-07-14 14:50:14 -07:00
										 |  |  | 	defer server.Stop()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-11-10 16:24:06 -05:00
										 |  |  | 	for _, tc := range dnsTestCases {
 | 
					
						
							| 
									
										
										
										
											2016-07-14 14:50:14 -07:00
										 |  |  | 		dnsClient := new(dns.Client)
 | 
					
						
							|  |  |  | 		dnsMessage := new(dns.Msg)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 16:24:06 -05:00
										 |  |  | 		dnsMessage.SetQuestion(tc.Qname, tc.Qtype)
 | 
					
						
							| 
									
										
										
										
											2016-07-14 14:50:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		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
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 16:24:06 -05:00
										 |  |  | 		// check the answer
 | 
					
						
							|  |  |  | 		if res.Rcode != tc.Rcode {
 | 
					
						
							|  |  |  | 			t.Errorf("Expected rcode %d but got %d for query %s, %d", tc.Rcode, res.Rcode, tc.Qname, tc.Qtype)
 | 
					
						
							| 
									
										
										
										
											2016-07-14 14:50:14 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 16:24:06 -05:00
										 |  |  | 		if len(res.Answer) != len(tc.Answer) {
 | 
					
						
							|  |  |  | 			t.Errorf("Expected %d answers but got %d for query %s, %d", len(tc.Answer), len(res.Answer), tc.Qname, tc.Qtype)
 | 
					
						
							| 
									
										
										
										
											2016-07-14 14:50:14 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 16:24:06 -05:00
										 |  |  | 		//TODO: Check the actual RR values
 | 
					
						
							| 
									
										
										
										
											2016-07-14 14:50:14 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 |