|
2 | 2 |
|
3 | 3 | require 'spec_helper'
|
4 | 4 |
|
5 |
| -RSpec.describe Gitlab::BackgroundMigration::BackfillOrDropCiPipelineOnProjectId, feature_category: :continuous_integration do |
| 5 | +RSpec.describe Gitlab::BackgroundMigration::BackfillOrDropCiPipelineOnProjectId, |
| 6 | + feature_category: :continuous_integration, |
| 7 | + migration: :gitlab_ci do |
6 | 8 | let(:project_id_with_build) { 137 }
|
7 | 9 | let(:project_id_for_merge_request) { 140 }
|
8 | 10 | let(:project_id_for_unaffected_pipeline) { 1 }
|
|
24 | 26 | table(:p_ci_builds, database: :ci).create!(partition_id: 100, project_id: project_id_with_build, commit_id: 2)
|
25 | 27 | end
|
26 | 28 |
|
27 |
| - let(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') } |
28 |
| - let(:project) do |
29 |
| - table(:projects) |
30 |
| - .create!(id: project_id_for_merge_request, namespace_id: namespace.id, project_namespace_id: namespace.id) |
31 |
| - end |
32 |
| - |
33 |
| - let!(:merge_request) do |
34 |
| - table(:merge_requests).create!( |
35 |
| - id: 1, |
36 |
| - target_branch: 'main', |
37 |
| - source_branch: 'feature', |
38 |
| - target_project_id: project.id, |
39 |
| - source_project_id: project.id |
40 |
| - ) |
41 |
| - end |
42 |
| - |
43 | 29 | subject(:migration) do
|
44 | 30 | described_class.new(
|
45 | 31 | start_id: 1,
|
|
53 | 39 | end
|
54 | 40 |
|
55 | 41 | describe '#perform' do
|
| 42 | + before do |
| 43 | + allow(Gitlab::BackgroundMigration::BackfillOrDropCiPipelineOnProjectId::MergeRequest) |
| 44 | + .to receive_message_chain(:where, :select) |
| 45 | + .and_return([]) |
| 46 | + end |
| 47 | + |
56 | 48 | it 'backfills if applicable otherwise deletes' do
|
57 | 49 | migration.perform
|
58 | 50 |
|
59 | 51 | expect { pipeline_with_nothing.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
60 | 52 | expect { trigger_request.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
61 | 53 | expect(pipeline_with_builds.reload.project_id).to eq(project_id_with_build)
|
62 |
| - expect(pipeline_with_merge_request.reload.project_id).to eq(project_id_for_merge_request) |
63 | 54 | expect(untouched_pipeline.reload.project_id).to eq(project_id_for_unaffected_pipeline)
|
64 | 55 | end
|
65 | 56 |
|
66 |
| - context 'when associations are invalid as well' do |
67 |
| - let!(:pipeline_with_bad_build) { table(:ci_pipelines, database: :ci).create!(id: 5, partition_id: 100) } |
68 |
| - let!(:bad_build) { table(:p_ci_builds, database: :ci).create!(partition_id: 100, commit_id: 5) } |
| 57 | + context 'for migrations with merge_request' do |
| 58 | + before do |
| 59 | + merge_request = double('merge_request') # rubocop:disable RSpec/VerifiedDoubles -- merge_request is a already a stub of applicationRecord |
| 60 | + allow(merge_request).to receive(:target_project_id) { project_id_for_merge_request } |
69 | 61 |
|
70 |
| - it 'deletes pipeline if associations do not have project_id' do |
71 |
| - migration.perform |
72 |
| - |
73 |
| - expect { pipeline_with_bad_build.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 62 | + allow(Gitlab::BackgroundMigration::BackfillOrDropCiPipelineOnProjectId::MergeRequest) |
| 63 | + .to receive_message_chain(:where, :select) |
| 64 | + .and_return([merge_request]) |
74 | 65 | end
|
75 |
| - end |
76 | 66 |
|
77 |
| - context 'when the merge request is from a fork project' do |
78 |
| - let(:another_namespace) { table(:namespaces).create!(name: 'user2', path: 'user2') } |
| 67 | + it 'backfills from merge_request' do |
| 68 | + migration.perform |
79 | 69 |
|
80 |
| - let(:another_project) do |
81 |
| - table(:projects) |
82 |
| - .create!(id: 141, namespace_id: another_namespace.id, project_namespace_id: another_namespace.id) |
| 70 | + expect(pipeline_with_merge_request.reload.project_id).to eq(project_id_for_merge_request) |
83 | 71 | end
|
| 72 | + end |
84 | 73 |
|
85 |
| - let!(:merge_request) do |
86 |
| - table(:merge_requests).create!( |
87 |
| - id: 1, |
88 |
| - target_branch: 'main', |
89 |
| - source_branch: 'feature', |
90 |
| - target_project_id: project.id, |
91 |
| - source_project_id: another_project.id |
92 |
| - ) |
93 |
| - end |
| 74 | + context 'when associations are invalid as well' do |
| 75 | + let!(:pipeline_with_bad_build) { table(:ci_pipelines, database: :ci).create!(id: 5, partition_id: 100) } |
| 76 | + let!(:bad_build) { table(:p_ci_builds, database: :ci).create!(partition_id: 100, commit_id: 5) } |
94 | 77 |
|
95 |
| - it 'deletes the pipeline as association is not definite' do |
| 78 | + it 'deletes pipeline if associations do not have project_id' do |
96 | 79 | migration.perform
|
97 | 80 |
|
98 |
| - expect { pipeline_with_merge_request.reload }.to raise_error(ActiveRecord::RecordNotFound) |
| 81 | + expect { pipeline_with_bad_build.reload }.to raise_error(ActiveRecord::RecordNotFound) |
99 | 82 | end
|
100 | 83 | end
|
101 | 84 | end
|
|
0 commit comments