This repository has been archived by the owner on Sep 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
route53_test.go
245 lines (217 loc) · 6.2 KB
/
route53_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"errors"
"reflect"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/route53/route53iface"
)
type mockRoute53API struct {
route53iface.Route53API
getZoneResp *route53.GetHostedZoneOutput
getZoneErr error
getChangeResp *route53.GetChangeOutput
getChangeErr error
changeRRResp *route53.ChangeResourceRecordSetsOutput
changeRRErr error
}
func (m mockRoute53API) GetHostedZone(in *route53.GetHostedZoneInput) (*route53.GetHostedZoneOutput, error) {
return m.getZoneResp, m.getZoneErr
}
func (m mockRoute53API) ChangeResourceRecordSets(in *route53.ChangeResourceRecordSetsInput) (*route53.ChangeResourceRecordSetsOutput, error) {
return m.changeRRResp, m.changeRRErr
}
func (m mockRoute53API) GetChange(in *route53.GetChangeInput) (*route53.GetChangeOutput, error) {
return m.getChangeResp, m.getChangeErr
}
func mockRoute53Timers() func() {
dwi := defaultRoute53ZoneWaitWatchInterval
dwt := defaultRoute53ZoneWaitWatchTimeout
defaultRoute53ZoneWaitWatchInterval = 1 * time.Second
defaultRoute53ZoneWaitWatchTimeout = 2 * time.Second
return func() {
defaultRoute53ZoneWaitWatchInterval = dwi
defaultRoute53ZoneWaitWatchTimeout = dwt
}
}
var (
errTestRoute53ZoneMock = errors.New("test error")
testRoute53ZoneGetZoneOK = &route53.GetHostedZoneOutput{
HostedZone: &route53.HostedZone{
ResourceRecordSetCount: aws.Int64(1),
CallerReference: aws.String(""),
Config: &route53.HostedZoneConfig{
Comment: aws.String(""),
PrivateZone: aws.Bool(false),
},
Id: aws.String("/hostedzone/XXXXXXXXXXXXXX"),
Name: aws.String("example.com."),
},
DelegationSet: &route53.DelegationSet{
NameServers: []*string{
aws.String("0.ns.example.com"),
aws.String("1.ns.example.com"),
aws.String("2.ns.example.com"),
aws.String("3.ns.example.com"),
},
CallerReference: aws.String(""),
Id: aws.String("/delegationset/XXXXXXXXXXXXXX"),
},
}
testRoute53ZoneChangeRROK = &route53.ChangeResourceRecordSetsOutput{
ChangeInfo: &route53.ChangeInfo{
Comment: aws.String(""),
Id: aws.String("123456789"),
Status: aws.String(route53.ChangeStatusPending),
SubmittedAt: aws.Time(time.Now()),
},
}
testRoute53ZoneGetChangeOK = &route53.GetChangeOutput{
ChangeInfo: &route53.ChangeInfo{
Comment: aws.String(""),
Id: aws.String("123456789"),
Status: aws.String(route53.ChangeStatusInsync),
SubmittedAt: aws.Time(time.Now()),
},
}
testRoute53ZoneGetChangePending = &route53.GetChangeOutput{
ChangeInfo: &route53.ChangeInfo{
Comment: aws.String(""),
Id: aws.String("123456789"),
Status: aws.String(route53.ChangeStatusPending),
SubmittedAt: aws.Time(time.Now()),
},
}
expectedNameservers = []string{
"0.ns.example.com",
"1.ns.example.com",
"2.ns.example.com",
"3.ns.example.com",
}
)
func TestRoute53Zone_UpsertCname(t *testing.T) {
testCases := []struct {
getZoneErr error
getZoneResponse *route53.GetHostedZoneOutput
changeRRErr error
changeRRResponse *route53.ChangeResourceRecordSetsOutput
getChangeErr error
getChangeResponse *route53.GetChangeOutput
zoneID string
record cnameRecord
expectedNewErr error
expectedUpsertErr error
}{
{ // error in get zone
errTestRoute53ZoneMock,
nil,
nil,
nil,
nil,
nil,
"example.com.",
cnameRecord{"test.example.com", "cname.example.com"},
errTestRoute53ZoneMock,
nil,
},
{ // error in change request
nil,
testRoute53ZoneGetZoneOK,
errTestRoute53ZoneMock,
nil,
nil,
nil,
"example.com.",
cnameRecord{"test.example.com", "cname.example.com"},
nil,
errTestRoute53ZoneMock,
},
{ // error in get change request
nil,
testRoute53ZoneGetZoneOK,
nil,
testRoute53ZoneChangeRROK,
errTestRoute53ZoneMock,
nil,
"example.com.",
cnameRecord{"test.example.com", "cname.example.com"},
nil,
errTestRoute53ZoneMock,
},
{ // timeout in get change
nil,
testRoute53ZoneGetZoneOK,
nil,
testRoute53ZoneChangeRROK,
nil,
testRoute53ZoneGetChangePending,
"example.com.",
cnameRecord{"test.example.com", "cname.example.com"},
nil,
errRoute53WaitWatchTimedOut,
},
{ // works end to end
nil,
testRoute53ZoneGetZoneOK,
nil,
testRoute53ZoneChangeRROK,
nil,
testRoute53ZoneGetChangeOK,
"example.com.",
cnameRecord{"test.example.com", "cname.example.com"},
nil,
nil,
},
}
defer mockRoute53Timers()()
for i, tc := range testCases {
p, err := newRoute53Zone(tc.zoneID, &mockRoute53API{
getZoneResp: tc.getZoneResponse,
getZoneErr: tc.getZoneErr,
getChangeResp: tc.getChangeResponse,
getChangeErr: tc.getChangeErr,
changeRRResp: tc.changeRRResponse,
changeRRErr: tc.changeRRErr,
})
if err != tc.expectedNewErr {
t.Fatalf("newRoute53Zone returned unexpected error: %+v", err)
}
if tc.expectedNewErr != nil {
continue
}
if p.Name != "example.com." {
t.Errorf("Route53Zone has unexpected Name: %+v", p.Name)
}
if p.ID != "/hostedzone/XXXXXXXXXXXXXX" {
t.Errorf("Route53Zone has unexpected ID: %+v", p.ID)
}
if !reflect.DeepEqual(p.Nameservers, expectedNameservers) {
t.Errorf("Route53Zone has unexpected Nameservers: %+v", p.Nameservers)
}
if err := p.UpsertCnames([]cnameRecord{tc.record}); err != tc.expectedUpsertErr {
t.Errorf("Route53Zone.UpsertCname returned unexpected error for case #%02d: %+v", i, err)
}
}
}
func TestRoute53Zone_DeleteCname(t *testing.T) {
defer mockRoute53Timers()()
p, err := newRoute53Zone("example.com.", &mockRoute53API{
getZoneResp: testRoute53ZoneGetZoneOK,
getChangeResp: testRoute53ZoneGetChangeOK,
changeRRResp: testRoute53ZoneChangeRROK,
})
if err != nil {
t.Fatalf("newRoute53Zone returned unexpected error: %+v", err)
}
if err := p.DeleteCnames([]cnameRecord{{Hostname: "test.example.com", Target: "foo.example.com"}}); err != nil {
t.Errorf("Route53Zone.DeleteCname returned unexpected error: %+v", err)
}
}
func TestRoute53Zone_Domain(t *testing.T) {
z := route53Zone{Name: "test"}
if z.Domain() != "test" {
t.Errorf("Route53Zone.Domain return unexpected value")
}
}