dns_mock 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e807a51b6ad704e5d7a20a311e20e76ad0f1319c649a990eb93512d1719b486f
4
- data.tar.gz: 2448fc6c17288aa8c359a4d6eaa0bc7a0cc3de3953c67940087984dfef327949
3
+ metadata.gz: 24eced6efdb48a74315875839beb3af4e693962a3eb1c5ea3b82f9de67ab5bf4
4
+ data.tar.gz: e11a83bb5ba9a8cf5f5ba0d6d98689a6886b9954b4ba0f33cfe3de026671a905
5
5
  SHA512:
6
- metadata.gz: efc57519f9ce59c1aa527cc316cf89eccbdc5b15c4c612bdf5c284a1af2874dfc52ed17d3125eed6435f1defc5338bbcfd0baafde4935448df639824084aab85
7
- data.tar.gz: 60a80d16dc4ec94ddfd6de6a093992db72e2e3960f6ca0f5f7d6d5974e776dc59395f3e75e5fb7913c804416e32a82a2fe842e6b3c8d954567876b6055451ada
6
+ metadata.gz: 5f3863ee0037d22f13e2a0b1e3126193cf485447b3f60edf998ff631204f46fe7ac443f1fc9431431e10a2577392000b388c9b110d26e13535524e6d53e18f66
7
+ data.tar.gz: 5c795bd05c4eb4502a248f483c01778aaefac1fe32a1d937364518c6e3299414145dd973ca2ffeb6688c940217be4fc11e6e321d9f1da0ef5bce6e1c03f4e025
data/.reek.yml CHANGED
@@ -20,6 +20,7 @@ detectors:
20
20
  - DnsMock::RecordsDictionaryHelper#create_records_dictionary_by_records
21
21
  - DnsMock::ServerHelper#start_random_server
22
22
  - DnsMock::ServerHelper#stop_all_running_servers
23
+ - DnsMock::Server::RecordsDictionaryBuilder#rdns_lookup_prefix
23
24
 
24
25
  ControlParameter:
25
26
  exclude:
@@ -119,7 +119,7 @@ Style/HashExcept:
119
119
  Enabled: true
120
120
 
121
121
  Layout/LineLength:
122
- Max: 140
122
+ Max: 150
123
123
 
124
124
  Layout/EmptyLinesAroundAttributeAccessor:
125
125
  Enabled: true
@@ -2,6 +2,40 @@
2
2
 
3
3
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
4
4
 
5
+ ## [0.2.0] - 2021-01-26
6
+
7
+ ### PTR record support
8
+
9
+ Added ability to mock PTR records. Please note, you can define host address without RDNS lookup prefix (`.in-addr.arpa`). `DnsMock` will do it for you.
10
+
11
+ ```ruby
12
+ records = {
13
+ '1.1.1.1' => {
14
+ ptr: %w[domain_1.com domain_2.com]
15
+ }
16
+ }
17
+
18
+ DnsMock.start_server(records: records)
19
+ ```
20
+
21
+ ```bash
22
+ dig @localhost -p 5300 -x 1.1.1.1
23
+ ```
24
+
25
+ ```
26
+ ; <<>> DiG 9.10.6 <<>> @localhost -p 5300 -x 1.1.1.1
27
+ ; (2 servers found)
28
+
29
+ ;; ANSWER SECTION:
30
+ 1.1.1.1.in-addr.arpa. 1 IN PTR domain_1.com.
31
+ 1.1.1.1.in-addr.arpa. 1 IN PTR domain_2.com.
32
+
33
+ ;; Query time: 0 msec
34
+ ;; SERVER: 127.0.0.1#5300(127.0.0.1)
35
+ ;; WHEN: Mon Jan 25 19:58:39 EET 2021
36
+ ;; MSG SIZE rcvd: 98
37
+ ```
38
+
5
39
  ## [0.1.0] - 2021-01-19
6
40
 
