summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/gitlab/ci/pipeline')
-rw-r--r--spec/lib/gitlab/ci/pipeline/chain/create_deployments_spec.rb14
-rw-r--r--spec/lib/gitlab/ci/pipeline/chain/create_spec.rb13
-rw-r--r--spec/lib/gitlab/ci/pipeline/logger_spec.rb84
-rw-r--r--spec/lib/gitlab/ci/pipeline/seed/build_spec.rb2
-rw-r--r--spec/lib/gitlab/ci/pipeline/seed/pipeline_spec.rb2
-rw-r--r--spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb2
6 files changed, 92 insertions, 25 deletions
diff --git a/spec/lib/gitlab/ci/pipeline/chain/create_deployments_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/create_deployments_spec.rb
index 28bc685286f..0a592395c3a 100644
--- a/spec/lib/gitlab/ci/pipeline/chain/create_deployments_spec.rb
+++ b/spec/lib/gitlab/ci/pipeline/chain/create_deployments_spec.rb
@@ -38,20 +38,6 @@ RSpec.describe Gitlab::Ci::Pipeline::Chain::CreateDeployments do
expect(job.deployment.environment).to eq(job.persisted_environment)
end
- context 'when creation failure occures' do
- before do
- allow_next_instance_of(Deployment) do |deployment|
- allow(deployment).to receive(:save!) { raise ActiveRecord::RecordInvalid }
- end
- end
-
- it 'trackes the exception' do
- expect { subject }.to raise_error(described_class::DeploymentCreationError)
-
- expect(Deployment.count).to eq(0)
- end
- end
-
context 'when the corresponding environment does not exist' do
let!(:environment) { }
diff --git a/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb b/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb
index 4206483b228..1d020d3ea79 100644
--- a/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb
+++ b/spec/lib/gitlab/ci/pipeline/chain/create_spec.rb
@@ -7,7 +7,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Chain::Create do
let_it_be(:user) { create(:user) }
let(:pipeline) do
- build(:ci_empty_pipeline, project: project, ref: 'master')
+ build(:ci_empty_pipeline, project: project, ref: 'master', user: user)
end
let(:command) do
@@ -59,7 +59,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Chain::Create do
context 'tags persistence' do
let(:stage) do
- build(:ci_stage_entity, pipeline: pipeline)
+ build(:ci_stage_entity, pipeline: pipeline, project: project)
end
let(:job) do
@@ -79,12 +79,11 @@ RSpec.describe Gitlab::Ci::Pipeline::Chain::Create do
it 'extracts an empty tag list' do
expect(CommitStatus)
.to receive(:bulk_insert_tags!)
- .with(stage.statuses, {})
+ .with([job])
.and_call_original
step.perform!
- expect(job.instance_variable_defined?(:@tag_list)).to be_falsey
expect(job).to be_persisted
expect(job.tag_list).to eq([])
end
@@ -98,14 +97,13 @@ RSpec.describe Gitlab::Ci::Pipeline::Chain::Create do
it 'bulk inserts tags' do
expect(CommitStatus)
.to receive(:bulk_insert_tags!)
- .with(stage.statuses, { job.name => %w[tag1 tag2] })
+ .with([job])
.and_call_original
step.perform!
- expect(job.instance_variable_defined?(:@tag_list)).to be_falsey
expect(job).to be_persisted
- expect(job.tag_list).to match_array(%w[tag1 tag2])
+ expect(job.reload.tag_list).to match_array(%w[tag1 tag2])
end
end
@@ -120,7 +118,6 @@ RSpec.describe Gitlab::Ci::Pipeline::Chain::Create do
step.perform!
- expect(job.instance_variable_defined?(:@tag_list)).to be_truthy
expect(job).to be_persisted
expect(job.reload.tag_list).to match_array(%w[tag1 tag2])
end
diff --git a/spec/lib/gitlab/ci/pipeline/logger_spec.rb b/spec/lib/gitlab/ci/pipeline/logger_spec.rb
index 0b44e35dec1..a488bc184f8 100644
--- a/spec/lib/gitlab/ci/pipeline/logger_spec.rb
+++ b/spec/lib/gitlab/ci/pipeline/logger_spec.rb
@@ -41,6 +41,90 @@ RSpec.describe ::Gitlab::Ci::Pipeline::Logger do
end
end
+ describe '#instrument_with_sql', :request_store do
+ subject(:instrument_with_sql) do
+ logger.instrument_with_sql(:expensive_operation, &operation)
+ end
+
+ def loggable_data(count:, db_count: nil)
+ keys = %w[
+ expensive_operation_duration_s
+ expensive_operation_db_count
+ expensive_operation_db_primary_count
+ expensive_operation_db_primary_duration_s
+ expensive_operation_db_main_count
+ expensive_operation_db_main_duration_s
+ ]
+
+ data = keys.each.with_object({}) do |key, accumulator|
+ accumulator[key] = {
+ 'count' => count,
+ 'avg' => a_kind_of(Numeric),
+ 'max' => a_kind_of(Numeric),
+ 'min' => a_kind_of(Numeric)
+ }
+ end
+
+ if db_count
+ data['expensive_operation_db_count']['max'] = db_count
+ data['expensive_operation_db_count']['min'] = db_count
+ data['expensive_operation_db_count']['avg'] = db_count
+ end
+
+ data
+ end
+
+ context 'with a single query' do
+ let(:operation) { -> { Project.count } }
+
+ it { is_expected.to eq(operation.call) }
+
+ it 'includes SQL metrics' do
+ instrument_with_sql
+
+ expect(logger.observations_hash)
+ .to match(a_hash_including(loggable_data(count: 1, db_count: 1)))
+ end
+ end
+
+ context 'with multiple queries' do
+ let(:operation) { -> { Ci::Build.count + Ci::Bridge.count } }
+
+ it { is_expected.to eq(operation.call) }
+
+ it 'includes SQL metrics' do
+ instrument_with_sql
+
+ expect(logger.observations_hash)
+ .to match(a_hash_including(loggable_data(count: 1, db_count: 2)))
+ end
+ end
+
+ context 'with multiple observations' do
+ let(:operation) { -> { Ci::Build.count + Ci::Bridge.count } }
+
+ it 'includes SQL metrics' do
+ 2.times { logger.instrument_with_sql(:expensive_operation, &operation) }
+
+ expect(logger.observations_hash)
+ .to match(a_hash_including(loggable_data(count: 2, db_count: 2)))
+ end
+ end
+
+ context 'when there are not SQL operations' do
+ let(:operation) { -> { 123 } }
+
+ it { is_expected.to eq(operation.call) }
+
+ it 'does not include SQL metrics' do
+ instrument_with_sql
+
+ expect(logger.observations_hash.keys)
+ .to match_array(['expensive_operation_duration_s'])
+ end
+ end
+ end
+
describe '#observe' do
it 'records durations of observed operations' do
loggable_data = {
diff --git a/spec/lib/gitlab/ci/pipeline/seed/build_spec.rb b/spec/lib/gitlab/ci/pipeline/seed/build_spec.rb
index 68806fbf287..2f9fcd7caac 100644
--- a/spec/lib/gitlab/ci/pipeline/seed/build_spec.rb
+++ b/spec/lib/gitlab/ci/pipeline/seed/build_spec.rb
@@ -8,7 +8,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Build do
let(:pipeline) { build(:ci_empty_pipeline, project: project, sha: head_sha) }
let(:root_variables) { [] }
- let(:seed_context) { double(pipeline: pipeline, root_variables: root_variables) }
+ let(:seed_context) { Gitlab::Ci::Pipeline::Seed::Context.new(pipeline, root_variables: root_variables) }
let(:attributes) { { name: 'rspec', ref: 'master', scheduling_type: :stage, when: 'on_success' } }
let(:previous_stages) { [] }
let(:current_stage) { double(seeds_names: [attributes[:name]]) }
diff --git a/spec/lib/gitlab/ci/pipeline/seed/pipeline_spec.rb b/spec/lib/gitlab/ci/pipeline/seed/pipeline_spec.rb
index 5d8a9358e10..a76b4874eca 100644
--- a/spec/lib/gitlab/ci/pipeline/seed/pipeline_spec.rb
+++ b/spec/lib/gitlab/ci/pipeline/seed/pipeline_spec.rb
@@ -6,7 +6,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Pipeline do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
- let(:seed_context) { double(pipeline: pipeline, root_variables: []) }
+ let(:seed_context) { Gitlab::Ci::Pipeline::Seed::Context.new(pipeline, root_variables: []) }
let(:stages_attributes) do
[
diff --git a/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb b/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb
index 5b04d2abd88..a632b5dedcf 100644
--- a/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb
+++ b/spec/lib/gitlab/ci/pipeline/seed/stage_spec.rb
@@ -6,7 +6,7 @@ RSpec.describe Gitlab::Ci::Pipeline::Seed::Stage do
let(:project) { create(:project, :repository) }
let(:pipeline) { create(:ci_empty_pipeline, project: project) }
let(:previous_stages) { [] }
- let(:seed_context) { double(pipeline: pipeline, root_variables: []) }
+ let(:seed_context) { Gitlab::Ci::Pipeline::Seed::Context.new(pipeline, root_variables: []) }
let(:attributes) do
{ name: 'test',