2019-08-22 10:57:44 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-02 13:09:55 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Gitlab::Workhorse do
|
2020-02-20 18:08:51 +00:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
2016-09-20 16:21:52 +00:00
|
|
|
let(:repository) { project.repository }
|
|
|
|
|
|
|
|
def decode_workhorse_header(array)
|
|
|
|
key, value = array
|
|
|
|
command, encoded_params = value.split(":")
|
2020-05-01 00:09:59 +00:00
|
|
|
params = Gitlab::Json.parse(Base64.urlsafe_decode64(encoded_params))
|
2016-09-20 16:21:52 +00:00
|
|
|
|
|
|
|
[key, command, params]
|
|
|
|
end
|
2016-02-02 13:09:55 +00:00
|
|
|
|
2019-08-28 16:51:17 +00:00
|
|
|
before do
|
2021-02-16 15:09:50 +00:00
|
|
|
stub_feature_flags(gitaly_enforce_requests_limits: true)
|
2019-08-28 16:51:17 +00:00
|
|
|
end
|
|
|
|
|
2016-08-19 17:10:41 +00:00
|
|
|
describe ".send_git_archive" do
|
2017-10-01 00:13:23 +00:00
|
|
|
let(:ref) { 'master' }
|
|
|
|
let(:format) { 'zip' }
|
|
|
|
let(:storage_path) { Gitlab.config.gitlab.repository_downloads_path }
|
2020-04-21 15:21:10 +00:00
|
|
|
let(:path) { 'some/path' }
|
2019-04-11 14:03:02 +00:00
|
|
|
let(:metadata) { repository.archive_metadata(ref, storage_path, format, append_sha: nil, path: path) }
|
2018-01-09 15:59:46 +00:00
|
|
|
let(:cache_disabled) { false }
|
2017-10-01 00:13:23 +00:00
|
|
|
|
|
|
|
subject do
|
2019-04-11 14:03:02 +00:00
|
|
|
described_class.send_git_archive(repository, ref: ref, format: format, append_sha: nil, path: path)
|
2017-10-01 00:13:23 +00:00
|
|
|
end
|
|
|
|
|
2018-01-09 15:59:46 +00:00
|
|
|
before do
|
|
|
|
allow(described_class).to receive(:git_archive_cache_disabled?).and_return(cache_disabled)
|
|
|
|
end
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
it 'sets the header correctly' do
|
|
|
|
key, command, params = decode_workhorse_header(subject)
|
|
|
|
|
|
|
|
expect(key).to eq('Gitlab-Workhorse-Send-Data')
|
|
|
|
expect(command).to eq('git-archive')
|
|
|
|
expect(params).to eq({
|
|
|
|
'GitalyServer' => {
|
2021-02-16 15:09:50 +00:00
|
|
|
features: { 'gitaly-feature-enforce-requests-limits' => 'true' },
|
2020-04-21 15:21:10 +00:00
|
|
|
address: Gitlab::GitalyClient.address(project.repository_storage),
|
|
|
|
token: Gitlab::GitalyClient.token(project.repository_storage)
|
|
|
|
},
|
|
|
|
'ArchivePath' => metadata['ArchivePath'],
|
|
|
|
'GetArchiveRequest' => Base64.encode64(
|
|
|
|
Gitaly::GetArchiveRequest.new(
|
|
|
|
repository: repository.gitaly_repository,
|
|
|
|
commit_id: metadata['CommitId'],
|
|
|
|
prefix: metadata['ArchivePrefix'],
|
|
|
|
format: Gitaly::GetArchiveRequest::Format::ZIP,
|
2020-10-15 21:09:12 +00:00
|
|
|
path: path,
|
|
|
|
include_lfs_blobs: true
|
2020-04-21 15:21:10 +00:00
|
|
|
).to_proto
|
2019-04-11 14:03:02 +00:00
|
|
|
)
|
2020-04-21 15:21:10 +00:00
|
|
|
}.deep_stringify_keys)
|
2018-07-09 10:50:17 +00:00
|
|
|
end
|
2018-01-09 15:59:46 +00:00
|
|
|
|
2020-10-15 21:09:12 +00:00
|
|
|
context 'when include_lfs_blobs_in_archive is disabled' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(include_lfs_blobs_in_archive: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets include_lfs_blobs to false' do
|
|
|
|
key, command, params = decode_workhorse_header(subject)
|
|
|
|
|
|
|
|
expect(key).to eq('Gitlab-Workhorse-Send-Data')
|
|
|
|
expect(command).to eq('git-archive')
|
|
|
|
expect(params).to eq({
|
|
|
|
'GitalyServer' => {
|
2021-02-16 15:09:50 +00:00
|
|
|
features: { 'gitaly-feature-enforce-requests-limits' => 'true' },
|
2020-10-15 21:09:12 +00:00
|
|
|
address: Gitlab::GitalyClient.address(project.repository_storage),
|
|
|
|
token: Gitlab::GitalyClient.token(project.repository_storage)
|
|
|
|
},
|
|
|
|
'ArchivePath' => metadata['ArchivePath'],
|
|
|
|
'GetArchiveRequest' => Base64.encode64(
|
|
|
|
Gitaly::GetArchiveRequest.new(
|
|
|
|
repository: repository.gitaly_repository,
|
|
|
|
commit_id: metadata['CommitId'],
|
|
|
|
prefix: metadata['ArchivePrefix'],
|
|
|
|
format: Gitaly::GetArchiveRequest::Format::ZIP,
|
|
|
|
path: path,
|
|
|
|
include_lfs_blobs: false
|
|
|
|
).to_proto
|
|
|
|
)
|
|
|
|
}.deep_stringify_keys)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
context 'when archive caching is disabled' do
|
|
|
|
let(:cache_disabled) { true }
|
2018-01-09 15:59:46 +00:00
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
it 'tells workhorse not to use the cache' do
|
|
|
|
_, _, params = decode_workhorse_header(subject)
|
|
|
|
expect(params).to include({ 'DisableCache' => true })
|
2018-01-09 15:59:46 +00:00
|
|
|
end
|
2017-10-01 00:13:23 +00:00
|
|
|
end
|
|
|
|
|
2016-02-02 13:09:55 +00:00
|
|
|
context "when the repository doesn't have an archive file path" do
|
|
|
|
before do
|
2020-09-02 15:10:54 +00:00
|
|
|
allow(project.repository).to receive(:archive_metadata).and_return({})
|
2016-02-02 13:09:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error" do
|
2017-10-01 00:13:23 +00:00
|
|
|
expect { subject }.to raise_error(RuntimeError)
|
2016-02-02 13:09:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-19 17:10:41 +00:00
|
|
|
|
2016-09-20 16:21:52 +00:00
|
|
|
describe '.send_git_patch' do
|
|
|
|
let(:diff_refs) { double(base_sha: "base", head_sha: "head") }
|
2019-12-12 12:07:33 +00:00
|
|
|
|
2016-09-20 16:21:52 +00:00
|
|
|
subject { described_class.send_git_patch(repository, diff_refs) }
|
|
|
|
|
2018-07-11 10:59:15 +00:00
|
|
|
it 'sets the header correctly' do
|
|
|
|
key, command, params = decode_workhorse_header(subject)
|
2017-10-02 18:51:20 +00:00
|
|
|
|
2018-07-11 10:59:15 +00:00
|
|
|
expect(key).to eq("Gitlab-Workhorse-Send-Data")
|
|
|
|
expect(command).to eq("git-format-patch")
|
|
|
|
expect(params).to eq({
|
|
|
|
'GitalyServer' => {
|
2021-02-16 15:09:50 +00:00
|
|
|
features: { 'gitaly-feature-enforce-requests-limits' => 'true' },
|
2018-07-11 10:59:15 +00:00
|
|
|
address: Gitlab::GitalyClient.address(project.repository_storage),
|
|
|
|
token: Gitlab::GitalyClient.token(project.repository_storage)
|
|
|
|
},
|
|
|
|
'RawPatchRequest' => Gitaly::RawPatchRequest.new(
|
|
|
|
repository: repository.gitaly_repository,
|
|
|
|
left_commit_id: 'base',
|
|
|
|
right_commit_id: 'head'
|
|
|
|
).to_json
|
|
|
|
}.deep_stringify_keys)
|
2016-09-20 16:21:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-04 18:32:02 +00:00
|
|
|
describe '.channel_websocket' do
|
2016-11-22 19:55:56 +00:00
|
|
|
def terminal(ca_pem: nil)
|
|
|
|
out = {
|
|
|
|
subprotocols: ['foo'],
|
|
|
|
url: 'wss://example.com/terminal.ws',
|
2017-01-26 18:16:50 +00:00
|
|
|
headers: { 'Authorization' => ['Token x'] },
|
|
|
|
max_session_time: 600
|
2016-11-22 19:55:56 +00:00
|
|
|
}
|
|
|
|
out[:ca_pem] = ca_pem if ca_pem
|
|
|
|
out
|
|
|
|
end
|
|
|
|
|
|
|
|
def workhorse(ca_pem: nil)
|
|
|
|
out = {
|
2019-04-04 18:32:02 +00:00
|
|
|
'Channel' => {
|
2016-11-22 19:55:56 +00:00
|
|
|
'Subprotocols' => ['foo'],
|
|
|
|
'Url' => 'wss://example.com/terminal.ws',
|
2017-01-26 18:16:50 +00:00
|
|
|
'Header' => { 'Authorization' => ['Token x'] },
|
|
|
|
'MaxSessionTime' => 600
|
2016-11-22 19:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 18:32:02 +00:00
|
|
|
out['Channel']['CAPem'] = ca_pem if ca_pem
|
2016-11-22 19:55:56 +00:00
|
|
|
out
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without ca_pem' do
|
2019-04-04 18:32:02 +00:00
|
|
|
subject { described_class.channel_websocket(terminal) }
|
2016-11-22 19:55:56 +00:00
|
|
|
|
|
|
|
it { is_expected.to eq(workhorse) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ca_pem' do
|
2019-04-04 18:32:02 +00:00
|
|
|
subject { described_class.channel_websocket(terminal(ca_pem: "foo")) }
|
2016-11-22 19:55:56 +00:00
|
|
|
|
|
|
|
it { is_expected.to eq(workhorse(ca_pem: "foo")) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-20 16:21:52 +00:00
|
|
|
describe '.send_git_diff' do
|
|
|
|
let(:diff_refs) { double(base_sha: "base", head_sha: "head") }
|
2019-12-12 12:07:33 +00:00
|
|
|
|
2017-10-02 18:51:20 +00:00
|
|
|
subject { described_class.send_git_diff(repository, diff_refs) }
|
2016-09-20 16:21:52 +00:00
|
|
|
|
2018-07-06 10:01:15 +00:00
|
|
|
it 'sets the header correctly' do
|
|
|
|
key, command, params = decode_workhorse_header(subject)
|
2017-10-02 18:51:20 +00:00
|
|
|
|
2018-07-06 10:01:15 +00:00
|
|
|
expect(key).to eq("Gitlab-Workhorse-Send-Data")
|
|
|
|
expect(command).to eq("git-diff")
|
|
|
|
expect(params).to eq({
|
|
|
|
'GitalyServer' => {
|
2021-02-16 15:09:50 +00:00
|
|
|
features: { 'gitaly-feature-enforce-requests-limits' => 'true' },
|
2018-07-06 10:01:15 +00:00
|
|
|
address: Gitlab::GitalyClient.address(project.repository_storage),
|
|
|
|
token: Gitlab::GitalyClient.token(project.repository_storage)
|
|
|
|
},
|
|
|
|
'RawDiffRequest' => Gitaly::RawDiffRequest.new(
|
|
|
|
repository: repository.gitaly_repository,
|
|
|
|
left_commit_id: 'base',
|
|
|
|
right_commit_id: 'head'
|
|
|
|
).to_json
|
|
|
|
}.deep_stringify_keys)
|
2016-09-20 16:21:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-19 17:10:41 +00:00
|
|
|
describe '#verify_api_request!' do
|
2016-09-06 15:26:16 +00:00
|
|
|
let(:header_key) { described_class::INTERNAL_API_REQUEST_HEADER }
|
2016-08-19 17:10:41 +00:00
|
|
|
let(:payload) { { 'iss' => 'gitlab-workhorse' } }
|
|
|
|
|
|
|
|
it 'accepts a correct header' do
|
|
|
|
headers = { header_key => JWT.encode(payload, described_class.secret, 'HS256') }
|
|
|
|
expect { call_verify(headers) }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when the header is not set' do
|
|
|
|
expect { call_verify({}) }.to raise_jwt_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when the header is not signed' do
|
|
|
|
headers = { header_key => JWT.encode(payload, nil, 'none') }
|
|
|
|
expect { call_verify(headers) }.to raise_jwt_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when the header is signed with the wrong key' do
|
|
|
|
headers = { header_key => JWT.encode(payload, 'wrongkey', 'HS256') }
|
|
|
|
expect { call_verify(headers) }.to raise_jwt_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when the issuer is incorrect' do
|
|
|
|
payload['iss'] = 'somebody else'
|
|
|
|
headers = { header_key => JWT.encode(payload, described_class.secret, 'HS256') }
|
|
|
|
expect { call_verify(headers) }.to raise_jwt_error
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_jwt_error
|
|
|
|
raise_error(JWT::DecodeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
def call_verify(headers)
|
|
|
|
described_class.verify_api_request!(headers)
|
|
|
|
end
|
|
|
|
end
|
2016-12-26 17:15:40 +00:00
|
|
|
|
|
|
|
describe '.git_http_ok' do
|
|
|
|
let(:user) { create(:user) }
|
2018-03-29 08:35:33 +00:00
|
|
|
let(:repo_path) { 'ignored but not allowed to be empty in gitlab-workhorse' }
|
2017-03-30 15:24:36 +00:00
|
|
|
let(:action) { 'info_refs' }
|
2017-05-03 21:07:54 +00:00
|
|
|
let(:params) do
|
2017-08-02 18:14:50 +00:00
|
|
|
{
|
|
|
|
GL_ID: "user-#{user.id}",
|
|
|
|
GL_USERNAME: user.username,
|
|
|
|
GL_REPOSITORY: "project-#{project.id}",
|
2017-10-12 00:31:59 +00:00
|
|
|
ShowAllRefs: false
|
2017-08-02 18:14:50 +00:00
|
|
|
}
|
2017-05-03 21:07:54 +00:00
|
|
|
end
|
|
|
|
|
2019-03-18 16:51:11 +00:00
|
|
|
subject { described_class.git_http_ok(repository, Gitlab::GlRepository::PROJECT, user, action) }
|
2017-05-03 21:07:54 +00:00
|
|
|
|
|
|
|
it { expect(subject).to include(params) }
|
2016-12-26 17:15:40 +00:00
|
|
|
|
2019-03-18 16:51:11 +00:00
|
|
|
context 'when the repo_type is a wiki' do
|
2017-05-03 21:07:54 +00:00
|
|
|
let(:params) do
|
2017-08-02 18:14:50 +00:00
|
|
|
{
|
|
|
|
GL_ID: "user-#{user.id}",
|
|
|
|
GL_USERNAME: user.username,
|
|
|
|
GL_REPOSITORY: "wiki-#{project.id}",
|
2017-10-12 00:31:59 +00:00
|
|
|
ShowAllRefs: false
|
2017-08-02 18:14:50 +00:00
|
|
|
}
|
2017-05-03 21:07:54 +00:00
|
|
|
end
|
|
|
|
|
2019-03-18 16:51:11 +00:00
|
|
|
subject { described_class.git_http_ok(repository, Gitlab::GlRepository::WIKI, user, action) }
|
2016-12-26 17:15:40 +00:00
|
|
|
|
2017-05-03 21:07:54 +00:00
|
|
|
it { expect(subject).to include(params) }
|
|
|
|
end
|
2016-12-26 17:15:40 +00:00
|
|
|
|
2017-03-24 17:22:42 +00:00
|
|
|
context 'when Gitaly is enabled' do
|
2017-03-30 15:24:36 +00:00
|
|
|
let(:gitaly_params) do
|
|
|
|
{
|
2017-06-20 14:09:58 +00:00
|
|
|
GitalyServer: {
|
2021-02-16 15:09:50 +00:00
|
|
|
features: { 'gitaly-feature-enforce-requests-limits' => 'true' },
|
2017-06-20 14:09:58 +00:00
|
|
|
address: Gitlab::GitalyClient.address('default'),
|
|
|
|
token: Gitlab::GitalyClient.token('default')
|
|
|
|
}
|
2017-03-30 15:24:36 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-12-26 17:15:40 +00:00
|
|
|
before do
|
2017-03-24 17:22:42 +00:00
|
|
|
allow(Gitlab.config.gitaly).to receive(:enabled).and_return(true)
|
2016-12-26 17:15:40 +00:00
|
|
|
end
|
|
|
|
|
2017-03-30 15:24:36 +00:00
|
|
|
it 'includes a Repository param' do
|
2017-09-28 17:07:22 +00:00
|
|
|
repo_param = {
|
2017-03-30 16:48:22 +00:00
|
|
|
storage_name: 'default',
|
2017-12-01 13:58:49 +00:00
|
|
|
relative_path: project.disk_path + '.git',
|
2017-09-20 22:33:56 +00:00
|
|
|
gl_repository: "project-#{project.id}"
|
2017-09-28 17:07:22 +00:00
|
|
|
}
|
2017-03-30 15:24:36 +00:00
|
|
|
|
2017-09-28 17:07:22 +00:00
|
|
|
expect(subject[:Repository]).to include(repo_param)
|
2017-03-30 15:24:36 +00:00
|
|
|
end
|
|
|
|
|
2017-04-04 11:31:24 +00:00
|
|
|
context "when git_upload_pack action is passed" do
|
|
|
|
let(:action) { 'git_upload_pack' }
|
|
|
|
let(:feature_flag) { :post_upload_pack }
|
|
|
|
|
2017-09-01 08:45:19 +00:00
|
|
|
it 'includes Gitaly params in the returned value' do
|
|
|
|
allow(Gitlab::GitalyClient).to receive(:feature_enabled?).with(feature_flag).and_return(true)
|
2017-04-04 11:31:24 +00:00
|
|
|
|
2017-09-01 08:45:19 +00:00
|
|
|
expect(subject).to include(gitaly_params)
|
2017-03-30 15:24:36 +00:00
|
|
|
end
|
2017-10-12 00:31:59 +00:00
|
|
|
|
|
|
|
context 'show_all_refs enabled' do
|
2019-03-18 16:51:11 +00:00
|
|
|
subject { described_class.git_http_ok(repository, Gitlab::GlRepository::PROJECT, user, action, show_all_refs: true) }
|
2017-10-12 00:31:59 +00:00
|
|
|
|
|
|
|
it { is_expected.to include(ShowAllRefs: true) }
|
|
|
|
end
|
2021-02-16 15:09:50 +00:00
|
|
|
|
|
|
|
context 'when a feature flag is set for a single project' do
|
|
|
|
before do
|
|
|
|
stub_feature_flags(gitaly_mep_mep: project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the flag to true for that project' do
|
|
|
|
response = described_class.git_http_ok(repository, Gitlab::GlRepository::PROJECT, user, action)
|
|
|
|
|
|
|
|
expect(response.dig(:GitalyServer, :features)).to eq('gitaly-feature-enforce-requests-limits' => 'true',
|
|
|
|
'gitaly-feature-mep-mep' => 'true')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the flag to false for other projects' do
|
|
|
|
other_project = create(:project, :public, :repository)
|
|
|
|
response = described_class.git_http_ok(other_project.repository, Gitlab::GlRepository::PROJECT, user, action)
|
|
|
|
|
|
|
|
expect(response.dig(:GitalyServer, :features)).to eq('gitaly-feature-enforce-requests-limits' => 'true',
|
|
|
|
'gitaly-feature-mep-mep' => 'false')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the flag to false when there is no project' do
|
|
|
|
snippet = create(:personal_snippet, :repository)
|
|
|
|
response = described_class.git_http_ok(snippet.repository, Gitlab::GlRepository::SNIPPET, user, action)
|
|
|
|
|
|
|
|
expect(response.dig(:GitalyServer, :features)).to eq('gitaly-feature-enforce-requests-limits' => 'true',
|
|
|
|
'gitaly-feature-mep-mep' => 'false')
|
|
|
|
end
|
|
|
|
end
|
2017-03-30 15:24:36 +00:00
|
|
|
end
|
|
|
|
|
2017-04-04 11:31:24 +00:00
|
|
|
context "when git_receive_pack action is passed" do
|
|
|
|
let(:action) { 'git_receive_pack' }
|
|
|
|
|
2017-05-31 02:16:43 +00:00
|
|
|
it { expect(subject).to include(gitaly_params) }
|
2017-04-04 11:31:24 +00:00
|
|
|
end
|
|
|
|
|
2017-03-30 15:24:36 +00:00
|
|
|
context "when info_refs action is passed" do
|
|
|
|
let(:action) { 'info_refs' }
|
|
|
|
|
|
|
|
it { expect(subject).to include(gitaly_params) }
|
2017-10-12 00:31:59 +00:00
|
|
|
|
|
|
|
context 'show_all_refs enabled' do
|
2019-03-18 16:51:11 +00:00
|
|
|
subject { described_class.git_http_ok(repository, Gitlab::GlRepository::PROJECT, user, action, show_all_refs: true) }
|
2017-10-12 00:31:59 +00:00
|
|
|
|
|
|
|
it { is_expected.to include(ShowAllRefs: true) }
|
|
|
|
end
|
2017-03-30 15:24:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when action passed is not supported by Gitaly' do
|
|
|
|
let(:action) { 'download' }
|
|
|
|
|
|
|
|
it { expect { subject }.to raise_exception('Unsupported action: download') }
|
2016-12-26 17:15:40 +00:00
|
|
|
end
|
|
|
|
end
|
2018-08-01 14:47:14 +00:00
|
|
|
|
|
|
|
context 'when receive_max_input_size has been updated' do
|
|
|
|
it 'returns custom git config' do
|
|
|
|
allow(Gitlab::CurrentSettings).to receive(:receive_max_input_size) { 1 }
|
|
|
|
|
|
|
|
expect(subject[:GitConfigOptions]).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when receive_max_input_size is empty' do
|
|
|
|
it 'returns an empty git config' do
|
|
|
|
allow(Gitlab::CurrentSettings).to receive(:receive_max_input_size) { nil }
|
|
|
|
|
|
|
|
expect(subject[:GitConfigOptions]).to be_empty
|
|
|
|
end
|
|
|
|
end
|
2016-12-26 17:15:40 +00:00
|
|
|
end
|
2017-03-03 12:51:49 +00:00
|
|
|
|
2017-03-06 10:44:45 +00:00
|
|
|
describe '.set_key_and_notify' do
|
2017-03-03 12:51:49 +00:00
|
|
|
let(:key) { 'test-key' }
|
|
|
|
let(:value) { 'test-value' }
|
|
|
|
|
2017-03-06 10:44:45 +00:00
|
|
|
subject { described_class.set_key_and_notify(key, value, overwrite: overwrite) }
|
2017-03-03 12:51:49 +00:00
|
|
|
|
|
|
|
shared_examples 'set and notify' do
|
|
|
|
it 'set and return the same value' do
|
|
|
|
is_expected.to eq(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'set and notify' do
|
2019-07-22 06:00:37 +00:00
|
|
|
expect(Gitlab::Redis::SharedState).to receive(:with).and_call_original
|
2017-07-11 03:35:47 +00:00
|
|
|
expect_any_instance_of(::Redis).to receive(:publish)
|
2017-03-03 12:51:49 +00:00
|
|
|
.with(described_class::NOTIFICATION_CHANNEL, "test-key=test-value")
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when we set a new key' do
|
|
|
|
let(:overwrite) { true }
|
|
|
|
|
|
|
|
it_behaves_like 'set and notify'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when we set an existing key' do
|
|
|
|
let(:old_value) { 'existing-key' }
|
|
|
|
|
|
|
|
before do
|
2017-03-06 13:22:23 +00:00
|
|
|
described_class.set_key_and_notify(key, old_value, overwrite: true)
|
2017-03-03 12:51:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'and overwrite' do
|
|
|
|
let(:overwrite) { true }
|
|
|
|
|
|
|
|
it_behaves_like 'set and notify'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'and do not overwrite' do
|
|
|
|
let(:overwrite) { false }
|
|
|
|
|
|
|
|
it 'try to set but return the previous value' do
|
|
|
|
is_expected.to eq(old_value)
|
|
|
|
end
|
|
|
|
|
2017-03-06 10:44:45 +00:00
|
|
|
it 'does not notify' do
|
2017-07-11 03:35:47 +00:00
|
|
|
expect_any_instance_of(::Redis).not_to receive(:publish)
|
2017-03-03 12:51:49 +00:00
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-07-10 03:43:20 +00:00
|
|
|
|
|
|
|
describe '.send_git_blob' do
|
|
|
|
include FakeBlobHelpers
|
|
|
|
|
|
|
|
let(:blob) { fake_blob }
|
|
|
|
|
|
|
|
subject { described_class.send_git_blob(repository, blob) }
|
|
|
|
|
2018-07-09 10:02:02 +00:00
|
|
|
it 'sets the header correctly' do
|
|
|
|
key, command, params = decode_workhorse_header(subject)
|
2017-07-10 03:43:20 +00:00
|
|
|
|
2018-07-09 10:02:02 +00:00
|
|
|
expect(key).to eq('Gitlab-Workhorse-Send-Data')
|
|
|
|
expect(command).to eq('git-blob')
|
|
|
|
expect(params).to eq({
|
|
|
|
'GitalyServer' => {
|
2021-02-16 15:09:50 +00:00
|
|
|
features: { 'gitaly-feature-enforce-requests-limits' => 'true' },
|
2018-07-09 10:02:02 +00:00
|
|
|
address: Gitlab::GitalyClient.address(project.repository_storage),
|
|
|
|
token: Gitlab::GitalyClient.token(project.repository_storage)
|
|
|
|
},
|
|
|
|
'GetBlobRequest' => {
|
|
|
|
repository: repository.gitaly_repository.to_h,
|
|
|
|
oid: blob.id,
|
|
|
|
limit: -1
|
|
|
|
}
|
|
|
|
}.deep_stringify_keys)
|
2017-07-10 03:43:20 +00:00
|
|
|
end
|
|
|
|
end
|
2018-02-06 14:31:39 +00:00
|
|
|
|
|
|
|
describe '.send_url' do
|
|
|
|
let(:url) { 'http://example.com' }
|
|
|
|
|
|
|
|
subject { described_class.send_url(url) }
|
|
|
|
|
|
|
|
it 'sets the header correctly' do
|
|
|
|
key, command, params = decode_workhorse_header(subject)
|
|
|
|
|
|
|
|
expect(key).to eq("Gitlab-Workhorse-Send-Data")
|
|
|
|
expect(command).to eq("send-url")
|
|
|
|
expect(params).to eq({
|
|
|
|
'URL' => url,
|
|
|
|
'AllowRedirects' => false
|
|
|
|
}.deep_stringify_keys)
|
|
|
|
end
|
|
|
|
end
|
2018-04-03 17:57:55 +00:00
|
|
|
|
2020-08-10 18:09:54 +00:00
|
|
|
describe '.send_scaled_image' do
|
|
|
|
let(:location) { 'http://example.com/avatar.png' }
|
|
|
|
let(:width) { '150' }
|
2020-08-28 06:10:45 +00:00
|
|
|
let(:content_type) { 'image/png' }
|
2020-08-10 18:09:54 +00:00
|
|
|
|
2020-08-28 06:10:45 +00:00
|
|
|
subject { described_class.send_scaled_image(location, width, content_type) }
|
2020-08-10 18:09:54 +00:00
|
|
|
|
|
|
|
it 'sets the header correctly' do
|
|
|
|
key, command, params = decode_workhorse_header(subject)
|
|
|
|
|
|
|
|
expect(key).to eq("Gitlab-Workhorse-Send-Data")
|
|
|
|
expect(command).to eq("send-scaled-img")
|
|
|
|
expect(params).to eq({
|
|
|
|
'Location' => location,
|
2020-08-28 06:10:45 +00:00
|
|
|
'Width' => width,
|
|
|
|
'ContentType' => content_type
|
2020-08-10 18:09:54 +00:00
|
|
|
}.deep_stringify_keys)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-03 17:57:55 +00:00
|
|
|
describe '.send_git_snapshot' do
|
|
|
|
let(:url) { 'http://example.com' }
|
|
|
|
|
|
|
|
subject(:request) { described_class.send_git_snapshot(repository) }
|
|
|
|
|
|
|
|
it 'sets the header correctly' do
|
|
|
|
key, command, params = decode_workhorse_header(request)
|
|
|
|
|
|
|
|
expect(key).to eq("Gitlab-Workhorse-Send-Data")
|
|
|
|
expect(command).to eq('git-snapshot')
|
|
|
|
expect(params).to eq(
|
|
|
|
'GitalyServer' => {
|
2021-02-16 15:09:50 +00:00
|
|
|
'features' => { 'gitaly-feature-enforce-requests-limits' => 'true' },
|
2018-04-03 17:57:55 +00:00
|
|
|
'address' => Gitlab::GitalyClient.address(project.repository_storage),
|
|
|
|
'token' => Gitlab::GitalyClient.token(project.repository_storage)
|
|
|
|
},
|
|
|
|
'GetSnapshotRequest' => Gitaly::GetSnapshotRequest.new(
|
|
|
|
repository: repository.gitaly_repository
|
|
|
|
).to_json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2016-02-02 13:09:55 +00:00
|
|
|
end
|