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

describe RepositoryForkWorker do
4 5
  include ProjectForksHelper

6 7 8 9
  describe 'modules' do
    it 'includes ProjectImportOptions' do
      expect(described_class).to include_module(ProjectImportOptions)
    end
10 11
  end

12
  describe "#perform" do
13
    let(:project) { create(:project, :public, :repository) }
14
    let(:shell) { Gitlab::Shell.new }
15 16 17 18 19
    let(:forked_project) { create(:project, :repository, :import_scheduled) }

    before do
      fork_project(project, forked_project.creator, target_project: forked_project, repository: true)
    end
20

21 22 23 24
    shared_examples 'RepositoryForkWorker performing' do
      before do
        allow(subject).to receive(:gitlab_shell).and_return(shell)
      end
25

26 27 28 29
      def expect_fork_repository
        expect(shell).to receive(:fork_repository).with(
          'default',
          project.disk_path,
30 31
          forked_project.repository_storage,
          forked_project.disk_path
32 33
        )
      end
34

35 36
      describe 'when a worker was reset without cleanup' do
        let(:jid) { '12345678' }
37

38 39
        it 'creates a new repository from a fork' do
          allow(subject).to receive(:jid).and_return(jid)
40

41
          expect_fork_repository.and_return(true)
42

43 44 45 46 47
          perform!
        end
      end

      it "creates a new repository from a fork" do
48 49 50
        expect_fork_repository.and_return(true)

        perform!
51 52
      end

53 54
      it 'protects the default branch' do
        expect_fork_repository.and_return(true)
55

56 57
        perform!

58
        expect(forked_project.protected_branches.first.name).to eq(forked_project.default_branch)
59 60 61 62
      end

      it 'flushes various caches' do
        expect_fork_repository.and_return(true)
63

64
        # Works around https://github.com/rspec/rspec-mocks/issues/910
65 66
        expect(Project).to receive(:find).with(forked_project.id).and_return(forked_project)
        expect(forked_project.repository).to receive(:expire_emptiness_caches)
67
          .and_call_original
68
        expect(forked_project.repository).to receive(:expire_exists_cache)
69
          .and_call_original
70
        expect(forked_project.wiki.repository).to receive(:expire_emptiness_caches)
71
          .and_call_original
72
        expect(forked_project.wiki.repository).to receive(:expire_exists_cache)
73
          .and_call_original
74

75 76 77 78
        perform!
      end

      it "handles bad fork" do
79
        error_message = "Unable to fork project #{forked_project.id} for repository #{project.disk_path} -> #{forked_project.disk_path}"
80

81
        expect_fork_repository.and_return(false)
82

83 84 85
        expect { perform! }.to raise_error(StandardError, error_message)
      end
    end
86

87 88
    context 'only project ID passed' do
      def perform!
89
        subject.perform(forked_project.id)
90
      end
91

92
      it_behaves_like 'RepositoryForkWorker performing'
93 94
    end

95 96
    context 'project ID, storage and repo paths passed' do
      def perform!
97
        subject.perform(forked_project.id, TestEnv.repos_path, project.disk_path)
98
      end
99

100
      it_behaves_like 'RepositoryForkWorker performing'
101 102 103
    end
  end
end