X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/a59152e34814f453a7380e68a55534e2f8922d57..ca0d33c522fad9b3467f4a58b8fe336665ed493b:/apps/workbench/test/test_helper.rb diff --git a/apps/workbench/test/test_helper.rb b/apps/workbench/test/test_helper.rb index 8bf1192ffe..145914f741 100644 --- a/apps/workbench/test/test_helper.rb +++ b/apps/workbench/test/test_helper.rb @@ -2,6 +2,9 @@ ENV["RAILS_ENV"] = "test" 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' + class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. # @@ -11,3 +14,78 @@ class ActiveSupport::TestCase # Add more helper methods to be used by all tests here... end + +module ApiFixtureLoader + def self.included(base) + base.extend(ClassMethods) + end + + module ClassMethods + @@api_fixtures = {} + def api_fixture(name) + # 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") + YAML.load(IO.read(path)) + end + end + end + def api_fixture name + self.class.api_fixture name + end +end + +class ActiveSupport::TestCase + include ApiFixtureLoader + def session_for api_client_auth_name + { + arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token'] + } + 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}" + end + end + + def _run(args=[]) + Capybara.javascript_driver = :poltergeist + server_pid = Dir.chdir($ARV_API_SERVER_DIR) do |apidir| + _system('bundle', 'exec', 'rake', 'db:test:load') + _system('bundle', 'exec', 'rake', 'db:fixtures:load') + _system('bundle', 'exec', 'rails', 'server', '-d') + timeout = Time.now.tv_sec + 10 + begin + 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) + 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 +end + +MiniTest::Unit.runner = ApiServerBackedTestRunner.new