2019-07-25 05:27:42 +00:00
# frozen_string_literal: true
2013-04-01 11:39:19 +00:00
module TestEnv
2021-06-07 09:10:26 +00:00
extend ActiveSupport :: Concern
2013-04-01 11:39:19 +00:00
extend self
2017-09-14 15:40:41 +00:00
ComponentFailedToInstallError = Class . new ( StandardError )
2014-09-29 14:50:20 +00:00
# When developing the seed repository, comment out the branch you will modify.
BRANCH_SHA = {
2018-08-02 16:17:20 +00:00
'signed-commits' = > '6101e87' ,
2016-09-27 15:39:29 +00:00
'not-merged-branch' = > 'b83d6e3' ,
'branch-merged' = > '498214d' ,
2016-08-05 11:15:06 +00:00
'empty-branch' = > '7efb185' ,
2016-08-29 11:43:09 +00:00
'ends-with.json' = > '98b0d8b' ,
2016-08-05 11:15:06 +00:00
'flatten-dir' = > 'e56497b' ,
'feature' = > '0b4bc9a' ,
'feature_conflict' = > 'bb5206f' ,
'fix' = > '48f0be4' ,
'improve/awesome' = > '5937ac0' ,
2017-09-20 05:52:43 +00:00
'merged-target' = > '21751bf' ,
2016-08-05 11:15:06 +00:00
'markdown' = > '0ed8c6c' ,
2018-01-11 23:12:34 +00:00
'lfs' = > '55bc176' ,
2016-09-27 15:39:29 +00:00
'master' = > 'b83d6e3' ,
2016-10-15 21:49:20 +00:00
'merge-test' = > '5937ac0' ,
2016-08-05 11:15:06 +00:00
" 'test' " = > 'e56497b' ,
'orphaned-branch' = > '45127a9' ,
'binary-encoding' = > '7b1cf43' ,
'gitattributes' = > '5a62481' ,
'expand-collapse-diffs' = > '4842455' ,
2016-11-10 17:24:12 +00:00
'symlink-expand-diff' = > '81e6355' ,
2020-06-19 21:08:32 +00:00
'diff-files-symlink-to-image' = > '8cfca84' ,
'diff-files-image-to-symlink' = > '3e94fda' ,
'diff-files-symlink-to-text' = > '689815e' ,
'diff-files-text-to-symlink' = > '5e2c270' ,
2016-08-05 11:15:06 +00:00
'expand-collapse-files' = > '025db92' ,
'expand-collapse-lines' = > '238e82d' ,
2018-10-09 16:17:40 +00:00
'pages-deploy' = > '7897d5b' ,
'pages-deploy-target' = > '7975be0' ,
2019-10-09 12:06:13 +00:00
'audio' = > 'c3c21fd' ,
2016-08-05 11:15:06 +00:00
'video' = > '8879059' ,
2017-04-11 12:06:45 +00:00
'add-balsamiq-file' = > 'b89b56d' ,
2016-08-05 11:15:06 +00:00
'crlf-diff' = > '5938907' ,
2016-09-01 15:20:29 +00:00
'conflict-start' = > '824be60' ,
2016-08-05 11:15:06 +00:00
'conflict-resolvable' = > '1450cd6' ,
'conflict-binary-file' = > '259a6fb' ,
2016-09-01 15:20:29 +00:00
'conflict-contains-conflict-markers' = > '78a3086' ,
2016-08-05 11:15:06 +00:00
'conflict-missing-side' = > 'eb227b3' ,
2016-08-23 15:37:14 +00:00
'conflict-non-utf8' = > 'd0a293c' ,
2016-08-05 11:15:06 +00:00
'conflict-too-large' = > '39fa04f' ,
2016-12-15 20:48:26 +00:00
'deleted-image-test' = > '6c17798' ,
2017-02-01 14:28:04 +00:00
'wip' = > 'b9238ee' ,
2017-02-24 12:13:42 +00:00
'csv' = > '3dd0896' ,
2017-04-11 20:25:10 +00:00
'v1.1.0' = > 'b83d6e3' ,
2021-04-21 12:09:16 +00:00
'add-ipython-files' = > 'f6b7a70' ,
2017-07-25 16:57:02 +00:00
'add-pdf-file' = > 'e774ebd' ,
2018-05-29 09:51:43 +00:00
'squash-large-files' = > '54cec52' ,
2017-10-07 04:25:17 +00:00
'add-pdf-text-binary' = > '79faa7b' ,
2018-07-10 14:00:13 +00:00
'add_images_and_changes' = > '010d106' ,
'update-gitlab-shell-v-6-0-1' = > '2f61d70' ,
2018-08-07 11:13:48 +00:00
'update-gitlab-shell-v-6-0-3' = > 'de78448' ,
2018-11-03 09:36:19 +00:00
'merge-commit-analyze-before' = > '1adbdef' ,
'merge-commit-analyze-side-branch' = > '8a99451' ,
'merge-commit-analyze-after' = > '646ece5' ,
2020-04-07 00:09:33 +00:00
'snippet/single-file' = > '43e4080aaa14fc7d4b77ee1f5c9d067d5a7df10e' ,
2020-05-19 12:08:21 +00:00
'snippet/multiple-files' = > '40232f7eb98b3f221886432def6e8bab2432add9' ,
2020-04-07 00:09:33 +00:00
'snippet/rename-and-edit-file' = > '220a1e4b4dff37feea0625a7947a4c60fbe78365' ,
'snippet/edit-file' = > 'c2f074f4f26929c92795a75775af79a6ed6d8430' ,
'snippet/no-files' = > '671aaa842a4875e5f30082d1ab6feda345fdb94d' ,
2018-08-20 14:48:08 +00:00
'2-mb-file' = > 'bf12d25' ,
2018-10-12 04:19:43 +00:00
'before-create-delete-modify-move' = > '845009f' ,
'between-create-delete-modify-move' = > '3f5f443' ,
'after-create-delete-modify-move' = > 'ba3faa7' ,
2018-07-31 16:35:02 +00:00
'with-codeowners' = > '219560e' ,
2018-12-13 17:49:05 +00:00
'submodule_inside_folder' = > 'b491b92' ,
2019-02-14 12:22:51 +00:00
'png-lfs' = > 'fe42f41' ,
2019-06-27 13:35:13 +00:00
'sha-starting-with-large-number' = > '8426165' ,
2020-04-15 12:09:18 +00:00
'invalid-utf8-diff-paths' = > '99e4853' ,
2020-07-31 06:09:53 +00:00
'compare-with-merge-head-source' = > 'f20a03d' ,
2021-03-04 18:09:08 +00:00
'compare-with-merge-head-target' = > '2f1e176' ,
'trailers' = > 'f0a5ed6'
2017-02-21 23:32:18 +00:00
} . freeze
2017-03-30 16:21:19 +00:00
2015-07-20 23:55:45 +00:00
# gitlab-test-fork is a fork of gitlab-fork, but we don't necessarily
# need to keep all the branches in sync.
# We currently only need a subset of the branches
FORKED_BRANCH_SHA = {
2016-08-29 11:43:09 +00:00
'add-submodule-version-bump' = > '3f547c0' ,
'master' = > '5937ac0' ,
'remove-submodule' = > '2a33e0c' ,
'conflict-resolvable-fork' = > '404fa3f'
2017-02-21 23:32:18 +00:00
} . freeze
2015-04-13 05:27:45 +00:00
2020-03-25 18:08:10 +00:00
TMP_TEST_PATH = Rails . root . join ( 'tmp' , 'tests' ) . freeze
2021-03-23 12:09:33 +00:00
REPOS_STORAGE = 'default'
2020-03-13 00:09:34 +00:00
SECOND_STORAGE_PATH = Rails . root . join ( 'tmp' , 'tests' , 'second_storage' )
2017-06-06 14:23:40 +00:00
2013-04-01 11:39:19 +00:00
# Test environment
#
2014-07-31 12:39:01 +00:00
# See gitlab.yml.example test section for paths
2013-04-01 11:39:19 +00:00
#
2021-06-07 09:10:26 +00:00
def init ( opts = { } )
2017-09-14 15:23:10 +00:00
unless Rails . env . test?
puts " \n TestEnv.init can only be run if `RAILS_ENV` is set to 'test' not ' #{ Rails . env } '! \n "
exit 1
end
2013-04-11 08:50:58 +00:00
# Disable mailer for spinach tests
2021-06-07 09:10:26 +00:00
disable_mailer if opts [ :mailer ] == false
2015-02-04 01:50:30 +00:00
clean_test_path
2014-07-31 18:01:42 +00:00
2021-06-07 09:10:26 +00:00
setup_gitlab_shell
setup_gitaly
# Feature specs are run through Workhorse
setup_workhorse
2019-07-12 10:44:17 +00:00
2021-06-07 09:10:26 +00:00
# Create repository for FactoryBot.create(:project)
setup_factory_repo
2021-06-05 18:10:05 +00:00
2021-06-07 09:10:26 +00:00
# Create repository for FactoryBot.create(:forked_project_with_submodules)
setup_forked_repo
2013-04-25 14:15:33 +00:00
end
2013-11-07 16:53:35 +00:00
2021-06-07 09:10:26 +00:00
included do | config |
config . append_before do
set_current_example_group
end
end
def disable_mailer
allow_any_instance_of ( NotificationService ) . to receive ( :mailer )
. and_return ( double . as_null_object )
end
def enable_mailer
allow_any_instance_of ( NotificationService ) . to receive ( :mailer )
. and_call_original
2013-04-25 14:15:33 +00:00
end
2015-02-04 01:50:30 +00:00
# Clean /tmp/tests
#
# Keeps gitlab-shell and gitlab-test
def clean_test_path
2020-03-25 18:08:10 +00:00
Dir [ File . join ( TMP_TEST_PATH , '**' ) ] . each do | entry |
2019-07-18 20:03:48 +00:00
unless test_dirs . include? ( File . basename ( entry ) )
2015-02-04 01:50:30 +00:00
FileUtils . rm_rf ( entry )
end
end
2017-03-28 17:27:44 +00:00
2021-02-16 15:09:50 +00:00
FileUtils . mkdir_p (
Gitlab :: GitalyClient :: StorageSettings . allow_disk_access { TestEnv . repos_path }
)
2020-03-13 00:09:34 +00:00
FileUtils . mkdir_p ( SECOND_STORAGE_PATH )
2017-03-28 17:27:44 +00:00
FileUtils . mkdir_p ( backup_path )
FileUtils . mkdir_p ( pages_path )
2017-11-08 09:46:47 +00:00
FileUtils . mkdir_p ( artifacts_path )
2015-02-04 01:50:30 +00:00
end
2014-07-31 12:39:01 +00:00
def setup_gitlab_shell
2019-10-18 15:06:05 +00:00
FileUtils . mkdir_p ( Gitlab . config . gitlab_shell . path )
2013-04-25 14:15:33 +00:00
end
2013-04-01 11:39:19 +00:00
2017-03-29 01:23:45 +00:00
def setup_gitaly
2021-06-07 09:10:26 +00:00
install_gitaly_args = [ gitaly_dir , repos_path , gitaly_url ] . compact . join ( ',' )
2017-09-14 15:40:41 +00:00
component_timed_setup ( 'Gitaly' ,
install_dir : gitaly_dir ,
version : Gitlab :: GitalyClient . expected_server_version ,
2021-06-07 09:10:26 +00:00
task : " gitlab:gitaly:install[ #{ install_gitaly_args } ] " ) do
2021-03-16 18:11:53 +00:00
Gitlab :: SetupHelper :: Gitaly . create_configuration (
gitaly_dir ,
{ 'default' = > repos_path } ,
force : true ,
options : {
2021-03-26 18:09:16 +00:00
prometheus_listen_addr : 'localhost:9236'
2021-03-16 18:11:53 +00:00
}
)
2020-12-17 03:10:36 +00:00
Gitlab :: SetupHelper :: Gitaly . create_configuration (
gitaly_dir ,
2021-03-03 18:11:16 +00:00
{ 'default' = > repos_path } ,
force : true ,
options : {
internal_socket_dir : File . join ( gitaly_dir , " internal_gitaly2 " ) ,
gitaly_socket : " gitaly2.socket " ,
config_filename : " gitaly2.config.toml "
}
2020-12-17 03:10:36 +00:00
)
2020-03-25 18:08:10 +00:00
Gitlab :: SetupHelper :: Praefect . create_configuration ( gitaly_dir , { 'praefect' = > repos_path } , force : true )
2018-10-24 09:22:32 +00:00
end
2021-06-07 09:10:26 +00:00
start_gitaly ( gitaly_dir )
2017-08-03 10:15:08 +00:00
end
2019-11-06 15:06:23 +00:00
def gitaly_socket_path
Gitlab :: GitalyClient . address ( 'default' ) . sub ( / \ Aunix: / , '' )
end
def gitaly_dir
2021-03-05 06:09:26 +00:00
socket_path = gitaly_socket_path
socket_path = File . expand_path ( gitaly_socket_path ) if expand_path?
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?
! ! ENV [ 'CI' ]
2019-11-06 15:06:23 +00:00
end
2017-04-10 21:33:41 +00:00
def start_gitaly ( gitaly_dir )
2019-11-06 15:06:23 +00:00
if ci?
2017-07-31 13:17:14 +00:00
# Gitaly has been spawned outside this process already
return
end
spawn_script = Rails . root . join ( 'scripts/gitaly-test-spawn' ) . to_s
2018-05-14 08:10:29 +00:00
Bundler . with_original_env do
2019-11-06 15:06:23 +00:00
unless system ( spawn_script )
message = 'gitaly spawn failed'
message += " (try `rm -rf #{ gitaly_dir } ` ?) " unless ci?
raise message
end
2018-05-14 08:10:29 +00:00
end
2019-11-06 15:06:23 +00:00
2020-03-25 18:08:10 +00:00
gitaly_pid = Integer ( File . read ( TMP_TEST_PATH . join ( 'gitaly.pid' ) ) )
2021-01-06 15:10:34 +00:00
gitaly2_pid = Integer ( File . read ( TMP_TEST_PATH . join ( 'gitaly2.pid' ) ) )
2020-03-25 18:08:10 +00:00
praefect_pid = Integer ( File . read ( TMP_TEST_PATH . join ( 'praefect.pid' ) ) )
2018-05-14 08:10:29 +00:00
2021-01-06 15:10:34 +00:00
Kernel . at_exit do
pids = [ gitaly_pid , gitaly2_pid , praefect_pid ]
pids . each { | pid | stop ( pid ) }
end
2018-01-12 16:41:37 +00:00
2020-03-25 18:08:10 +00:00
wait ( 'gitaly' )
wait ( 'praefect' )
2017-09-05 13:37:48 +00:00
end
2020-03-25 18:08:10 +00:00
def stop ( pid )
Process . kill ( 'KILL' , pid )
rescue Errno :: ESRCH
# The process can already be gone if the test run was INTerrupted.
end
def gitaly_url
ENV . fetch ( 'GITALY_REPO_URL' , nil )
end
def socket_path ( service )
TMP_TEST_PATH . join ( 'gitaly' , " #{ service } .socket " ) . to_s
end
def praefect_socket_path
" unix: " + socket_path ( :praefect )
end
def wait ( service )
2017-09-05 13:37:48 +00:00
sleep_time = 10
sleep_interval = 0 . 1
2020-03-25 18:08:10 +00:00
socket = socket_path ( service )
2017-09-05 13:37:48 +00:00
Integer ( sleep_time / sleep_interval ) . times do
2019-03-13 13:42:43 +00:00
Socket . unix ( socket )
return
2021-04-26 12:09:44 +00:00
rescue StandardError
2019-03-13 13:42:43 +00:00
sleep sleep_interval
2017-09-05 13:37:48 +00:00
end
2020-03-25 18:08:10 +00:00
raise " could not connect to #{ service } at #{ socket . inspect } after #{ sleep_time } seconds "
2019-01-23 18:26:57 +00:00
end
2020-03-10 18:08:17 +00:00
def setup_workhorse
2020-12-11 15:10:04 +00:00
start = Time . now
2020-12-11 18:09:57 +00:00
return if skip_compile_workhorse?
2021-06-07 09:10:26 +00:00
puts " \n ==> Setting up GitLab Workhorse... "
2020-12-11 15:10:04 +00:00
FileUtils . rm_rf ( workhorse_dir )
Gitlab :: SetupHelper :: Workhorse . compile_into ( workhorse_dir )
Gitlab :: SetupHelper :: Workhorse . create_configuration ( workhorse_dir , nil )
2020-12-11 18:09:57 +00:00
File . write ( workhorse_tree_file , workhorse_tree ) if workhorse_source_clean?
2021-06-07 09:10:26 +00:00
puts " GitLab Workhorse set up in #{ Time . now - start } seconds... \n "
2020-03-10 18:08:17 +00:00
end
2020-12-11 18:09:57 +00:00
def skip_compile_workhorse?
File . directory? ( workhorse_dir ) &&
workhorse_source_clean? &&
File . exist? ( workhorse_tree_file ) &&
workhorse_tree == File . read ( workhorse_tree_file )
end
def workhorse_source_clean?
out = IO . popen ( %w[ git status --porcelain workhorse ] , & :read )
$? . success? && out . empty?
end
def workhorse_tree
IO . popen ( %w[ git rev-parse HEAD:workhorse ] , & :read )
end
def workhorse_tree_file
File . join ( workhorse_dir , 'WORKHORSE_TREE' )
end
2020-03-10 18:08:17 +00:00
def workhorse_dir
@workhorse_path || = File . join ( 'tmp' , 'tests' , 'gitlab-workhorse' )
end
2021-01-06 12:10:58 +00:00
def with_workhorse ( host , port , upstream , & blk )
2020-03-10 18:08:17 +00:00
host = " [ #{ host } ] " if host . include? ( ':' )
listen_addr = [ host , port ] . join ( ':' )
2020-12-17 03:10:36 +00:00
config_path = Gitlab :: SetupHelper :: Workhorse . get_config_path ( workhorse_dir , { } )
2020-08-24 18:10:19 +00:00
# This should be set up in setup_workhorse, but since
# component_needs_update? only checks that versions are consistent,
# we need to ensure the config file exists. This line can be removed
# later after a new Workhorse version is updated.
Gitlab :: SetupHelper :: Workhorse . create_configuration ( workhorse_dir , nil ) unless File . exist? ( config_path )
2020-03-10 18:08:17 +00:00
workhorse_pid = spawn (
2020-08-20 15:10:18 +00:00
{ 'PATH' = > " #{ ENV [ 'PATH' ] } : #{ workhorse_dir } " } ,
2020-03-10 18:08:17 +00:00
File . join ( workhorse_dir , 'gitlab-workhorse' ) ,
'-authSocket' , upstream ,
'-documentRoot' , Rails . root . join ( 'public' ) . to_s ,
'-listenAddr' , listen_addr ,
'-secretPath' , Gitlab :: Workhorse . secret_path . to_s ,
2020-08-24 18:10:19 +00:00
'-config' , config_path ,
2020-03-10 18:08:17 +00:00
'-logFile' , 'log/workhorse-test.log' ,
'-logFormat' , 'structured' ,
'-developmentMode' # to serve assets and rich error messages
)
begin
yield
ensure
Process . kill ( 'TERM' , workhorse_pid )
Process . wait ( workhorse_pid )
end
end
def workhorse_url
ENV . fetch ( 'GITLAB_WORKHORSE_URL' , nil )
end
2014-07-31 12:39:01 +00:00
def setup_factory_repo
2020-05-14 15:08:14 +00:00
setup_repo ( factory_repo_path , factory_repo_path_bare , factory_repo_name , BRANCH_SHA )
2015-04-13 05:27:45 +00:00
end
# This repo has a submodule commit that is not present in the main test
# repository.
def setup_forked_repo
2020-05-14 15:08:14 +00:00
setup_repo ( forked_repo_path , forked_repo_path_bare , forked_repo_name , FORKED_BRANCH_SHA )
2015-04-13 05:27:45 +00:00
end
2017-05-10 15:31:50 +00:00
def setup_repo ( repo_path , repo_path_bare , repo_name , refs )
2015-04-13 05:27:45 +00:00
clone_url = " https://gitlab.com/gitlab-org/ #{ repo_name } .git "
2013-04-25 14:15:33 +00:00
2015-04-13 05:27:45 +00:00
unless File . directory? ( repo_path )
2021-06-07 09:10:26 +00:00
puts " \n ==> Setting up #{ repo_name } repository in #{ repo_path } ... "
2020-05-14 15:08:14 +00:00
start = Time . now
system ( * %W( #{ Gitlab . config . git . bin_path } clone --quiet -- #{ clone_url } #{ repo_path } ) )
2021-06-07 09:10:26 +00:00
puts " #{ repo_path } set up in #{ Time . now - start } seconds... \n "
2014-09-29 14:50:20 +00:00
end
2017-05-10 15:31:50 +00:00
set_repo_refs ( repo_path , refs )
2014-09-29 14:50:20 +00:00
2017-03-31 06:46:51 +00:00
unless File . directory? ( repo_path_bare )
2021-06-07 09:10:26 +00:00
puts " \n ==> Setting up #{ repo_name } bare repository in #{ repo_path_bare } ... "
2020-05-14 15:08:14 +00:00
start = Time . now
2017-03-31 06:46:51 +00:00
# We must copy bare repositories because we will push to them.
2020-05-14 15:08:14 +00:00
system ( git_env , * %W( #{ Gitlab . config . git . bin_path } clone --quiet --bare -- #{ repo_path } #{ repo_path_bare } ) )
2021-06-07 09:10:26 +00:00
puts " #{ repo_path_bare } set up in #{ Time . now - start } seconds... \n "
2017-03-31 06:46:51 +00:00
end
2013-11-12 14:11:46 +00:00
end
2019-12-27 15:08:16 +00:00
def copy_repo ( subject , bare_repo : , refs : )
target_repo_path = File . expand_path ( repos_path + " / #{ subject . disk_path } .git " )
2018-04-13 10:57:19 +00:00
2014-07-31 18:30:35 +00:00
FileUtils . mkdir_p ( target_repo_path )
2017-05-10 15:31:50 +00:00
FileUtils . cp_r ( " #{ File . expand_path ( bare_repo ) } /. " , target_repo_path )
2014-07-31 18:30:35 +00:00
FileUtils . chmod_R 0755 , target_repo_path
2013-04-01 11:39:19 +00:00
end
2019-10-23 09:06:03 +00:00
def rm_storage_dir ( storage , dir )
Gitlab :: GitalyClient :: StorageSettings . allow_disk_access do
repos_path = Gitlab . config . repositories . storages [ storage ] . legacy_disk_path
target_repo_refs_path = File . join ( repos_path , dir )
FileUtils . remove_dir ( target_repo_refs_path )
end
rescue Errno :: ENOENT
end
def storage_dir_exists? ( storage , dir )
Gitlab :: GitalyClient :: StorageSettings . allow_disk_access do
repos_path = Gitlab . config . repositories . storages [ storage ] . legacy_disk_path
File . exist? ( File . join ( repos_path , dir ) )
end
end
2018-07-27 01:18:28 +00:00
def create_bare_repository ( path )
FileUtils . mkdir_p ( path )
system ( git_env , * %W( #{ Gitlab . config . git . bin_path } -C #{ path } init --bare ) ,
out : '/dev/null' ,
err : '/dev/null' )
end
2014-07-31 12:39:01 +00:00
def repos_path
2018-04-13 10:57:19 +00:00
@repos_path || = Gitlab . config . repositories . storages [ REPOS_STORAGE ] . legacy_disk_path
2013-04-01 11:39:19 +00:00
end
2014-09-27 07:56:19 +00:00
2015-07-24 16:54:06 +00:00
def backup_path
Gitlab . config . backup . path
end
2017-03-28 17:27:44 +00:00
def pages_path
Gitlab . config . pages . path
end
2017-11-08 09:46:47 +00:00
def artifacts_path
2018-01-29 17:57:34 +00:00
Gitlab . config . artifacts . storage_path
2017-11-08 09:46:47 +00:00
end
2016-01-29 01:45:03 +00:00
# When no cached assets exist, manually hit the root path to create them
#
# Otherwise they'd be created by the first test, often timing out and
# causing a transient test failure
2017-03-30 14:30:05 +00:00
def eager_load_driver_server
2016-01-29 01:45:03 +00:00
return unless defined? ( Capybara )
2017-04-02 03:23:18 +00:00
puts " Starting the Capybara driver server... "
2017-03-31 07:55:46 +00:00
Capybara . current_session . visit '/'
2016-01-29 01:45:03 +00:00
end
2017-05-10 15:31:50 +00:00
def factory_repo_path_bare
" #{ factory_repo_path } _bare "
end
def forked_repo_path_bare
" #{ forked_repo_path } _bare "
end
2017-08-08 11:05:40 +00:00
def with_empty_bare_repository ( name = nil )
path = Rails . root . join ( 'tmp/tests' , name || 'empty-bare-repository' ) . to_s
yield ( Rugged :: Repository . init_at ( path , :bare ) )
ensure
FileUtils . rm_rf ( path )
end
2019-07-12 10:44:17 +00:00
def current_example_group
Thread . current [ :current_example_group ]
end
# looking for a top-level `describe`
def topmost_example_group
example_group = current_example_group
example_group = example_group [ :parent_example_group ] until example_group [ :parent_example_group ] . nil?
example_group
end
2014-09-27 07:56:19 +00:00
private
2021-06-07 09:10:26 +00:00
def set_current_example_group
Thread . current [ :current_example_group ] = :: RSpec . current_example . metadata [ :example_group ]
end
2019-05-16 15:49:26 +00:00
# These are directories that should be preserved at cleanup time
def test_dirs
@test_dirs || = %w[
2019-07-18 20:03:48 +00:00
frontend
2019-05-16 15:49:26 +00:00
gitaly
gitlab - shell
gitlab - test
gitlab - test_bare
gitlab - test - fork
gitlab - test - fork_bare
2020-03-10 18:08:17 +00:00
gitlab - workhorse
gitlab_workhorse_secret
2019-05-16 15:49:26 +00:00
]
end
2014-09-27 07:56:19 +00:00
def factory_repo_path
2014-09-29 14:50:20 +00:00
@factory_repo_path || = Rails . root . join ( 'tmp' , 'tests' , factory_repo_name )
end
2014-09-27 07:56:19 +00:00
def factory_repo_name
'gitlab-test'
end
2015-03-27 20:39:41 +00:00
2015-04-13 05:27:45 +00:00
def forked_repo_path
@forked_repo_path || = Rails . root . join ( 'tmp' , 'tests' , forked_repo_name )
end
def forked_repo_name
'gitlab-test-fork'
end
2015-03-27 20:39:41 +00:00
# Prevent developer git configurations from being persisted to test
# repositories
def git_env
2015-04-13 05:27:45 +00:00
{ 'GIT_TEMPLATE_DIR' = > '' }
2015-03-27 20:39:41 +00:00
end
2016-08-29 11:43:09 +00:00
def set_repo_refs ( repo_path , branch_sha )
2017-05-10 15:31:50 +00:00
instructions = branch_sha . map { | branch , sha | " update refs/heads/ #{ branch } \x00 #{ sha } \x00 " } . join ( " \x00 " ) << " \x00 "
2016-11-04 04:47:39 +00:00
update_refs = %W( #{ Gitlab . config . git . bin_path } update-ref --stdin -z )
reset = proc do
2017-05-10 15:31:50 +00:00
Dir . chdir ( repo_path ) do
IO . popen ( update_refs , " w " ) { | io | io . write ( instructions ) }
$? . success?
end
2016-11-04 04:47:39 +00:00
end
2017-05-10 15:31:50 +00:00
# Try to reset without fetching to avoid using the network.
unless reset . call
raise 'Could not fetch test seed repository.' unless system ( * %W( #{ Gitlab . config . git . bin_path } -C #{ repo_path } fetch origin ) )
2019-07-18 09:55:40 +00:00
raise " Could not update test seed repository, please delete #{ repo_path } and try again " unless reset . call
2016-08-29 11:43:09 +00:00
end
end
2017-05-23 19:27:39 +00:00
2021-06-07 09:10:26 +00:00
def component_timed_setup ( component , install_dir : , version : , task : )
2017-09-14 15:40:41 +00:00
start = Time . now
ensure_component_dir_name_is_correct! ( component , install_dir )
2017-09-23 21:22:15 +00:00
# On CI, once installed, components never need update
2019-11-06 15:06:23 +00:00
return if File . exist? ( install_dir ) && ci?
2017-09-23 21:22:15 +00:00
2017-09-14 15:40:41 +00:00
if component_needs_update? ( install_dir , version )
2021-06-07 09:10:26 +00:00
puts " \n ==> Setting up #{ component } ... "
2017-09-14 15:40:41 +00:00
# Cleanup the component entirely to ensure we start fresh
FileUtils . rm_rf ( install_dir )
2018-01-11 16:34:01 +00:00
2021-06-07 09:10:26 +00:00
unless system ( 'rake' , task )
2017-09-14 15:40:41 +00:00
raise ComponentFailedToInstallError
end
2020-06-25 18:08:50 +00:00
yield if block_given?
2017-09-14 15:40:41 +00:00
2021-06-07 09:10:26 +00:00
puts " #{ component } set up in #{ Time . now - start } seconds... \n "
2020-06-25 18:08:50 +00:00
end
2017-09-14 15:40:41 +00:00
rescue ComponentFailedToInstallError
puts " \n #{ component } failed to install, cleaning up #{ install_dir } ! \n "
FileUtils . rm_rf ( install_dir )
exit 1
end
2019-11-06 15:06:23 +00:00
def ci?
ENV [ 'CI' ] . present?
end
2017-09-14 15:40:41 +00:00
def ensure_component_dir_name_is_correct! ( component , path )
actual_component_dir_name = File . basename ( path )
expected_component_dir_name = component . parameterize
unless actual_component_dir_name == expected_component_dir_name
puts " #{ component } install dir should be named ' #{ expected_component_dir_name } ', not ' #{ actual_component_dir_name } ' (full install path given was ' #{ path } ')! \n "
exit 1
end
end
2017-06-19 21:06:17 +00:00
def component_needs_update? ( component_folder , expected_version )
2018-01-05 11:31:12 +00:00
# Allow local overrides of the component for tests during development
return false if Rails . env . test? && File . symlink? ( component_folder )
2020-07-17 06:09:11 +00:00
return false if component_matches_git_sha? ( component_folder , expected_version )
2020-11-02 18:09:03 +00:00
return false if component_ahead_of_target? ( component_folder , expected_version )
2017-06-19 21:06:17 +00:00
version = File . read ( File . join ( component_folder , 'VERSION' ) ) . strip
2017-05-23 19:27:39 +00:00
# Notice that this will always yield true when using branch versions
# (`=branch_name`), but that actually makes sure the server is always based
# on the latest branch revision.
2017-06-19 21:06:17 +00:00
version != expected_version
2017-05-23 19:27:39 +00:00
rescue Errno :: ENOENT
true
end
2020-07-17 06:09:11 +00:00
2020-11-02 18:09:03 +00:00
def component_ahead_of_target? ( component_folder , expected_version )
# The HEAD of the component_folder will be used as heuristic for the version
# of the binaries, allowing to use Git to determine if HEAD is later than
# the expected version. Note: Git considers HEAD to be an anchestor of HEAD.
_out , exit_status = Gitlab :: Popen . popen ( %W[
#{Gitlab.config.git.bin_path}
- C #{component_folder}
merge - base - - is - ancestor
#{expected_version} HEAD
] )
exit_status == 0
end
2020-07-17 06:09:11 +00:00
def component_matches_git_sha? ( component_folder , expected_version )
# Not a git SHA, so return early
2020-07-20 09:09:22 +00:00
return false unless expected_version =~ :: Gitlab :: Git :: COMMIT_ID
2020-07-17 06:09:11 +00:00
sha , exit_status = Gitlab :: Popen . popen ( %W( #{ Gitlab . config . git . bin_path } rev-parse HEAD ) , component_folder )
return false if exit_status != 0
expected_version == sha . chomp
end
2013-04-01 11:39:19 +00:00
end
2019-09-13 13:26:31 +00:00
require_relative ( '../../../ee/spec/support/helpers/ee/test_env' ) if Gitlab . ee?
2021-05-11 21:10:21 +00:00
:: TestEnv . prepend_mod_with ( 'TestEnv' )
:: TestEnv . extend_mod_with ( 'TestEnv' )