debian-mirror-gitlab/spec/support/helpers/gitaly_setup.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

353 lines
9.5 KiB
Ruby
Raw Normal View History

2021-03-11 19:13:27 +05:30
# frozen_string_literal: true
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'
2022-07-23 23:45:48 +05:30
require 'fileutils'
2018-10-15 14:42:47 +05:30
2022-08-27 11:52:29 +05:30
require_relative '../../../lib/gitlab/utils'
2021-06-08 01:23:25 +05:30
module GitalySetup
2022-03-02 08:16:31 +05:30
extend self
REPOS_STORAGE = 'default'
2020-07-28 23:09:34 +05:30
LOGGER = begin
2021-04-29 21:17:54 +05:30
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-09-04 01:27:46 +05:30
Logger.new($stdout, level: level, formatter: ->(_, _, _, msg) { msg })
2021-04-29 21:17:54 +05:30
end
2020-07-28 23:09:34 +05:30
2022-01-26 12:08:38 +05:30
def expand_path(path)
File.expand_path(path, File.join(__dir__, '../../..'))
end
2018-10-15 14:42:47 +05:30
def tmp_tests_gitaly_dir
2022-01-26 12:08:38 +05:30
expand_path('tmp/tests/gitaly')
2021-06-08 01:23:25 +05:30
end
2022-07-16 23:28:13 +05:30
def runtime_dir
expand_path('tmp/run')
end
2021-06-08 01:23:25 +05:30
def tmp_tests_gitaly_bin_dir
File.join(tmp_tests_gitaly_dir, '_build', 'bin')
2018-10-15 14:42:47 +05:30
end
2020-06-23 00:09:42 +05:30
def tmp_tests_gitlab_shell_dir
2022-01-26 12:08:38 +05:30
expand_path('tmp/tests/gitlab-shell')
2020-06-23 00:09:42 +05:30
end
def rails_gitlab_shell_secret
2022-01-26 12:08:38 +05:30
expand_path('.gitlab_shell_secret')
2020-06-23 00:09:42 +05:30
end
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
2021-04-29 21:17:54 +05:30
{
2019-09-04 21:01:54 +05:30
# Git hooks can't run during tests as the internal API is not running.
2021-06-08 01:23:25 +05:30
'GITALY_TESTING_NO_GIT_HOOKS' => "1",
'GITALY_TESTING_ENABLE_ALL_FEATURE_FLAGS' => "true"
2018-10-15 14:42:47 +05:30
}
2021-04-29 21:17:54 +05:30
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
2022-03-02 08:16:31 +05:30
def repos_path(storage = REPOS_STORAGE)
Gitlab.config.repositories.storages[REPOS_STORAGE].legacy_disk_path
end
2022-07-16 23:28:13 +05:30
def service_cmd(service, toml = nil)
toml ||= config_path(service)
2021-02-22 17:27:13 +05:30
case service
when :gitaly, :gitaly2
2022-07-16 23:28:13 +05:30
[File.join(tmp_tests_gitaly_bin_dir, 'gitaly'), toml]
2021-02-22 17:27:13 +05:30
when :praefect
2022-07-16 23:28:13 +05:30
[File.join(tmp_tests_gitaly_bin_dir, 'praefect'), '-config', toml]
2021-02-22 17:27:13 +05:30
end
end
2022-03-02 08:16:31 +05:30
def run_command(cmd, env: {})
system(env, *cmd, exception: true, chdir: tmp_tests_gitaly_dir)
end
2021-03-08 18:12:59 +05:30
def build_gitaly
2022-04-04 11:22:00 +05:30
run_command(%w[make all WITH_BUNDLED_GIT=YesPlease], env: env.merge('GIT_VERSION' => nil))
2021-03-08 18:12:59 +05:30
end
2022-03-02 08:16:31 +05:30
def start_gitaly(toml = nil)
start(:gitaly, toml)
2020-04-22 19:07:51 +05:30
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
2022-08-27 11:52:29 +05:30
if praefect_with_db?
2022-07-16 23:28:13 +05:30
LOGGER.debug 'Starting Praefect with database election strategy'
start(:praefect, File.join(tmp_tests_gitaly_dir, 'praefect-db.config.toml'))
else
LOGGER.debug 'Starting Praefect with in-memory election strategy'
start(:praefect)
end
2020-04-22 19:07:51 +05:30
end
2022-03-02 08:16:31 +05:30
def start(service, toml = nil)
toml ||= config_path(service)
2022-07-16 23:28:13 +05:30
args = service_cmd(service, toml)
2022-03-02 08:16:31 +05:30
2022-07-23 23:45:48 +05:30
# Ensure that tmp/run exists
FileUtils.mkdir_p(runtime_dir)
2022-03-02 08:16:31 +05:30
# Ensure user configuration does not affect Git
# Context: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/58776#note_547613780
env = self.env.merge('HOME' => nil, 'XDG_CONFIG_HOME' => nil)
2020-04-22 19:07:51 +05:30
pid = spawn(env, *args, [:out, :err] => "log/#{service}-test.log")
2018-10-15 14:42:47 +05:30
begin
2022-03-02 08:16:31 +05:30
try_connect!(service, toml)
2021-06-08 01:23:25 +05:30
rescue StandardError
2018-10-15 14:42:47 +05:30
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
2022-03-02 08:16:31 +05:30
def connect_proc(toml)
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.
2023-03-04 22:38:38 +05:30
config_text = File.read(toml)
2018-10-15 14:42:47 +05:30
config_text.lines.each do |line|
2022-03-02 08:16:31 +05:30
match_data = line.match(/^\s*(socket_path|listen_addr)\s*=\s*"([^"]*)"$/)
2018-10-15 14:42:47 +05:30
2022-03-02 08:16:31 +05:30
next unless match_data
case match_data[1]
when 'socket_path'
return -> { UNIXSocket.new(match_data[2]) }
when 'listen_addr'
addr, port = match_data[2].split(':')
return -> { TCPSocket.new(addr, port.to_i) }
end
2018-10-15 14:42:47 +05:30
end
2022-03-02 08:16:31 +05:30
raise "failed to find socket_path or listen_addr in #{toml}"
2018-10-15 14:42:47 +05:30
end
2022-03-02 08:16:31 +05:30
def try_connect!(service, toml)
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
2022-03-02 08:16:31 +05:30
connect = connect_proc(toml)
2018-10-15 14:42:47 +05:30
Integer(timeout / delay).times do
2022-03-02 08:16:31 +05:30
connect.call
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
2022-03-02 08:16:31 +05:30
raise "could not connect to #{service}"
end
def gitaly_socket_path
Gitlab::GitalyClient.address(REPOS_STORAGE).delete_prefix('unix:')
end
def gitaly_dir
socket_path = gitaly_socket_path
socket_path = File.expand_path(gitaly_socket_path) if expand_path_for_socket?
File.dirname(socket_path)
end
# Linux fails with "bind: invalid argument" if a UNIX socket path exceeds 108 characters:
# https://github.com/golang/go/issues/6895. We use absolute paths in CI to ensure
# that changes in the current working directory don't affect GRPC reconnections.
def expand_path_for_socket?
!!ENV['CI']
end
def setup_gitaly
unless ENV['CI']
# In CI Gitaly is built in the setup-test-env job and saved in the
# artifacts. So when tests are started, there's no need to build Gitaly.
build_gitaly
end
Gitlab::SetupHelper::Gitaly.create_configuration(
gitaly_dir,
{ 'default' => repos_path },
force: true,
options: {
2022-07-16 23:28:13 +05:30
runtime_dir: runtime_dir,
2022-03-02 08:16:31 +05:30
prometheus_listen_addr: 'localhost:9236'
}
)
Gitlab::SetupHelper::Gitaly.create_configuration(
gitaly_dir,
{ 'default' => repos_path },
force: true,
options: {
2022-07-16 23:28:13 +05:30
runtime_dir: runtime_dir,
2022-03-02 08:16:31 +05:30
gitaly_socket: "gitaly2.socket",
config_filename: "gitaly2.config.toml"
}
)
2022-07-16 23:28:13 +05:30
# In CI we need to pre-generate both config files.
# For local testing we'll create the correct file on-demand.
2022-08-27 11:52:29 +05:30
if ENV['CI'] || !praefect_with_db?
2022-07-16 23:28:13 +05:30
Gitlab::SetupHelper::Praefect.create_configuration(
gitaly_dir,
{ 'praefect' => repos_path },
force: true
)
end
2022-08-27 11:52:29 +05:30
if ENV['CI'] || praefect_with_db?
2022-07-16 23:28:13 +05:30
Gitlab::SetupHelper::Praefect.create_configuration(
gitaly_dir,
{ 'praefect' => repos_path },
force: true,
options: {
per_repository: true,
config_filename: 'praefect-db.config.toml',
pghost: ENV['CI'] ? 'postgres' : ENV.fetch('PGHOST'),
pgport: ENV['CI'] ? 5432 : ENV.fetch('PGPORT').to_i,
pguser: ENV['CI'] ? 'postgres' : ENV.fetch('USER')
}
)
end
# In CI no database is running when Gitaly is set up
# so scripts/gitaly-test-spawn will take care of it instead.
setup_praefect unless ENV['CI']
end
def setup_praefect
2022-08-27 11:52:29 +05:30
return unless praefect_with_db?
2022-07-16 23:28:13 +05:30
migrate_cmd = service_cmd(:praefect, File.join(tmp_tests_gitaly_dir, 'praefect-db.config.toml')) + ['sql-migrate']
system(env, *migrate_cmd, [:out, :err] => 'log/praefect-test.log')
2022-03-02 08:16:31 +05:30
end
def socket_path(service)
File.join(tmp_tests_gitaly_dir, "#{service}.socket")
end
def praefect_socket_path
"unix:" + socket_path(:praefect)
end
def stop(pid)
Process.kill('KILL', pid)
rescue Errno::ESRCH
# The process can already be gone if the test run was INTerrupted.
end
def spawn_gitaly(toml = nil)
pids = []
if toml
pids << start_gitaly(toml)
else
pids << start_gitaly
pids << start_gitaly2
pids << start_praefect
end
Kernel.at_exit do
# In CI, this function is called by scripts/gitaly-test-spawn, triggered
# in a before_script. Gitaly needs to remain running until the container
# is stopped.
next if ENV['CI']
# In Workhorse tests (locally or in CI), this function is called by
# scripts/gitaly-test-spawn during `make test`. Gitaly needs to remain
# running until `make test` cleans it up.
next if ENV['GITALY_PID_FILE']
pids.each { |pid| stop(pid) }
end
rescue StandardError
raise gitaly_failure_message
end
def gitaly_failure_message
message = "gitaly spawn failed\n\n"
message += "- The `gitaly` binary does not exist: #{gitaly_binary}\n" unless File.exist?(gitaly_binary)
message += "- The `praefect` binary does not exist: #{praefect_binary}\n" unless File.exist?(praefect_binary)
2022-07-23 23:45:48 +05:30
message += "- No `git` binaries exist\n" if git_binaries.empty?
2022-03-02 08:16:31 +05:30
2022-07-16 23:28:13 +05:30
message += "\nCheck log/gitaly-test.log & log/praefect-test.log for errors.\n"
2022-03-02 08:16:31 +05:30
2022-04-04 11:22:00 +05:30
unless ENV['CI']
message += "\nIf binaries are missing, try running `make -C tmp/tests/gitaly all WITH_BUNDLED_GIT=YesPlease`.\n"
2022-03-02 08:16:31 +05:30
message += "\nOtherwise, try running `rm -rf #{tmp_tests_gitaly_dir}`."
end
message
end
2022-07-23 23:45:48 +05:30
def git_binaries
Dir.glob(File.join(tmp_tests_gitaly_dir, "_build", "bin", "gitaly-git-v*"))
2022-03-02 08:16:31 +05:30
end
def gitaly_binary
File.join(tmp_tests_gitaly_dir, "_build", "bin", "gitaly")
end
def praefect_binary
File.join(tmp_tests_gitaly_dir, "_build", "bin", "praefect")
end
2022-08-27 11:52:29 +05:30
def praefect_with_db?
Gitlab::Utils.to_boolean(ENV['GITALY_PRAEFECT_WITH_DB'], default: false)
end
2018-10-15 14:42:47 +05:30
end