mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-03 18:53:13 -05:00 
			
		
		
		
	Fix metrics endpoint on a failed reload, follows the same lines as the
previous PRs, see for e.g. 076b8d4f. Test with a Corefile with 2 server
blocks and metrics enabled and then introducing a syntax error:
~~~
[ERROR] Restart failed: Corefile:5 - Error during parsing: Unknown directive 'jfkdjk'
[ERROR] SIGUSR1: starting with listener file descriptors: Corefile:5 - Error during parsing: Unknown directive 'jfkdjk'
~~~
And then curl-ing the metrics endpoint.
See #2659 and as this is the last one.
Fixes: #2659
Getting this all right turns out to be tricky, also it's not easy
testable which is something I should fix.
Signed-off-by: Miek Gieben <miek@miek.nl>
		
	
		
			
				
	
	
		
			43 lines
		
	
	
		
			897 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			897 B
		
	
	
	
		
			Go
		
	
	
	
	
	
package metrics
 | 
						|
 | 
						|
import (
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/mholt/caddy"
 | 
						|
)
 | 
						|
 | 
						|
func TestPrometheusParse(t *testing.T) {
 | 
						|
	tests := []struct {
 | 
						|
		input     string
 | 
						|
		shouldErr bool
 | 
						|
		addr      string
 | 
						|
	}{
 | 
						|
		// oks
 | 
						|
		{`prometheus`, false, "localhost:9153"},
 | 
						|
		{`prometheus localhost:53`, false, "localhost:53"},
 | 
						|
		// fails
 | 
						|
		{`prometheus {}`, true, ""},
 | 
						|
		{`prometheus /foo`, true, ""},
 | 
						|
		{`prometheus a b c`, true, ""},
 | 
						|
	}
 | 
						|
	for i, test := range tests {
 | 
						|
		c := caddy.NewTestController("dns", test.input)
 | 
						|
		m, err := parse(c)
 | 
						|
		if test.shouldErr && err == nil {
 | 
						|
			t.Errorf("Test %v: Expected error but found nil", i)
 | 
						|
			continue
 | 
						|
		} else if !test.shouldErr && err != nil {
 | 
						|
			t.Errorf("Test %v: Expected no error but found error: %v", i, err)
 | 
						|
			continue
 | 
						|
		}
 | 
						|
 | 
						|
		if test.shouldErr {
 | 
						|
			continue
 | 
						|
		}
 | 
						|
 | 
						|
		if test.addr != m.Addr {
 | 
						|
			t.Errorf("Test %v: Expected address %s but found: %s", i, test.addr, m.Addr)
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 |