destroy_service_spec.rb 5.72 KB
Newer Older
1
2
require 'spec_helper'

3
describe Groups::DestroyService do
4
5
  include DatabaseConnectionHelpers

6
7
  let!(:user)         { create(:user) }
  let!(:group)        { create(:group) }
8
  let!(:nested_group) { create(:group, parent: group) }
Nick Thomas's avatar
Nick Thomas committed
9
  let!(:project)      { create(:project, :legacy_storage, namespace: group) }
James Lopez's avatar
James Lopez committed
10
  let!(:notification_setting) { create(:notification_setting, source: group)}
11
12
  let(:gitlab_shell) { Gitlab::Shell.new }
  let(:remove_path)  { group.path + "+#{group.id}+deleted" }
13

14
15
16
17
  before do
    group.add_user(user, Gitlab::Access::OWNER)
  end

18
19
20
21
22
23
24
25
  def destroy_group(group, user, async)
    if async
      Groups::DestroyService.new(group, user).async_execute
    else
      Groups::DestroyService.new(group, user).execute
    end
  end

26
27
28
29
30
31
  shared_examples 'group destruction' do |async|
    context 'database records' do
      before do
        destroy_group(group, user, async)
      end

32
      it { expect(Group.unscoped.all).not_to include(group) }
33
      it { expect(Group.unscoped.all).not_to include(nested_group) }
34
      it { expect(Project.unscoped.all).not_to include(project) }
James Lopez's avatar
James Lopez committed
35
      it { expect(NotificationSetting.unscoped.all).not_to include(notification_setting) }
36
37
    end

38
39
40
41
42
43
44
45
46
47
    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

48
49
50
    context 'file system' do
      context 'Sidekiq inline' do
        before do
51
          # Run sidekiq immediately to check that renamed dir will be removed
52
          perform_enqueued_jobs { destroy_group(group, user, async) }
53
        end
54

55
        it 'verifies that paths have been deleted' do
56
57
          expect(gitlab_shell.exists?(project.repository_storage, group.path)).to be_falsey
          expect(gitlab_shell.exists?(project.repository_storage, remove_path)).to be_falsey
58
59
        end
      end
60
61
62
    end
  end

63
64
  describe 'asynchronous delete' do
    it_behaves_like 'group destruction', true
65

66
67
68
69
70
71
72
73
    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
74
75
        gitlab_shell.rm_namespace(project.repository_storage, group.path)
        gitlab_shell.rm_namespace(project.repository_storage, remove_path)
76
77
78
      end

      it 'verifies original paths and projects still exist' do
79
80
        expect(gitlab_shell.exists?(project.repository_storage, group.path)).to be_truthy
        expect(gitlab_shell.exists?(project.repository_storage, remove_path)).to be_falsey
81
82
83
84
85
        expect(Project.unscoped.count).to eq(1)
        expect(Group.unscoped.count).to eq(2)
      end
    end

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    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.
113
          Thread.new { described_class.new(group, user).async_execute }.join(5)
114
115
116
117
118
119
120
121
122

          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
123
124
125
126
  end

  describe 'synchronous delete' do
    it_behaves_like 'group destruction', false
127
  end
128
129
130
131
132
133
134
135
136

  context 'projects in pending_delete' do
    before do
      project.pending_delete = true
      project.save
    end

    it_behaves_like 'group destruction', false
  end
137

138
139
140
141
142
143
144
145
146
147
148
  context 'repository removal status is taken into account' do
    it 'raises exception' do
      expect_next_instance_of(::Projects::DestroyService) do |destroy_service|
        expect(destroy_service).to receive(:execute).and_return(false)
      end

      expect { destroy_group(group, user, false) }
        .to raise_error(Groups::DestroyService::DestroyError, "Project #{project.id} can't be deleted" )
    end
  end

149
150
151
152
153
154
  describe 'repository removal' do
    before do
      destroy_group(group, user, false)
    end

    context 'legacy storage' do
Nick Thomas's avatar
Nick Thomas committed
155
      let!(:project) { create(:project, :legacy_storage, :empty_repo, namespace: group) }
156
157

      it 'removes repository' do
158
        expect(gitlab_shell.exists?(project.repository_storage, "#{project.disk_path}.git")).to be_falsey
159
160
161
162
      end
    end

    context 'hashed storage' do
Nick Thomas's avatar
Nick Thomas committed
163
      let!(:project) { create(:project, :empty_repo, namespace: group) }
164
165

      it 'removes repository' do
166
        expect(gitlab_shell.exists?(project.repository_storage, "#{project.disk_path}.git")).to be_falsey
167
168
169
      end
    end
  end
170
end