2015-06-03 10:16:27 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Groups::DestroyService do
|
2016-11-17 08:53:42 -05:00
|
|
|
include DatabaseConnectionHelpers
|
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
let!(:user) { create(:user) }
|
|
|
|
let!(:group) { create(:group) }
|
2017-02-27 07:28:02 -05:00
|
|
|
let!(:nested_group) { create(:group, parent: group) }
|
2017-12-01 08:58:49 -05:00
|
|
|
let!(:project) { create(:project, :legacy_storage, namespace: group) }
|
2017-04-19 04:15:52 -04:00
|
|
|
let!(:notification_setting) { create(:notification_setting, source: group)}
|
2017-08-21 09:28:07 -04:00
|
|
|
let(:gitlab_shell) { Gitlab::Shell.new }
|
|
|
|
let(:remove_path) { group.path + "+#{group.id}+deleted" }
|
2015-06-03 10:16:27 -04:00
|
|
|
|
2017-02-16 02:50:05 -05:00
|
|
|
before do
|
|
|
|
group.add_user(user, Gitlab::Access::OWNER)
|
|
|
|
end
|
|
|
|
|
2017-06-23 20:02:33 -04:00
|
|
|
def destroy_group(group, user, async)
|
|
|
|
if async
|
|
|
|
Groups::DestroyService.new(group, user).async_execute
|
|
|
|
else
|
|
|
|
Groups::DestroyService.new(group, user).execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-28 22:54:17 -04:00
|
|
|
shared_examples 'group destruction' do |async|
|
|
|
|
context 'database records' do
|
|
|
|
before do
|
|
|
|
destroy_group(group, user, async)
|
|
|
|
end
|
|
|
|
|
2017-02-16 02:50:05 -05:00
|
|
|
it { expect(Group.unscoped.all).not_to include(group) }
|
2017-02-27 07:28:02 -05:00
|
|
|
it { expect(Group.unscoped.all).not_to include(nested_group) }
|
2017-02-16 02:50:05 -05:00
|
|
|
it { expect(Project.unscoped.all).not_to include(project) }
|
2017-04-19 04:15:52 -04:00
|
|
|
it { expect(NotificationSetting.unscoped.all).not_to include(notification_setting) }
|
2015-06-03 10:16:27 -04:00
|
|
|
end
|
|
|
|
|
2017-05-14 06:57:08 -04:00
|
|
|
context 'mattermost team' do
|
|
|
|
let!(:chat_team) { create(:chat_team, namespace: group) }
|
|
|
|
|
|
|
|
it 'destroys the team too' do
|
|
|
|
expect_any_instance_of(Mattermost::Team).to receive(:destroy)
|
|
|
|
|
|
|
|
destroy_group(group, user, async)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-28 22:54:17 -04:00
|
|
|
context 'file system' do
|
|
|
|
context 'Sidekiq inline' do
|
|
|
|
before do
|
2017-06-23 20:02:33 -04:00
|
|
|
# Run sidekiq immediately to check that renamed dir will be removed
|
2016-05-28 22:54:17 -04:00
|
|
|
Sidekiq::Testing.inline! { destroy_group(group, user, async) }
|
|
|
|
end
|
2015-06-03 10:16:27 -04:00
|
|
|
|
2017-06-23 20:02:33 -04:00
|
|
|
it 'verifies that paths have been deleted' do
|
2018-04-13 06:57:19 -04:00
|
|
|
expect(gitlab_shell.exists?(project.repository_storage, group.path)).to be_falsey
|
|
|
|
expect(gitlab_shell.exists?(project.repository_storage, remove_path)).to be_falsey
|
2016-05-28 22:54:17 -04:00
|
|
|
end
|
|
|
|
end
|
2015-06-03 10:16:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-28 22:54:17 -04:00
|
|
|
describe 'asynchronous delete' do
|
|
|
|
it_behaves_like 'group destruction', true
|
2016-11-17 08:53:42 -05:00
|
|
|
|
2017-06-23 20:02:33 -04:00
|
|
|
context 'Sidekiq fake' do
|
|
|
|
before do
|
|
|
|
# Don't run Sidekiq to verify that group and projects are not actually destroyed
|
|
|
|
Sidekiq::Testing.fake! { destroy_group(group, user, true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
# Clean up stale directories
|
2018-04-13 06:57:19 -04:00
|
|
|
gitlab_shell.rm_namespace(project.repository_storage, group.path)
|
|
|
|
gitlab_shell.rm_namespace(project.repository_storage, remove_path)
|
2017-06-23 20:02:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'verifies original paths and projects still exist' do
|
2018-04-13 06:57:19 -04:00
|
|
|
expect(gitlab_shell.exists?(project.repository_storage, group.path)).to be_truthy
|
|
|
|
expect(gitlab_shell.exists?(project.repository_storage, remove_path)).to be_falsey
|
2017-06-23 20:02:33 -04:00
|
|
|
expect(Project.unscoped.count).to eq(1)
|
|
|
|
expect(Group.unscoped.count).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-17 08:53:42 -05:00
|
|
|
context 'potential race conditions' do
|
|
|
|
context "when the `GroupDestroyWorker` task runs immediately" do
|
|
|
|
it "deletes the group" do
|
|
|
|
# Commit the contents of this spec's transaction so far
|
|
|
|
# so subsequent db connections can see it.
|
|
|
|
#
|
|
|
|
# DO NOT REMOVE THIS LINE, even if you see a WARNING with "No
|
|
|
|
# transaction is currently in progress". Without this, this
|
|
|
|
# spec will always be green, since the group created in setup
|
|
|
|
# cannot be seen by any other connections / threads in this spec.
|
|
|
|
Group.connection.commit_db_transaction
|
|
|
|
|
|
|
|
group_record = run_with_new_database_connection do |conn|
|
|
|
|
conn.execute("SELECT * FROM namespaces WHERE id = #{group.id}").first
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(group_record).not_to be_nil
|
|
|
|
|
|
|
|
# Execute the contents of `GroupDestroyWorker` in a separate thread, to
|
|
|
|
# simulate data manipulation by the Sidekiq worker (different database
|
|
|
|
# connection / transaction).
|
|
|
|
expect(GroupDestroyWorker).to receive(:perform_async).and_wrap_original do |m, group_id, user_id|
|
|
|
|
Thread.new { m[group_id, user_id] }.join(5)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Kick off the initial group destroy in a new thread, so that
|
|
|
|
# it doesn't share this spec's database transaction.
|
2017-07-25 13:09:00 -04:00
|
|
|
Thread.new { described_class.new(group, user).async_execute }.join(5)
|
2016-11-17 08:53:42 -05:00
|
|
|
|
|
|
|
group_record = run_with_new_database_connection do |conn|
|
|
|
|
conn.execute("SELECT * FROM namespaces WHERE id = #{group.id}").first
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(group_record).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-05-28 22:54:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'synchronous delete' do
|
|
|
|
it_behaves_like 'group destruction', false
|
2015-06-03 10:16:27 -04:00
|
|
|
end
|
2017-02-16 02:50:05 -05:00
|
|
|
|
|
|
|
context 'projects in pending_delete' do
|
|
|
|
before do
|
|
|
|
project.pending_delete = true
|
|
|
|
project.save
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'group destruction', false
|
|
|
|
end
|
2017-08-21 09:28:07 -04:00
|
|
|
|
|
|
|
describe 'repository removal' do
|
|
|
|
before do
|
|
|
|
destroy_group(group, user, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'legacy storage' do
|
2017-12-01 08:58:49 -05:00
|
|
|
let!(:project) { create(:project, :legacy_storage, :empty_repo, namespace: group) }
|
2017-08-21 09:28:07 -04:00
|
|
|
|
|
|
|
it 'removes repository' do
|
2018-04-13 06:57:19 -04:00
|
|
|
expect(gitlab_shell.exists?(project.repository_storage, "#{project.disk_path}.git")).to be_falsey
|
2017-08-21 09:28:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'hashed storage' do
|
2017-12-01 08:58:49 -05:00
|
|
|
let!(:project) { create(:project, :empty_repo, namespace: group) }
|
2017-08-21 09:28:07 -04:00
|
|
|
|
|
|
|
it 'removes repository' do
|
2018-04-13 06:57:19 -04:00
|
|
|
expect(gitlab_shell.exists?(project.repository_storage, "#{project.disk_path}.git")).to be_falsey
|
2017-08-21 09:28:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-06-03 10:16:27 -04:00
|
|
|
end
|