rspec-dns 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/Gemfile +3 -0
- data/LICENSE +13 -0
- data/README.markdown +94 -0
- data/Rakefile +2 -0
- data/lib/rspec-dns/have_dns.rb +99 -0
- data/lib/rspec-dns.rb +5 -0
- data/rspec-dns.gemspec +17 -0
- metadata +92 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3@rspec-dns --create
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright 2012 Seth Vargo, CustomInk, LLC
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/README.markdown
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
rspec-dns
|
2
|
+
=========
|
3
|
+
RSpec DNS is an rspec plugin for easy DNS testing. It uses the built-in Ruby 1.9 library Resolv and is customizable.
|
4
|
+
|
5
|
+
Installation
|
6
|
+
------------
|
7
|
+
If you're using bundler, add this line to your application's `Gemfile`:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rspec-dns'
|
11
|
+
```
|
12
|
+
|
13
|
+
Don't forget to run the `bundle` command to install.
|
14
|
+
|
15
|
+
Or install it manually with:
|
16
|
+
|
17
|
+
$ gem install rspec-dns
|
18
|
+
|
19
|
+
Usage
|
20
|
+
-----
|
21
|
+
RSpec DNS is best described by example. First, require `rspec-dns` in your `spec_helper.rb`:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# spec/spec_helper.rb
|
25
|
+
require 'rspec'
|
26
|
+
require 'rspec-dns'
|
27
|
+
```
|
28
|
+
|
29
|
+
Then, create a spec like this:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'spec_helper'
|
33
|
+
|
34
|
+
describe 'www.example.com' do
|
35
|
+
it { should have_dns.with_type('TXT').and_ttl(300).and_data('a=b') }
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Or group all your DNS tests into a single file or context:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'spec_helper'
|
43
|
+
|
44
|
+
describe 'DNS tests' do
|
45
|
+
'www.example.com'.should have_dns.with_type('TXT').and_ttl(300).and_data('a=b')
|
46
|
+
'www.example.com'.should have_dns.with_type('A').and_ttl(300).and_value('1.2.3.4')
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
### Chain Methods
|
51
|
+
All of the method chains are actually [Resolv](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/resolv/rdoc/index.html) attributes on the record. You can prefix them with `and_`, `with_`, `and_with` or whatever your heart desires. The predicate is what is checked. The rest is syntactic sugar.
|
52
|
+
|
53
|
+
Depending on the type of record, the following attributes may be available:
|
54
|
+
|
55
|
+
- address
|
56
|
+
- bitmap
|
57
|
+
- cpu
|
58
|
+
- data
|
59
|
+
- emailbx
|
60
|
+
- exchange
|
61
|
+
- expire
|
62
|
+
- minimum
|
63
|
+
- mname
|
64
|
+
- name
|
65
|
+
- os
|
66
|
+
- port
|
67
|
+
- preference
|
68
|
+
- priority
|
69
|
+
- protocol
|
70
|
+
- refresh
|
71
|
+
- retry
|
72
|
+
- rmailbx
|
73
|
+
- rname
|
74
|
+
- serial
|
75
|
+
- target
|
76
|
+
- ttl
|
77
|
+
- type
|
78
|
+
- weight
|
79
|
+
|
80
|
+
If you try checking an attribute on a record that is non-existent (like checking the `rmailbx` on an `A` record), you'll get an error like this:
|
81
|
+
|
82
|
+
```text
|
83
|
+
Failure/Error: it { should have_dns.with_type('TXT').and_ftl(300).and_data('a=b') }
|
84
|
+
NoMethodError:
|
85
|
+
undefined method `rmailbx' for #<Resolv::DNS::Resource::IN::A:0x007fb56302ed90>
|
86
|
+
```
|
87
|
+
|
88
|
+
For this reason, you should always check the `type` attribute first in your chain.
|
89
|
+
|
90
|
+
Contributing
|
91
|
+
------------
|
92
|
+
1. Fork the project on github
|
93
|
+
2. Create your feature branch
|
94
|
+
3. Open a Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'resolv'
|
2
|
+
|
3
|
+
RSpec::Matchers.define :have_dns do
|
4
|
+
match do |dns|
|
5
|
+
@dns = dns
|
6
|
+
|
7
|
+
_records.any? do |record|
|
8
|
+
_options.all? do |option, value|
|
9
|
+
# Resolv returns an object instead of just the IP
|
10
|
+
if option == :address
|
11
|
+
record.send(option).to_s == value.to_s
|
12
|
+
else
|
13
|
+
record.send(option) == value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
failure_message_for_should do |actual|
|
20
|
+
"expected #{actual} to have: #{_pretty_print_options}, but did not. other records were: #{_pretty_print_records}"
|
21
|
+
end
|
22
|
+
|
23
|
+
failure_message_for_should_not do |actual|
|
24
|
+
"expected #{actual} not to have #{_pretty_print_options}, but it did"
|
25
|
+
end
|
26
|
+
|
27
|
+
description do
|
28
|
+
'have the correct dns entries'
|
29
|
+
end
|
30
|
+
|
31
|
+
def method_missing(m, *args, &block)
|
32
|
+
if m.to_s =~ /(and\_with|and|with)?\_(.*)$/
|
33
|
+
_options[$2.to_sym] = args.first
|
34
|
+
self
|
35
|
+
else
|
36
|
+
super
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def _config
|
41
|
+
@config ||= if File.exists?(_config_file)
|
42
|
+
config = _symbolize_keys(YAML::load(ERB.new(File.read(_config_file) ).result))
|
43
|
+
else
|
44
|
+
nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def _config_file
|
49
|
+
File.join('config', 'dns.yml')
|
50
|
+
end
|
51
|
+
|
52
|
+
def _symbolize_keys(hash)
|
53
|
+
hash.inject({}){|result, (key, value)|
|
54
|
+
new_key = case key
|
55
|
+
when String then key.to_sym
|
56
|
+
else key
|
57
|
+
end
|
58
|
+
new_value = case value
|
59
|
+
when Hash then _symbolize_keys(value)
|
60
|
+
else value
|
61
|
+
end
|
62
|
+
result[new_key] = new_value
|
63
|
+
result
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
def _options
|
68
|
+
@_options ||= {}
|
69
|
+
end
|
70
|
+
|
71
|
+
def _pretty_print_options
|
72
|
+
"\n (#{_options.sort.collect{ |k,v| "#{k}:#{v.inspect}" }.join(', ')})\n"
|
73
|
+
end
|
74
|
+
|
75
|
+
def _records
|
76
|
+
@_records ||= begin
|
77
|
+
Timeout::timeout(1) {
|
78
|
+
if _config.nil?
|
79
|
+
Resolv::DNS.new.getresources(@dns, Resolv::DNS::Resource::IN::ANY)
|
80
|
+
else
|
81
|
+
Resolv::DNS.new(_config).getresources(@dns, Resolv::DNS::Resource::IN::ANY)
|
82
|
+
end
|
83
|
+
}
|
84
|
+
rescue
|
85
|
+
$stderr.puts "Connection timed out for #{@dns}"
|
86
|
+
[]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def _pretty_print_records
|
91
|
+
"\n" + _records.collect { |record| _pretty_print_record(record) }.join("\n")
|
92
|
+
end
|
93
|
+
|
94
|
+
def _pretty_print_record(record)
|
95
|
+
' (' + %w(address bitmap cpu data emailbx exchange expire minimum mname name os port preference priority protocol refresh retry rmailbx rname serial target ttl type weight).collect do |method|
|
96
|
+
"#{method}:#{record.send(method.to_sym).to_s.inspect}" if record.respond_to?(method.to_sym)
|
97
|
+
end.compact.join(', ') + ')'
|
98
|
+
end
|
99
|
+
end
|
data/lib/rspec-dns.rb
ADDED
data/rspec-dns.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.version = '0.0.1'
|
3
|
+
s.name = 'rspec-dns'
|
4
|
+
s.author = 'Seth Vargo'
|
5
|
+
s.email = 'sethvargo@gmail.com'
|
6
|
+
s.description = 'Easily test your DNS entries with RSpec'
|
7
|
+
s.summary = 'rspec-dns provides an easy-to-use DSL for testing your DNS records are responding as they should.'
|
8
|
+
s.homepage = 'https://github.com/customink/rspec-dns'
|
9
|
+
|
10
|
+
s.files = `git ls-files`.split($\)
|
11
|
+
s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
13
|
+
s.require_paths = ['lib']
|
14
|
+
|
15
|
+
s.add_dependency 'rake'
|
16
|
+
s.add_dependency 'rspec', '>= 2.9'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-dns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Seth Vargo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.9'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.9'
|
46
|
+
description: Easily test your DNS entries with RSpec
|
47
|
+
email: sethvargo@gmail.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- .gitignore
|
53
|
+
- .rvmrc
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE
|
56
|
+
- README.markdown
|
57
|
+
- Rakefile
|
58
|
+
- lib/rspec-dns.rb
|
59
|
+
- lib/rspec-dns/have_dns.rb
|
60
|
+
- rspec-dns.gemspec
|
61
|
+
homepage: https://github.com/customink/rspec-dns
|
62
|
+
licenses: []
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
hash: -1474966448062495488
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
hash: -1474966448062495488
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.8.24
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: rspec-dns provides an easy-to-use DSL for testing your DNS records are responding
|
91
|
+
as they should.
|
92
|
+
test_files: []
|