2016-06-17 01:51:17 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'fileutils'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Gitlab::Git::Hook do
|
2017-07-12 08:45:42 -04:00
|
|
|
before do
|
|
|
|
# We need this because in the spec/spec_helper.rb we define it like this:
|
|
|
|
# allow_any_instance_of(Gitlab::Git::Hook).to receive(:trigger).and_return([true, nil])
|
2017-07-25 13:09:00 -04:00
|
|
|
allow_any_instance_of(described_class).to receive(:trigger).and_call_original
|
2017-07-12 08:45:42 -04:00
|
|
|
end
|
|
|
|
|
2018-06-05 11:58:28 -04:00
|
|
|
around do |example|
|
|
|
|
# TODO move hook tests to gitaly-ruby. Hook will disappear from gitlab-ce
|
|
|
|
Gitlab::GitalyClient::StorageSettings.allow_disk_access do
|
|
|
|
example.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-17 01:51:17 -04:00
|
|
|
describe "#trigger" do
|
2018-06-11 06:42:09 -04:00
|
|
|
set(:project) { create(:project, :repository) }
|
2017-08-22 07:54:14 -04:00
|
|
|
let(:repository) { project.repository.raw_repository }
|
|
|
|
let(:repo_path) { repository.path }
|
2018-06-11 06:42:09 -04:00
|
|
|
let(:hooks_dir) { File.join(repo_path, 'hooks') }
|
2016-06-17 01:51:17 -04:00
|
|
|
let(:user) { create(:user) }
|
2017-06-29 15:22:40 -04:00
|
|
|
let(:gl_id) { Gitlab::GlId.gl_id(user) }
|
2017-08-02 14:14:50 -04:00
|
|
|
let(:gl_username) { user.username }
|
2016-06-17 01:51:17 -04:00
|
|
|
|
|
|
|
def create_hook(name)
|
2018-06-11 06:42:09 -04:00
|
|
|
FileUtils.mkdir_p(hooks_dir)
|
|
|
|
hook_path = File.join(hooks_dir, name)
|
|
|
|
File.open(hook_path, 'w', 0755) do |f|
|
|
|
|
f.write(<<~HOOK)
|
|
|
|
#!/bin/sh
|
|
|
|
exit 0
|
|
|
|
HOOK
|
2016-06-17 01:51:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_failing_hook(name)
|
2018-06-11 06:42:09 -04:00
|
|
|
FileUtils.mkdir_p(hooks_dir)
|
|
|
|
hook_path = File.join(hooks_dir, name)
|
|
|
|
File.open(hook_path, 'w', 0755) do |f|
|
|
|
|
f.write(<<~HOOK)
|
|
|
|
#!/bin/sh
|
2016-06-17 01:51:17 -04:00
|
|
|
echo 'regular message from the hook'
|
|
|
|
echo 'error message from the hook' 1>&2
|
2017-09-11 22:50:12 -04:00
|
|
|
echo 'error message from the hook line 2' 1>&2
|
2016-06-17 01:51:17 -04:00
|
|
|
exit 1
|
|
|
|
HOOK
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
['pre-receive', 'post-receive', 'update'].each do |hook_name|
|
|
|
|
context "when triggering a #{hook_name} hook" do
|
|
|
|
context "when the hook is successful" do
|
2018-06-11 06:42:09 -04:00
|
|
|
let(:hook_path) { File.join(hooks_dir, hook_name) }
|
2017-06-29 15:22:40 -04:00
|
|
|
let(:gl_repository) { Gitlab::GlRepository.gl_repository(project, false) }
|
|
|
|
let(:env) do
|
|
|
|
{
|
|
|
|
'GL_ID' => gl_id,
|
2017-08-02 14:14:50 -04:00
|
|
|
'GL_USERNAME' => gl_username,
|
2017-06-29 15:22:40 -04:00
|
|
|
'PWD' => repo_path,
|
|
|
|
'GL_PROTOCOL' => 'web',
|
|
|
|
'GL_REPOSITORY' => gl_repository
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-06-17 01:51:17 -04:00
|
|
|
it "returns success with no errors" do
|
|
|
|
create_hook(hook_name)
|
2017-08-22 07:54:14 -04:00
|
|
|
hook = described_class.new(hook_name, repository)
|
2016-06-17 01:51:17 -04:00
|
|
|
blank = Gitlab::Git::BLANK_SHA
|
|
|
|
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
|
|
|
|
|
2017-06-29 15:22:40 -04:00
|
|
|
if hook_name != 'update'
|
|
|
|
expect(Open3).to receive(:popen3)
|
|
|
|
.with(env, hook_path, chdir: repo_path).and_call_original
|
|
|
|
end
|
|
|
|
|
2017-08-02 14:14:50 -04:00
|
|
|
status, errors = hook.trigger(gl_id, gl_username, blank, blank, ref)
|
2016-06-17 01:51:17 -04:00
|
|
|
expect(status).to be true
|
|
|
|
expect(errors).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the hook is unsuccessful" do
|
|
|
|
it "returns failure with errors" do
|
|
|
|
create_failing_hook(hook_name)
|
2017-08-22 07:54:14 -04:00
|
|
|
hook = described_class.new(hook_name, repository)
|
2016-06-17 01:51:17 -04:00
|
|
|
blank = Gitlab::Git::BLANK_SHA
|
|
|
|
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
|
|
|
|
|
2017-08-02 14:14:50 -04:00
|
|
|
status, errors = hook.trigger(gl_id, gl_username, blank, blank, ref)
|
2016-06-17 01:51:17 -04:00
|
|
|
expect(status).to be false
|
2018-06-11 06:42:09 -04:00
|
|
|
expect(errors).to eq("error message from the hook\nerror message from the hook line 2\n")
|
2016-06-17 01:51:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the hook doesn't exist" do
|
|
|
|
it "returns success with no errors" do
|
2017-08-22 07:54:14 -04:00
|
|
|
hook = described_class.new('unknown_hook', repository)
|
2016-06-17 01:51:17 -04:00
|
|
|
blank = Gitlab::Git::BLANK_SHA
|
|
|
|
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
|
|
|
|
|
2017-08-02 14:14:50 -04:00
|
|
|
status, errors = hook.trigger(gl_id, gl_username, blank, blank, ref)
|
2016-06-17 01:51:17 -04:00
|
|
|
expect(status).to be true
|
|
|
|
expect(errors).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|