dns_monkey 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d8cce7f6719d57e77d5dc9c4ce83c265e578dafe2ceb0a30176439ecd202177b
4
+ data.tar.gz: d73bf5872231a9969ee5976d0b42baaed062c409906d8248140e5e5abbe106be
5
+ SHA512:
6
+ metadata.gz: 5b26890e41e969009eab9272170fd1c2b72f301fec86718a9a05b2c94c6e15712d74ea45b43e1d1775617121473fefba2dc942272d42b0c7623f12776146af29
7
+ data.tar.gz: aab352bbac653632edfed56f23a04ce0d2a3c12a0778f56551ea98913563c0491b9af9b760089d75d0dd1dc0a66a80e9ee99672fa3b641b885d56589b7b70604
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ group :development do
6
+ gem "rspec", "~> 3.0"
7
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.3)
5
+ rspec (3.8.0)
6
+ rspec-core (~> 3.8.0)
7
+ rspec-expectations (~> 3.8.0)
8
+ rspec-mocks (~> 3.8.0)
9
+ rspec-core (3.8.2)
10
+ rspec-support (~> 3.8.0)
11
+ rspec-expectations (3.8.4)
12
+ diff-lcs (>= 1.2.0, < 2.0)
13
+ rspec-support (~> 3.8.0)
14
+ rspec-mocks (3.8.1)
15
+ diff-lcs (>= 1.2.0, < 2.0)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-support (3.8.2)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ rspec (~> 3.0)
24
+
25
+ BUNDLED WITH
26
+ 1.17.3
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Jason Barnett
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "dns_monkey/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "dns_monkey"
7
+ gem.version = DNSMonkey::VERSION
8
+ gem.authors = [ "Jason Barnett" ]
9
+ gem.email = [ "jason.w.barnett@gmail.com" ]
10
+ gem.description = "Simple DNS things"
11
+ gem.summary = gem.description
12
+ gem.license = "MIT"
13
+ gem.homepage = "https://github.com/jasonwbarnett/dns_monkey"
14
+
15
+ gem.required_ruby_version = ">= 2.4"
16
+
17
+ gem.files = %w{LICENSE} +
18
+ Dir.glob("Gemfile*") + # Includes Gemfile and locks
19
+ Dir.glob("*.gemspec") +
20
+ Dir.glob("{lib,spec}/**/*", File::FNM_DOTMATCH).reject { |f| File.directory?(f) }
21
+ gem.test_files = gem.files.grep(%r{^spec/})
22
+ gem.require_paths = ["lib"]
23
+
24
+ gem.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,3 @@
1
+ class DNSMonkey
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/dns_monkey.rb ADDED
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby
2
+ require 'resolv'
3
+
4
+ class DNSMonkey
5
+ #
6
+ # @return [Resolv::DNS]
7
+ #
8
+ attr_reader :resolver
9
+
10
+ def initialize(resolver: Resolv::DNS.new)
11
+ @resolver = resolver
12
+ end
13
+
14
+ #
15
+ # @return [String, nil] returns name of A record or nil if A record does not
16
+ # exist
17
+ #
18
+ def a_record_name(name)
19
+ if cname_record?(name)
20
+ a_record_name(resolv_cname_record(name).name)
21
+ elsif a_record?(name)
22
+ name.to_s
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def resolv_a_record(name)
29
+ resolver.getresource(name, Resolv::DNS::Resource::IN::A)
30
+ end
31
+
32
+ def resolv_cname_record(name)
33
+ resolver.getresource(name, Resolv::DNS::Resource::IN::CNAME)
34
+ end
35
+
36
+ def cname_record?(name)
37
+ resolv_cname_record(name)
38
+ true
39
+ rescue Resolv::ResolvError
40
+ false
41
+ end
42
+
43
+ def a_record?(name)
44
+ resolv_a_record(name)
45
+ true
46
+ rescue Resolv::ResolvError
47
+ false
48
+ end
49
+ end
@@ -0,0 +1,100 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
+ RSpec.configure do |config|
17
+ # rspec-expectations config goes here. You can use an alternate
18
+ # assertion/expectation library such as wrong or the stdlib/minitest
19
+ # assertions if you prefer.
20
+ config.expect_with :rspec do |expectations|
21
+ # This option will default to `true` in RSpec 4. It makes the `description`
22
+ # and `failure_message` of custom matchers include text for helper methods
23
+ # defined using `chain`, e.g.:
24
+ # be_bigger_than(2).and_smaller_than(4).description
25
+ # # => "be bigger than 2 and smaller than 4"
26
+ # ...rather than:
27
+ # # => "be bigger than 2"
28
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29
+ end
30
+
31
+ # rspec-mocks config goes here. You can use an alternate test double
32
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
33
+ config.mock_with :rspec do |mocks|
34
+ # Prevents you from mocking or stubbing a method that does not exist on
35
+ # a real object. This is generally recommended, and will default to
36
+ # `true` in RSpec 4.
37
+ mocks.verify_partial_doubles = true
38
+ end
39
+
40
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41
+ # have no way to turn it off -- the option exists only for backwards
42
+ # compatibility in RSpec 3). It causes shared context metadata to be
43
+ # inherited by the metadata hash of host groups and examples, rather than
44
+ # triggering implicit auto-inclusion in groups with matching metadata.
45
+ config.shared_context_metadata_behavior = :apply_to_host_groups
46
+
47
+ # The settings below are suggested to provide a good initial experience
48
+ # with RSpec, but feel free to customize to your heart's content.
49
+ =begin
50
+ # This allows you to limit a spec run to individual examples or groups
51
+ # you care about by tagging them with `:focus` metadata. When nothing
52
+ # is tagged with `:focus`, all examples get run. RSpec also provides
53
+ # aliases for `it`, `describe`, and `context` that include `:focus`
54
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
55
+ config.filter_run_when_matching :focus
56
+
57
+ # Allows RSpec to persist some state between runs in order to support
58
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
59
+ # you configure your source control system to ignore this file.
60
+ config.example_status_persistence_file_path = "spec/examples.txt"
61
+
62
+ # Limits the available syntax to the non-monkey patched syntax that is
63
+ # recommended. For more details, see:
64
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
65
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
66
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
67
+ config.disable_monkey_patching!
68
+
69
+ # This setting enables warnings. It's recommended, but in some cases may
70
+ # be too noisy due to issues in dependencies.
71
+ config.warnings = true
72
+
73
+ # Many RSpec users commonly either run the entire suite or an individual
74
+ # file, and it's useful to allow more verbose output when running an
75
+ # individual spec file.
76
+ if config.files_to_run.one?
77
+ # Use the documentation formatter for detailed output,
78
+ # unless a formatter has already been configured
79
+ # (e.g. via a command-line flag).
80
+ config.default_formatter = "doc"
81
+ end
82
+
83
+ # Print the 10 slowest examples and example groups at the
84
+ # end of the spec run, to help surface which specs are running
85
+ # particularly slow.
86
+ config.profile_examples = 10
87
+
88
+ # Run specs in random order to surface order dependencies. If you find an
89
+ # order dependency and want to debug it, you can fix the order by providing
90
+ # the seed, which is printed after each run.
91
+ # --seed 1234
92
+ config.order = :random
93
+
94
+ # Seed global randomization in this process using the `--seed` CLI option.
95
+ # Setting this allows you to use `--seed` to deterministically reproduce
96
+ # test failures related to randomization by passing the same `--seed` value
97
+ # as the one that triggered the failure.
98
+ Kernel.srand config.seed
99
+ =end
100
+ end
@@ -0,0 +1,62 @@
1
+ require 'dns_monkey'
2
+
3
+ describe DNSMonkey do
4
+ subject { DNSMonkey.new(resolver: resolver) }
5
+ let(:resolver) { Resolv::DNS.new }
6
+
7
+ it 'responds to #a_record_name' do
8
+ expect(subject).to respond_to(:a_record_name)
9
+ end
10
+
11
+ describe '#a_record_name' do
12
+ let(:bad_record_name) { 'bad-record.domain.com' }
13
+ let(:a_record_name) { 'a-record.domain.com' }
14
+ let(:a_record_ip) { '8.8.4.4' }
15
+ let(:cname_record_name) { 'cname-record.domain.com' }
16
+ let(:a_record) { Resolv::DNS::Resource::IN::A.new(a_record_ip) }
17
+ let(:cname_record_dns_name) { Resolv::DNS::Name.create(a_record_name) }
18
+ let(:cname_record) { Resolv::DNS::Resource::IN::CNAME.new(cname_record_dns_name) }
19
+
20
+ before do
21
+ allow(resolver).to receive(:getresource)
22
+ .with(cname_record_name, Resolv::DNS::Resource::IN::CNAME)
23
+ .and_return(cname_record)
24
+
25
+ allow(resolver).to receive(:getresource)
26
+ .with(cname_record.name, Resolv::DNS::Resource::IN::CNAME)
27
+ .and_raise(Resolv::ResolvError)
28
+
29
+ allow(resolver).to receive(:getresource)
30
+ .with(cname_record.name, Resolv::DNS::Resource::IN::A)
31
+ .and_return(a_record)
32
+
33
+ allow(resolver).to receive(:getresource)
34
+ .with(a_record_name, Resolv::DNS::Resource::IN::CNAME)
35
+ .and_raise(Resolv::ResolvError)
36
+
37
+ allow(resolver).to receive(:getresource)
38
+ .with(a_record_name, Resolv::DNS::Resource::IN::A)
39
+ .and_return(a_record)
40
+
41
+ allow(resolver).to receive(:getresource)
42
+ .with(bad_record_name, Resolv::DNS::Resource::IN::CNAME)
43
+ .and_raise(Resolv::ResolvError)
44
+
45
+ allow(resolver).to receive(:getresource)
46
+ .with(bad_record_name, Resolv::DNS::Resource::IN::A)
47
+ .and_raise(Resolv::ResolvError)
48
+ end
49
+
50
+ it 'returns name of a record, when passed an a record name' do
51
+ expect(subject.a_record_name(a_record_name)).to eq(a_record_name)
52
+ end
53
+
54
+ it 'returns name of a record, when passed a cname record name' do
55
+ expect(subject.a_record_name(cname_record_name)).to eq(a_record_name)
56
+ end
57
+
58
+ it 'returns nil when passed an invalid a record name' do
59
+ expect(subject.a_record_name(bad_record_name)).to be nil
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dns_monkey
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Barnett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: Simple DNS things
28
+ email:
29
+ - jason.w.barnett@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - Gemfile.lock
36
+ - LICENSE
37
+ - dns_monkey.gemspec
38
+ - lib/dns_monkey.rb
39
+ - lib/dns_monkey/version.rb
40
+ - spec/spec_helper.rb
41
+ - spec/unit/dns_monkey_spec.rb
42
+ homepage: https://github.com/jasonwbarnett/dns_monkey
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '2.4'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.0.3
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Simple DNS things
65
+ test_files:
66
+ - spec/spec_helper.rb
67
+ - spec/unit/dns_monkey_spec.rb