| 1 |
package log_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"log/slog" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"codeberg.org/miekg/dns/cmd/atomdns/atomtest" |
| 11 |
"codeberg.org/miekg/dns/cmd/atomdns/handlers/log" |
| 12 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnsctx" |
| 13 |
"codeberg.org/miekg/dns/dnstest" |
| 14 |
) |
| 15 |
|
| 16 |
func TestLog(t *testing.T) { |
| 17 |
l := &log.Log{ |
| 18 |
Contexts: map[string][]string{ |
| 19 |
"hello": {"here", "there"}, |
| 20 |
}, |
| 21 |
} |
| 22 |
|
| 23 |
b := &bytes.Buffer{} |
| 24 |
logger := slog.New(slog.NewTextHandler(b, nil)) |
| 25 |
slog.SetDefault(logger) |
| 26 |
m := dnstest.NewMsg() |
| 27 |
|
| 28 |
ctx := context.Background() |
| 29 |
ctx = dnsctx.WithValue(ctx, "hello/here", "not far") |
| 30 |
ctx = dnsctx.WithValue(ctx, "hello/there", "far") |
| 31 |
|
| 32 |
testcases := []struct { |
| 33 |
name string |
| 34 |
exp []string |
| 35 |
}{ |
| 36 |
{"ipv4", |
| 37 |
[]string{`hello.here="not far" hello.there=far`, "remote=198.51.100.1"}, |
| 38 |
}, |
| 39 |
{"ipv6", |
| 40 |
[]string{`hello.here="not far" hello.there=far`, "remote=2001:db8::1"}, |
| 41 |
}, |
| 42 |
} |
| 43 |
|
| 44 |
for _, tc := range testcases { |
| 45 |
t.Run(tc.name, func(t *testing.T) { |
| 46 |
b.Reset() |
| 47 |
tw := dnstest.NewTestRecorder() |
| 48 |
if tc.name == "ipv6" { |
| 49 |
tw = dnstest.NewTestRecorder6() |
| 50 |
} |
| 51 |
l.HandlerFunc(atomtest.Echo).ServeDNS(ctx, tw, m) |
| 52 |
s := b.String() |
| 53 |
for _, exp := range tc.exp { |
| 54 |
if !strings.Contains(s, exp) { |
| 55 |
t.Fatalf("expected %s, got none", exp) |
| 56 |
} |
| 57 |
} |
| 58 |
}) |
| 59 |
} |
| 60 |
} |