From 68328b6e15a38a74261ee46b06091b8e63f85499 Mon Sep 17 00:00:00 2001 From: elijah Date: Mon, 9 Dec 2013 23:06:19 -0800 Subject: added initial white-box tests for couchdb and webapp nodes --- bin/run_tests | 101 ++++++++++++++++++++++++++++++++++++++------- tests/README.md | 12 ++++++ tests/dummy.rb | 44 -------------------- tests/network.rb | 12 ------ tests/webapp.rb | 13 ------ tests/white-box/couchdb.rb | 54 ++++++++++++++++++++++++ tests/white-box/dummy.rb | 44 ++++++++++++++++++++ tests/white-box/network.rb | 13 ++++++ tests/white-box/webapp.rb | 53 ++++++++++++++++++++++++ 9 files changed, 262 insertions(+), 84 deletions(-) create mode 100644 tests/README.md delete mode 100644 tests/dummy.rb delete mode 100644 tests/network.rb delete mode 100644 tests/webapp.rb create mode 100644 tests/white-box/couchdb.rb create mode 100644 tests/white-box/dummy.rb create mode 100644 tests/white-box/network.rb create mode 100644 tests/white-box/webapp.rb diff --git a/bin/run_tests b/bin/run_tests index 91c742c5..8c5fb492 100755 --- a/bin/run_tests +++ b/bin/run_tests @@ -14,6 +14,7 @@ require 'minitest/unit' require 'yaml' require 'tsort' +require 'net/http' ## ## EXCEPTIONS @@ -61,11 +62,84 @@ class LeapTest < MiniTest::Unit::TestCase end # + # the default fail() is part of the kernel and it just throws a runtime exception. for tests, + # we want the same behavior as assert(false) + # + def fail(msg=nil) + assert(false, msg) + end + # Always runs test methods within a test class in alphanumeric order # def self.test_order :alpha end + + # + # attempts a http GET on the url, yields |body, response, error| + # + def get(url, params=nil) + uri = URI(url) + if params + uri.query = URI.encode_www_form(params) + end + response = Net::HTTP.get_response(uri) + if response.is_a?(Net::HTTPSuccess) + yield response.body, response, nil + else + yield nil, response, nil + end + rescue => exc + yield nil, nil, exc + end + + def assert_get(url, params=nil) + get(url, params) do |body, response, error| + if body + yield body + elsif response + fail "Expected a 200 status code from #{url}, but got #{response.code} instead." + else + fail "Expected a response from #{url}, but got \"#{error}\" instead." + end + end + end + + # + # Matches the regexp in the file, and returns the first matched string (or fails if no match). + # + def file_match(filename, regexp) + if match = File.read(filename).match(regexp) + match.captures.first + else + fail "Regexp #{regexp.inspect} not found in file #{filename.inspect}." + end + end + + # + # Matches the regexp in the file, and returns array of matched strings (or fails if no match). + # + def file_matches(filename, regexp) + if match = File.read(filename).match(regexp) + match.captures + else + fail "Regexp #{regexp.inspect} not found in file #{filename.inspect}." + end + end + + # + # checks to make sure the given property path exists in $node (e.g. hiera.yaml) + # and returns the value + # + def assert_property(property) + latest = $node + property.split('.').each do |segment| + latest = latest[segment] + fail "Required node property `#{property}` is missing." if latest.nil? + end + return latest + end + end # @@ -93,16 +167,12 @@ class LeapRunner < MiniTest::Unit results = _run_suites(suites, :test) @test_count = results.inject(0) { |sum, (tc, _)| sum + tc } @assertion_count = results.inject(0) { |sum, (_, ac)| sum + ac } - report.each {|msg| output.puts msg} status rescue Interrupt - report.each {|msg| output.puts msg} abort 'Tests halted on interrupt.' rescue TestFailure - report.each {|msg| output.puts msg} abort 'Tests halted on failure (because of --no-continue).' rescue TestError - report.each {|msg| output.puts msg} abort 'Tests halted on error (because of --no-continue).' end @@ -114,21 +184,21 @@ class LeapRunner < MiniTest::Unit when MiniTest::Skip then @skips += 1 #if @verbose - @report << report_line("SKIP", klass, meth, e, e.message) + report_line("SKIP", klass, meth, e, e.message) #end when LeapTest::Pass then @passes += 1 @report << report_line("PASS", klass, meth) when MiniTest::Assertion then @failures += 1 - @report << report_line("FAIL", klass, meth, e, e.message) + report_line("FAIL", klass, meth, e, e.message) if $halt_on_failure raise TestFailure.new end else @errors += 1 bt = MiniTest::filter_backtrace(e.backtrace).join "\n" - @report << report_line("ERROR", klass, meth, e, "#{e.class}: #{e.message}\n#{bt}") + report_line("ERROR", klass, meth, e, "#{e.class}: #{e.message}\n#{bt}") if $halt_on_failure raise TestError.new end @@ -141,7 +211,7 @@ class LeapRunner < MiniTest::Unit # def status(io = self.output) format = "%d tests, %d assertions, %d passes, %d failures, %d errors, %d skips" - io.puts format % [test_count, assertion_count, passes, failures, errors, skips] + output.puts format % [test_count, assertion_count, passes, failures, errors, skips] end private @@ -151,13 +221,14 @@ class LeapRunner < MiniTest::Unit # def report_line(prefix, klass, meth, e=nil, message=nil) if e && message - #indent = "\n" + (" " * (prefix.length+4)) indent = "\n " msg_txt = indent + message.split("\n").join(indent) - "#{prefix}: #{readable(klass.name)} > #{readable(meth)} [#{File.basename(location(e))}]:#{msg_txt}\n" + output.puts "#{prefix}: #{readable(klass.name)} > #{readable(meth)} [#{File.basename(location(e))}]:#{msg_txt}" else - "#{prefix}: #{readable(klass.name)} > #{readable(meth)}\n" + output.puts "#{prefix}: #{readable(klass.name)} > #{readable(meth)}" end + # I don't understand at all why, but adding a very tiny sleep here will + sleep(0.0001) # keep lines from being joined together by the logger. output.flush doesn't. end # @@ -167,8 +238,8 @@ class LeapRunner < MiniTest::Unit str.gsub(/([A-Z]+)([A-Z][a-z])/, '\1 \2'). gsub(/([a-z])([A-Z])/, '\1 \2'). gsub(/_/, ' '). - sub(/^test /i, ''). - downcase + sub(/^test (\d* )?/i, ''). + downcase.capitalize end end @@ -211,11 +282,11 @@ end if File.exists?('/etc/leap/hiera.yaml') $node = YAML.load_file('/etc/leap/hiera.yaml') else - $node = {"services" => ['webapp'], "dummy" => true} + $node = {"services" => [], "dummy" => true} end # load all test classes -Dir[File.expand_path('../../tests/*.rb', __FILE__)].each do |test_file| +Dir[File.expand_path('../../tests/white-box/*.rb', __FILE__)].each do |test_file| begin require test_file rescue SkipTest diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 00000000..debbf700 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,12 @@ +This directory contains to kinds of tests: + +White Box Tests +================================ + +These tests are run on the server as superuser. They are for troubleshooting any problems with the internal setup of the server. + +Black Box Tests +================================ + +These test are run the user's local machine. They are for troubleshooting any external problems with the service exposed by the server. + diff --git a/tests/dummy.rb b/tests/dummy.rb deleted file mode 100644 index e7964a6c..00000000 --- a/tests/dummy.rb +++ /dev/null @@ -1,44 +0,0 @@ -# only run in the dummy case where there is no hiera.yaml file. -raise SkipTest unless $node["dummy"] - -class Robot - def can_shoot_lasers? - "OHAI!" - end - - def can_fly? - "YES!" - end -end - -class TestDummy < LeapTest - def setup - @robot = Robot.new - end - - def test_lasers - assert_equal "OHAI!", @robot.can_shoot_lasers? - pass - end - - def test_fly - refute_match /^no/i, @robot.can_fly? - pass - end - - def test_blah - assert false - pass - end - - def test_that_will_be_skipped - skip "test this later" - pass - end - - def test_err - 12/0 - pass - end - -end diff --git a/tests/network.rb b/tests/network.rb deleted file mode 100644 index 115d356a..00000000 --- a/tests/network.rb +++ /dev/null @@ -1,12 +0,0 @@ -require File.dirname(__FILE__) + '/webapp' - -class TestNetwork < LeapTest - - def setup - end - - def test_test - pass - end - -end diff --git a/tests/webapp.rb b/tests/webapp.rb deleted file mode 100644 index 1762de9e..00000000 --- a/tests/webapp.rb +++ /dev/null @@ -1,13 +0,0 @@ -raise SkipTest unless $node["services"].include?("webapp") - -class TestAWebapp < LeapTest - depends_on "TestNetwork" - - def setup - end - - def test_test - assert false, 'hey, stop here' - end - -end diff --git a/tests/white-box/couchdb.rb b/tests/white-box/couchdb.rb new file mode 100644 index 00000000..0fc4d3b2 --- /dev/null +++ b/tests/white-box/couchdb.rb @@ -0,0 +1,54 @@ +raise SkipTest unless $node["services"].include?("couchdb") + +require 'json' + +class TestCouchdb < LeapTest + + def setup + end + + # + # check to make sure we can get welcome response from local couchdb + # + def test_01_is_running + assert_get(couchdb_url) do |body| + assert_match /"couchdb":"Welcome"/, body, "Could not get welcome message from #{couchdb_url}. Probably couchdb is not running." + end + pass + end + + # + # compare the configured nodes to the nodes that are actually listed in bigcouch + # + def test_02_nodes_are_in_replication_database + url = couchdb_admin_url("/nodes/_all_docs") + neighbors = assert_property('couch.bigcouch.neighbors') + neighbors << assert_property('domain.full') + neighbors.sort! + assert_get(url) do |body| + response = JSON.parse(body) + nodes_in_db = response['rows'].collect{|row| row['id'].sub(/^bigcouch@/, '')}.sort + assert_equal neighbors, nodes_in_db, "The couchdb replication node list is wrong (/nodes/_all_docs)" + end + pass + end + + private + + def couchdb_url(path="", port=nil) + @port ||= begin + assert_property 'couch.port' + $node['couch']['port'] + end + @password ||= begin + assert_property 'couch.users.admin.password' + $node['couch']['users']['admin']['password'] + end + "http://admin:#{@password}@localhost:#{port || @port}#{path}" + end + + def couchdb_admin_url(path="") + couchdb_url(path, "5986") # admin port is hardcoded for now. + end + +end diff --git a/tests/white-box/dummy.rb b/tests/white-box/dummy.rb new file mode 100644 index 00000000..dd343769 --- /dev/null +++ b/tests/white-box/dummy.rb @@ -0,0 +1,44 @@ +# only run in the dummy case where there is no hiera.yaml file. +raise SkipTest unless $node["dummy"] + +class Robot + def can_shoot_lasers? + "OHAI!" + end + + def can_fly? + "YES!" + end +end + +class TestDummy < LeapTest + def setup + @robot = Robot.new + end + + def test_lasers + assert_equal "OHAI!", @robot.can_shoot_lasers? + pass + end + + def test_fly + refute_match /^no/i, @robot.can_fly? + pass + end + + def test_blah + fail "blah" #assert false + pass + end + + def test_01_will_be_skipped + skip "test this later" + pass + end + + def test_err + 12/0 + pass + end + +end diff --git a/tests/white-box/network.rb b/tests/white-box/network.rb new file mode 100644 index 00000000..9680cb5f --- /dev/null +++ b/tests/white-box/network.rb @@ -0,0 +1,13 @@ +class TestNetwork < LeapTest + + def setup + end + + # + # TODO: write an actual test to confirm the network is up and working. + # + def test_working + pass + end + +end diff --git a/tests/white-box/webapp.rb b/tests/white-box/webapp.rb new file mode 100644 index 00000000..65f3217b --- /dev/null +++ b/tests/white-box/webapp.rb @@ -0,0 +1,53 @@ +raise SkipTest unless $node["services"].include?("webapp") + +class TestWebapp < LeapTest + depends_on "TestNetwork" + + HAPROXY_CONFIG = '/etc/haproxy/haproxy.cfg' + + def setup + end + + # + # example properties: + # + # stunnel: + # couch_client: + # couch1_5984: + # accept_port: 4000 + # connect: couch1.bitmask.i + # connect_port: 15984 + # + def test_01_stunnel_is_working + assert_property('stunnel.couch_client') + $node['stunnel']['couch_client'].values.each do |stunnel_conf| + assert port = stunnel_conf['accept_port'], 'Field `accept_port` must be present in `stunnel` property.' + local_stunnel_url = "http://localhost:#{port}" + assert_get(local_stunnel_url) do |body| + assert_match /"couchdb":"Welcome"/, body, "Request to #{local_stunnel_url} should return couchdb welcome message." + end + end + pass + end + + # + # example properties: + # + # haproxy: + # servers: + # couch1: + # backup: false + # host: localhost + # port: 4000 + # weight: 10 + # + def test_02_haproxy_is_working + port = file_match(HAPROXY_CONFIG, /^ bind localhost:(\d+)$/) + url = "http://localhost:#{port}" + assert_get(url) do |body| + assert_match /"couchdb":"Welcome"/, body, "Request to #{url} should return couchdb welcome message." + end + pass + end + +end -- cgit v1.2.3