BigW Consortium Gitlab

repository_fork_worker_spec.rb 2.25 KB
Newer Older
1 2 3
require 'spec_helper'

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

8
  subject { described_class.new }
9

10 11 12 13
  before do
    allow(subject).to receive(:gitlab_shell).and_return(shell)
  end

14 15
  describe "#perform" do
    it "creates a new repository from a fork" do
16
      expect(shell).to receive(:fork_repository).with(
17
        '/test/path',
18
        project.full_path,
19
        project.repository_storage_path,
20
        fork_project.namespace.full_path
21
      ).and_return(true)
22

23 24
      subject.perform(
        project.id,
25
        '/test/path',
26 27
        project.full_path,
        fork_project.namespace.full_path)
28 29
    end

30
    it 'flushes various caches' do
31
      expect(shell).to receive(:fork_repository).with(
32
        '/test/path',
33
        project.full_path,
34
        project.repository_storage_path,
35
        fork_project.namespace.full_path
36
      ).and_return(true)
37

38 39
      expect_any_instance_of(Repository).to receive(:expire_emptiness_caches)
        .and_call_original
40

41 42
      expect_any_instance_of(Repository).to receive(:expire_exists_cache)
        .and_call_original
43

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

48
    it "handles bad fork" do
49 50 51 52
      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}"

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

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

60 61 62 63 64 65 66 67 68 69
    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')
70 71 72
    end
  end
end