1 ENV["RAILS_ENV"] = "test"
2 require File.expand_path('../../config/environment', __FILE__)
3 require 'rails/test_help'
5 $ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__)
6 SERVER_PID_PATH = 'tmp/pids/server.pid'
8 class ActiveSupport::TestCase
9 # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
11 # Note: You'll currently still have to declare fixtures explicitly in integration tests
12 # -- they do not yet inherit this setting
15 # Add more helper methods to be used by all tests here...
18 module ApiFixtureLoader
19 def self.included(base)
20 base.extend(ClassMethods)
26 # Returns the data structure from the named API server test fixture.
27 @@api_fixtures[name] ||= \
29 path = File.join($ARV_API_SERVER_DIR, 'test', 'fixtures', "#{name}.yml")
30 YAML.load(IO.read(path))
35 self.class.api_fixture name
39 class ActiveSupport::TestCase
40 include ApiFixtureLoader
41 def session_for api_client_auth_name
43 arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token']
48 class ApiServerBackedTestRunner < MiniTest::Unit
49 # Make a hash that unsets Bundle's environment variables.
50 # We'll use this environment when we launch Bundle commands in the API
51 # server. Otherwise, those commands will try to use Workbench's gems, etc.
52 @@APIENV = Hash[ENV.map { |key, val|
53 (key =~ /^BUNDLE_/) ? [key, nil] : nil
57 if not system(@@APIENV, *cmd)
58 raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}"
63 Capybara.javascript_driver = :poltergeist
64 server_pid = Dir.chdir($ARV_API_SERVER_DIR) do |apidir|
65 _system('bundle', 'exec', 'rake', 'db:test:load')
66 _system('bundle', 'exec', 'rake', 'db:fixtures:load')
67 _system('bundle', 'exec', 'rails', 'server', '-d')
68 timeout = Time.now.tv_sec + 10
72 server_pid = IO.read(SERVER_PID_PATH).to_i
73 good_pid = (server_pid > 0) and (Process.kill(0, pid) rescue false)
77 end while (not good_pid) and (Time.now.tv_sec < timeout)
79 raise RuntimeError, "could not find API server Rails pid"
86 Process.kill('TERM', server_pid)
91 MiniTest::Unit.runner = ApiServerBackedTestRunner.new