| 
									
										
										
										
											2018-06-29 12:44:16 +03:00
										 |  |  | package metadata
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"context"
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/test"
 | 
					
						
							| 
									
										
										
										
											2018-06-29 15:03:25 +01:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2018-06-29 12:44:16 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-01 20:01:17 +01:00
										 |  |  | type testProvider map[string]Func
 | 
					
						
							| 
									
										
										
										
											2018-06-29 12:44:16 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-01 20:01:17 +01:00
										 |  |  | func (tp testProvider) Metadata(ctx context.Context, state request.Request) context.Context {
 | 
					
						
							|  |  |  | 	for k, v := range tp {
 | 
					
						
							|  |  |  | 		SetValueFunc(ctx, k, v)
 | 
					
						
							| 
									
										
										
										
											2018-06-29 12:44:16 +03:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-07-01 20:01:17 +01:00
										 |  |  | 	return ctx
 | 
					
						
							| 
									
										
										
										
											2018-06-29 12:44:16 +03:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type testHandler struct{ ctx context.Context }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-01 20:01:17 +01:00
										 |  |  | func (m *testHandler) Name() string { return "test" }
 | 
					
						
							| 
									
										
										
										
											2018-06-29 12:44:16 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (m *testHandler) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 	m.ctx = ctx
 | 
					
						
							|  |  |  | 	return 0, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 15:03:25 +01:00
										 |  |  | func TestMetadataServeDNS(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2018-06-29 12:44:16 +03:00
										 |  |  | 	expectedMetadata := []testProvider{
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 		{"test/key1": func() string { return "testvalue1" }},
 | 
					
						
							|  |  |  | 		{"test/key2": func() string { return "two" }, "test/key3": func() string { return "testvalue3" }},
 | 
					
						
							| 
									
										
										
										
											2018-06-29 12:44:16 +03:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	// Create fake Providers based on expectedMetadata
 | 
					
						
							|  |  |  | 	providers := []Provider{}
 | 
					
						
							|  |  |  | 	for _, e := range expectedMetadata {
 | 
					
						
							|  |  |  | 		providers = append(providers, e)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 15:03:25 +01:00
										 |  |  | 	next := &testHandler{} // fake handler which stores the resulting context
 | 
					
						
							| 
									
										
										
										
											2018-07-01 20:01:17 +01:00
										 |  |  | 	m := Metadata{
 | 
					
						
							| 
									
										
										
										
											2018-06-29 12:44:16 +03:00
										 |  |  | 		Zones:     []string{"."},
 | 
					
						
							|  |  |  | 		Providers: providers,
 | 
					
						
							|  |  |  | 		Next:      next,
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-01 20:01:17 +01:00
										 |  |  | 	ctx := context.TODO()
 | 
					
						
							|  |  |  | 	m.ServeDNS(ctx, &test.ResponseWriter{}, new(dns.Msg))
 | 
					
						
							|  |  |  | 	nctx := next.ctx
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 12:44:16 +03:00
										 |  |  | 	for _, expected := range expectedMetadata {
 | 
					
						
							| 
									
										
										
										
											2018-07-01 20:01:17 +01:00
										 |  |  | 		for label, expVal := range expected {
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 			if !IsLabel(label) {
 | 
					
						
							|  |  |  | 				t.Errorf("Expected label %s is not considered a valid label", label)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-07-01 20:01:17 +01:00
										 |  |  | 			val := ValueFunc(nctx, label)
 | 
					
						
							|  |  |  | 			if val() != expVal() {
 | 
					
						
							|  |  |  | 				t.Errorf("Expected value %s for %s, but got %s", expVal(), label, val())
 | 
					
						
							| 
									
										
										
										
											2018-06-29 12:44:16 +03:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestLabelFormat(t *testing.T) {
 | 
					
						
							|  |  |  | 	labels := []struct {
 | 
					
						
							|  |  |  | 		label   string
 | 
					
						
							|  |  |  | 		isValid bool
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							| 
									
										
										
										
											2018-07-09 07:58:14 +01:00
										 |  |  | 		// ok
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 		{"plugin/LABEL", true},
 | 
					
						
							|  |  |  | 		{"p/LABEL", true},
 | 
					
						
							|  |  |  | 		{"plugin/L", true},
 | 
					
						
							| 
									
										
										
										
											2021-07-14 08:25:30 +01:00
										 |  |  | 		{"PLUGIN/LABEL/SUB-LABEL", true},
 | 
					
						
							| 
									
										
										
										
											2018-07-09 07:58:14 +01:00
										 |  |  | 		// fails
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 		{"LABEL", false},
 | 
					
						
							|  |  |  | 		{"plugin.LABEL", false},
 | 
					
						
							|  |  |  | 		{"/NO-PLUGIN-NOT-ACCEPTED", false},
 | 
					
						
							|  |  |  | 		{"ONLY-PLUGIN-NOT-ACCEPTED/", false},
 | 
					
						
							|  |  |  | 		{"/", false},
 | 
					
						
							| 
									
										
										
										
											2018-07-09 07:58:14 +01:00
										 |  |  | 		{"//", false},
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, test := range labels {
 | 
					
						
							| 
									
										
										
										
											2018-07-09 07:58:14 +01:00
										 |  |  | 		if x := IsLabel(test.label); x != test.isValid {
 | 
					
						
							|  |  |  | 			t.Errorf("Label %v expected %v, got: %v", test.label, test.isValid, x)
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |