-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdns_test.go
184 lines (139 loc) · 5.13 KB
/
dns_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) David Bond, Tailscale Inc, & Contributors
// SPDX-License-Identifier: MIT
package tailscale
import (
"context"
"encoding/json"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestClient_DNSNameservers(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
expectedNameservers := map[string][]string{
"dns": {"127.0.0.1"},
}
server.ResponseBody = expectedNameservers
nameservers, err := client.DNS().Nameservers(context.Background())
assert.NoError(t, err)
assert.Equal(t, http.MethodGet, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/dns/nameservers", server.Path)
assert.Equal(t, expectedNameservers["dns"], nameservers)
}
func TestClient_DNSPreferences(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
server.ResponseBody = &DNSPreferences{
MagicDNS: true,
}
preferences, err := client.DNS().Preferences(context.Background())
assert.NoError(t, err)
assert.Equal(t, http.MethodGet, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/dns/preferences", server.Path)
assert.Equal(t, server.ResponseBody, preferences)
}
func TestClient_DNSSearchPaths(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
expectedPaths := map[string][]string{
"searchPaths": {"test"},
}
server.ResponseBody = expectedPaths
paths, err := client.DNS().SearchPaths(context.Background())
assert.NoError(t, err)
assert.Equal(t, http.MethodGet, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/dns/searchpaths", server.Path)
assert.Equal(t, expectedPaths["searchPaths"], paths)
}
func TestClient_SplitDNS(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
expectedNameservers := SplitDNSResponse{
"example.com": {"1.1.1.1", "1.2.3.4"},
}
server.ResponseBody = expectedNameservers
nameservers, err := client.DNS().SplitDNS(context.Background())
assert.NoError(t, err)
assert.Equal(t, http.MethodGet, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/dns/split-dns", server.Path)
assert.EqualValues(t, expectedNameservers, nameservers)
}
func TestClient_SetDNSNameservers(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
nameservers := []string{"127.0.0.1"}
assert.NoError(t, client.DNS().SetNameservers(context.Background(), nameservers))
assert.Equal(t, http.MethodPost, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/dns/nameservers", server.Path)
body := make(map[string][]string)
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &body))
assert.EqualValues(t, nameservers, body["dns"])
}
func TestClient_SetDNSPreferences(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
preferences := DNSPreferences{
MagicDNS: true,
}
assert.NoError(t, client.DNS().SetPreferences(context.Background(), preferences))
assert.Equal(t, http.MethodPost, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/dns/preferences", server.Path)
var body DNSPreferences
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &body))
assert.EqualValues(t, preferences, body)
}
func TestClient_SetDNSSearchPaths(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
paths := []string{"test"}
assert.NoError(t, client.DNS().SetSearchPaths(context.Background(), paths))
assert.Equal(t, http.MethodPost, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/dns/searchpaths", server.Path)
body := make(map[string][]string)
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &body))
assert.EqualValues(t, paths, body["searchPaths"])
}
func TestClient_UpdateSplitDNS(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
nameservers := []string{"1.1.2.1", "3.3.3.4"}
request := SplitDNSRequest{
"example.com": nameservers,
}
expectedNameservers := SplitDNSResponse{
"example.com": nameservers,
}
server.ResponseBody = expectedNameservers
resp, err := client.DNS().UpdateSplitDNS(context.Background(), request)
assert.NoError(t, err)
assert.Equal(t, http.MethodPatch, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/dns/split-dns", server.Path)
body := make(SplitDNSResponse)
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &body))
assert.EqualValues(t, nameservers, body["example.com"])
assert.EqualValues(t, expectedNameservers, resp)
}
func TestClient_SetSplitDNS(t *testing.T) {
t.Parallel()
client, server := NewTestHarness(t)
server.ResponseCode = http.StatusOK
nameservers := []string{"1.1.2.1", "3.3.3.4"}
request := SplitDNSRequest{
"example.com": nameservers,
}
assert.NoError(t, client.DNS().SetSplitDNS(context.Background(), request))
assert.Equal(t, http.MethodPut, server.Method)
assert.Equal(t, "/api/v2/tailnet/example.com/dns/split-dns", server.Path)
body := make(SplitDNSResponse)
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &body))
assert.EqualValues(t, nameservers, body["example.com"])
}