destroy_service_spec.rb 5.95 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
    context 'site statistics' do
      it 'doesnt trigger project deletion hooks twice' do
        expect_any_instance_of(Project).to receive(:untrack_site_statistics).once

        destroy_group(group, user, async)
      end
    end

46
47
48
49
50
51
52
53
54
55
    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

56
57
58
    context 'file system' do
      context 'Sidekiq inline' do
        before do
59
          # Run sidekiq immediately to check that renamed dir will be removed
60
          perform_enqueued_jobs { destroy_group(group, user, async) }
61
        end
62

63
        it 'verifies that paths have been deleted' do
64
65
          expect(gitlab_shell.exists?(project.repository_storage, group.path)).to be_falsey
          expect(gitlab_shell.exists?(project.repository_storage, remove_path)).to be_falsey
66
67
        end
      end
68
69
70
    end
  end

71
72
  describe 'asynchronous delete' do
    it_behaves_like 'group destruction', true
73

74
75
76
77
78
79
80
81
    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
82
83
        gitlab_shell.rm_namespace(project.repository_storage, group.path)
        gitlab_shell.rm_namespace(project.repository_storage, remove_path)
84
85
86
      end

      it 'verifies original paths and projects still exist' do
87
88
        expect(gitlab_shell.exists?(project.repository_storage, group.path)).to be_truthy
        expect(gitlab_shell.exists?(project.repository_storage, remove_path)).to be_falsey
89
90
91
92
93
        expect(Project.unscoped.count).to eq(1)
        expect(Group.unscoped.count).to eq(2)
      end
    end

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    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.
121
          Thread.new { described_class.new(group, user).async_execute }.join(5)
122
123
124
125
126
127
128
129
130

          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
131
132
133
134
  end

  describe 'synchronous delete' do
    it_behaves_like 'group destruction', false
135
  end
136
137
138
139
140
141
142
143
144

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

    it_behaves_like 'group destruction', false
  end
145

146
147
148
149
150
151
152
153
154
155
156
  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

157
158
159
160
161
162
  describe 'repository removal' do
    before do
      destroy_group(group, user, false)
    end

    context 'legacy storage' do
Nick Thomas's avatar
Nick Thomas committed
163
      let!(:project) { create(:project, :legacy_storage, :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
170
      end
    end

    context 'hashed storage' do
Nick Thomas's avatar
Nick Thomas committed
171
      let!(:project) { create(:project, :empty_repo, namespace: group) }
172
173

      it 'removes repository' do
174
        expect(gitlab_shell.exists?(project.repository_storage, "#{project.disk_path}.git")).to be_falsey
175
176
177
      end
    end
  end
178
end