Merge branch 'master' into 1971-show-image-thumbnails
[arvados.git] / apps / workbench / test / test_helper.rb
1 ENV["RAILS_ENV"] = "test"
2 require File.expand_path('../../config/environment', __FILE__)
3 require 'rails/test_help'
4
5 $ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__)
6 SERVER_PID_PATH = 'tmp/pids/server.pid'
7
8 class ActiveSupport::TestCase
9   # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
10   #
11   # Note: You'll currently still have to declare fixtures explicitly in integration tests
12   # -- they do not yet inherit this setting
13   fixtures :all
14
15   # Add more helper methods to be used by all tests here...
16 end
17
18 module ApiFixtureLoader
19   def self.included(base)
20     base.extend(ClassMethods)
21   end
22
23   module ClassMethods
24     @@api_fixtures = {}
25     def api_fixture(name)
26       # Returns the data structure from the named API server test fixture.
27       @@api_fixtures[name] ||= \
28       begin
29         path = File.join($ARV_API_SERVER_DIR, 'test', 'fixtures', "#{name}.yml")
30         YAML.load(IO.read(path))
31       end
32     end
33   end
34   def api_fixture name
35     self.class.api_fixture name
36   end
37 end
38
39 class ActiveSupport::TestCase
40   include ApiFixtureLoader
41   def session_for api_client_auth_name
42     {
43       arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token']
44     }
45   end
46 end
47
48 class ApiServerBackedTestRunner < MiniTest::Unit
49   # Make a hash that unsets Bundle's environment variables.
50   # We'll use this environment when we launch Bundle commands in the API
51   # server.  Otherwise, those commands will try to use Workbench's gems, etc.
52   @@APIENV = Hash[ENV.map { |key, val|
53                     (key =~ /^BUNDLE_/) ? [key, nil] : nil
54                   }.compact]
55
56   def _system(*cmd)
57     if not system(@@APIENV, *cmd)
58       raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}"
59     end
60   end
61
62   def _run(args=[])
63     Capybara.javascript_driver = :poltergeist
64     server_pid = Dir.chdir($ARV_API_SERVER_DIR) do |apidir|
65       _system('bundle', 'exec', 'rake', 'db:test:load')
66       _system('bundle', 'exec', 'rake', 'db:fixtures:load')
67       _system('bundle', 'exec', 'rails', 'server', '-d')
68       timeout = Time.now.tv_sec + 10
69       begin
70         sleep 0.2
71         begin
72           server_pid = IO.read(SERVER_PID_PATH).to_i
73           good_pid = (server_pid > 0) and (Process.kill(0, pid) rescue false)
74         rescue Errno::ENOENT
75           good_pid = false
76         end
77       end while (not good_pid) and (Time.now.tv_sec < timeout)
78       if not good_pid
79         raise RuntimeError, "could not find API server Rails pid"
80       end
81       server_pid
82     end
83     begin
84       super(args)
85     ensure
86       Process.kill('TERM', server_pid)
87     end
88   end
89 end
90
91 MiniTest::Unit.runner = ApiServerBackedTestRunner.new