2013-04-11 20:54:20 +00:00
|
|
|
require 'rspec/mocks'
|
|
|
|
|
2013-04-01 11:39:19 +00:00
|
|
|
module TestEnv
|
|
|
|
extend self
|
|
|
|
|
2014-09-29 14:50:20 +00:00
|
|
|
# When developing the seed repository, comment out the branch you will modify.
|
|
|
|
BRANCH_SHA = {
|
2015-07-20 23:55:45 +00:00
|
|
|
'empty-branch' => '7efb185',
|
2015-01-11 06:48:16 +00:00
|
|
|
'flatten-dir' => 'e56497b',
|
2014-09-29 14:50:20 +00:00
|
|
|
'feature' => '0b4bc9a',
|
|
|
|
'feature_conflict' => 'bb5206f',
|
|
|
|
'fix' => '12d65c8',
|
|
|
|
'improve/awesome' => '5937ac0',
|
|
|
|
'markdown' => '0ed8c6c',
|
2015-07-26 09:17:34 +00:00
|
|
|
'master' => '5937ac0',
|
|
|
|
"'test'" => 'e56497b',
|
2014-09-29 14:50:20 +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 = {
|
|
|
|
'add-submodule-version-bump' => '3f547c08',
|
|
|
|
'master' => '5937ac0'
|
|
|
|
}
|
2015-04-13 05:27:45 +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
|
|
|
#
|
2013-04-10 20:28:42 +00:00
|
|
|
def init(opts = {})
|
2013-04-11 08:50:58 +00:00
|
|
|
# Disable mailer for spinach tests
|
|
|
|
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
|
|
|
|
2014-09-29 14:50:20 +00:00
|
|
|
FileUtils.mkdir_p(repos_path)
|
2015-07-24 16:54:06 +00:00
|
|
|
FileUtils.mkdir_p(backup_path)
|
2014-07-31 16:24:53 +00:00
|
|
|
|
2014-07-31 16:35:07 +00:00
|
|
|
# Setup GitLab shell for test instance
|
|
|
|
setup_gitlab_shell
|
|
|
|
|
2014-07-31 12:39:01 +00:00
|
|
|
# Create repository for FactoryGirl.create(:project)
|
|
|
|
setup_factory_repo
|
2015-05-02 20:01:33 +00:00
|
|
|
|
2015-04-13 05:27:45 +00:00
|
|
|
# Create repository for FactoryGirl.create(:forked_project_with_submodules)
|
|
|
|
setup_forked_repo
|
2013-04-25 14:15:33 +00:00
|
|
|
end
|
|
|
|
|
2013-06-07 20:39:32 +00:00
|
|
|
def disable_mailer
|
2015-05-21 21:49:06 +00:00
|
|
|
allow_any_instance_of(NotificationService).to receive(:mailer).
|
|
|
|
and_return(double.as_null_object)
|
2013-04-25 14:15:33 +00:00
|
|
|
end
|
2013-11-07 16:53:35 +00:00
|
|
|
|
2013-06-07 20:39:32 +00:00
|
|
|
def enable_mailer
|
2015-05-21 21:49:06 +00:00
|
|
|
allow_any_instance_of(NotificationService).to receive(:mailer).
|
|
|
|
and_call_original
|
2013-04-25 14:15:33 +00:00
|
|
|
end
|
|
|
|
|
2015-08-14 11:27:51 +00:00
|
|
|
def disable_pre_receive
|
2015-08-14 14:00:36 +00:00
|
|
|
allow_any_instance_of(Gitlab::Git::Hook).to receive(:trigger).and_return(true)
|
2015-08-14 11:27:51 +00:00
|
|
|
end
|
|
|
|
|
2015-02-04 01:50:30 +00:00
|
|
|
# Clean /tmp/tests
|
|
|
|
#
|
|
|
|
# Keeps gitlab-shell and gitlab-test
|
|
|
|
def clean_test_path
|
|
|
|
tmp_test_path = Rails.root.join('tmp', 'tests', '**')
|
|
|
|
|
|
|
|
Dir[tmp_test_path].each do |entry|
|
2015-05-02 20:01:33 +00:00
|
|
|
unless File.basename(entry) =~ /\Agitlab-(shell|test|test-fork)\z/
|
2015-02-04 01:50:30 +00:00
|
|
|
FileUtils.rm_rf(entry)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-31 12:39:01 +00:00
|
|
|
def setup_gitlab_shell
|
2015-02-04 01:50:30 +00:00
|
|
|
unless File.directory?(Rails.root.join(*%w(tmp tests gitlab-shell)))
|
|
|
|
`rake gitlab:shell:install`
|
|
|
|
end
|
2013-04-25 14:15:33 +00:00
|
|
|
end
|
2013-04-01 11:39:19 +00:00
|
|
|
|
2014-07-31 12:39:01 +00:00
|
|
|
def setup_factory_repo
|
2015-04-13 05:27:45 +00:00
|
|
|
setup_repo(factory_repo_path, factory_repo_path_bare, factory_repo_name,
|
|
|
|
BRANCH_SHA)
|
|
|
|
end
|
|
|
|
|
|
|
|
# This repo has a submodule commit that is not present in the main test
|
|
|
|
# repository.
|
|
|
|
def setup_forked_repo
|
|
|
|
setup_repo(forked_repo_path, forked_repo_path_bare, forked_repo_name,
|
|
|
|
FORKED_BRANCH_SHA)
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_repo(repo_path, repo_path_bare, repo_name, branch_sha)
|
|
|
|
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)
|
|
|
|
system(*%W(git clone -q #{clone_url} #{repo_path}))
|
2014-09-29 14:50:20 +00:00
|
|
|
end
|
|
|
|
|
2015-04-13 05:27:45 +00:00
|
|
|
Dir.chdir(repo_path) do
|
|
|
|
branch_sha.each do |branch, sha|
|
2014-09-29 14:50:20 +00:00
|
|
|
# Try to reset without fetching to avoid using the network.
|
|
|
|
reset = %W(git update-ref refs/heads/#{branch} #{sha})
|
|
|
|
unless system(*reset)
|
|
|
|
if system(*%w(git fetch origin))
|
|
|
|
unless system(*reset)
|
|
|
|
raise 'The fetched test seed '\
|
|
|
|
'does not contain the required revision.'
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise 'Could not fetch test seed repository.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-11-12 14:11:46 +00:00
|
|
|
end
|
2014-09-29 14:50:20 +00:00
|
|
|
|
|
|
|
# We must copy bare repositories because we will push to them.
|
2015-04-13 05:27:45 +00:00
|
|
|
system(git_env, *%W(git clone -q --bare #{repo_path} #{repo_path_bare}))
|
2013-11-12 14:11:46 +00:00
|
|
|
end
|
|
|
|
|
2014-07-31 12:39:01 +00:00
|
|
|
def copy_repo(project)
|
2014-09-29 14:50:20 +00:00
|
|
|
base_repo_path = File.expand_path(factory_repo_path_bare)
|
2014-07-31 18:30:35 +00:00
|
|
|
target_repo_path = File.expand_path(repos_path + "/#{project.namespace.path}/#{project.path}.git")
|
|
|
|
FileUtils.mkdir_p(target_repo_path)
|
|
|
|
FileUtils.cp_r("#{base_repo_path}/.", target_repo_path)
|
|
|
|
FileUtils.chmod_R 0755, target_repo_path
|
2013-04-01 11:39:19 +00:00
|
|
|
end
|
|
|
|
|
2014-07-31 12:39:01 +00:00
|
|
|
def repos_path
|
|
|
|
Gitlab.config.gitlab_shell.repos_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
|
|
|
|
|
2015-04-13 05:27:45 +00:00
|
|
|
def copy_forked_repo_with_submodules(project)
|
|
|
|
base_repo_path = File.expand_path(forked_repo_path_bare)
|
|
|
|
target_repo_path = File.expand_path(repos_path + "/#{project.namespace.path}/#{project.path}.git")
|
|
|
|
FileUtils.mkdir_p(target_repo_path)
|
|
|
|
FileUtils.cp_r("#{base_repo_path}/.", target_repo_path)
|
|
|
|
FileUtils.chmod_R 0755, target_repo_path
|
|
|
|
end
|
|
|
|
|
2014-09-27 07:56:19 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def factory_repo_path
|
2014-09-29 14:50:20 +00:00
|
|
|
@factory_repo_path ||= Rails.root.join('tmp', 'tests', factory_repo_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def factory_repo_path_bare
|
2015-02-04 01:50:30 +00:00
|
|
|
"#{factory_repo_path}_bare"
|
2014-09-27 07:56:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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_path_bare
|
|
|
|
"#{forked_repo_path}_bare"
|
|
|
|
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
|
2013-04-01 11:39:19 +00:00
|
|
|
end
|