2013-04-11 20:54:20 +00:00
|
|
|
require 'rspec/mocks'
|
2014-10-22 08:54:59 +00:00
|
|
|
require 'webrick'
|
2013-04-11 20:54:20 +00:00
|
|
|
|
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 = {
|
|
|
|
'feature' => '0b4bc9a',
|
|
|
|
'feature_conflict' => 'bb5206f',
|
|
|
|
'fix' => '12d65c8',
|
|
|
|
'improve/awesome' => '5937ac0',
|
|
|
|
'markdown' => '0ed8c6c',
|
|
|
|
'master' => '5937ac0'
|
|
|
|
}
|
|
|
|
|
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 20:54:20 +00:00
|
|
|
RSpec::Mocks::setup(self)
|
|
|
|
|
2013-04-11 08:50:58 +00:00
|
|
|
# Disable mailer for spinach tests
|
|
|
|
disable_mailer if opts[:mailer] == false
|
|
|
|
|
2014-07-31 16:24:53 +00:00
|
|
|
# Clean /tmp/tests
|
2014-07-31 18:01:42 +00:00
|
|
|
if File.directory?(tmp_test_path)
|
2014-09-22 17:46:57 +00:00
|
|
|
Dir.entries(tmp_test_path).each do |entry|
|
2014-09-29 14:50:20 +00:00
|
|
|
unless ['.', '..', 'gitlab-shell', factory_repo_name].include?(entry)
|
2014-09-22 17:46:57 +00:00
|
|
|
FileUtils.rm_r(File.join(tmp_test_path, entry))
|
|
|
|
end
|
|
|
|
end
|
2014-07-31 18:01:42 +00:00
|
|
|
end
|
|
|
|
|
2014-09-29 14:50:20 +00:00
|
|
|
FileUtils.mkdir_p(repos_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-10-22 08:54:59 +00:00
|
|
|
setup_internal_api_mock
|
|
|
|
|
2014-07-31 12:39:01 +00:00
|
|
|
# Create repository for FactoryGirl.create(:project)
|
|
|
|
setup_factory_repo
|
2013-04-25 14:15:33 +00:00
|
|
|
end
|
|
|
|
|
2013-06-07 20:39:32 +00:00
|
|
|
def disable_mailer
|
|
|
|
NotificationService.any_instance.stub(mailer: 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
|
|
|
|
NotificationService.any_instance.unstub(:mailer)
|
2013-04-25 14:15:33 +00:00
|
|
|
end
|
|
|
|
|
2014-07-31 12:39:01 +00:00
|
|
|
def setup_gitlab_shell
|
2014-09-22 17:46:57 +00:00
|
|
|
`rake gitlab:shell:install`
|
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
|
2014-09-27 07:56:19 +00:00
|
|
|
clone_url = "https://gitlab.com/gitlab-org/#{factory_repo_name}.git"
|
2013-04-25 14:15:33 +00:00
|
|
|
|
2014-09-27 07:56:19 +00:00
|
|
|
unless File.directory?(factory_repo_path)
|
2014-09-29 14:50:20 +00:00
|
|
|
system(*%W(git clone #{clone_url} #{factory_repo_path}))
|
|
|
|
end
|
|
|
|
|
|
|
|
Dir.chdir(factory_repo_path) do
|
|
|
|
BRANCH_SHA.each do |branch, sha|
|
|
|
|
# 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.
|
|
|
|
system(*%W(git clone --bare #{factory_repo_path} #{factory_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
|
|
|
|
|
|
|
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
|
|
|
|
factory_repo_path.to_s + '_bare'
|
2014-09-27 07:56:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def factory_repo_name
|
|
|
|
'gitlab-test'
|
|
|
|
end
|
2014-10-22 08:54:59 +00:00
|
|
|
|
|
|
|
def tmp_test_path
|
|
|
|
Rails.root.join('tmp', 'tests')
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_api_mock_pid_path
|
|
|
|
File.join(tmp_test_path, 'internal_api_mock.pid')
|
|
|
|
end
|
|
|
|
|
|
|
|
# This mock server exists because during testing GitLab is not served
|
|
|
|
# on any port, but gitlab-shell needs to ask the GitLab internal API
|
|
|
|
# if it is OK to push to repositories. This can happen during blob web
|
|
|
|
# edit tests. The server always replies yes: this should not modify affect
|
|
|
|
# web interface tests.
|
|
|
|
def setup_internal_api_mock
|
|
|
|
begin
|
|
|
|
server = WEBrick::HTTPServer.new(
|
|
|
|
BindAddress: '0.0.0.0',
|
|
|
|
Port: Gitlab.config.gitlab.port,
|
|
|
|
AccessLog: [],
|
|
|
|
Logger: WEBrick::Log.new('/dev/null')
|
|
|
|
)
|
|
|
|
rescue => ex
|
|
|
|
ex.message.prepend('could not start mock server on configured port. ')
|
|
|
|
raise ex
|
|
|
|
end
|
|
|
|
fork do
|
|
|
|
trap(:INT) { server.shutdown }
|
|
|
|
server.mount_proc('/') do |_req, res|
|
|
|
|
res.status = 200
|
|
|
|
res.body = 'true'
|
|
|
|
end
|
|
|
|
WEBrick::Daemon.start do
|
|
|
|
File.write(internal_api_mock_pid_path, Process.pid)
|
|
|
|
end
|
|
|
|
server.start
|
|
|
|
end
|
|
|
|
# Ideally this should be called from `config.after(:suite)`,
|
|
|
|
# but on Spinach when user hits Ctrl+C the server does not get killed
|
|
|
|
# if the hook is set up with `Spinach.hooks.after_run`.
|
|
|
|
at_exit do
|
|
|
|
# The file should exist on normal operation,
|
|
|
|
# but certain errors can lead to it not existing.
|
|
|
|
if File.exists?(internal_api_mock_pid_path)
|
|
|
|
Process.kill(:INT, File.read(internal_api_mock_pid_path).to_i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-04-01 11:39:19 +00:00
|
|
|
end
|