From bb33ab5301908b165e9df65b3f879d4f2bc3e4eb Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Mon, 2 Jun 2025 02:30:01 +0300 Subject: [PATCH] test(plugin): add tests for any (#7341) --- plugin/any/any_test.go | 39 +++++++++++++++++++++++++++++++++++++++ plugin/any/setup_test.go | 21 +++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 plugin/any/setup_test.go diff --git a/plugin/any/any_test.go b/plugin/any/any_test.go index 85df7d672..846a804a0 100644 --- a/plugin/any/any_test.go +++ b/plugin/any/any_test.go @@ -26,3 +26,42 @@ func TestAny(t *testing.T) { t.Errorf("Expected HINFO, but got %q", rec.Msg.Answer[0].(*dns.HINFO).Cpu) } } + +func TestAnyNonANYQuery(t *testing.T) { + tests := []struct { + name string + qtype uint16 + }{ + {"A query", dns.TypeA}, + {"AAAA query", dns.TypeAAAA}, + {"MX query", dns.TypeMX}, + {"TXT query", dns.TypeTXT}, + {"CNAME query", dns.TypeCNAME}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := new(dns.Msg) + req.SetQuestion("example.org.", tt.qtype) + + nextCalled := false + a := &Any{ + Next: test.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { + nextCalled = true + return 0, nil + }), + } + + rec := dnstest.NewRecorder(&test.ResponseWriter{}) + _, err := a.ServeDNS(context.TODO(), rec, req) + + if err != nil { + t.Errorf("Expected no error, but got %q", err) + } + + if !nextCalled { + t.Error("Expected Next handler to be called for non-ANY query") + } + }) + } +} diff --git a/plugin/any/setup_test.go b/plugin/any/setup_test.go new file mode 100644 index 000000000..49c5ae827 --- /dev/null +++ b/plugin/any/setup_test.go @@ -0,0 +1,21 @@ +package any + +import ( + "testing" + + "github.com/coredns/caddy" + "github.com/coredns/coredns/core/dnsserver" +) + +func TestSetup(t *testing.T) { + c := caddy.NewTestController("dns", `any`) + if err := setup(c); err != nil { + t.Fatalf("Expected no errors, but got: %v", err) + } + + // Check that the plugin was added to the config + cfg := dnsserver.GetConfig(c) + if len(cfg.Plugin) == 0 { + t.Error("Expected plugin to be added to config") + } +}