This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
have_dns_spec.rb
199 lines (171 loc) · 7.72 KB
/
have_dns_spec.rb
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
require 'spec_helper'
def stub_records(strings)
records = strings.map { |s| Dnsruby::RR.new_from_string(s) }
resolver = Dnsruby::Resolver.new
allow(Dnsruby::Resolver).to receive(:new) do
yield if block_given?
resolver
end
allow(resolver).to receive_message_chain(:query, :answer).and_return(records)
end
describe 'rspec-dns matchers' do
describe '#have_dns' do
context 'with a sigle record' do
it 'can evalutate an A record' do
stub_records(['example.com 86400 A 192.0.2.4'])
expect('example.com').to have_dns.with_type('A')
expect('example.com').to_not have_dns.with_type('TXT')
expect('example.com').to have_dns.with_type('A').and_address('192.0.2.4')
end
it 'can evalutate a AAAA record' do
stub_records(['example.com 86400 AAAA 2001:DB8:6c::430'])
expect('example.com').to have_dns.with_type('AAAA')
expect('example.com').to_not have_dns.with_type('A')
expect('example.com').to have_dns.with_type('AAAA')
.and_address('2001:DB8:6C::430')
end
it 'can evalutate an A/AAAA record with IPAddr range' do
stub_records(['example.com 86400 A 192.0.2.4',
'example.com 86400 AAAA 2001:DB8:6c::430'])
expect('example.com').to have_dns.with_type('A')
.and_address('192.0.2.0/24')
expect('example.com').to have_dns.with_type('AAAA')
.and_address('2001:DB8:6C::/64')
end
it 'can evalutate a CNAME record' do
stub_records(['www.example.com 300 IN CNAME example.com'])
expect('example.com').to have_dns.with_type('CNAME')
expect('example.com').to_not have_dns.with_type('AAAA')
expect('example.com').to have_dns.with_type('CNAME').and_name('www.example.com')
end
it 'can evalutate an MX record' do
stub_records(['example.com. 7200 MX 40 mail.example.com.'])
expect('example.com').to have_dns.with_type('MX')
expect('example.com').to_not have_dns.with_type('CNAME')
expect('example.com').to have_dns.with_type('MX').and_preference(40)
expect('example.com').to have_dns.with_type('MX')
.and_preference(40).and_exchange('mail.example.com')
expect('example.com').to_not have_dns.with_type('MX')
.and_preference(30).and_exchange('mail.example.com')
expect('example.com').to_not have_dns.with_type('MX')
.and_preference(40).and_exchange('example.com')
end
it 'can evalutate an NS record' do
stub_records(['sub.example.com. 300 IN NS ns.sub.example.com.'])
expect('example.com').to have_dns.with_type('NS')
expect('example.com').to_not have_dns.with_type('MX')
expect('example.com').to have_dns.with_type('NS').and_domainname('ns.sub.example.com')
expect('example.com').to_not have_dns.with_type('NS').and_domainname('sub.example.com')
end
it 'can evalutate a PTR record' do
stub_records(['ptrs.example.com. 300 IN PTR ptr.example.com.'])
expect('192.168.100.100').to have_dns.with_type('PTR')
expect('192.168.100.100').to_not have_dns.with_type('MX')
expect('192.168.100.100').to have_dns.with_type('PTR').and_domainname('ptr.example.com')
expect('192.168.100.100').to_not have_dns.with_type('PTR').and_domainname('ptrs.example.com')
end
it 'can evalutate an SOA record' do
stub_records(['example.com 210 IN SOA ns.example.com a.example.com. 2014030712 60 25 3628800 900'])
expect('example.com').to have_dns.with_type('SOA')
expect('example.com').to_not have_dns.with_type('PTR')
expect('example.com').to have_dns.with_type('SOA')
.and_mname('ns.example.com')
.and_rname('a.example.com')
.and_serial('2014030712')
.and_refresh(60)
.and_retry(25)
.and_expire(3628800)
.and_minimum(900)
expect('example.com').to_not have_dns.with_type('SOA')
.and_mname('nstest.example.com')
end
it 'can evalutate a TXT record' do
stub_records(['example.com. 300 IN TXT "v=spf1 a:example.com ~all"'])
expect('example.com').to have_dns.with_type('TXT')
expect('example.com').to_not have_dns.with_type('SOA')
expect('example.com').to have_dns.with_type('TXT').and_data('v=spf1 a:example.com ~all')
expect('example.com').to_not have_dns.with_type('TXT').and_data('v=spf2 a:example.com ~all')
end
end
context 'with config chain' do
it 'should accept config chain' do
stub_records(['example.com 86400 A 192.0.2.1'])
expect('example.com').to have_dns.with_type('A')
.config(nameserver: '192.0.2.4')
end
end
context 'with zone file' do
it 'can evalutate records in zone file with origin' do
file = 'spec/rspec-dns/example.zone'
origin = 'example.com.'
expect('example.com').to have_dns.with_type('NS')
.and_domainname('ns.example.com')
.in_zone_file(file, origin)
expect('example.com').to have_dns.with_type('A')
.and_address('192.0.2.4')
.in_zone_file(file, origin)
expect('example.com').to have_dns.with_type('MX')
.and_preference(40).and_exchange('mail.example.com')
.in_zone_file(file, origin)
expect('www.example.com').to have_dns.with_type('A')
.and_address('192.0.2.4')
.in_zone_file(file, origin)
expect('doubt.example.com').not_to have_dns.with_type('A')
.and_address('192.0.2.4')
.in_zone_file(file, origin)
expect('www.example.com').to have_dns.with_type('AAAA')
.and_address('2001:DB8:6C::430')
.in_zone_file(file, origin)
end
context 'in reverse' do
it 'can evalutate PTR records in zone file' do
file = 'spec/rspec-dns/0.2.0.192.rev.zone'
origin = '2.0.192.in-addr.arpa.'
expect('4.2.0.192.in-addr.arpa').to have_dns.with_type('PTR')
.and_domainname('www.example.com')
.in_zone_file(file, origin)
expect('5.2.0.192.in-addr.arpa').not_to have_dns.with_type('PTR')
.and_domainname('www.example.com')
.in_zone_file(file, origin)
expect('192.0.2.4').to have_dns.with_type('PTR')
.and_domainname('www.example.com')
.in_zone_file(file, origin)
end
end
it 'can evalutate records in zone file without origin' do
file = 'spec/rspec-dns/example.zone'
expect('').to have_dns.with_type('NS')
.and_domainname('ns').in_zone_file(file)
expect('www').to have_dns.with_type('A')
.and_address('192.0.2.4').in_zone_file(file)
expect('doubt').not_to have_dns.with_type('A')
.and_address('192.0.2.4').in_zone_file(file)
end
it 'can evalutate records with dns servers if file is nil' do
file = nil
stub_records(['example.com 86400 A 192.0.2.4'])
expect('example.com').to have_dns.with_type('A')
.and_address('192.0.2.4')
.in_zone_file(file)
expect('example.com').to have_dns.with_type('A')
.and_address('192.0.2.4')
.in_zone_file
end
end
context 'with changable connection timeout' do
it 'is_expected.to timeout within 3 seconds in default' do
stub_records(['example.com 86400 A 192.168.100.100']) do
sleep 3
end
expect('example.com').to_not have_dns.with_type('A')
end
it 'is_expected.to not timeout within 3 seconds when timeout is 5' do
RSpec.configuration.rspec_dns_connection_timeout = 5
stub_records(['example.com 86400 A 192.168.100.100']) do
sleep 3
end
expect('example.com').to have_dns.with_type('A')
end
end
end
end