2019-08-22 10:57:44 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-09-07 05:16:29 +00:00
|
|
|
require 'spec_helper'
|
2016-09-16 09:43:05 +00:00
|
|
|
require 'stringio'
|
2012-09-07 05:16:29 +00:00
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe Gitlab::Shell do
|
2020-02-20 18:08:51 +00:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
2018-01-05 02:34:31 +00:00
|
|
|
let(:repository) { project.repository }
|
2017-07-25 17:09:00 +00:00
|
|
|
let(:gitlab_shell) { described_class.new }
|
2012-09-07 05:16:29 +00:00
|
|
|
|
2015-02-12 18:17:35 +00:00
|
|
|
it { is_expected.to respond_to :remove_repository }
|
2012-09-07 05:16:29 +00:00
|
|
|
|
2016-09-29 16:46:54 +00:00
|
|
|
describe 'memoized secret_token' do
|
2016-06-24 19:06:46 +00:00
|
|
|
let(:secret_file) { 'tmp/tests/.secret_shell_test' }
|
|
|
|
let(:link_file) { 'tmp/tests/shell-secret-test/.gitlab_shell_secret' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab.config.gitlab_shell).to receive(:secret_file).and_return(secret_file)
|
2016-09-29 16:46:54 +00:00
|
|
|
allow(Gitlab.config.gitlab_shell).to receive(:path).and_return('tmp/tests/shell-secret-test')
|
2016-06-24 19:06:46 +00:00
|
|
|
FileUtils.mkdir('tmp/tests/shell-secret-test')
|
2017-07-25 17:09:00 +00:00
|
|
|
described_class.ensure_secret_token!
|
2016-06-24 19:06:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
FileUtils.rm_rf('tmp/tests/shell-secret-test')
|
|
|
|
FileUtils.rm_rf(secret_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates and links the secret token file' do
|
2017-07-25 17:09:00 +00:00
|
|
|
secret_token = described_class.secret_token
|
2016-09-29 16:46:54 +00:00
|
|
|
|
2016-06-24 19:06:46 +00:00
|
|
|
expect(File.exist?(secret_file)).to be(true)
|
2016-09-29 16:46:54 +00:00
|
|
|
expect(File.read(secret_file).chomp).to eq(secret_token)
|
2016-06-24 19:06:46 +00:00
|
|
|
expect(File.symlink?(link_file)).to be(true)
|
|
|
|
expect(File.readlink(link_file)).to eq(secret_file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-14 01:53:30 +00:00
|
|
|
describe 'projects commands' do
|
2017-08-14 11:29:47 +00:00
|
|
|
let(:gitlab_shell_path) { File.expand_path('tmp/tests/gitlab-shell') }
|
|
|
|
let(:projects_path) { File.join(gitlab_shell_path, 'bin/gitlab-projects') }
|
2017-04-14 01:53:30 +00:00
|
|
|
|
|
|
|
before do
|
2017-08-14 11:29:47 +00:00
|
|
|
allow(Gitlab.config.gitlab_shell).to receive(:path).and_return(gitlab_shell_path)
|
2017-04-14 01:53:30 +00:00
|
|
|
allow(Gitlab.config.gitlab_shell).to receive(:git_timeout).and_return(800)
|
|
|
|
end
|
|
|
|
|
2017-07-03 18:28:29 +00:00
|
|
|
describe '#remove_repository' do
|
2017-12-01 13:58:49 +00:00
|
|
|
let!(:project) { create(:project, :repository, :legacy_storage) }
|
2018-01-05 12:29:04 +00:00
|
|
|
let(:disk_path) { "#{project.disk_path}.git" }
|
2017-12-11 17:52:07 +00:00
|
|
|
|
2017-07-03 18:28:29 +00:00
|
|
|
it 'returns true when the command succeeds' do
|
2019-10-23 09:06:03 +00:00
|
|
|
expect(TestEnv.storage_dir_exists?(project.repository_storage, disk_path)).to be(true)
|
2017-07-03 18:28:29 +00:00
|
|
|
|
2018-04-13 10:57:19 +00:00
|
|
|
expect(gitlab_shell.remove_repository(project.repository_storage, project.disk_path)).to be(true)
|
2018-01-05 12:29:04 +00:00
|
|
|
|
2019-10-23 09:06:03 +00:00
|
|
|
expect(TestEnv.storage_dir_exists?(project.repository_storage, disk_path)).to be(false)
|
2017-07-03 18:28:29 +00:00
|
|
|
end
|
|
|
|
|
2018-01-05 12:29:04 +00:00
|
|
|
it 'keeps the namespace directory' do
|
2018-04-13 10:57:19 +00:00
|
|
|
gitlab_shell.remove_repository(project.repository_storage, project.disk_path)
|
2017-07-03 18:28:29 +00:00
|
|
|
|
2019-10-23 09:06:03 +00:00
|
|
|
expect(TestEnv.storage_dir_exists?(project.repository_storage, disk_path)).to be(false)
|
|
|
|
expect(TestEnv.storage_dir_exists?(project.repository_storage, project.disk_path.gsub(project.name, ''))).to be(true)
|
2017-07-03 18:28:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#mv_repository' do
|
2018-01-05 12:29:04 +00:00
|
|
|
let!(:project2) { create(:project, :repository) }
|
|
|
|
|
2017-07-03 18:28:29 +00:00
|
|
|
it 'returns true when the command succeeds' do
|
2018-01-05 12:29:04 +00:00
|
|
|
old_path = project2.disk_path
|
|
|
|
new_path = "project/new_path"
|
2017-07-03 18:28:29 +00:00
|
|
|
|
2019-10-23 09:06:03 +00:00
|
|
|
expect(TestEnv.storage_dir_exists?(project2.repository_storage, "#{old_path}.git")).to be(true)
|
|
|
|
expect(TestEnv.storage_dir_exists?(project2.repository_storage, "#{new_path}.git")).to be(false)
|
2018-01-05 12:29:04 +00:00
|
|
|
|
2018-04-13 10:57:19 +00:00
|
|
|
expect(gitlab_shell.mv_repository(project2.repository_storage, old_path, new_path)).to be_truthy
|
2018-01-05 12:29:04 +00:00
|
|
|
|
2019-10-23 09:06:03 +00:00
|
|
|
expect(TestEnv.storage_dir_exists?(project2.repository_storage, "#{old_path}.git")).to be(false)
|
|
|
|
expect(TestEnv.storage_dir_exists?(project2.repository_storage, "#{new_path}.git")).to be(true)
|
2017-07-03 18:28:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when the command fails' do
|
2018-04-13 10:57:19 +00:00
|
|
|
expect(gitlab_shell.mv_repository(project2.repository_storage, project2.disk_path, '')).to be_falsy
|
2019-10-23 09:06:03 +00:00
|
|
|
expect(TestEnv.storage_dir_exists?(project2.repository_storage, "#{project2.disk_path}.git")).to be(true)
|
2017-07-03 18:28:29 +00:00
|
|
|
end
|
|
|
|
end
|
2017-04-14 01:53:30 +00:00
|
|
|
end
|
2017-09-12 10:26:59 +00:00
|
|
|
|
|
|
|
describe 'namespace actions' do
|
|
|
|
subject { described_class.new }
|
2019-10-18 11:11:44 +00:00
|
|
|
|
2020-02-13 15:08:52 +00:00
|
|
|
let(:storage) { Gitlab.config.repositories.storages.each_key.first }
|
2017-09-12 10:26:59 +00:00
|
|
|
|
|
|
|
describe '#add_namespace' do
|
|
|
|
it 'creates a namespace' do
|
2019-11-18 18:06:53 +00:00
|
|
|
Gitlab::GitalyClient::NamespaceService.allow { subject.add_namespace(storage, "mepmep") }
|
2017-09-12 10:26:59 +00:00
|
|
|
|
2019-10-23 09:06:03 +00:00
|
|
|
expect(TestEnv.storage_dir_exists?(storage, "mepmep")).to be(true)
|
2017-09-12 10:26:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-23 09:06:03 +00:00
|
|
|
describe '#repository_exists?' do
|
|
|
|
context 'when the repository does not exist' do
|
2017-09-12 10:26:59 +00:00
|
|
|
it 'returns false' do
|
2019-10-23 09:06:03 +00:00
|
|
|
expect(subject.repository_exists?(storage, "non-existing.git")).to be(false)
|
2017-09-12 10:26:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-23 09:06:03 +00:00
|
|
|
context 'when the repository exists' do
|
2017-09-12 10:26:59 +00:00
|
|
|
it 'returns true' do
|
2019-10-23 09:06:03 +00:00
|
|
|
project = create(:project, :repository, :legacy_storage)
|
2017-09-12 10:26:59 +00:00
|
|
|
|
2019-10-23 09:06:03 +00:00
|
|
|
expect(subject.repository_exists?(storage, project.repository.disk_path + ".git")).to be(true)
|
2017-09-12 10:26:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#remove' do
|
|
|
|
it 'removes the namespace' do
|
2019-11-18 18:06:53 +00:00
|
|
|
Gitlab::GitalyClient::NamespaceService.allow do
|
|
|
|
subject.add_namespace(storage, "mepmep")
|
|
|
|
subject.rm_namespace(storage, "mepmep")
|
|
|
|
end
|
2017-09-12 10:26:59 +00:00
|
|
|
|
2019-10-23 09:06:03 +00:00
|
|
|
expect(TestEnv.storage_dir_exists?(storage, "mepmep")).to be(false)
|
2017-09-12 10:26:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#mv_namespace' do
|
|
|
|
it 'renames the namespace' do
|
2019-11-18 18:06:53 +00:00
|
|
|
Gitlab::GitalyClient::NamespaceService.allow do
|
|
|
|
subject.add_namespace(storage, "mepmep")
|
|
|
|
subject.mv_namespace(storage, "mepmep", "2mep")
|
|
|
|
end
|
2017-09-12 10:26:59 +00:00
|
|
|
|
2019-10-23 09:06:03 +00:00
|
|
|
expect(TestEnv.storage_dir_exists?(storage, "mepmep")).to be(false)
|
|
|
|
expect(TestEnv.storage_dir_exists?(storage, "2mep")).to be(true)
|
2017-09-12 10:26:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-09-07 05:16:29 +00:00
|
|
|
end
|