Skip to content

Commit

Permalink
dnsforward: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Nov 23, 2023
1 parent 120ba4b commit 49fefc3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/dnsforward/upstreams.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,10 @@ func (cv *upstreamConfigValidator) status() (results map[string]string) {
cv.private,
} {
for _, res := range results {
if _, ok := result[res.original]; ok {
log.Debug("dnsforward: warning: duplicate configuration %q", res.original)
}

if res.err != nil {
result[res.original] = res.err.Error()
} else {
Expand Down
46 changes: 46 additions & 0 deletions internal/dnsforward/upstreams_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"
"net/url"
"strings"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -161,3 +162,48 @@ func TestUpstreamConfigValidator(t *testing.T) {
})
}
}

func TestUpstreamConfigValidator_Check_once(t *testing.T) {
reqs := atomic.Int32{}
reset := func() { reqs.Store(0) }

hdlr := dns.HandlerFunc(func(w dns.ResponseWriter, m *dns.Msg) {
err := w.WriteMsg(new(dns.Msg).SetReply(m))
require.NoError(testutil.PanicT{}, err)
reqs.Add(1)
})

addr := (&url.URL{
Scheme: "tcp",
Host: newLocalUpstreamListener(t, 0, hdlr).String(),
}).String()
twoAddrs := strings.Join([]string{addr, addr}, " ")

testCases := []struct {
name string
ups []string
}{{
name: "common",
ups: []string{addr, addr, addr},
}, {
name: "domain-specific",
ups: []string{"[/one.example/]" + addr, "[/two.example/]" + twoAddrs},
}, {
name: "both",
ups: []string{addr, "[/one.example/]" + addr, addr, "[/two.example/]" + twoAddrs},
}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Cleanup(reset)

cv := newUpstreamConfigValidator(tc.ups, nil, nil, &upstream.Options{
Timeout: 100 * time.Millisecond,
})
cv.check()
cv.close()

assert.Equal(t, int32(1), reqs.Load())
})
}
}

0 comments on commit 49fefc3

Please sign in to comment.