1 ENV["RAILS_ENV"] = "test"
2 unless ENV["NO_COVERAGE_TEST"]
5 require 'simplecov-rcov'
6 class SimpleCov::Formatter::MergedFormatter
8 SimpleCov::Formatter::HTMLFormatter.new.format(result)
9 SimpleCov::Formatter::RcovFormatter.new.format(result)
12 SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
15 add_filter 'initializers/secret_token'
18 $stderr.puts "SimpleCov unavailable (#{e}). Proceeding without."
22 require File.expand_path('../../config/environment', __FILE__)
23 require 'rails/test_help'
25 class ActiveSupport::TestCase
26 # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in
29 # Note: You'll currently still have to declare fixtures explicitly
30 # in integration tests -- they do not yet inherit this setting
32 def use_token token_name
33 auth = api_fixture('api_client_authorizations')[token_name.to_s]
34 Thread.current[:arvados_api_token] = auth['api_token']
38 Thread.current[:arvados_api_token] = nil
39 Thread.current[:reader_tokens] = nil
44 module ApiFixtureLoader
45 def self.included(base)
46 base.extend(ClassMethods)
52 # Returns the data structure from the named API server test fixture.
53 @@api_fixtures[name] ||= \
55 path = File.join(ApiServerForTests::ARV_API_SERVER_DIR,
56 'test', 'fixtures', "#{name}.yml")
57 YAML.load(IO.read(path))
62 self.class.api_fixture name
66 class ActiveSupport::TestCase
67 include ApiFixtureLoader
68 def session_for api_client_auth_name
70 arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token']
74 Oj.load(@response.body)
78 class ApiServerForTests
79 ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__)
80 SERVER_PID_PATH = File.expand_path('tmp/pids/wbtest-server.pid', ARV_API_SERVER_DIR)
81 @main_process_pid = $$
83 def self._system(*cmd)
84 $stderr.puts "_system #{cmd.inspect}"
85 Bundler.with_clean_env do
86 if not system({'RAILS_ENV' => 'test'}, *cmd)
87 raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}"
92 def self.make_ssl_cert
93 unless File.exists? './self-signed.key'
94 _system('openssl', 'req', '-new', '-x509', '-nodes',
95 '-out', './self-signed.pem',
96 '-keyout', './self-signed.key',
98 '-subj', '/CN=localhost')
103 if (pid = find_server_pid)
104 $stderr.puts "Sending TERM to API server, pid #{pid}"
105 Process.kill 'TERM', pid
109 def self.find_server_pid
112 pid = IO.read(SERVER_PID_PATH).to_i
113 $stderr.puts "API server is running, pid #{pid.inspect}"
119 def self.run(args=[])
120 ::MiniTest.after_run do
124 # Kill server left over from previous test run
127 Capybara.javascript_driver = :poltergeist
128 Dir.chdir(ARV_API_SERVER_DIR) do |apidir|
129 ENV["NO_COVERAGE_TEST"] = "1"
131 _system('bundle', 'exec', 'rake', 'db:test:load')
132 _system('bundle', 'exec', 'rake', 'db:fixtures:load')
133 _system('bundle', 'exec', 'passenger', 'start', '-d', '-p3001',
134 '--pid-file', SERVER_PID_PATH,
136 '--ssl-certificate', 'self-signed.pem',
137 '--ssl-certificate-key', 'self-signed.key')
138 timeout = Time.now.tv_sec + 10
140 while (not good_pid) and (Time.now.tv_sec < timeout)
142 server_pid = find_server_pid
143 good_pid = (server_pid and
145 (Process.kill(0, server_pid) rescue false))
148 raise RuntimeError, "could not find API server Rails pid"
154 ApiServerForTests.run