-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver_test.go
164 lines (152 loc) · 3.72 KB
/
resolver_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"context"
"fmt"
lru "github.com/hashicorp/golang-lru/v2"
"io"
"net"
"reflect"
"testing"
)
type fakeResolver struct {
fnLookupIP func(ctx context.Context, network, host string) ([]net.IP, error)
}
func (f fakeResolver) LookupIP(ctx context.Context, network, host string) ([]net.IP, error) {
return f.fnLookupIP(ctx, network, host)
}
func Test_resolver_LookupIP(t *testing.T) {
var (
localhost = net.ParseIP("127.0.0.1")
ips = []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("8.8.8.8"), net.ParseIP("8.8.4.4")}
)
type fields struct {
resolver interface {
LookupIP(ctx context.Context, network, host string) ([]net.IP, error)
}
cache *lru.Cache[string, []net.IP]
}
type args struct {
ctx context.Context
network string
host string
}
tests := []struct {
name string
fields fields
args args
check func([]net.IP, error) error
}{
{
name: "common valid case 1 ip",
fields: fields{
resolver: fakeResolver{
fnLookupIP: func(ctx context.Context, network, host string) ([]net.IP, error) {
return append([]net.IP{}, localhost), nil
},
},
cache: newSyncCache[string, []net.IP](100),
},
args: args{},
check: func(ip []net.IP, err error) error {
if err != nil {
return fmt.Errorf("got unexcepted error: %w", err)
}
if !reflect.DeepEqual(ip, append([]net.IP{}, localhost)) {
return fmt.Errorf("got %v want %v", ip, localhost)
}
return nil
},
},
{
name: "common valid case multiple ip",
fields: fields{
resolver: fakeResolver{
fnLookupIP: func(ctx context.Context, network, host string) ([]net.IP, error) {
return append([]net.IP{}, ips...), nil
},
},
cache: newSyncCache[string, []net.IP](100),
},
args: args{},
check: func(ip []net.IP, err error) error {
if err != nil {
return fmt.Errorf("got unexcepted error: %w", err)
}
if !reflect.DeepEqual(ip, ips) {
return fmt.Errorf("got %v want %v", ip, ips)
}
return nil
},
},
{
name: "invalid case ips <nil>",
fields: fields{
resolver: fakeResolver{
fnLookupIP: func(ctx context.Context, network, host string) ([]net.IP, error) {
return nil, nil
},
},
cache: newSyncCache[string, []net.IP](100),
},
args: args{},
check: func(ip []net.IP, err error) error {
if err == nil {
return fmt.Errorf("must be error but got nil")
}
return nil
},
},
{
name: "invalid case ips empty",
fields: fields{
resolver: fakeResolver{
fnLookupIP: func(ctx context.Context, network, host string) ([]net.IP, error) {
return nil, nil
},
},
cache: newSyncCache[string, []net.IP](100),
},
args: args{},
check: func(ip []net.IP, err error) error {
if err == nil {
return fmt.Errorf("must be error but got nil")
}
return nil
},
},
{
name: "lookup error",
fields: fields{
resolver: fakeResolver{
fnLookupIP: func(ctx context.Context, network, host string) ([]net.IP, error) {
return nil, io.EOF
},
},
cache: newSyncCache[string, []net.IP](100),
},
args: args{},
check: func(ip []net.IP, err error) error {
if !reflect.DeepEqual(err, io.EOF) {
return fmt.Errorf("got %v, want %v", err, io.EOF)
}
return nil
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &resolver{
resolver: tt.fields.resolver,
cache: tt.fields.cache,
sg: &singleflight[string, []net.IP]{
m: make(map[string]*singleflightResult[[]net.IP]),
},
}
got, err := r.LookupIP(tt.args.ctx, tt.args.network, tt.args.host)
if err := tt.check(got, err); err != nil {
t.Errorf("LookupIP(): %v", err)
return
}
})
}
}