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

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

require 'spec_helper'

RSpec.describe RepositoryImportWorker, feature_category: :importers do
  describe '#perform' do
    let(:project) { build_stubbed(:project, :import_scheduled, import_state: import_state, import_url: 'url') }
    let(:import_state) { create(:import_state, status: :scheduled) }
    let(:jid) { '12345678' }

    before do
      allow(subject).to receive(:jid).and_return(jid)
      allow(Project).to receive(:find_by_id).with(project.id).and_return(project)
      allow(project).to receive(:after_import)
      allow(import_state).to receive(:start).and_return(true)
    end

    context 'when project not found (deleted)' do
      before do
        allow(Project).to receive(:find_by_id).with(project.id).and_return(nil)
      end

      it 'does not raise any exception' do
        expect(Projects::ImportService).not_to receive(:new)

        expect { subject.perform(project.id) }.not_to raise_error
      end
    end

    context 'when import_state is scheduled' do
      it 'imports the project successfully' do
        expect_next_instance_of(Projects::ImportService) do |instance|
          expect(instance).to receive(:execute).and_return({ status: :ok })
        end

        subject.perform(project.id)

        expect(project).to have_received(:after_import)
        expect(import_state).to have_received(:start)
      end
    end

    context 'when worker was reset without cleanup (import_state is started)' do
      let(:import_state) { create(:import_state, :started, jid: jid) }

      it 'imports the project successfully' do
        expect_next_instance_of(Projects::ImportService) do |instance|
          expect(instance).to receive(:execute).and_return({ status: :ok })
        end

        subject.perform(project.id)

        expect(project).to have_received(:after_import)
        expect(import_state).not_to have_received(:start)
      end
    end

    context 'when using an asynchronous importer' do
      it 'does not mark the import process as finished' do
        expect_next_instance_of(Projects::ImportService) do |instance|
          expect(instance).to receive(:execute).and_return({ status: :ok })
          expect(instance).to receive(:async?).and_return(true)
        end

        subject.perform(project.id)

        expect(project).not_to have_received(:after_import)
      end
    end

    context 'when the import has failed' do
      let(:error) { "https://user:pass@test.com/root/repoC.git/ not found" }

      before do
        allow(import_state).to receive(:mark_as_failed)
      end

      it 'marks import_state as failed' do
        expect_next_instance_of(Projects::ImportService) do |instance|
          expect(instance).to receive(:execute).and_return({ status: :error, message: error })
        end

        subject.perform(project.id)

        expect(import_state).to have_received(:mark_as_failed).with(error)
        expect(project).not_to have_received(:after_import)
      end
    end
  end
end