2021-02-12 07:09:02 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-14 04:10:29 -04:00
|
|
|
# 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-02 05:08:01 -04:00
|
|
|
require 'securerandom'
|
2018-05-14 04:10:29 -04:00
|
|
|
require 'socket'
|
2020-06-25 14:08:50 -04:00
|
|
|
require 'logger'
|
2018-05-14 04:10:29 -04:00
|
|
|
|
2021-04-21 05:09:15 -04:00
|
|
|
module GitalySetup
|
2020-06-25 14:08:50 -04:00
|
|
|
LOGGER = begin
|
2021-03-17 17:11:29 -04:00
|
|
|
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
|
2021-06-08 11:10:00 -04:00
|
|
|
Logger.new($stdout, level: level, formatter: ->(_, _, _, msg) { msg })
|
2021-03-17 17:11:29 -04:00
|
|
|
end
|
2020-06-25 14:08:50 -04:00
|
|
|
|
2018-05-14 04:10:29 -04:00
|
|
|
def tmp_tests_gitaly_dir
|
2021-04-21 05:09:15 -04:00
|
|
|
File.expand_path('../../../tmp/tests/gitaly', __dir__)
|
2018-05-14 04:10:29 -04:00
|
|
|
end
|
|
|
|
|
2021-04-29 11:10:07 -04:00
|
|
|
def tmp_tests_gitaly_bin_dir
|
|
|
|
File.join(tmp_tests_gitaly_dir, '_build', 'bin')
|
|
|
|
end
|
|
|
|
|
2020-06-02 05:08:01 -04:00
|
|
|
def tmp_tests_gitlab_shell_dir
|
2021-04-21 05:09:15 -04:00
|
|
|
File.expand_path('../../../tmp/tests/gitlab-shell', __dir__)
|
2020-06-02 05:08:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def rails_gitlab_shell_secret
|
2021-04-21 05:09:15 -04:00
|
|
|
File.expand_path('../../../.gitlab_shell_secret', __dir__)
|
2020-06-02 05:08:01 -04:00
|
|
|
end
|
|
|
|
|
2018-05-14 04:10:29 -04:00
|
|
|
def gemfile
|
|
|
|
File.join(tmp_tests_gitaly_dir, 'ruby', 'Gemfile')
|
|
|
|
end
|
|
|
|
|
2021-04-01 08:08:56 -04:00
|
|
|
def gemfile_dir
|
|
|
|
File.dirname(gemfile)
|
|
|
|
end
|
|
|
|
|
2020-06-02 05:08:01 -04:00
|
|
|
def gitlab_shell_secret_file
|
|
|
|
File.join(tmp_tests_gitlab_shell_dir, '.gitlab_shell_secret')
|
|
|
|
end
|
|
|
|
|
2018-05-14 04:10:29 -04:00
|
|
|
def env
|
2021-04-19 11:09:08 -04:00
|
|
|
{
|
2018-05-14 04:10:29 -04:00
|
|
|
'HOME' => File.expand_path('tmp/tests'),
|
|
|
|
'GEM_PATH' => Gem.path.join(':'),
|
2021-04-01 08:08:56 -04:00
|
|
|
'BUNDLE_APP_CONFIG' => File.join(gemfile_dir, '.bundle'),
|
2018-05-14 04:10:29 -04:00
|
|
|
'BUNDLE_INSTALL_FLAGS' => nil,
|
|
|
|
'BUNDLE_GEMFILE' => gemfile,
|
2019-06-06 06:05:11 -04:00
|
|
|
'RUBYOPT' => nil,
|
|
|
|
|
|
|
|
# Git hooks can't run during tests as the internal API is not running.
|
2021-04-26 08:09:44 -04:00
|
|
|
'GITALY_TESTING_NO_GIT_HOOKS' => "1",
|
|
|
|
'GITALY_TESTING_ENABLE_ALL_FEATURE_FLAGS' => "true"
|
2018-05-14 04:10:29 -04:00
|
|
|
}
|
2021-04-01 08:08:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop:disable GitlabSecurity/SystemCommandInjection
|
|
|
|
def set_bundler_config
|
|
|
|
system('bundle config set --local jobs 4', chdir: gemfile_dir)
|
|
|
|
system('bundle config set --local retry 3', chdir: gemfile_dir)
|
|
|
|
|
2018-05-14 04:10:29 -04:00
|
|
|
if ENV['CI']
|
2021-04-21 05:09:15 -04:00
|
|
|
bundle_path = File.expand_path('../../../vendor/gitaly-ruby', __dir__)
|
2021-04-01 08:08:56 -04:00
|
|
|
system('bundle', 'config', 'set', '--local', 'path', bundle_path, chdir: gemfile_dir)
|
2018-05-14 04:10:29 -04:00
|
|
|
end
|
|
|
|
end
|
2021-04-01 08:08:56 -04:00
|
|
|
# rubocop:enable GitlabSecurity/SystemCommandInjection
|
2018-05-14 04:10:29 -04:00
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
def config_path(service)
|
|
|
|
case service
|
|
|
|
when :gitaly
|
|
|
|
File.join(tmp_tests_gitaly_dir, 'config.toml')
|
2020-12-16 22:10:36 -05:00
|
|
|
when :gitaly2
|
|
|
|
File.join(tmp_tests_gitaly_dir, 'gitaly2.config.toml')
|
2020-03-25 14:08:10 -04:00
|
|
|
when :praefect
|
|
|
|
File.join(tmp_tests_gitaly_dir, 'praefect.config.toml')
|
|
|
|
end
|
2018-05-14 04:10:29 -04:00
|
|
|
end
|
|
|
|
|
2020-12-16 22:10:36 -05:00
|
|
|
def service_binary(service)
|
|
|
|
case service
|
|
|
|
when :gitaly, :gitaly2
|
|
|
|
'gitaly'
|
|
|
|
when :praefect
|
|
|
|
'praefect'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-06 07:10:58 -05:00
|
|
|
def install_gitaly_gems
|
|
|
|
system(env, "make #{tmp_tests_gitaly_dir}/.ruby-bundle", chdir: tmp_tests_gitaly_dir) # rubocop:disable GitlabSecurity/SystemCommandInjection
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_gitaly
|
|
|
|
system(env, 'make', chdir: tmp_tests_gitaly_dir) # rubocop:disable GitlabSecurity/SystemCommandInjection
|
|
|
|
end
|
|
|
|
|
2018-05-14 04:10:29 -04:00
|
|
|
def start_gitaly
|
2020-03-25 14:08:10 -04:00
|
|
|
start(:gitaly)
|
|
|
|
end
|
|
|
|
|
2020-12-16 22:10:36 -05:00
|
|
|
def start_gitaly2
|
|
|
|
start(:gitaly2)
|
|
|
|
end
|
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
def start_praefect
|
|
|
|
start(:praefect)
|
|
|
|
end
|
|
|
|
|
|
|
|
def start(service)
|
2021-04-29 11:10:07 -04:00
|
|
|
args = ["#{tmp_tests_gitaly_bin_dir}/#{service_binary(service)}"]
|
2020-03-25 14:08:10 -04:00
|
|
|
args.push("-config") if service == :praefect
|
|
|
|
args.push(config_path(service))
|
|
|
|
pid = spawn(env, *args, [:out, :err] => "log/#{service}-test.log")
|
2018-05-14 04:10:29 -04:00
|
|
|
|
|
|
|
begin
|
2020-03-25 14:08:10 -04:00
|
|
|
try_connect!(service)
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError
|
2018-05-14 04:10:29 -04:00
|
|
|
Process.kill('TERM', pid)
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
|
|
|
pid
|
|
|
|
end
|
|
|
|
|
2020-06-02 05:08:01 -04:00
|
|
|
# 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-05-14 04:10:29 -04:00
|
|
|
def check_gitaly_config!
|
2020-06-25 14:08:50 -04:00
|
|
|
LOGGER.debug "Checking gitaly-ruby Gemfile...\n"
|
2019-12-10 02:53:40 -05:00
|
|
|
|
|
|
|
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-06-25 14:08:50 -04:00
|
|
|
LOGGER.debug "Checking gitaly-ruby bundle...\n"
|
2021-06-08 11:10:00 -04:00
|
|
|
out = ENV['CI'] ? $stdout : '/dev/null'
|
2020-06-25 14:08:50 -04:00
|
|
|
abort 'bundle check failed' unless system(env, 'bundle', 'check', out: out, chdir: File.dirname(gemfile))
|
2018-05-14 04:10:29 -04:00
|
|
|
end
|
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
def read_socket_path(service)
|
2018-05-14 04:10:29 -04:00
|
|
|
# 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-03-25 14:08:10 -04:00
|
|
|
config_text = IO.read(config_path(service))
|
2018-05-14 04:10:29 -04:00
|
|
|
|
|
|
|
config_text.lines.each do |line|
|
|
|
|
match_data = line.match(/^\s*socket_path\s*=\s*"([^"]*)"$/)
|
|
|
|
|
|
|
|
return match_data[1] if match_data
|
|
|
|
end
|
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
raise "failed to find socket_path in #{config_path(service)}"
|
2018-05-14 04:10:29 -04:00
|
|
|
end
|
|
|
|
|
2020-03-25 14:08:10 -04:00
|
|
|
def try_connect!(service)
|
2020-06-25 14:08:50 -04:00
|
|
|
LOGGER.debug "Trying to connect to #{service}: "
|
2018-05-14 04:10:29 -04:00
|
|
|
timeout = 20
|
|
|
|
delay = 0.1
|
2020-03-25 14:08:10 -04:00
|
|
|
socket = read_socket_path(service)
|
2018-05-14 04:10:29 -04:00
|
|
|
|
|
|
|
Integer(timeout / delay).times do
|
2019-03-13 09:42:43 -04:00
|
|
|
UNIXSocket.new(socket)
|
2020-06-25 14:08:50 -04:00
|
|
|
LOGGER.debug " OK\n"
|
2019-03-13 09:42:43 -04:00
|
|
|
|
|
|
|
return
|
|
|
|
rescue Errno::ENOENT, Errno::ECONNREFUSED
|
2020-06-25 14:08:50 -04:00
|
|
|
LOGGER.debug '.'
|
2019-03-13 09:42:43 -04:00
|
|
|
sleep delay
|
2018-05-14 04:10:29 -04:00
|
|
|
end
|
|
|
|
|
2020-06-25 14:08:50 -04:00
|
|
|
LOGGER.warn " FAILED to connect to #{service}\n"
|
2018-05-14 04:10:29 -04:00
|
|
|
|
|
|
|
raise "could not connect to #{socket}"
|
|
|
|
end
|
|
|
|
end
|