debian-mirror-gitlab/scripts/gitaly_test.rb

173 lines
4.4 KiB
Ruby
Raw Normal View History

2018-10-15 14:42:47 +05:30
# This file contains environment settings for gitaly when it's running
# as part of the gitlab-ce/ee test suite.
#
# Please be careful when modifying this file. Your changes must work
# both for local development rspec runs, and in CI.
2020-06-23 00:09:42 +05:30
require 'securerandom'
2018-10-15 14:42:47 +05:30
require 'socket'
2020-07-28 23:09:34 +05:30
require 'logger'
2018-10-15 14:42:47 +05:30
module GitalyTest
2020-07-28 23:09:34 +05:30
LOGGER = begin
default_name = ENV['CI'] ? 'DEBUG' : 'WARN'
level_name = ENV['GITLAB_TESTING_LOG_LEVEL']&.upcase
level = Logger.const_get(level_name || default_name, true) # rubocop: disable Gitlab/ConstGetInheritFalse
Logger.new(STDOUT, level: level, formatter: ->(_, _, _, msg) { msg })
end
2018-10-15 14:42:47 +05:30
def tmp_tests_gitaly_dir
File.expand_path('../tmp/tests/gitaly', __dir__)
end
2020-06-23 00:09:42 +05:30
def tmp_tests_gitlab_shell_dir
File.expand_path('../tmp/tests/gitlab-shell', __dir__)
end
def rails_gitlab_shell_secret
File.expand_path('../.gitlab_shell_secret', __dir__)
end
2018-10-15 14:42:47 +05:30
def gemfile
File.join(tmp_tests_gitaly_dir, 'ruby', 'Gemfile')
end
2020-06-23 00:09:42 +05:30
def gitlab_shell_secret_file
File.join(tmp_tests_gitlab_shell_dir, '.gitlab_shell_secret')
end
2018-10-15 14:42:47 +05:30
def env
env_hash = {
'HOME' => File.expand_path('tmp/tests'),
'GEM_PATH' => Gem.path.join(':'),
'BUNDLE_APP_CONFIG' => File.join(File.dirname(gemfile), '.bundle/config'),
2020-05-24 23:13:21 +05:30
'BUNDLE_FLAGS' => "--jobs=4 --retry=3 --quiet",
2018-10-15 14:42:47 +05:30
'BUNDLE_INSTALL_FLAGS' => nil,
'BUNDLE_GEMFILE' => gemfile,
2019-09-04 21:01:54 +05:30
'RUBYOPT' => nil,
# Git hooks can't run during tests as the internal API is not running.
'GITALY_TESTING_NO_GIT_HOOKS' => "1"
2018-10-15 14:42:47 +05:30
}
if ENV['CI']
bundle_path = File.expand_path('../vendor/gitaly-ruby', __dir__)
env_hash['BUNDLE_FLAGS'] << " --path=#{bundle_path}"
end
env_hash
end
2020-04-22 19:07:51 +05:30
def config_path(service)
case service
when :gitaly
File.join(tmp_tests_gitaly_dir, 'config.toml')
2021-02-22 17:27:13 +05:30
when :gitaly2
File.join(tmp_tests_gitaly_dir, 'gitaly2.config.toml')
2020-04-22 19:07:51 +05:30
when :praefect
File.join(tmp_tests_gitaly_dir, 'praefect.config.toml')
end
2018-10-15 14:42:47 +05:30
end
2021-02-22 17:27:13 +05:30
def service_binary(service)
case service
when :gitaly, :gitaly2
'gitaly'
when :praefect
'praefect'
end
end
2018-10-15 14:42:47 +05:30
def start_gitaly
2020-04-22 19:07:51 +05:30
start(:gitaly)
end
2021-02-22 17:27:13 +05:30
def start_gitaly2
start(:gitaly2)
end
2020-04-22 19:07:51 +05:30
def start_praefect
start(:praefect)
end
def start(service)
2021-02-22 17:27:13 +05:30
args = ["#{tmp_tests_gitaly_dir}/#{service_binary(service)}"]
2020-04-22 19:07:51 +05:30
args.push("-config") if service == :praefect
args.push(config_path(service))
pid = spawn(env, *args, [:out, :err] => "log/#{service}-test.log")
2018-10-15 14:42:47 +05:30
begin
2020-04-22 19:07:51 +05:30
try_connect!(service)
2018-10-15 14:42:47 +05:30
rescue
Process.kill('TERM', pid)
raise
end
pid
end
2020-06-23 00:09:42 +05:30
# Taken from Gitlab::Shell.generate_and_link_secret_token
def ensure_gitlab_shell_secret!
secret_file = rails_gitlab_shell_secret
shell_link = gitlab_shell_secret_file
unless File.size?(secret_file)
File.write(secret_file, SecureRandom.hex(16))
end
unless File.exist?(shell_link)
FileUtils.ln_s(secret_file, shell_link)
end
end
2018-10-15 14:42:47 +05:30
def check_gitaly_config!
2020-07-28 23:09:34 +05:30
LOGGER.debug "Checking gitaly-ruby Gemfile...\n"
2020-01-01 13:55:28 +05:30
unless File.exist?(gemfile)
message = "#{gemfile} does not exist."
message += "\n\nThis might have happened if the CI artifacts for this build were destroyed." if ENV['CI']
abort message
end
2020-07-28 23:09:34 +05:30
LOGGER.debug "Checking gitaly-ruby bundle...\n"
out = ENV['CI'] ? STDOUT : '/dev/null'
abort 'bundle check failed' unless system(env, 'bundle', 'check', out: out, chdir: File.dirname(gemfile))
2018-10-15 14:42:47 +05:30
end
2020-04-22 19:07:51 +05:30
def read_socket_path(service)
2018-10-15 14:42:47 +05:30
# This code needs to work in an environment where we cannot use bundler,
# so we cannot easily use the toml-rb gem. This ad-hoc parser should be
# good enough.
2020-04-22 19:07:51 +05:30
config_text = IO.read(config_path(service))
2018-10-15 14:42:47 +05:30
config_text.lines.each do |line|
match_data = line.match(/^\s*socket_path\s*=\s*"([^"]*)"$/)
return match_data[1] if match_data
end
2020-04-22 19:07:51 +05:30
raise "failed to find socket_path in #{config_path(service)}"
2018-10-15 14:42:47 +05:30
end
2020-04-22 19:07:51 +05:30
def try_connect!(service)
2020-07-28 23:09:34 +05:30
LOGGER.debug "Trying to connect to #{service}: "
2018-10-15 14:42:47 +05:30
timeout = 20
delay = 0.1
2020-04-22 19:07:51 +05:30
socket = read_socket_path(service)
2018-10-15 14:42:47 +05:30
Integer(timeout / delay).times do
2019-07-07 11:18:12 +05:30
UNIXSocket.new(socket)
2020-07-28 23:09:34 +05:30
LOGGER.debug " OK\n"
2019-07-07 11:18:12 +05:30
return
rescue Errno::ENOENT, Errno::ECONNREFUSED
2020-07-28 23:09:34 +05:30
LOGGER.debug '.'
2019-07-07 11:18:12 +05:30
sleep delay
2018-10-15 14:42:47 +05:30
end
2020-07-28 23:09:34 +05:30
LOGGER.warn " FAILED to connect to #{service}\n"
2018-10-15 14:42:47 +05:30
raise "could not connect to #{socket}"
end
end