Welcome to mirror list, hosted at ThFree Co, Russian Federation.

destroy_service_spec.rb « repositories « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3766467d708908d23f9e0b34afb600aa165039e9 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Repositories::DestroyService do
  let_it_be(:user) { create(:user) }

  let!(:project) { create(:project, :repository, namespace: user.namespace) }
  let(:repository) { project.repository }
  let(:path) { repository.disk_path }
  let(:remove_path) { "#{path}+#{project.id}#{described_class::DELETED_FLAG}" }

  subject { described_class.new(repository).execute }

  it 'moves the repository to a +deleted folder' do
    expect(project.gitlab_shell.repository_exists?(project.repository_storage, path + '.git')).to be_truthy
    expect(project.gitlab_shell.repository_exists?(project.repository_storage, remove_path + '.git')).to be_falsey

    subject

    expect(project.gitlab_shell.repository_exists?(project.repository_storage, path + '.git')).to be_falsey
    expect(project.gitlab_shell.repository_exists?(project.repository_storage, remove_path + '.git')).to be_truthy
  end

  it 'schedules the repository deletion' do
    subject

    expect(Repositories::ShellDestroyService).to receive(:new).with(repository).and_call_original

    expect(GitlabShellWorker).to receive(:perform_in)
      .with(Repositories::ShellDestroyService::REPO_REMOVAL_DELAY, :remove_repository, project.repository_storage, remove_path)

    # Because GitlabShellWorker is inside a run_after_commit callback we need to
    # trigger the callback
    project.touch
  end

  context 'on a read-only instance' do
    before do
      allow(Gitlab::Database).to receive(:read_only?).and_return(true)
    end

    it 'schedules the repository deletion' do
      expect(Repositories::ShellDestroyService).to receive(:new).with(repository).and_call_original

      expect(GitlabShellWorker).to receive(:perform_in)
        .with(Repositories::ShellDestroyService::REPO_REMOVAL_DELAY, :remove_repository, project.repository_storage, remove_path)

      subject
    end
  end

  it 'removes the repository', :sidekiq_inline do
    subject

    project.touch

    expect(project.gitlab_shell.repository_exists?(project.repository_storage, path + '.git')).to be_falsey
    expect(project.gitlab_shell.repository_exists?(project.repository_storage, remove_path + '.git')).to be_falsey
  end

  it 'flushes the repository cache' do
    expect(repository).to receive(:before_delete)

    subject
  end

  it 'does not perform any action if repository path does not exist and returns success' do
    expect(repository).to receive(:disk_path).and_return('foo')
    expect(repository).not_to receive(:before_delete)

    expect(subject[:status]).to eq :success
  end

  it 'gracefully handles exception if the repository does not exist on disk' do
    expect(repository).to receive(:before_delete).and_raise(Gitlab::Git::Repository::NoRepository)
    expect(subject[:status]).to eq :success
  end

  context 'when move operation cannot be performed' do
    let(:service) { described_class.new(repository) }

    before do
      expect(service).to receive(:mv_repository).and_return(false)
    end

    it 'returns error' do
      expect(service.execute[:status]).to eq :error
    end

    it 'logs the error' do
      expect(Gitlab::AppLogger).to receive(:error)

      service.execute
    end

    context 'when repository does not exist' do
      it 'returns success' do
        allow(service).to receive(:repo_exists?).and_return(true, false)

        expect(Repositories::ShellDestroyService).not_to receive(:new)
        expect(service.execute[:status]).to eq :success
      end
    end
  end

  context 'with a project wiki repository' do
    let(:project) { create(:project, :wiki_repo) }
    let(:repository) { project.wiki.repository }

    it 'schedules the repository deletion' do
      subject

      expect(Repositories::ShellDestroyService).to receive(:new).with(repository).and_call_original

      expect(GitlabShellWorker).to receive(:perform_in)
        .with(Repositories::ShellDestroyService::REPO_REMOVAL_DELAY, :remove_repository, project.repository_storage, remove_path)

      # Because GitlabShellWorker is inside a run_after_commit callback we need to
      # trigger the callback
      project.touch
    end
  end
end