-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_example_mock_test.go
66 lines (50 loc) · 1.22 KB
/
e2e_example_mock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package dnsmock
import (
"testing"
"github.com/miekg/dns"
"github.com/shawnburke/dnsmock/resolver"
"github.com/shawnburke/dnsmock/spec"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
// This is an example E22 usage of the library where
// we create a DNS server and mock out Google DNS
// to 4.3.2.1
const specYaml = `
rules:
- name: google.com.
records:
A:
- "google.com. 300 IN A 4.3.2.1"
`
func TestMock(t *testing.T) {
logger := zap.NewNop()
// spec is our wrapper around the YAML config
s := spec.FromYAML(specYaml)
// a replay resolver resolves DNS using the spec
resolver := resolver.NewReplay(s, logger)
// finally, createa proxy that uses the spec resolver
// only
proxy := New(":0", resolver, logger)
err := proxy.Start()
require.NoError(t, err)
defer proxy.Stop()
// test it using the miekg/dns library
client := &dns.Client{
Net: "udp",
}
query := &dns.Msg{
Question: []dns.Question{
{
Name: "google.com.",
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
},
},
}
res, _, err := client.Exchange(query, proxy.Addr())
require.NoError(t, err)
require.Equal(t, 1, len(res.Answer))
a := res.Answer[0].(*dns.A)
require.Equal(t, "4.3.2.1", a.A.String())
}