2760: Replace dashboard tables with news feed. More ability to link via names rather...
[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 class ActiveSupport::TestCase
26   # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in
27   # alphabetical order.
28   #
29   # Note: You'll currently still have to declare fixtures explicitly
30   # in integration tests -- they do not yet inherit this setting
31   fixtures :all
32   def use_token token_name
33     auth = api_fixture('api_client_authorizations')[token_name.to_s]
34     token_was = Thread.current[:arvados_api_token]
35     Thread.current[:arvados_api_token] = auth['api_token']
36     if block_given?
37       yield
38       Thread.current[:arvados_api_token] = token_was
39     end
40   end
41
42   def teardown
43     Thread.current[:arvados_api_token] = nil
44     super
45   end
46 end
47
48 module ApiFixtureLoader
49   def self.included(base)
50     base.extend(ClassMethods)
51   end
52
53   module ClassMethods
54     @@api_fixtures = {}
55     def api_fixture(name)
56       # Returns the data structure from the named API server test fixture.
57       @@api_fixtures[name] ||= \
58       begin
59         path = File.join(ApiServerForTests::ARV_API_SERVER_DIR,
60                          'test', 'fixtures', "#{name}.yml")
61         YAML.load(IO.read(path))
62       end
63     end
64   end
65   def api_fixture name
66     self.class.api_fixture name
67   end
68 end
69
70 class ActiveSupport::TestCase
71   include ApiFixtureLoader
72   def session_for api_client_auth_name
73     {
74       arvados_api_token: api_fixture('api_client_authorizations')[api_client_auth_name.to_s]['api_token']
75     }
76   end
77 end
78
79 class ApiServerForTests
80   ARV_API_SERVER_DIR = File.expand_path('../../../../services/api', __FILE__)
81   SERVER_PID_PATH = File.expand_path('tmp/pids/wbtest-server.pid', ARV_API_SERVER_DIR)
82   @main_process_pid = $$
83
84   def self._system(*cmd)
85     $stderr.puts "_system #{cmd.inspect}"
86     Bundler.with_clean_env do
87       if not system({'RAILS_ENV' => 'test'}, *cmd)
88         raise RuntimeError, "#{cmd[0]} returned exit code #{$?.exitstatus}"
89       end
90     end
91   end
92
93   def self.make_ssl_cert
94     unless File.exists? './self-signed.key'
95       _system('openssl', 'req', '-new', '-x509', '-nodes',
96               '-out', './self-signed.pem',
97               '-keyout', './self-signed.key',
98               '-days', '3650',
99               '-subj', '/CN=localhost')
100     end
101   end
102
103   def self.kill_server
104     if (pid = find_server_pid)
105       $stderr.puts "Sending TERM to API server, pid #{pid}"
106       Process.kill 'TERM', pid
107     end
108   end
109
110   def self.find_server_pid
111     pid = nil
112     begin
113       pid = IO.read(SERVER_PID_PATH).to_i
114       $stderr.puts "API server is running, pid #{pid.inspect}"
115     rescue Errno::ENOENT
116     end
117     return pid
118   end
119
120   def self.run(args=[])
121     ::MiniTest.after_run do
122       self.kill_server
123     end
124
125     # Kill server left over from previous test run
126     self.kill_server
127
128     Capybara.javascript_driver = :poltergeist
129     Dir.chdir(ARV_API_SERVER_DIR) do |apidir|
130       ENV["NO_COVERAGE_TEST"] = "1"
131       make_ssl_cert
132       _system('bundle', 'exec', 'rake', 'db:test:load')
133       _system('bundle', 'exec', 'rake', 'db:fixtures:load')
134       _system('bundle', 'exec', 'passenger', 'start', '-d', '-p3001',
135               '--pid-file', SERVER_PID_PATH,
136               '--ssl',
137               '--ssl-certificate', 'self-signed.pem',
138               '--ssl-certificate-key', 'self-signed.key')
139       timeout = Time.now.tv_sec + 10
140       good_pid = false
141       while (not good_pid) and (Time.now.tv_sec < timeout)
142         sleep 0.2
143         server_pid = find_server_pid
144         good_pid = (server_pid and
145                     (server_pid > 0) and
146                     (Process.kill(0, server_pid) rescue false))
147       end
148       if not good_pid
149         raise RuntimeError, "could not find API server Rails pid"
150       end
151     end
152   end
153 end
154
155 ApiServerForTests.run