7
41
  ### First release
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dns_mock (0.1.0)
4
+ dns_mock (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,17 +1,27 @@
1
1
  # Ruby DnsMock
2
2
 
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/5ea9da61ef468b8ad4c4/maintainability)](https://codeclimate.com/github/mocktools/ruby-dns-mock/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/5ea9da61ef468b8ad4c4/test_coverage)](https://codeclimate.com/github/mocktools/ruby-dns-mock/test_coverage)
3
5
  [![CircleCI](https://circleci.com/gh/mocktools/ruby-dns-mock/tree/develop.svg?style=svg)](https://circleci.com/gh/mocktools/ruby-dns-mock/tree/develop)
4
6
  [![Gem Version](https://badge.fury.io/rb/dns_mock.svg)](https://badge.fury.io/rb/dns_mock)
5
7
  [![Downloads](https://img.shields.io/gem/dt/dns_mock.svg?colorA=004d99&colorB=0073e6)](https://rubygems.org/gems/dns_mock)
6
8
  [![GitHub](https://img.shields.io/github/license/mocktools/ruby-dns-mock)](LICENSE.txt)
7
9
  [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v1.4%20adopted-ff69b4.svg)](CODE_OF_CONDUCT.md)
8
10
 
11
+ 💎 Ruby DNS mock. Mimic any DNS records for your test environment and even more.
12
+
9
13
  ## Table of Contents
10
14
 
11
15
  - [Features](#features)
12
16
  - [Requirements](#requirements)
13
17
  - [Installation](#installation)
14
18
  - [Usage](#usage)
19
+ - [Contributing](#contributing)
20
+ - [License](#license)
21
+ - [Code of Conduct](#code-of-conduct)
22
+ - [Credits](#credits)
23
+ - [Versioning](#versioning)
24
+ - [Changelog](CHANGELOG.md)
15
25
 
16
26
  ## Requirements
17
27
 
@@ -19,9 +29,11 @@ Ruby MRI 2.5.0+
19
29
 
20
30
  ## Features
21
31
 
22
- - Ability to mimic any DNS records for your test environment
32
+ - Ability to mimic any DNS records (`A`, `AAAA`, `CNAME`, `MX`, `NS`, `PTR`, `SOA` and `TXT`)
23
33
  - Zero runtime dependencies
24
- - Test framework agnostic (`RSpec`, `Test::Unit`, `MiniTest`). Even can be used outside of test frameworks
34
+ - Lightweight UDP DNS mock server with dynamic/manual port assignment
35
+ - Test framework agnostic (it's PORO, so you can use it outside of `RSpec`, `Test::Unit` or `MiniTest`)
36
+ - Simple and intuitive DSL
25
37
 
26
38
  ## Installation
27
39
 
@@ -44,10 +56,10 @@ Or install it yourself as:
44
56
  ## Usage
45
57
 
46
58
  ```ruby
47
- # Example of mocked DNS records
59
+ # Example of mocked DNS records structure
48
60
  records = {
49
61
  'example.com' => {
50
- a: %w[1.1.1.1, 2.2.2.2],
62
+ a: %w[1.1.1.1 2.2.2.2],
51
63
  aaaa: %w[2a00:1450:4001:81e::200e],
52
64
  ns: %w[ns1.domain.com ns2.domain.com],
53
65
  mx: %w[mx1.domain.com mx2.domain.com],
@@ -64,16 +76,55 @@ records = {
64
76
  minimum: 3_600
65
77
  }
66
78
  ]
79
+ },
80
+ '1.1.1.1' => {
81
+ ptr: %w[domain_1.com domain_2.com]
67
82
  }
68
83
  }
69
84
 
70
- # Main DnsMock interface:
71
- dns_mock_server = DnsMock.start_server(records: records) # records, port are an optional params
72
- dns_mock_server.port
73
- dns_mock_server.assign_mocks(records)
74
- dns_mock_server.reset_mocks!
75
- dns_mock_server.stop!
85
+ # Main DnsMock interface. records:Hash, port:Integer are an
86
+ # optional params. By default creates dns mock server with empty
87
+ # records. A free port for server will be randomly assigned in
88
+ # the range from 49152 to 65535. Returns current dns mock server
89
+ dns_mock_server = DnsMock.start_server(records: records) # => DnsMock::Server instance
90
+
91
+ # returns current dns mock server port
92
+ dns_mock_server.port # => 49322
93
+
94
+ # interface to setup mock records.
95
+ # Available only in case when server mocked records is empty
96
+ dns_mock_server.assign_mocks(records) # => true/nil
97
+
98
+ # interface to reset current mocked records
99
+ dns_mock_server.reset_mocks! # => true
76
100
 
77
- DnsMock.running_servers
78
- DnsMock.stop_running_servers!
101
+ # interface to stop current dns mock server
102
+ dns_mock_server.stop! # => true
103
+
104
+ # returns list of running dns mock servers
105
+ DnsMock.running_servers # => [DnsMock::Server instance]
106
+
107
+ # interface to stop all running dns mock servers
108
+ DnsMock.stop_running_servers! # => true
79
109
  ```
110
+
111
+ ## Contributing
112
+
113
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mocktools/ruby-dns-mock. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. Please check the [open tikets](https://github.com/mocktools/ruby-dns-mock/issues). Be shure to follow Contributor Code of Conduct below and our [Contributing Guidelines](CONTRIBUTING.md).
114
+
115
+ ## License
116
+
117
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
118
+
119
+ ## Code of Conduct
120
+
121
+ Everyone interacting in the DnsMock project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
122
+
123
+ ## Credits
124
+
125
+ - [The Contributors](https://github.com/mocktools/ruby-dns-mock/graphs/contributors) for code and awesome suggestions
126
+ - [The Stargazers](https://github.com/mocktools/ruby-dns-mock/stargazers) for showing their support
127
+
128
+ ## Versioning
129
+
130
+ DnsMock uses [Semantic Versioning 2.0.0](https://semver.org)
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.email = ['admin@bestweb.com.ua']
12
12
 
13
13
  spec.summary = %(dns_mock)
14
- spec.description = %(Ruby DNS mock)
14
+ spec.description = %(💎 Ruby DNS mock. Mimic any DNS records for your test environment and even more.)
15
15
 
16
16
  spec.homepage = 'https://github.com/mocktools/ruby-dns-mock'
17
17
  spec.license = 'MIT'
@@ -4,7 +4,7 @@ require 'resolv'
4
4
  require 'socket'
5
5
 
6
6
  module DnsMock
7
- AVAILABLE_DNS_RECORD_TYPES = %i[a aaaa cname mx ns soa txt].freeze
7
+ AVAILABLE_DNS_RECORD_TYPES = %i[a aaaa cname mx ns ptr soa txt].freeze
8
8
 
9
9
  module Error
10
10
  require_relative '../dns_mock/error/argument_type'
@@ -26,6 +26,7 @@ module DnsMock
26
26
  require_relative '../dns_mock/record/factory/cname'
27
27
  require_relative '../dns_mock/record/factory/mx'
28
28
  require_relative '../dns_mock/record/factory/ns'
29
+ require_relative '../dns_mock/record/factory/ptr'
29
30
  require_relative '../dns_mock/record/factory/soa'
30
31
  require_relative '../dns_mock/record/factory/txt'
31
32
  end
@@ -39,6 +40,7 @@ module DnsMock
39
40
  require_relative '../dns_mock/record/builder/cname'
40
41
  require_relative '../dns_mock/record/builder/mx'
41
42
  require_relative '../dns_mock/record/builder/ns'
43
+ require_relative '../dns_mock/record/builder/ptr'
42
44
  require_relative '../dns_mock/record/builder/soa'
43
45
  require_relative '../dns_mock/record/builder/txt'
44
46
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DnsMock
4
+ module Record
5
+ module Builder
6
+ Ptr = ::Class.new(DnsMock::Record::Builder::Base)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DnsMock
4
+ module Record
5
+ module Factory
6
+ Ptr = ::Class.new(DnsMock::Record::Factory::Base) do
7
+ record_type :ptr
8
+
9
+ def instance_params
10
+ [create_dns_name(record_data)]
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -5,6 +5,8 @@ module DnsMock
5
5
  class RecordsDictionaryBuilder
6
6
  include DnsMock::Error::Helper
7
7
 
8
+ IP_ADDRESS_PATTERN = /\A((1\d|[1-9]|2[0-4])?\d|25[0-5])(\.\g<1>){3}\z/.freeze
9
+ RDNS_LOOKUP_PREFIX = '.in-addr.arpa'
8
10
  TYPE_MAPPER = DnsMock::AVAILABLE_DNS_RECORD_TYPES.zip(
9
11
  [
10
12
  [DnsMock::Record::Builder::A, DnsMock::Record::Factory::A, ::Array],
@@ -12,6 +14,7 @@ module DnsMock
12
14
  [DnsMock::Record::Builder::Cname, DnsMock::Record::Factory::Cname, ::String],
13
15
  [DnsMock::Record::Builder::Mx, DnsMock::Record::Factory::Mx, ::Array],
14
16
  [DnsMock::Record::Builder::Ns, DnsMock::Record::Factory::Ns, ::Array],
17
+ [DnsMock::Record::Builder::Ptr, DnsMock::Record::Factory::Ptr, ::Array],
15
18
  [DnsMock::Record::Builder::Soa, DnsMock::Record::Factory::Soa, ::Array],
16
19
  [DnsMock::Record::Builder::Txt, DnsMock::Record::Factory::Txt, ::Array]
17
20
  ]
@@ -31,7 +34,7 @@ module DnsMock
31
34
  raise_unless(DnsMock::Error::ArgumentType.new(records_to_build.class), records_to_build.is_a?(::Hash))
32
35
  records_to_build.each do |hostname, dns_records|
33
36
  raise_unless(DnsMock::Error::RecordHostType.new(hostname, hostname.class), hostname.is_a?(::String))
34
- records[hostname] = dns_records.each_with_object({}) do |(record_type, records_data), records_instances_by_type|
37
+ records[rdns_lookup_prefix(hostname)] = dns_records.each_with_object({}) do |(record_type, records_data), records_instances_by_type|
35
38
  records_instances_by_type[record_type] = build_records_instances_by_type(record_type, records_data)
36
39
  end
37
40
  end
@@ -41,6 +44,12 @@ module DnsMock
41
44
 
42
45
  private
43
46
 
47
+ def rdns_lookup_prefix(hostname)
48
+ return hostname unless hostname[DnsMock::Server::RecordsDictionaryBuilder::IP_ADDRESS_PATTERN]
49
+
50
+ "#{hostname}#{DnsMock::Server::RecordsDictionaryBuilder::RDNS_LOOKUP_PREFIX}"
51
+ end
52
+
44
53
  def build_records_instances_by_type(record_type, records_to_build)
45
54
  target_builder, target_factory, expected_type = DnsMock::Server::RecordsDictionaryBuilder::TYPE_MAPPER[record_type]
46
55
  raise_record_type_error(record_type, target_builder)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DnsMock
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dns_mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladislav Trotsenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-19 00:00:00.000000000 Z
11
+ date: 2021-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -236,7 +236,8 @@ dependencies:
236
236
  - - "~>"
237
237
  - !ruby/object:Gem::Version
238
238
  version: 0.17.1
239
- description: Ruby DNS mock
239
+ description: "\U0001F48E Ruby DNS mock. Mimic any DNS records for your test environment
240
+ and even more."
240
241
  email:
241
242
  - admin@bestweb.com.ua
242
243
  executables: []
@@ -286,6 +287,7 @@ files:
286
287
  - lib/dns_mock/record/builder/cname.rb
287
288
  - lib/dns_mock/record/builder/mx.rb
288
289
  - lib/dns_mock/record/builder/ns.rb
290
+ - lib/dns_mock/record/builder/ptr.rb
289
291
  - lib/dns_mock/record/builder/soa.rb
290
292
  - lib/dns_mock/record/builder/txt.rb
291
293
  - lib/dns_mock/record/factory/a.rb
@@ -294,6 +296,7 @@ files:
294
296
  - lib/dns_mock/record/factory/cname.rb
295
297
  - lib/dns_mock/record/factory/mx.rb
296
298
  - lib/dns_mock/record/factory/ns.rb
299
+ - lib/dns_mock/record/factory/ptr.rb
297
300
  - lib/dns_mock/record/factory/soa.rb
298
301
  - lib/dns_mock/record/factory/txt.rb
299
302
  - lib/dns_mock/response/answer.rb