f00860
f00860

Reputation: 3576

rspec with different DNS server

I have a bunch of rspec test which uses external databases (I know, this is not a good idea, but there are a lot of reasons this is the best solution in this case). I've set up a different vlan with a test environment to run the tests within a isolated environment. Now I want to define that all rspec tests use a different DNS server to resolve the hostnames (to work with the dev environment). Is there something I can use?

Upvotes: 1

Views: 376

Answers (2)

Vladislav
Vladislav

Reputation: 1

You can use DnsMock instead. It's testing framework agnostic, lightweight UDP DNS mock server with dynamic/manual port assignment: https://github.com/mocktools/ruby-dns-mock

require 'dns_mock'

records = {
  'example.com' => {
    a: %w[1.1.1.1 2.2.2.2],
    aaaa: %w[2a00:1450:4001:81e::200e],
    ns: %w[ns1.domain.com ns2.domain.com],
    mx: %w[mx1.domain.com mx2.domain.com],
    txt: %w[txt_record_1 txt_record_2],
    cname: 'some.domain.com',
    soa: [
      {
        mname: 'dns1.domain.com',
        rname: 'dns2.domain.com',
        serial: 2_035_971_683,
        refresh: 10_000,
        retry: 2_400,
        expire: 604_800,
        minimum: 3_600
      }
    ]
  },
  '1.1.1.1' => {
    ptr: %w[domain_1.com domain_2.com]
  }
}

DnsMock.start_server(records: records, port: 5300)

Upvotes: 0

rainkinz
rainkinz

Reputation: 10394

RubyDNS might work for you. This example is almost verbatim from their GH page:

#!/usr/bin/env ruby
require 'rubydns'

INTERFACES = [
  [:udp, "0.0.0.0", 5300],
  [:tcp, "0.0.0.0", 5300]
]
Name = Resolv::DNS::Name
IN = Resolv::DNS::Resource::IN

# Use upstream DNS for name resolution.
UPSTREAM = RubyDNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]])

# start the RubyDNS server
RubyDNS::run_server(:listen => INTERFACES) do
    match(/database\.testing\.com/, IN::A) do |transaction|
        transaction.respond!("10.0.0.80")
    end

    # Default DNS handler
    otherwise do |transaction|
        transaction.passthrough!(UPSTREAM)
    end
end

then to query:

➜  ruby_dns_example  dig @localhost -p 5300 database.testing.com

; <<>> DiG 9.8.3-P1 <<>> @localhost -p 5300 database.testing.com
; (3 servers found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34286
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;database.testing.com.      IN  A

;; ANSWER SECTION:
database.testing.com.   86400   IN  A   10.0.0.80

;; Query time: 2 msec
;; SERVER: 127.0.0.1#5300(127.0.0.1)
;; WHEN: Sat Jan 31 13:14:14 2015
;; MSG SIZE  rcvd: 54

There are lots of examples here:

https://github.com/ioquatix/rubydns/tree/master/examples

Upvotes: 1

Related Questions