summaryrefslogtreecommitdiff
path: root/spec/workers/repository_fork_worker_spec.rb
blob: 6ea5569b4385f41f9528f71887ef59115b16e446 (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
require 'spec_helper'

describe RepositoryForkWorker do
  let(:project) { create(:project, :repository, :import_scheduled) }
  let(:fork_project) { create(:project, :repository, forked_from_project: project) }
  let(:shell) { Gitlab::Shell.new }

  subject { described_class.new }

  before do
    allow(subject).to receive(:gitlab_shell).and_return(shell)
  end

  describe "#perform" do
    it "creates a new repository from a fork" do
      expect(shell).to receive(:fork_repository).with(
        '/test/path',
        project.full_path,
        project.repository_storage_path,
        fork_project.namespace.full_path
      ).and_return(true)

      subject.perform(
        project.id,
        '/test/path',
        project.full_path,
        fork_project.namespace.full_path)
    end

    it 'flushes various caches' do
      expect(shell).to receive(:fork_repository).with(
        '/test/path',
        project.full_path,
        project.repository_storage_path,
        fork_project.namespace.full_path
      ).and_return(true)

      expect_any_instance_of(Repository).to receive(:expire_emptiness_caches).
        and_call_original

      expect_any_instance_of(Repository).to receive(:expire_exists_cache).
        and_call_original

      subject.perform(project.id, '/test/path', project.full_path,
                      fork_project.namespace.full_path)
    end

    it "handles bad fork" do
      source_path = project.full_path
      target_path = fork_project.namespace.full_path
      error_message = "Unable to fork project #{project.id} for repository #{source_path} -> #{target_path}"

      expect(shell).to receive(:fork_repository).and_return(false)

      expect do
        subject.perform(project.id, '/test/path', source_path, target_path)
      end.to raise_error(RepositoryForkWorker::ForkError, error_message)
    end

    it 'handles unexpected error' do
      source_path = project.full_path
      target_path = fork_project.namespace.full_path

      allow_any_instance_of(Gitlab::Shell).to receive(:fork_repository).and_raise(RuntimeError)

      expect do
        subject.perform(project.id, '/test/path', source_path, target_path)
      end.to raise_error(RepositoryForkWorker::ForkError)
      expect(project.reload.import_status).to eq('failed')
    end
  end
end