X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/7fb83a3380e62721801a4980c48ba78208c7b2e2..4b3e5e50e262fc51a158c1aed1942b1ef176e2eb:/apps/workbench/test/test_helper.rb diff --git a/apps/workbench/test/test_helper.rb b/apps/workbench/test/test_helper.rb index 797551e7fb..1195798312 100644 --- a/apps/workbench/test/test_helper.rb +++ b/apps/workbench/test/test_helper.rb @@ -1,4 +1,5 @@ -ENV["RAILS_ENV"] = "test" +ENV["RAILS_ENV"] = "test" if (ENV["RAILS_ENV"] != "diagnostics") + unless ENV["NO_COVERAGE_TEST"] begin require 'simplecov' @@ -21,9 +22,7 @@ end require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' - -$ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__) -SERVER_PID_PATH = 'tmp/pids/server.pid' +require 'mocha/mini_test' class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in @@ -39,6 +38,7 @@ class ActiveSupport::TestCase def teardown Thread.current[:arvados_api_token] = nil + Thread.current[:reader_tokens] = nil super end end @@ -54,7 +54,8 @@ module ApiFixtureLoader # Returns the data structure from the named API server test fixture. @@api_fixtures[name] ||= \ begin - path = File.join($ARV_API_SERVER_DIR, 'test', 'fixtures', "#{name}.yml") + path = File.join(ApiServerForTests::ARV_API_SERVER_DIR, + 'test', 'fixtures', "#{name}.yml") YAML.load(IO.read(path)) end end @@ -71,50 +72,107 @@ class ActiveSupport::TestCase arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token'] } end + def json_response + Oj.load(@response.body) + end end -class ApiServerBackedTestRunner < MiniTest::Unit - # Make a hash that unsets Bundle's environment variables. - # We'll use this environment when we launch Bundle commands in the API - # server. Otherwise, those commands will try to use Workbench's gems, etc. - @@APIENV = Hash[ENV.map { |key, val| - (key =~ /^BUNDLE_/) ? [key, nil] : nil - }.compact] - - def _system(*cmd) - if not system(@@APIENV, *cmd) - raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}" +class ApiServerForTests + ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__) + SERVER_PID_PATH = File.expand_path('tmp/pids/wbtest-server.pid', ARV_API_SERVER_DIR) + @main_process_pid = $$ + + def self._system(*cmd) + $stderr.puts "_system #{cmd.inspect}" + Bundler.with_clean_env do + if not system({'RAILS_ENV' => 'test'}, *cmd) + raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}" + end + end + end + + def self.make_ssl_cert + unless File.exists? './self-signed.key' + _system('openssl', 'req', '-new', '-x509', '-nodes', + '-out', './self-signed.pem', + '-keyout', './self-signed.key', + '-days', '3650', + '-subj', '/CN=localhost') + end + end + + def self.kill_server + if (pid = find_server_pid) + $stderr.puts "Sending TERM to API server, pid #{pid}" + Process.kill 'TERM', pid + end + end + + def self.find_server_pid + pid = nil + begin + pid = IO.read(SERVER_PID_PATH).to_i + $stderr.puts "API server is running, pid #{pid.inspect}" + rescue Errno::ENOENT end + return pid end - def _run(args=[]) + def self.run(args=[]) + ::MiniTest.after_run do + self.kill_server + end + + # Kill server left over from previous test run + self.kill_server + Capybara.javascript_driver = :poltergeist - server_pid = Dir.chdir($ARV_API_SERVER_DIR) do |apidir| + Dir.chdir(ARV_API_SERVER_DIR) do |apidir| ENV["NO_COVERAGE_TEST"] = "1" + make_ssl_cert _system('bundle', 'exec', 'rake', 'db:test:load') _system('bundle', 'exec', 'rake', 'db:fixtures:load') - _system('bundle', 'exec', 'rails', 'server', '-d') + _system('bundle', 'exec', 'passenger', 'start', '-d', '-p3000', + '--pid-file', SERVER_PID_PATH, + '--ssl', + '--ssl-certificate', 'self-signed.pem', + '--ssl-certificate-key', 'self-signed.key') timeout = Time.now.tv_sec + 10 - begin + good_pid = false + while (not good_pid) and (Time.now.tv_sec < timeout) sleep 0.2 - begin - server_pid = IO.read(SERVER_PID_PATH).to_i - good_pid = (server_pid > 0) and (Process.kill(0, pid) rescue false) - rescue Errno::ENOENT - good_pid = false - end - end while (not good_pid) and (Time.now.tv_sec < timeout) + server_pid = find_server_pid + good_pid = (server_pid and + (server_pid > 0) and + (Process.kill(0, server_pid) rescue false)) + end if not good_pid raise RuntimeError, "could not find API server Rails pid" end - server_pid end - begin - super(args) - ensure - Process.kill('TERM', server_pid) + end +end + +class ActionController::TestCase + setup do + @counter = 0 + end + + def check_counter action + @counter += 1 + if @counter == 2 + assert_equal 1, 2, "Multiple actions in functional test" + end + end + + [:get, :post, :put, :patch, :delete].each do |method| + define_method method do |action, *args| + check_counter action + super action, *args end end end -MiniTest::Unit.runner = ApiServerBackedTestRunner.new +if ENV["RAILS_ENV"].eql? 'test' + ApiServerForTests.run +end