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