Merge branch '1968-monitor-disk-usage'
[arvados.git] / apps / workbench / test / test_helper.rb
1 ENV["RAILS_ENV"] = "test"
2 unless ENV["NO_COVERAGE_TEST"]
3   begin
4     require 'simplecov'
5     require 'simplecov-rcov'
6     class SimpleCov::Formatter::MergedFormatter
7       def format(result)
8         SimpleCov::Formatter::HTMLFormatter.new.format(result)
9         SimpleCov::Formatter::RcovFormatter.new.format(result)
10       end
11     end
12     SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
13     SimpleCov.start do
14       add_filter '/test/'
15       add_filter 'initializers/secret_token'
16     end
17   rescue Exception => e
18     $stderr.puts "SimpleCov unavailable (#{e}). Proceeding without."
19   end
20 end
21
22 require File.expand_path('../../config/environment', __FILE__)
23 require 'rails/test_help'
24
25 $ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__)
26 SERVER_PID_PATH = 'tmp/pids/server.pid'
27
28 class ActiveSupport::TestCase
29   # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in
30   # alphabetical order.
31   #
32   # Note: You'll currently still have to declare fixtures explicitly
33   # in integration tests -- they do not yet inherit this setting
34   fixtures :all
35   def use_token token_name
36     auth = api_fixture('api_client_authorizations')[token_name.to_s]
37     Thread.current[:arvados_api_token] = auth['api_token']
38   end
39
40   def teardown
41     Thread.current[:arvados_api_token] = nil
42     super
43   end
44 end
45
46 module ApiFixtureLoader
47   def self.included(base)
48     base.extend(ClassMethods)
49   end
50
51   module ClassMethods
52     @@api_fixtures = {}
53     def api_fixture(name)
54       # Returns the data structure from the named API server test fixture.
55       @@api_fixtures[name] ||= \
56       begin
57         path = File.join($ARV_API_SERVER_DIR, 'test', 'fixtures', "#{name}.yml")
58         YAML.load(IO.read(path))
59       end
60     end
61   end
62   def api_fixture name
63     self.class.api_fixture name
64   end
65 end
66
67 class ActiveSupport::TestCase
68   include ApiFixtureLoader
69   def session_for api_client_auth_name
70     {
71       arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token']
72     }
73   end
74 end
75
76 class ApiServerBackedTestRunner < MiniTest::Unit
77   def _system(*cmd)
78     Bundler.with_clean_env do
79       if not system({'RAILS_ENV' => 'test'}, *cmd)
80         raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}"
81       end
82     end
83   end
84
85   def _run(args=[])
86     Capybara.javascript_driver = :poltergeist
87     server_pid = Dir.chdir($ARV_API_SERVER_DIR) do |apidir|
88       ENV["NO_COVERAGE_TEST"] = "1"
89       _system('bundle', 'exec', 'rake', 'db:test:load')
90       _system('bundle', 'exec', 'rake', 'db:fixtures:load')
91       _system('bundle', 'exec', 'rails', 'server', '-d')
92       timeout = Time.now.tv_sec + 10
93       good_pid = false
94       while (not good_pid) and (Time.now.tv_sec < timeout)
95         sleep 0.2
96         begin
97           server_pid = IO.read(SERVER_PID_PATH).to_i
98           good_pid = (server_pid > 0) and (Process.kill(0, server_pid) rescue false)
99         rescue Errno::ENOENT
100           good_pid = false
101         end
102       end
103       if not good_pid
104         raise RuntimeError, "could not find API server Rails pid"
105       end
106       server_pid
107     end
108     begin
109       super(args)
110     ensure
111       Process.kill('TERM', server_pid)
112     end
113   end
114 end
115
116 MiniTest::Unit.runner = ApiServerBackedTestRunner.new