Commit 8fe210bd authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch '335814-fix-spec-violations-cross-modification' into 'master'

Fix spec violations for internal_ids cross-modification

See merge request gitlab-org/gitlab!75316
parents f266b59f 76d28f2a
...@@ -18,15 +18,13 @@ FactoryBot.define do ...@@ -18,15 +18,13 @@ FactoryBot.define do
transient { child_of { nil } } transient { child_of { nil } }
transient { upstream_of { nil } } transient { upstream_of { nil } }
before(:create) do |pipeline, evaluator|
pipeline.ensure_project_iid!
end
after(:build) do |pipeline, evaluator| after(:build) do |pipeline, evaluator|
if evaluator.child_of if evaluator.child_of
pipeline.project = evaluator.child_of.project pipeline.project = evaluator.child_of.project
pipeline.source = :parent_pipeline pipeline.source = :parent_pipeline
end end
pipeline.ensure_project_iid!
end end
after(:create) do |pipeline, evaluator| after(:create) do |pipeline, evaluator|
......
...@@ -14,8 +14,19 @@ RSpec.describe API::CommitStatuses do ...@@ -14,8 +14,19 @@ RSpec.describe API::CommitStatuses do
let(:get_url) { "/projects/#{project.id}/repository/commits/#{sha}/statuses" } let(:get_url) { "/projects/#{project.id}/repository/commits/#{sha}/statuses" }
context 'ci commit exists' do context 'ci commit exists' do
let!(:master) { project.ci_pipelines.create!(source: :push, sha: commit.id, ref: 'master', protected: false) } let!(:master) do
let!(:develop) { project.ci_pipelines.create!(source: :push, sha: commit.id, ref: 'develop', protected: false) } project.ci_pipelines.build(source: :push, sha: commit.id, ref: 'master', protected: false).tap do |p|
p.ensure_project_iid! # Necessary to avoid cross-database modification error
p.save!
end
end
let!(:develop) do
project.ci_pipelines.build(source: :push, sha: commit.id, ref: 'develop', protected: false).tap do |p|
p.ensure_project_iid! # Necessary to avoid cross-database modification error
p.save!
end
end
context "reporter user" do context "reporter user" do
let(:statuses_id) { json_response.map { |status| status['id'] } } let(:statuses_id) { json_response.map { |status| status['id'] } }
...@@ -310,8 +321,19 @@ RSpec.describe API::CommitStatuses do ...@@ -310,8 +321,19 @@ RSpec.describe API::CommitStatuses do
end end
context 'when a pipeline id is specified' do context 'when a pipeline id is specified' do
let!(:first_pipeline) { project.ci_pipelines.create!(source: :push, sha: commit.id, ref: 'master', status: 'created') } let!(:first_pipeline) do
let!(:other_pipeline) { project.ci_pipelines.create!(source: :push, sha: commit.id, ref: 'master', status: 'created') } project.ci_pipelines.build(source: :push, sha: commit.id, ref: 'master', status: 'created').tap do |p|
p.ensure_project_iid! # Necessary to avoid cross-database modification error
p.save!
end
end
let!(:other_pipeline) do
project.ci_pipelines.build(source: :push, sha: commit.id, ref: 'master', status: 'created').tap do |p|
p.ensure_project_iid! # Necessary to avoid cross-database modification error
p.save!
end
end
subject do subject do
post api(post_url, developer), params: { post api(post_url, developer), params: {
......
...@@ -46,7 +46,6 @@ ...@@ -46,7 +46,6 @@
- "./spec/models/ci/group_variable_spec.rb" - "./spec/models/ci/group_variable_spec.rb"
- "./spec/models/ci/job_artifact_spec.rb" - "./spec/models/ci/job_artifact_spec.rb"
- "./spec/models/ci/job_variable_spec.rb" - "./spec/models/ci/job_variable_spec.rb"
- "./spec/models/ci/pipeline_spec.rb"
- "./spec/models/ci/runner_spec.rb" - "./spec/models/ci/runner_spec.rb"
- "./spec/models/ci/variable_spec.rb" - "./spec/models/ci/variable_spec.rb"
- "./spec/models/clusters/applications/runner_spec.rb" - "./spec/models/clusters/applications/runner_spec.rb"
...@@ -63,7 +62,6 @@ ...@@ -63,7 +62,6 @@
- "./spec/models/user_status_spec.rb" - "./spec/models/user_status_spec.rb"
- "./spec/requests/api/ci/pipeline_schedules_spec.rb" - "./spec/requests/api/ci/pipeline_schedules_spec.rb"
- "./spec/requests/api/ci/pipelines_spec.rb" - "./spec/requests/api/ci/pipelines_spec.rb"
- "./spec/requests/api/commit_statuses_spec.rb"
- "./spec/requests/api/commits_spec.rb" - "./spec/requests/api/commits_spec.rb"
- "./spec/requests/api/graphql/mutations/ci/pipeline_destroy_spec.rb" - "./spec/requests/api/graphql/mutations/ci/pipeline_destroy_spec.rb"
- "./spec/requests/api/resource_access_tokens_spec.rb" - "./spec/requests/api/resource_access_tokens_spec.rb"
......
...@@ -80,15 +80,22 @@ RSpec.shared_examples 'AtomicInternalId' do |validate_presence: true| ...@@ -80,15 +80,22 @@ RSpec.shared_examples 'AtomicInternalId' do |validate_presence: true|
it 'calls InternalId.generate_next and sets internal id attribute' do it 'calls InternalId.generate_next and sets internal id attribute' do
iid = rand(1..1000) iid = rand(1..1000)
expect(InternalId).to receive(:generate_next).with(instance, scope_attrs, usage, any_args).and_return(iid) # Need to do this before evaluating instance otherwise it gets set
# already in factory
allow(InternalId).to receive(:generate_next).and_return(iid)
subject subject
expect(read_internal_id).to eq(iid) expect(read_internal_id).to eq(iid)
expect(InternalId).to have_received(:generate_next).with(instance, scope_attrs, usage, any_args)
end end
it 'does not overwrite an existing internal id' do it 'does not overwrite an existing internal id' do
write_internal_id(4711) write_internal_id(4711)
expect { subject }.not_to change { read_internal_id } allow_cross_database_modification_within_transaction(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/347091') do
expect { subject }.not_to change { read_internal_id }
end
end end
context 'when the instance has an internal ID set' do context 'when the instance has an internal ID set' do
...@@ -101,6 +108,7 @@ RSpec.shared_examples 'AtomicInternalId' do |validate_presence: true| ...@@ -101,6 +108,7 @@ RSpec.shared_examples 'AtomicInternalId' do |validate_presence: true|
.to receive(:track_greatest) .to receive(:track_greatest)
.with(instance, scope_attrs, usage, internal_id, any_args) .with(instance, scope_attrs, usage, internal_id, any_args)
.and_return(internal_id) .and_return(internal_id)
subject subject
end end
end end
...@@ -110,7 +118,11 @@ RSpec.shared_examples 'AtomicInternalId' do |validate_presence: true| ...@@ -110,7 +118,11 @@ RSpec.shared_examples 'AtomicInternalId' do |validate_presence: true|
context 'when the internal id has been changed' do context 'when the internal id has been changed' do
context 'when the internal id is automatically set' do context 'when the internal id is automatically set' do
it 'clears it on the instance' do it 'clears it on the instance' do
expect_iid_to_be_set_and_rollback write_internal_id(nil)
allow_cross_database_modification_within_transaction(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/347091') do
expect_iid_to_be_set_and_rollback
end
expect(read_internal_id).to be_nil expect(read_internal_id).to be_nil
end end
...@@ -120,7 +132,9 @@ RSpec.shared_examples 'AtomicInternalId' do |validate_presence: true| ...@@ -120,7 +132,9 @@ RSpec.shared_examples 'AtomicInternalId' do |validate_presence: true|
it 'does not clear it on the instance' do it 'does not clear it on the instance' do
write_internal_id(100) write_internal_id(100)
expect_iid_to_be_set_and_rollback allow_cross_database_modification_within_transaction(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/347091') do
expect_iid_to_be_set_and_rollback
end
expect(read_internal_id).not_to be_nil expect(read_internal_id).not_to be_nil
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment