1 ENV["RAILS_ENV"] = "test" if (ENV["RAILS_ENV"] != "diagnostics" and ENV["RAILS_ENV"] != "performance")
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[:user] = nil
42 Thread.current[:reader_tokens] = nil
43 # Diagnostics suite doesn't run a server, so there's no cache to clear.
44 Rails.cache.clear unless (Rails.env == "diagnostics")
45 # Restore configuration settings changed during tests
46 self.class.reset_application_config
49 def self.reset_application_config
50 $application_config.each do |k,v|
52 Rails.configuration.send (k + '='), v
58 module ApiFixtureLoader
59 def self.included(base)
60 base.extend(ClassMethods)
65 def api_fixture(name, *keys)
66 # Returns the data structure from the named API server test fixture.
67 @@api_fixtures[name] ||= \
69 path = File.join(ApiServerForTests::ARV_API_SERVER_DIR,
70 'test', 'fixtures', "#{name}.yml")
72 trim_index = file.index('# Test Helper trims the rest of the file')
73 file = file[0, trim_index] if trim_index
76 keys.inject(@@api_fixtures[name]) { |hash, key| hash[key] }
79 def api_fixture(name, *keys)
80 self.class.api_fixture(name, *keys)
83 def find_fixture(object_class, name)
84 object_class.find(api_fixture(object_class.to_s.pluralize.underscore,
89 class ActiveSupport::TestCase
90 include ApiFixtureLoader
91 def session_for api_client_auth_name
93 arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token']
97 Oj.load(@response.body)
101 class ApiServerForTests
102 PYTHON_TESTS_DIR = File.expand_path('../../../../sdk/python/tests', __FILE__)
103 ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__)
104 SERVER_PID_PATH = File.expand_path('tmp/pids/test-server.pid', ARV_API_SERVER_DIR)
105 WEBSOCKET_PID_PATH = File.expand_path('tmp/pids/test-server.pid', ARV_API_SERVER_DIR)
106 @main_process_pid = $$
107 @@server_is_running = false
111 Bundler.with_clean_env do
112 output = IO.popen *args do |io|
116 raise RuntimeError, "Command failed (#{$?}): #{args.inspect}"
124 Dir.chdir PYTHON_TESTS_DIR do
125 env_script = check_call %w(python ./run_test_server.py start --auth admin)
128 env_script.each_line do |line|
130 if 0 == line.index('export ')
131 toks = line.sub('export ', '').split '=', 2
132 $stderr.puts "run_test_server.py: #{toks[0]}=#{toks[1]}"
133 test_env[toks[0]] = toks[1]
140 Dir.chdir PYTHON_TESTS_DIR do
141 # This is a no-op if we're running within run-tests.sh
142 check_call %w(python ./run_test_server.py stop)
144 @@server_is_running = false
148 return if @@server_is_running
150 # Stop server left over from interrupted previous run
153 ::MiniTest.after_run do
157 test_env = run_test_server
158 $application_config['arvados_login_base'] = "https://#{test_env['ARVADOS_API_HOST']}/login"
159 $application_config['arvados_v1_base'] = "https://#{test_env['ARVADOS_API_HOST']}/arvados/v1"
160 $application_config['arvados_insecure_host'] = true
161 ActiveSupport::TestCase.reset_application_config
163 @@server_is_running = true
166 def run_rake_task task_name, arg_string
167 Dir.chdir ARV_API_SERVER_DIR do
168 check_call ['bundle', 'exec', 'rake', "#{task_name}[#{arg_string}]"]
173 class ActionController::TestCase
178 def check_counter action
181 assert_equal 1, 2, "Multiple actions in controller test"
185 [:get, :post, :put, :patch, :delete].each do |method|
186 define_method method do |action, *args|
193 # Test classes can call reset_api_fixtures(when_to_reset,flag) to
194 # override the default. Example:
196 # class MySuite < ActionDispatch::IntegrationTest
197 # reset_api_fixtures :after_each_test, false
198 # reset_api_fixtures :after_suite, true
202 # The default behavior is reset_api_fixtures(:after_each_test,true).
204 class ActiveSupport::TestCase
206 def self.inherited subclass
207 subclass.class_eval do
209 attr_accessor :want_reset_api_fixtures
211 @want_reset_api_fixtures = {
212 after_each_test: true,
219 # Existing subclasses of ActiveSupport::TestCase (ones that already
220 # existed before we set up the self.inherited hook above) will not
221 # get their own instance variable. They're not real test cases
222 # anyway, so we give them a "don't reset anywhere" stub.
223 def self.want_reset_api_fixtures
227 def self.reset_api_fixtures where, t=true
228 if not want_reset_api_fixtures.has_key? where
229 raise ArgumentError, "There is no #{where.inspect} hook"
231 self.want_reset_api_fixtures[where] = t
235 reset_api_fixtures_now if want_reset_api_fixtures[:before_suite]
237 reset_api_fixtures_now if want_reset_api_fixtures[:after_suite]
242 if self.class.want_reset_api_fixtures[:after_each_test]
243 self.class.reset_api_fixtures_now
249 def self.reset_api_fixtures_now
250 # Never try to reset fixtures when we're just using test
251 # infrastructure to run performance/diagnostics suites.
252 return unless Rails.env == 'test'
254 auth = api_fixture('api_client_authorizations')['admin_trustedclient']
255 Thread.current[:arvados_api_token] = auth['api_token']
256 ArvadosApiClient.new.api(nil, '../../database/reset', {})
257 Thread.current[:arvados_api_token] = nil
261 # If it quacks like a duck, it must be a HTTP request object.
278 # apps/workbench$ RAILS_ENV=test bundle exec irb -Ilib:test
279 # > load 'test/test_helper.rb'
280 # > singletest 'integration/collection_upload_test.rb', 'Upload two empty files'
282 def singletest test_class_file, test_name
283 load File.join('test', test_class_file)
284 Minitest.run ['-v', '-n', "test_#{test_name.gsub ' ', '_'}"]
285 Object.send(:remove_const,
286 test_class_file.gsub(/.*\/|\.rb$/, '').camelize.to_sym)
287 ::Minitest::Runnable.runnables.reject! { true }
290 if ENV["RAILS_ENV"].eql? 'test'
291 ApiServerForTests.new.run
292 ApiServerForTests.new.run ["--websockets"]
295 # Reset fixtures now (i.e., before any tests run).
296 ActiveSupport::TestCase.reset_api_fixtures_now