raise SkipTest unless service?(:mx) require 'json' require 'net/smtp' class Mx < LeapTest depends_on "Network" depends_on "Webapp" if service?(:webapp) def setup end def test_01_Can_contact_couchdb? dbs = ["identities"] dbs.each do |db_name| couchdb_urls("/"+db_name, couch_url_options).each do |url| assert_get(url) do |body| assert response = JSON.parse(body) assert_equal db_name, response['db_name'] end end end pass end def test_02_Can_contact_couchdb_via_haproxy? if property('haproxy.couch') url = couchdb_url_via_haproxy("", couch_url_options) assert_get(url) do |body| assert_match /"couchdb":"Welcome"/, body, "Request to #{url} should return couchdb welcome message." end pass end end # # this test picks a random identity document, then queries # using the by_address view for that same document again. # def test_03_Can_query_identities_db? assert_get(couchdb_url("/identities", couch_url_options)) do |body| assert response = JSON.parse(body) doc_count = response['doc_count'].to_i if doc_count <= 1 # the design document counts as one document. skip "There are no identity documents yet." else # try five times to get a valid doc for i in 1..5 offset = rand(doc_count) # pick a random document count_url = couchdb_url("/identities/_all_docs?include_docs=true&limit=1&skip=#{offset}", couch_url_options) assert_get(count_url) do |body| assert response = JSON.parse(body) record = response['rows'].first if record['id'] =~ /_design/ next else address = record['doc']['address'] assert address, "Identity document #{record['id']} is missing an address field. #{record['doc'].inspect}" url_base = %(/identities/_design/Identity/_view/by_address) params = %(?include_docs=true&reduce=false&startkey="#{address}"&endkey="#{address}") assert_get(couchdb_url(url_base+params, couch_url_options)) do |body| assert response = JSON.parse(body) assert record = response['rows'].first assert_equal address, record['doc']['address'] pass end break end end end end end end def test_04_Are_MX_daemons_running? assert_running '.*/usr/bin/twistd.*mx.tac' assert_running '^/usr/lib/postfix/master$' assert_running '^/usr/sbin/postfwd' assert_running 'postfwd2::cache$' assert_running 'postfwd2::policy$' assert_running '^/usr/sbin/unbound$' assert_running '^/usr/bin/freshclam' assert_running '^/usr/sbin/opendkim' if Dir.glob("/var/lib/clamav/main.{c[vl]d,inc}").size > 0 and Dir.glob("/var/lib/clamav/daily.{c[vl]d,inc}").size > 0 assert_running '^/usr/sbin/clamd' assert_running '^/usr/sbin/clamav-milter' else skip "Downloading the clamav signature files (/var/lib/clamav/{daily,main}.{c[vl]d,inc}) is still in progress, so clamd is not running.\nDon't worry, mail delivery will work without clamav. The download should finish soon." end pass end # # The email sent by this test might get bounced back. # In this case, the test will pass, but the bounce message will # get sent to root, so the sysadmin will still figure out pretty # quickly that something is wrong. # def test_05_Can_deliver_email? addr = [TEST_EMAIL_USER, property('domain.full_suffix')].join('@') bad_addr = [TEST_BAD_USER, property('domain.full_suffix')].join('@') assert !identity_exists?(bad_addr), "the address #{bad_addr} must not exist." if !identity_exists?(addr) user = assert_create_user(TEST_EMAIL_USER, :monitor) upload_public_key(user.id, TEST_EMAIL_PUBLIC_KEY) end assert identity_exists?(addr), "The identity #{addr} should have been created, but it doesn't exist yet." assert_send_email(addr) assert_raises(Net::SMTPError) do send_email(bad_addr) end pass end private def couch_url_options { :username => property('couchdb_leap_mx_user.username'), :password => property('couchdb_leap_mx_user.password') } end TEST_EMAIL_PUBLIC_KEY=<