Skip to content

Commit b1e6640

Browse files
authored
Merge pull request #4339 from alexandear/refactor/dns-simplify-newStaticClientConfig
dns: simplify newStaticClientConfig implementation
2 parents 0ade289 + 38a94b3 commit b1e6640

File tree

2 files changed

+110
-7
lines changed

2 files changed

+110
-7
lines changed

pkg/hostagent/dns/dns.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package dns
77

88
import (
99
"context"
10-
"fmt"
1110
"net"
1211
"runtime"
1312
"strconv"
@@ -69,12 +68,8 @@ func (s *Server) Shutdown() {
6968

7069
func newStaticClientConfig(ips []string) (*dns.ClientConfig, error) {
7170
logrus.Tracef("newStaticClientConfig creating config for the following IPs: %v", ips)
72-
s := ``
73-
for _, ip := range ips {
74-
s += fmt.Sprintf("nameserver %s\n", ip)
75-
}
76-
r := strings.NewReader(s)
77-
return dns.ClientConfigFromReader(r)
71+
config := "nameserver " + strings.Join(ips, "\nnameserver ") + "\n"
72+
return dns.ClientConfigFromReader(strings.NewReader(config))
7873
}
7974

8075
func (h *Handler) lookupCnameToHost(cname string) string {

pkg/hostagent/dns/dns_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,114 @@ import (
2020

2121
var dnsResult *dns.Msg
2222

23+
func TestNewHandler(t *testing.T) {
24+
t.Run("with upstream servers", func(t *testing.T) {
25+
upstreamServers := []string{"8.8.4.4", "1.1.1.1", "9.9.9.9"}
26+
opts := HandlerOptions{
27+
IPv6: true,
28+
UpstreamServers: upstreamServers,
29+
StaticHosts: map[string]string{
30+
"test.local": "192.168.1.1",
31+
"alias.local": "test.local",
32+
},
33+
}
34+
h, err := NewHandler(opts)
35+
assert.NilError(t, err)
36+
assert.Assert(t, h != nil)
37+
38+
handler := h.(*Handler)
39+
assert.Equal(t, handler.ipv6, true)
40+
assert.Equal(t, len(handler.clients), 2)
41+
assert.DeepEqual(t, handler.clientConfig.Servers, upstreamServers)
42+
assert.Equal(t, handler.hostToIP["test.local."].String(), "192.168.1.1")
43+
assert.Equal(t, handler.cnameToHost["alias.local."], "test.local.")
44+
})
45+
46+
t.Run("without upstream servers on non-Windows", func(t *testing.T) {
47+
if runtime.GOOS == "windows" {
48+
t.Skip("Skipping on Windows")
49+
}
50+
opts := HandlerOptions{
51+
IPv6: false,
52+
StaticHosts: map[string]string{},
53+
}
54+
h, err := NewHandler(opts)
55+
assert.NilError(t, err)
56+
assert.Assert(t, h != nil)
57+
58+
handler := h.(*Handler)
59+
assert.Equal(t, handler.ipv6, false)
60+
assert.Assert(t, handler.clientConfig != nil)
61+
})
62+
63+
t.Run("without upstream servers on Windows", func(t *testing.T) {
64+
if runtime.GOOS != "windows" {
65+
t.Skip("Skipping on non-Windows")
66+
}
67+
opts := HandlerOptions{
68+
IPv6: false,
69+
StaticHosts: map[string]string{},
70+
}
71+
h, err := NewHandler(opts)
72+
assert.NilError(t, err)
73+
assert.Assert(t, h != nil)
74+
75+
handler := h.(*Handler)
76+
assert.Equal(t, handler.ipv6, false)
77+
assert.Assert(t, handler.clientConfig != nil)
78+
// Should use default fallback IPs on Windows
79+
assert.Assert(t, len(handler.clientConfig.Servers) > 0)
80+
})
81+
82+
t.Run("with invalid upstream servers fallback", func(t *testing.T) {
83+
opts := HandlerOptions{
84+
IPv6: true,
85+
UpstreamServers: []string{}, // empty should trigger default behavior
86+
StaticHosts: map[string]string{},
87+
}
88+
h, err := NewHandler(opts)
89+
assert.NilError(t, err)
90+
assert.Assert(t, h != nil)
91+
})
92+
93+
t.Run("with static hosts IP and CNAME", func(t *testing.T) {
94+
opts := HandlerOptions{
95+
IPv6: true,
96+
UpstreamServers: []string{"8.8.8.8"},
97+
StaticHosts: map[string]string{
98+
"host1.local": "10.0.0.1",
99+
"host2.local": "10.0.0.2",
100+
"cname1": "host1.local",
101+
"cname2": "cname1",
102+
},
103+
}
104+
h, err := NewHandler(opts)
105+
assert.NilError(t, err)
106+
assert.Assert(t, h != nil)
107+
108+
handler := h.(*Handler)
109+
assert.Equal(t, handler.hostToIP["host1.local."].String(), "10.0.0.1")
110+
assert.Equal(t, handler.hostToIP["host2.local."].String(), "10.0.0.2")
111+
assert.Equal(t, handler.cnameToHost["cname1."], "host1.local.")
112+
assert.Equal(t, handler.cnameToHost["cname2."], "cname1.")
113+
})
114+
115+
t.Run("with truncate option", func(t *testing.T) {
116+
opts := HandlerOptions{
117+
IPv6: false,
118+
UpstreamServers: []string{"1.1.1.1"},
119+
TruncateReply: true,
120+
StaticHosts: map[string]string{},
121+
}
122+
h, err := NewHandler(opts)
123+
assert.NilError(t, err)
124+
assert.Assert(t, h != nil)
125+
126+
handler := h.(*Handler)
127+
assert.Equal(t, handler.truncate, true)
128+
})
129+
}
130+
23131
func TestDNSRecords(t *testing.T) {
24132
if runtime.GOOS == "windows" {
25133
// "On Windows, the resolver always uses C library functions, such as GetAddrInfo and DnsQuery."

0 commit comments

Comments
 (0)