mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	* Commenting out unused functions. TODO: remove when it is not needed * Update README with namespace and template example * Adding note about changing the record name format via a template * Adding test scripts to automate k8s startup * Automating k8s namespace creation * Adding automation to start 4 k8s services * Updating documentation for k8s tests * Avoid downloading kubectl if already exists * Adding debug statement when namespace is not exposed. * Adding basic kubernetes integration tests * Makefile now contains a "testk8s" target. This target requires k8s to be running. * Adding test/kubernetes_test.go file with a couple of basic A record tests. * Updating k8s integration tests to only run k8s integration tests * Adding support for namespace wildcards * Refactoring to move filtering logic to kubernetes.go file * go fmt fixes * Adding wildcard support for namespaces and service names * Kubernetes integration tests updated for A records. * Expanded record name assembly for answer section not yet implemented. * Refactoring to focus k8sclient code just on accessing k8s API. Filtering now handled in kubernetes.go * Adding wildcard test cases * Adding skydns startup script. (To allow side by side testing of wildcards.) * Commenting out record name assmebly based on NameTemplate. Need to improve template before this makes sense. * Adding basic SRV integration tests * Need to add verification for additional answer section * Fixing comments and formatting * Moving wildcard constants to vars * Travis test execution appears to be failing on access to these constants * Fixing access to util package * Trying to work around Travis test bug * Reverting to access kubernetes/util as "util" Travis breakage is due to "Infoblox-CTO" in src path
		
			
				
	
	
		
			183 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			183 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // +build k8sIntegration
 | |
| 
 | |
| package test
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"io/ioutil"
 | |
| 	"log"
 | |
| 	"net/http"
 | |
| 	"testing"
 | |
| 
 | |
| 	"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
 | |
| }
 | |
| 
 | |
| // checkKubernetesRunning performs a basic
 | |
| func checkKubernetesRunning() bool {
 | |
| 	_, err := http.Get("http://localhost:8080/api/v1")
 | |
| 	return err == nil
 | |
| }
 | |
| 
 | |
| func TestK8sIntegration(t *testing.T) {
 | |
| 	t.Log("   === RUN testLookupA")
 | |
| 	testLookupA(t)
 | |
| 	t.Log("   === RUN testLookupSRV")
 | |
| 	testLookupSRV(t)
 | |
| }
 | |
| 
 | |
| func testLookupA(t *testing.T) {
 | |
| 	if !checkKubernetesRunning() {
 | |
| 		t.Skip("Skipping Kubernetes Integration tests. Kubernetes is not running")
 | |
| 	}
 | |
| 
 | |
| 	// Note: Use different port to avoid conflict with servers used in other tests.
 | |
| 	coreFile :=
 | |
| 		`.:2053 {
 | |
|     kubernetes coredns.local {
 | |
| 		endpoint http://localhost:8080
 | |
| 		namespaces demo
 | |
|     }
 | |
| `
 | |
| 
 | |
| 	server, _, udp, err := Server(t, coreFile)
 | |
| 	if err != nil {
 | |
| 		t.Fatal("Could not get server: %s", err)
 | |
| 	}
 | |
| 	defer server.Stop()
 | |
| 
 | |
| 	log.SetOutput(ioutil.Discard)
 | |
| 
 | |
| 	for _, testData := range testdataLookupA {
 | |
| 		t.Logf("[log] Testing query string: '%v'\n", testData.Query)
 | |
| 		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 {
 | |
| 			t.Fatal("Could not send query: %s", err)
 | |
| 		}
 | |
| 		// 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) {
 | |
| 	if !checkKubernetesRunning() {
 | |
| 		t.Skip("Skipping Kubernetes Integration tests. Kubernetes is not running")
 | |
| 	}
 | |
| 
 | |
| 	// Note: Use different port to avoid conflict with servers used in other tests.
 | |
| 	coreFile :=
 | |
| 		`.:2054 {
 | |
|     kubernetes coredns.local {
 | |
| 		endpoint http://localhost:8080
 | |
| 		namespaces demo
 | |
|     }
 | |
| `
 | |
| 
 | |
| 	server, _, udp, err := Server(t, coreFile)
 | |
| 	if err != nil {
 | |
| 		t.Fatal("Could not get server: %s", err)
 | |
| 	}
 | |
| 	defer server.Stop()
 | |
| 
 | |
| 	log.SetOutput(ioutil.Discard)
 | |
| 
 | |
| 	// TODO: Add checks for A records in additional section
 | |
| 
 | |
| 	for _, testData := range testdataLookupSRV {
 | |
| 		t.Logf("[log] Testing query string: '%v'\n", testData.Query)
 | |
| 		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 {
 | |
| 			t.Fatal("Could not send query: %s", err)
 | |
| 		}
 | |
| 		// Count SRV records in the answer section
 | |
| 		srvRecordCount := 0
 | |
| 		for _, a := range res.Answer {
 | |
| 			fmt.Printf("RR: %v\n", a)
 | |
| 			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)
 | |
| 		}
 | |
| 	}
 | |
| }
 |