mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	test(metrics): improve test coverage (#7538)
Add more unit tests for metrics plugin, around registration deduplication, zone management, restart/shutdown behavior and context helpers. Increases test coverage from 54.8% to 76.1%. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
This commit is contained in:
		| @@ -2,7 +2,9 @@ package metrics | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"io" | ||||
| 	"net" | ||||
| 	"net/http" | ||||
| 	"testing" | ||||
| 	"time" | ||||
|  | ||||
| @@ -11,6 +13,8 @@ import ( | ||||
| 	"github.com/coredns/coredns/plugin/test" | ||||
|  | ||||
| 	"github.com/miekg/dns" | ||||
| 	"github.com/prometheus/client_golang/prometheus" | ||||
| 	"github.com/prometheus/client_golang/prometheus/promauto" | ||||
| ) | ||||
|  | ||||
| func TestMetrics(t *testing.T) { | ||||
| @@ -127,3 +131,87 @@ func TestMetricsHTTPTimeout(t *testing.T) { | ||||
| 		t.Error("HTTP request did not time out") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestMustRegister_DuplicateOK(t *testing.T) { | ||||
| 	met := New("localhost:0") | ||||
| 	met.Reg = prometheus.NewRegistry() | ||||
|  | ||||
| 	g := promauto.NewGaugeVec(prometheus.GaugeOpts{Namespace: "test", Name: "dup"}, []string{"l"}) | ||||
| 	met.MustRegister(g) | ||||
| 	// registering the same collector again should yield AlreadyRegisteredError internally and be ignored | ||||
| 	met.MustRegister(g) | ||||
| } | ||||
|  | ||||
| func TestRemoveZone(t *testing.T) { | ||||
| 	met := New("localhost:0") | ||||
|  | ||||
| 	met.AddZone("example.org.") | ||||
| 	met.AddZone("example.net.") | ||||
| 	met.RemoveZone("example.net.") | ||||
|  | ||||
| 	zones := met.ZoneNames() | ||||
| 	for _, z := range zones { | ||||
| 		if z == "example.net." { | ||||
| 			t.Fatalf("zone %q still present after RemoveZone", z) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestOnRestartStopsServer(t *testing.T) { | ||||
| 	met := New("localhost:0") | ||||
| 	if err := met.OnStartup(); err != nil { | ||||
| 		t.Fatalf("startup failed: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	// server should respond before restart | ||||
| 	resp, err := http.Get("http://" + ListenAddr + "/metrics") | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("pre-restart GET failed: %v", err) | ||||
| 	} | ||||
| 	if resp != nil { | ||||
| 		io.Copy(io.Discard, resp.Body) | ||||
| 		resp.Body.Close() | ||||
| 	} | ||||
|  | ||||
| 	if err := met.OnRestart(); err != nil { | ||||
| 		t.Fatalf("restart failed: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	// after restart, the listener should be closed and request should fail | ||||
| 	if _, err := http.Get("http://" + ListenAddr + "/metrics"); err == nil { | ||||
| 		t.Fatalf("expected GET to fail after restart, but it succeeded") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestRegistryGetOrSet(t *testing.T) { | ||||
| 	r := newReg() | ||||
| 	addr := "localhost:12345" | ||||
| 	pr1 := prometheus.NewRegistry() | ||||
| 	got1 := r.getOrSet(addr, pr1) | ||||
| 	if got1 != pr1 { | ||||
| 		t.Fatalf("first getOrSet should return provided registry") | ||||
| 	} | ||||
|  | ||||
| 	pr2 := prometheus.NewRegistry() | ||||
| 	got2 := r.getOrSet(addr, pr2) | ||||
| 	if got2 != pr1 { | ||||
| 		t.Fatalf("second getOrSet should return original registry, got different one") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestOnRestartNoop(t *testing.T) { | ||||
| 	met := New("localhost:0") | ||||
| 	// without OnStartup, OnRestart should be a no-op | ||||
| 	if err := met.OnRestart(); err != nil { | ||||
| 		t.Fatalf("OnRestart returned error on no-op: %v", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestContextHelpersEmpty(t *testing.T) { | ||||
| 	if got := WithServer(context.TODO()); got != "" { | ||||
| 		t.Fatalf("WithServer(nil) = %q, want empty", got) | ||||
| 	} | ||||
| 	if got := WithView(context.TODO()); got != "" { | ||||
| 		t.Fatalf("WithView(nil) = %q, want empty", got) | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -40,3 +40,10 @@ func TestPrometheusParse(t *testing.T) { | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestSetupBasic(t *testing.T) { | ||||
| 	c := caddy.NewTestController("dns", "prometheus localhost:9153") | ||||
| 	if err := setup(c); err != nil { | ||||
| 		t.Fatalf("setup returned error: %v", err) | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user