+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
ENV["RAILS_ENV"] = "test" if (ENV["RAILS_ENV"] != "diagnostics" and ENV["RAILS_ENV"] != "performance")
unless ENV["NO_COVERAGE_TEST"]
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
-require 'mocha/mini_test'
+require 'mocha/minitest'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in
# Note: You'll currently still have to declare fixtures explicitly
# in integration tests -- they do not yet inherit this setting
fixtures :all
- def use_token token_name
+ def use_token(token_name)
+ user_was = Thread.current[:user]
+ token_was = Thread.current[:arvados_api_token]
auth = api_fixture('api_client_authorizations')[token_name.to_s]
- Thread.current[:arvados_api_token] = auth['api_token']
+ Thread.current[:arvados_api_token] = "v2/#{auth['uuid']}/#{auth['api_token']}"
+ if block_given?
+ begin
+ yield
+ ensure
+ Thread.current[:user] = user_was
+ Thread.current[:arvados_api_token] = token_was
+ end
+ end
end
teardown do
# Diagnostics suite doesn't run a server, so there's no cache to clear.
Rails.cache.clear unless (Rails.env == "diagnostics")
# Restore configuration settings changed during tests
- $application_config.each do |k,v|
- if k.match /^[^.]*$/
- Rails.configuration.send (k + '='), v
- end
- end
+ self.class.reset_application_config
+ end
+
+ def self.reset_application_config
+ # Restore configuration settings changed during tests
+ ConfigLoader.copy_into_config $arvados_config, Rails.configuration
+ ConfigLoader.copy_into_config $remaining_config, Rails.configuration
+ Rails.configuration.Services.Controller.ExternalURL = URI("https://#{ENV['ARVADOS_API_HOST']}")
+ Rails.configuration.TLS.Insecure = true
end
end
file = IO.read(path)
trim_index = file.index('# Test Helper trims the rest of the file')
file = file[0, trim_index] if trim_index
- YAML.load(file)
+ YAML.load(file).each do |name, ob|
+ ob.reject! { |k, v| k.start_with?('secret_') }
+ end
end
- keys.inject(@@api_fixtures[name]) { |hash, key| hash[key] }
+ keys.inject(@@api_fixtures[name]) { |hash, key| hash[key] }.deep_dup
end
end
+
def api_fixture(name, *keys)
self.class.api_fixture(name, *keys)
end
+ def api_token(name)
+ auth = api_fixture('api_client_authorizations')[name]
+ "v2/#{auth['uuid']}/#{auth['api_token']}"
+ end
+
def find_fixture(object_class, name)
object_class.find(api_fixture(object_class.to_s.pluralize.underscore,
name, "uuid"))
end
end
+module ApiMockHelpers
+ def fake_api_response body, status_code, headers
+ resp = mock
+ resp.responds_like_instance_of HTTP::Message
+ resp.stubs(:headers).returns headers
+ resp.stubs(:content).returns body
+ resp.stubs(:status_code).returns status_code
+ resp
+ end
+
+ def stub_api_calls_with_body body, status_code=200, headers={}
+ stub_api_calls
+ resp = fake_api_response body, status_code, headers
+ stub_api_client.stubs(:post).returns resp
+ end
+
+ def stub_api_calls
+ @stubbed_client = ArvadosApiClient.new
+ @stubbed_client.instance_eval do
+ @api_client = HTTPClient.new
+ end
+ ArvadosApiClient.stubs(:new_or_current).returns(@stubbed_client)
+ end
+
+ def stub_api_calls_with_invalid_json
+ stub_api_calls_with_body ']"omg,bogus"['
+ end
+
+ # Return the HTTPClient mock used by the ArvadosApiClient mock. You
+ # must have called stub_api_calls first.
+ def stub_api_client
+ @stubbed_client.instance_eval do
+ @api_client
+ end
+ end
+end
+
+class ActiveSupport::TestCase
+ include ApiMockHelpers
+end
+
class ActiveSupport::TestCase
include ApiFixtureLoader
def session_for api_client_auth_name
+ auth = api_fixture('api_client_authorizations')[api_client_auth_name.to_s]
{
- arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token']
+ arvados_api_token: "v2/#{auth['uuid']}/#{auth['api_token']}"
}
end
def json_response
- Oj.load(@response.body)
+ Oj.safe_load(@response.body)
end
end
class ApiServerForTests
+ PYTHON_TESTS_DIR = File.expand_path('../../../../sdk/python/tests', __FILE__)
ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__)
- SERVER_PID_PATH = File.expand_path('tmp/pids/wbtest-server.pid', ARV_API_SERVER_DIR)
- WEBSOCKET_PID_PATH = File.expand_path('tmp/pids/wstest-server.pid', ARV_API_SERVER_DIR)
+ SERVER_PID_PATH = File.expand_path('tmp/pids/test-server.pid', ARV_API_SERVER_DIR)
+ WEBSOCKET_PID_PATH = File.expand_path('tmp/pids/test-server.pid', ARV_API_SERVER_DIR)
@main_process_pid = $$
+ @@server_is_running = false
- def _system(*cmd)
- $stderr.puts "_system #{cmd.inspect}"
+ def check_output *args
+ output = nil
Bundler.with_clean_env do
- if not system({'RAILS_ENV' => 'test', "ARVADOS_WEBSOCKETS" => (if @websocket then "ws-only" end)}, *cmd)
- raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}"
+ output = IO.popen *args do |io|
+ io.read
+ end
+ if not $?.success?
+ raise RuntimeError, "Command failed (#{$?}): #{args.inspect}"
end
end
+ output
end
- def make_ssl_cert
- unless File.exists? './self-signed.key'
- _system('openssl', 'req', '-new', '-x509', '-nodes',
- '-out', './self-signed.pem',
- '-keyout', './self-signed.key',
- '-days', '3650',
- '-subj', '/CN=localhost')
+ def run_test_server
+ Dir.chdir PYTHON_TESTS_DIR do
+ check_output %w(python ./run_test_server.py start_keep)
end
end
- def kill_server
- if (pid = find_server_pid)
- $stderr.puts "Sending TERM to API server, pid #{pid}"
- Process.kill 'TERM', pid
+ def stop_test_server
+ Dir.chdir PYTHON_TESTS_DIR do
+ check_output %w(python ./run_test_server.py stop_keep)
end
+ @@server_is_running = false
end
- def find_server_pid
- pid = nil
- begin
- pid = IO.read(@pidfile).to_i
- $stderr.puts "API server is running, pid #{pid.inspect}"
- rescue Errno::ENOENT
- end
- return pid
- end
+ def run args=[]
+ return if @@server_is_running
+
+ # Stop server left over from interrupted previous run
+ stop_test_server
- def run(args=[])
::MiniTest.after_run do
- self.kill_server
+ stop_test_server
end
- @websocket = args.include?("--websockets")
-
- @pidfile = if @websocket
- WEBSOCKET_PID_PATH
- else
- SERVER_PID_PATH
- end
-
- # Kill server left over from previous test run
- self.kill_server
-
- Capybara.javascript_driver = :poltergeist
- Dir.chdir(ARV_API_SERVER_DIR) do |apidir|
- ENV["NO_COVERAGE_TEST"] = "1"
- if @websocket
- _system('bundle', 'exec', 'passenger', 'start', '-d', '-p3333',
- '--pid-file', @pidfile)
- else
- make_ssl_cert
- _system('bundle', 'exec', 'rake', 'db:test:load')
- _system('bundle', 'exec', 'rake', 'db:fixtures:load')
- _system('bundle', 'exec', 'passenger', 'start', '-d', '-p3000',
- '--pid-file', @pidfile,
- '--ssl',
- '--ssl-certificate', 'self-signed.pem',
- '--ssl-certificate-key', 'self-signed.key')
- end
- timeout = Time.now.tv_sec + 10
- good_pid = false
- while (not good_pid) and (Time.now.tv_sec < timeout)
- sleep 0.2
- server_pid = find_server_pid
- good_pid = (server_pid and
- (server_pid > 0) and
- (Process.kill(0, server_pid) rescue false))
- end
- if not good_pid
- raise RuntimeError, "could not find API server Rails pid"
- end
- end
+ run_test_server
+ ActiveSupport::TestCase.reset_application_config
+
+ @@server_is_running = true
end
- def run_rake_task(task_name, arg_string)
- Dir.chdir(ARV_API_SERVER_DIR) do
- _system('bundle', 'exec', 'rake', "#{task_name}[#{arg_string}]")
+ def run_rake_task task_name, arg_string
+ Dir.chdir ARV_API_SERVER_DIR do
+ check_output ['bundle', 'exec', 'rake', "#{task_name}[#{arg_string}]"]
end
end
end
class ActionController::TestCase
setup do
- @counter = 0
+ @test_counter = 0
end
def check_counter action
- @counter += 1
- if @counter == 2
+ @test_counter += 1
+ if @test_counter == 2
assert_equal 1, 2, "Multiple actions in controller test"
end
end
end
def after_teardown
- if self.class.want_reset_api_fixtures[:after_each_test]
+ if self.class.want_reset_api_fixtures[:after_each_test] and
+ (!defined?(@want_reset_api_fixtures) or @want_reset_api_fixtures != false)
self.class.reset_api_fixtures_now
end
super
end
+ def reset_api_fixtures_after_test t=true
+ @want_reset_api_fixtures = t
+ end
+
protected
def self.reset_api_fixtures_now
# Never try to reset fixtures when we're just using test
return unless Rails.env == 'test'
auth = api_fixture('api_client_authorizations')['admin_trustedclient']
- Thread.current[:arvados_api_token] = auth['api_token']
+ Thread.current[:arvados_api_token] = "v2/#{auth['uuid']}/#{auth['api_token']}"
ArvadosApiClient.new.api(nil, '../../database/reset', {})
Thread.current[:arvados_api_token] = nil
end
ApiServerForTests.new.run
ApiServerForTests.new.run ["--websockets"]
end
+
+# Reset fixtures now (i.e., before any tests run).
+ActiveSupport::TestCase.reset_api_fixtures_now
+
+module Minitest
+ class Test
+ def capture_exceptions *args
+ begin
+ n = 0
+ begin
+ yield
+ rescue *PASSTHROUGH_EXCEPTIONS
+ raise
+ rescue Exception => e
+ n += 1
+ raise if n > 2 || e.is_a?(Skip)
+ STDERR.puts "Test failed, retrying (##{n})"
+ ActiveSupport::TestCase.reset_api_fixtures_now
+ retry
+ end
+ rescue *PASSTHROUGH_EXCEPTIONS
+ raise
+ rescue Assertion => e
+ self.failures << e
+ rescue Exception => e
+ self.failures << UnexpectedError.new(e)
+ end
+ end
+ end
+end