summaryrefslogtreecommitdiff
path: root/spec/tasks/gitlab/cleanup_rake_spec.rb
blob: 4aee6d005a8cb093716de68ca53e17f9e7700aa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
require 'rake_helper'

describe 'gitlab:cleanup rake tasks' do
  before do
    Rake.application.rake_require 'tasks/gitlab/cleanup'
  end

  describe 'cleanup namespaces and repos' do
    let(:gitlab_shell) { Gitlab::Shell.new }
    let(:storage) { storages.keys.first }
    let(:storages) do
      {
        'default' => Gitlab::GitalyClient::StorageSettings.new(@default_storage_hash.merge('path' => 'tmp/tests/default_storage'))
      }
    end

    before(:all) do
      @default_storage_hash = Gitlab.config.repositories.storages.default.to_h
    end

    before do
      allow(Gitlab.config.repositories).to receive(:storages).and_return(storages)
    end

    after do
      Gitlab::GitalyClient::StorageService.new(storage).delete_all_repositories
    end

    describe 'cleanup:repos' do
      before do
        gitlab_shell.add_namespace(storage, 'broken/project.git')
        gitlab_shell.add_namespace(storage, '@hashed/12/34/5678.git')
      end

      it 'moves it to an orphaned path' do
        now = Time.now

        Timecop.freeze(now) do
          run_rake_task('gitlab:cleanup:repos')
          repo_list = Gitlab::GitalyClient::StorageService.new(storage).list_directories(depth: 0)

          expect(repo_list.last).to include("broken+orphaned+#{now.to_i}")
        end
      end

      it 'ignores @hashed repos' do
        run_rake_task('gitlab:cleanup:repos')

        expect(gitlab_shell.exists?(storage, '@hashed/12/34/5678.git')).to be(true)
      end
    end

    describe 'cleanup:dirs' do
      it 'removes missing namespaces' do
        gitlab_shell.add_namespace(storage, "namespace_1/project.git")
        gitlab_shell.add_namespace(storage, "namespace_2/project.git")
        allow(Namespace).to receive(:pluck).and_return(['namespace_1'])

        stub_env('REMOVE', 'true')
        run_rake_task('gitlab:cleanup:dirs')

        expect(gitlab_shell.exists?(storage, 'namespace_1')).to be(true)
        expect(gitlab_shell.exists?(storage, 'namespace_2')).to be(false)
      end

      it 'ignores @hashed directory' do
        gitlab_shell.add_namespace(storage, '@hashed/12/34/5678.git')

        run_rake_task('gitlab:cleanup:dirs')

        expect(gitlab_shell.exists?(storage, '@hashed/12/34/5678.git')).to be(true)
      end
    end
  end

  # A single integration test that is redundant with one part of the
  # Gitlab::Cleanup::ProjectUploads spec.
  #
  # Additionally, this tests DRY_RUN env var values, and the extra line of
  # output that says you can disable DRY_RUN if it's enabled.
  describe 'cleanup:project_uploads' do
    let!(:logger) { double(:logger) }

    before do
      expect(main_object).to receive(:logger).and_return(logger).at_least(1).times

      allow(logger).to receive(:info).at_least(1).times
      allow(logger).to receive(:debug).at_least(1).times
    end

    context 'with a fixable orphaned project upload file' do
      let(:orphaned) { create(:upload, :issuable_upload, :with_file, model: build(:project, :legacy_storage)) }
      let(:new_path) { orphaned.absolute_path }
      let(:path) { File.join(FileUploader.root, 'some', 'wrong', 'location', orphaned.path) }

      before do
        FileUtils.mkdir_p(File.dirname(path))
        FileUtils.mv(new_path, path)
      end

      context 'with DRY_RUN disabled' do
        before do
          stub_env('DRY_RUN', 'false')
        end

        it 'moves the file to its proper location' do
          run_rake_task('gitlab:cleanup:project_uploads')

          expect(File.exist?(path)).to be_falsey
          expect(File.exist?(new_path)).to be_truthy
        end

        it 'logs action as done' do
          expect(logger).to receive(:info).with("Looking for orphaned project uploads to clean up...")
          expect(logger).to receive(:info).with("Did fix #{path} -> #{new_path}")

          run_rake_task('gitlab:cleanup:project_uploads')
        end
      end

      shared_examples_for 'does not move the file' do
        it 'does not move the file' do
          run_rake_task('gitlab:cleanup:project_uploads')

          expect(File.exist?(path)).to be_truthy
          expect(File.exist?(new_path)).to be_falsey
        end

        it 'logs action as able to be done' do
          expect(logger).to receive(:info).with("Looking for orphaned project uploads to clean up. Dry run...")
          expect(logger).to receive(:info).with("Can fix #{path} -> #{new_path}")
          expect(logger).to receive(:info).with(/To clean up these files run this command with DRY_RUN=false/)

          run_rake_task('gitlab:cleanup:project_uploads')
        end
      end

      context 'with DRY_RUN explicitly enabled' do
        before do
          stub_env('DRY_RUN', 'true')
        end

        it_behaves_like 'does not move the file'
      end

      context 'with DRY_RUN set to an unknown value' do
        before do
          stub_env('DRY_RUN', 'foo')
        end

        it_behaves_like 'does not move the file'
      end

      context 'with DRY_RUN unset' do
        it_behaves_like 'does not move the file'
      end
    end
  end

  describe 'gitlab:cleanup:orphan_job_artifact_files' do
    subject(:rake_task) { run_rake_task('gitlab:cleanup:orphan_job_artifact_files') }

    it 'runs the task without errors' do
      expect(Gitlab::Cleanup::OrphanJobArtifactFiles)
        .to receive(:new).and_call_original

      expect { rake_task }.not_to raise_error
    end

    context 'with DRY_RUN set to false' do
      before do
        stub_env('DRY_RUN', 'false')
      end

      it 'passes dry_run correctly' do
        expect(Gitlab::Cleanup::OrphanJobArtifactFiles)
          .to receive(:new)
          .with(limit: anything,
                dry_run: false,
                niceness: anything,
                logger: anything)
          .and_call_original

        rake_task
      end
    end
  end

  context 'sessions' do
    describe 'gitlab:cleanup:sessions:active_sessions_lookup_keys', :clean_gitlab_redis_shared_state do
      subject(:rake_task) { run_rake_task('gitlab:cleanup:sessions:active_sessions_lookup_keys') }

      let!(:user) { create(:user) }
      let(:existing_session_id) { '5' }

      before do
        Gitlab::Redis::SharedState.with do |redis|
          redis.set("session:user:gitlab:#{user.id}:#{existing_session_id}",
                    Marshal.dump(true))
          redis.sadd("session:lookup:user:gitlab:#{user.id}", (1..10).to_a)
        end
      end

      it 'runs the task without errors' do
        expect { rake_task }.not_to raise_error
      end

      it 'removes expired active session lookup keys' do
        Gitlab::Redis::SharedState.with do |redis|
          lookup_key = "session:lookup:user:gitlab:#{user.id}"
          expect { subject }.to change { redis.scard(lookup_key) }.from(10).to(1)
          expect(redis.smembers("session:lookup:user:gitlab:#{user.id}")).to(
            eql([existing_session_id]))
        end
      end
    end
  end
end