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'
25 require 'mocha/mini_test'
27 class ActiveSupport::TestCase
28 # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in
31 # Note: You'll currently still have to declare fixtures explicitly
32 # in integration tests -- they do not yet inherit this setting
34 def use_token token_name
35 auth = api_fixture('api_client_authorizations')[token_name.to_s]
36 Thread.current[:arvados_api_token] = auth['api_token']
40 Thread.current[:arvados_api_token] = nil
41 Thread.current[:reader_tokens] = nil
42 # Restore configuration settings changed during tests
43 $application_config.each do |k,v|
45 Rails.configuration.send (k + '='), v
51 module ApiFixtureLoader
52 def self.included(base)
53 base.extend(ClassMethods)
59 # Returns the data structure from the named API server test fixture.
60 @@api_fixtures[name] ||= \
62 path = File.join(ApiServerForTests::ARV_API_SERVER_DIR,
63 'test', 'fixtures', "#{name}.yml")
64 YAML.load(IO.read(path))
69 self.class.api_fixture name
73 class ActiveSupport::TestCase
74 include ApiFixtureLoader
75 def session_for api_client_auth_name
77 arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token']
81 Oj.load(@response.body)
85 class ApiServerForTests
86 ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__)
87 SERVER_PID_PATH = File.expand_path('tmp/pids/wbtest-server.pid', ARV_API_SERVER_DIR)
88 @main_process_pid = $$
90 def self._system(*cmd)
91 $stderr.puts "_system #{cmd.inspect}"
92 Bundler.with_clean_env do
93 if not system({'RAILS_ENV' => 'test'}, *cmd)
94 raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}"
99 def self.make_ssl_cert
100 unless File.exists? './self-signed.key'
101 _system('openssl', 'req', '-new', '-x509', '-nodes',
102 '-out', './self-signed.pem',
103 '-keyout', './self-signed.key',
105 '-subj', '/CN=localhost')
110 if (pid = find_server_pid)
111 $stderr.puts "Sending TERM to API server, pid #{pid}"
112 Process.kill 'TERM', pid
116 def self.find_server_pid
119 pid = IO.read(SERVER_PID_PATH).to_i
120 $stderr.puts "API server is running, pid #{pid.inspect}"
126 def self.run(args=[])
127 ::MiniTest.after_run do
131 # Kill server left over from previous test run
134 Capybara.javascript_driver = :poltergeist
135 Dir.chdir(ARV_API_SERVER_DIR) do |apidir|
136 ENV["NO_COVERAGE_TEST"] = "1"
138 _system('bundle', 'exec', 'rake', 'db:test:load')
139 _system('bundle', 'exec', 'rake', 'db:fixtures:load')
140 _system('bundle', 'exec', 'passenger', 'start', '-d', '-p3000',
141 '--pid-file', SERVER_PID_PATH,
143 '--ssl-certificate', 'self-signed.pem',
144 '--ssl-certificate-key', 'self-signed.key')
145 timeout = Time.now.tv_sec + 10
147 while (not good_pid) and (Time.now.tv_sec < timeout)
149 server_pid = find_server_pid
150 good_pid = (server_pid and
152 (Process.kill(0, server_pid) rescue false))
155 raise RuntimeError, "could not find API server Rails pid"
161 class ActionController::TestCase
166 def check_counter action
169 assert_equal 1, 2, "Multiple actions in functional test"
173 [:get, :post, :put, :patch, :delete].each do |method|
174 define_method method do |action, *args|
181 if ENV["RAILS_ENV"].eql? 'test'
182 ApiServerForTests.run