coredns/middleware/metrics/setup_test.go
Miek Gieben db6c9a3f01 middleware/metrics: fix crash on startup (#318)
Make the methods that handle Metrics all use pointer receivers to fix
sync.Once not being initialized.

Finish the setup_test to test for failures. And make the check for the
address more strict and return an error when it does not have a port
number.

Add a toplevel test that starts a CoreDNS server with metrics enabled
so we catch these errors in the future.
2016-10-04 11:05:04 +01:00

41 lines
868 B
Go

package metrics
import (
"testing"
"github.com/mholt/caddy"
)
func TestPrometheus(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, ""},
}
for i, test := range tests {
c := caddy.NewTestController("dns", test.input)
m, err := prometheusParse(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)
}
}
}