1 ENV["RAILS_ENV"] = "test" if (ENV["RAILS_ENV"] != "diagnostics")
3 unless ENV["NO_COVERAGE_TEST"]
6 require 'simplecov-rcov'
7 class SimpleCov::Formatter::MergedFormatter
9 SimpleCov::Formatter::HTMLFormatter.new.format(result)
10 SimpleCov::Formatter::RcovFormatter.new.format(result)
13 SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
16 add_filter 'initializers/secret_token'
19 $stderr.puts "SimpleCov unavailable (#{e}). Proceeding without."
23 require File.expand_path('../../config/environment', __FILE__)
24 require 'rails/test_help'
26 class ActiveSupport::TestCase
27 # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in
30 # Note: You'll currently still have to declare fixtures explicitly
31 # in integration tests -- they do not yet inherit this setting
33 def use_token token_name
34 auth = api_fixture('api_client_authorizations')[token_name.to_s]
35 Thread.current[:arvados_api_token] = auth['api_token']
39 Thread.current[:arvados_api_token] = nil
40 Thread.current[:reader_tokens] = nil
45 module ApiFixtureLoader
46 def self.included(base)
47 base.extend(ClassMethods)
53 # Returns the data structure from the named API server test fixture.
54 @@api_fixtures[name] ||= \
56 path = File.join(ApiServerForTests::ARV_API_SERVER_DIR,
57 'test', 'fixtures', "#{name}.yml")
58 YAML.load(IO.read(path))
63 self.class.api_fixture name
67 class ActiveSupport::TestCase
68 include ApiFixtureLoader
69 def session_for api_client_auth_name
71 arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token']
75 Oj.load(@response.body)
79 class ApiServerForTests
80 ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__)
81 SERVER_PID_PATH = File.expand_path('tmp/pids/wbtest-server.pid', ARV_API_SERVER_DIR)
82 @main_process_pid = $$
84 def self._system(*cmd)
85 $stderr.puts "_system #{cmd.inspect}"
86 Bundler.with_clean_env do
87 if not system({'RAILS_ENV' => 'test'}, *cmd)
88 raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}"
93 def self.make_ssl_cert
94 unless File.exists? './self-signed.key'
95 _system('openssl', 'req', '-new', '-x509', '-nodes',
96 '-out', './self-signed.pem',
97 '-keyout', './self-signed.key',
99 '-subj', '/CN=localhost')
104 if (pid = find_server_pid)
105 $stderr.puts "Sending TERM to API server, pid #{pid}"
106 Process.kill 'TERM', pid
110 def self.find_server_pid
113 pid = IO.read(SERVER_PID_PATH).to_i
114 $stderr.puts "API server is running, pid #{pid.inspect}"
120 def self.run(args=[])
121 ::MiniTest.after_run do
125 # Kill server left over from previous test run
128 Capybara.javascript_driver = :poltergeist
129 Dir.chdir(ARV_API_SERVER_DIR) do |apidir|
130 ENV["NO_COVERAGE_TEST"] = "1"
132 _system('bundle', 'exec', 'rake', 'db:test:load')
133 _system('bundle', 'exec', 'rake', 'db:fixtures:load')
134 _system('bundle', 'exec', 'passenger', 'start', '-d', '-p3001',
135 '--pid-file', SERVER_PID_PATH,
137 '--ssl-certificate', 'self-signed.pem',
138 '--ssl-certificate-key', 'self-signed.key')
139 timeout = Time.now.tv_sec + 10
141 while (not good_pid) and (Time.now.tv_sec < timeout)
143 server_pid = find_server_pid
144 good_pid = (server_pid and
146 (Process.kill(0, server_pid) rescue false))
149 raise RuntimeError, "could not find API server Rails pid"
155 if ENV["RAILS_ENV"].eql? 'test'
156 ApiServerForTests.run