summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinya Maeda <shinya@gitlab.com>2018-05-11 16:49:18 +0900
committerShinya Maeda <shinya@gitlab.com>2018-05-11 16:49:18 +0900
commit46fa3089c84642170d86799c4f60fe87507e575d (patch)
tree9c0959ff4fbc247beabf2dea22d188fb5822f7c5
parent910a7d02a812b1203e320d843a77cad2c7069b63 (diff)
downloadgitlab-ce-46fa3089c84642170d86799c4f60fe87507e575d.tar.gz
Rescue RecordNotUnique when pipeline is created with non-unique iid
-rw-r--r--lib/gitlab/ci/pipeline/chain/create.rb2
-rw-r--r--spec/lib/gitlab/ci/pipeline/chain/create_spec.rb37
-rw-r--r--spec/models/ci/pipeline_spec.rb2
-rw-r--r--spec/support/shared_examples/models/atomic_internal_id_spec.rb8
4 files changed, 35 insertions, 14 deletions
diff --git a/lib/gitlab/ci/pipeline/chain/create.rb b/lib/gitlab/ci/pipeline/chain/create.rb
index 918a0d151fc..e62056766bd 100644
--- a/lib/gitlab/ci/pipeline/chain/create.rb
+++ b/lib/gitlab/ci/pipeline/chain/create.rb
@@ -23,7 +23,7 @@ module Gitlab
end
end
end
- rescue ActiveRecord::RecordInvalid => e
+ rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique => e
error("Failed to persist the pipeline: #{e}")
end
diff --git a/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb
index 0edc3f315bb..b8ab0135092 100644
--- a/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb
+++ b/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb
@@ -37,21 +37,38 @@ describe Gitlab::Ci::Pipeline::Chain::Create do
end
context 'when pipeline has validation errors' do
- let(:pipeline) do
- build(:ci_pipeline, project: project, ref: nil)
+ shared_examples_for 'expectations' do
+ it 'breaks the chain' do
+ expect(step.break?).to be true
+ end
+
+ it 'appends validation error' do
+ expect(pipeline.errors.to_a)
+ .to include /Failed to persist the pipeline/
+ end
end
+
+ context 'when ref is nil' do
+ let(:pipeline) do
+ build(:ci_pipeline, project: project, ref: nil)
+ end
- before do
- step.perform!
- end
+ before do
+ step.perform!
+ end
- it 'breaks the chain' do
- expect(step.break?).to be true
+ it_behaves_like 'expectations'
end
- it 'appends validation error' do
- expect(pipeline.errors.to_a)
- .to include /Failed to persist the pipeline/
+ context 'when pipeline has a duplicate iid' do
+ before do
+ allow_any_instance_of(Ci::Pipeline).to receive(:ensure_project_iid!) { |p| p.send(:write_attribute, :iid, 1) }
+ create(:ci_pipeline, project: project)
+
+ step.perform!
+ end
+
+ it_behaves_like 'expectations'
end
end
end
diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb
index d3e0389cc72..c10d0eb55da 100644
--- a/spec/models/ci/pipeline_spec.rb
+++ b/spec/models/ci/pipeline_spec.rb
@@ -41,7 +41,7 @@ describe Ci::Pipeline, :mailer do
let(:instance) { build(:ci_pipeline) }
let(:scope_attrs) { { project: instance.project } }
let(:usage) { :ci_pipelines }
- let(:validate_presence) { false }
+ let(:allow_nil) { true }
end
end
diff --git a/spec/support/shared_examples/models/atomic_internal_id_spec.rb b/spec/support/shared_examples/models/atomic_internal_id_spec.rb
index ac1cfa47def..dce2622172b 100644
--- a/spec/support/shared_examples/models/atomic_internal_id_spec.rb
+++ b/spec/support/shared_examples/models/atomic_internal_id_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'
shared_examples_for 'AtomicInternalId' do
- let(:validate_presence) { true }
+ let(:allow_nil) { false }
describe '.has_internal_id' do
describe 'Module inclusion' do
@@ -18,7 +18,11 @@ shared_examples_for 'AtomicInternalId' do
it 'validates presence' do
instance.valid?
- expect(instance.errors[:iid]).to include("can't be blank") if validate_presence
+ if allow_nil
+ expect(instance.errors[:iid]).to be_empty
+ else
+ expect(instance.errors[:iid]).to include("can't be blank")
+ end
end
end