Module: TestUtils

Defined in:
common/test_utils.rb

Overview

A set of utils to start/stop the various servers that make up Aspace. Used for running tests. rubocop:disable Lint/HandleExceptions, Lint/RescueWithoutErrorClass

Defined Under Namespace

Modules: SpecIndexing

Class Method Summary collapse

Class Method Details

.build_config_string(config) ⇒ Object

rubocop:enable Metrics/MethodLength



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'common/test_utils.rb', line 51

def self.build_config_string(config)
  java_opts = ENV.fetch('JAVA_OPTS', '')
  config.each do |key, value|
    java_opts += " -Daspace.config.#{key}=#{value}"
  end

  # Pass through any system properties from the parent JVM too
  java.lang.System.getProperties.each do |property, value|
    next unless property =~ /aspace.config.(.*)/
    key = Regexp.last_match(1)
    java_opts += " -Daspace.config.#{key}=#{value}" unless config.key?(key)
  end

  ' ' + java_opts
end

.find_antObject



67
68
69
70
71
72
73
74
75
# File 'common/test_utils.rb', line 67

def self.find_ant
  fullpath = ''
  [nil, '..', '../..'].each do |root|
    base = ASUtils.find_base_directory(root)
    fullpath = File.join(File.realpath(base), 'build', 'run')
    break if File.exist? fullpath
  end
  fullpath
end

.free_port_from(port) ⇒ Object



139
140
141
142
143
144
145
146
# File 'common/test_utils.rb', line 139

def self.free_port_from(port)
  server = TCPServer.new('127.0.0.1', port)
  server.close
  port
rescue Errno::EADDRINUSE
  port += 1
  retry
end

.kill(pid) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'common/test_utils.rb', line 14

def self.kill(pid)
  if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
    system("taskkill /pid #{pid} /f /t")
  else
    begin
      Process.kill(15, pid)
      Process.waitpid(pid)
    rescue
      # Already dead.
    end
  end
end

.start_backend(port, config = {}, config_file = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'common/test_utils.rb', line 77

def self.start_backend(port, config = {}, config_file = nil)
  db_url = config.delete(:db_url)
  java_opts = build_config_string(config)
  java_opts += " -Daspace.config=#{config_file}" if config_file

  # although we are testing, we need to pass the db we are using
  # through as aspace.db_url.dev because the backend:devserver
  # ant task is hardcoded to used that build arg
  build_args = ['backend:devserver:integration',
                "-Daspace.backend.port=#{port}",
                '-Daspace_integration_test=1',
                "-Daspace.db_url.dev=#{db_url}"]
  java_opts += ' -Xmx1024m'

  puts "Spawning backend with opts: #{java_opts}"
  logfile = File.join(ASUtils.find_base_directory, "ci_logs", "backend_test_log.out")
  process_log = File.join(ASUtils.find_base_directory, "ci_logs", "backend_process.out")
  process_options = { :out => process_log, :err => process_log }
  env = { 'JAVA_OPTS' => java_opts, 'APPCONFIG_BACKEND_LOG' => logfile, 'INTEGRATION_LOGFILE' => process_log }
  pid = Process.spawn(env, find_ant, *build_args, process_options)

  TestUtils.wait_for_url("http://localhost:#{port}", logfile)
  puts "Backend started with pid: #{pid}"

  pid
end

.start_frontend(port, backend_url, config = {}, config_file = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'common/test_utils.rb', line 104

def self.start_frontend(port, backend_url, config = {}, config_file = nil)
  java_opts = "-Daspace.config.backend_url=#{backend_url}"
  java_opts += build_config_string(config)
  java_opts += " -Daspace.config=#{config_file}" if config_file

  build_args = ['frontend:devserver:integration',
                "-Daspace.frontend.port=#{port}"]
  java_opts += ' -Xmx1512m'

  puts "Spawning frontend with opts: #{java_opts}"
  pid = Process.spawn({ 'JAVA_OPTS' => java_opts, 'TEST_MODE' => 'true' },
                      find_ant, *build_args)

  TestUtils.wait_for_url("http://localhost:#{port}")
  puts "Frontend started with pid: #{pid}"

  pid
end

.start_public(port, backend_url, config = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'common/test_utils.rb', line 123

def self.start_public(port, backend_url, config = {})
  java_opts = "-Daspace.config.backend_url=#{backend_url}"
  config.each do |key, value|
    java_opts += " -Daspace.config.#{key}=#{value}"
  end

  pid = Process.spawn({ 'JAVA_OPTS' => java_opts, 'TEST_MODE' => 'true' },
                      find_ant, 'public:devserver:integration',
                      "-Daspace.public.port=#{port}")

  TestUtils.wait_for_url("http://localhost:#{port}")
  puts "Public started with pid: #{pid}"

  pid
end

.wait_for_url(url, out = nil) ⇒ Object

rubocop:disable Metrics/MethodLength



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'common/test_utils.rb', line 28

def self.wait_for_url(url, out = nil)
  24.times do |idx|
    begin
      uri = URI(url)
      req = Net::HTTP::Get.new(uri.request_uri)
      ASHTTP.start_uri(uri, open_timeout: 60, read_timeout: 60) do |http|
        http.request(req)
      end
      break
    rescue
      # Keep trying
      raise "Giving up waiting for #{url}" if idx > 22
      puts "Waiting for #{url} (#{$ERROR_INFO.inspect})"
      if idx == 10 && !out.nil? && File.file?(out)
        puts "Server is taking a long time to startup, dumping last 50 lines of log:"
        puts IO.readlines(out)[-50..-1]
      end
      sleep(5)
    end
  end
end