Skip to content

Commit

Permalink
Rewrite integration tests to ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
ekohl committed Oct 10, 2015
1 parent 489ab49 commit bd5f16c
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 90 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,16 @@ Fork and send a Pull Request. Thanks!

### Running the integration tests

Since I'm mostly a python developer, I've written the integration tests in python.

First you need to run the smart proxy on `http://localhost:8000` and a powerdns instance on `127.0.0.1:5300` or change it in the fixtures.
First you need to run the smart proxy on `http://localhost:8000` and a powerdns instance on `127.0.0.1:5300`.

It is assumed the powerdns instance has both the `example.com` and `in-addr.arpa` domains configured. If not, create them:

INSERT INTO domains (name, type) VALUES ('example.com', 'master'), ('in-addr.arpa', 'master');
INSERT INTO records (domain_id, name, type, content) SELECT id domain_id, name, 'SOA', 'ns1.example.com hostmaster.example.com. 0 3600 1800 1209600 3600' FROM domains WHERE NOT EXISTS (SELECT 1 FROM records WHERE records.domain_id=domains.id AND records.name=domains.name AND type='SOA');

Then you need to install the required dependencies (dnspython, pytest and requests). Then run the tests:
Then run the tests:

py.test test/integration_tests.py
bundle exec rake test:integration

## Copyright

Expand Down
31 changes: 25 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,31 @@ require 'rake/testtask'

desc 'Default: run unit tests.'
task :default => :test
task :test => "test:unit"

desc 'Test the Foreman Proxy plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << '.'
t.libs << 'lib'
t.libs << 'test'
t.test_files = FileList['test/**/*_test.rb']
t.verbose = true
namespace :test do
desc "Run all tests"
Rake::TestTask.new(:all) do |t|
t.libs << 'lib'
t.libs << 'test'
t.test_files = FileList['test/**/*_test.rb']
t.verbose = true
end

desc "Run unit tests"
Rake::TestTask.new(:unit) do |t|
t.libs << 'lib'
t.libs << 'test'
t.test_files = FileList['test/unit/**/*_test.rb']
t.verbose = true
end

desc "Run integration tests"
Rake::TestTask.new(:integration) do |t|
t.libs << 'lib'
t.libs << 'test'
t.test_files = FileList['test/integration/**/*_test.rb']
t.verbose = true
end
end
83 changes: 83 additions & 0 deletions test/integration/integration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require 'test_helper'

require 'ipaddr'
require 'net/http'

class DnsPowerdnsIntegrationTest < Test::Unit::TestCase

def test_forward_dns
data = {'fqdn' => fqdn, 'value' => ip, 'type' => 'A'}

uri = URI(smart_proxy_url)

Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Post.new(URI(smart_proxy_url + 'dns/'))
request.form_data = data
response = http.request request
assert_equal(200, response.code.to_i)

addresses = resolver.getresources(data['fqdn'], Resolv::DNS::Resource::IN::A)
assert_equal([Resolv::DNS::Resource::IN::A.new(Resolv::IPv4.create(data['value']))], addresses)

request = Net::HTTP::Delete.new(URI(smart_proxy_url + 'dns/' + data['fqdn']))
response = http.request request
assert_equal(200, response.code.to_i)

assert(purge_cache data['fqdn'])

addresses = resolver.getresources(data['fqdn'], Resolv::DNS::Resource::IN::A)
assert_equal([], addresses)
end
end

def test_reverse_dns
data = {'fqdn' => fqdn, 'value' => ip, 'type' => 'PTR'}

uri = URI(smart_proxy_url)

Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Post.new(URI(smart_proxy_url + 'dns/'))
request.form_data = data
response = http.request request
assert_equal(200, response.code.to_i)

name = Resolv::IPv4.create(data['value']).to_name.to_s

addresses = resolver.getresources(name, Resolv::DNS::Resource::IN::PTR)
assert_equal([Resolv::DNS::Resource::IN::PTR.new(Resolv::DNS::Name.create(data['fqdn'] + '.'))], addresses, "#{data['value']} should reverse to #{data['fqdn']}")

request = Net::HTTP::Delete.new(URI(smart_proxy_url + 'dns/' + data['fqdn']))
response = http.request request
assert_equal(200, response.code.to_i)

assert(purge_cache name)

addresses = resolver.getresources(data['fqdn'], Resolv::DNS::Resource::IN::PTR)
assert_equal([], addresses)
end
end

private

def resolver
Resolv::DNS.new(:nameserver_port => [['127.0.0.1', 5300]])
end

def smart_proxy_url
'http://localhost:8000/'
end

def fqdn
set = ('a' .. 'z').to_a + ('0' .. '9').to_a
10.times.collect {|i| set[rand(set.size)] }.join + '.example.com'
end

def ip
IPAddr.new(rand(2 ** 32), Socket::AF_INET).to_s
end

def purge_cache name
%x{#{ENV['PDNS_CONTROL'] || "pdns_control"} purge "#{name}"}
$? == 0
end
end
79 changes: 0 additions & 79 deletions test/integration_tests.py

This file was deleted.

File renamed without changes.

0 comments on commit bd5f16c

Please sign in to comment.