summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/query_analyzers
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-12-20 14:22:11 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2022-12-20 14:22:11 +0000
commit0c872e02b2c822e3397515ec324051ff540f0cd5 (patch)
treece2fb6ce7030e4dad0f4118d21ab6453e5938cdd /spec/lib/gitlab/database/query_analyzers
parentf7e05a6853b12f02911494c4b3fe53d9540d74fc (diff)
downloadgitlab-ce-0c872e02b2c822e3397515ec324051ff540f0cd5.tar.gz
Add latest changes from gitlab-org/gitlab@15-7-stable-eev15.7.0-rc42
Diffstat (limited to 'spec/lib/gitlab/database/query_analyzers')
-rw-r--r--spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb77
1 files changed, 62 insertions, 15 deletions
diff --git a/spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb b/spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb
index ec01ae623ae..bcc39c0c3db 100644
--- a/spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb
+++ b/spec/lib/gitlab/database/query_analyzers/query_recorder_spec.rb
@@ -10,29 +10,76 @@ RSpec.describe Gitlab::Database::QueryAnalyzers::QueryRecorder, query_analyzers:
end
end
- context 'when analyzer is enabled for tests' do
+ context 'with query analyzer' do
let(:query) { 'SELECT 1 FROM projects' }
- let(:log_path) { Rails.root.join(described_class::LOG_FILE) }
+ let(:log_path) { Rails.root.join(described_class::LOG_PATH) }
+ let(:log_file) { described_class.log_file }
- before do
- stub_env('CI', 'true')
+ after do
+ ::Gitlab::Database::QueryAnalyzer.instance.end!([described_class])
+ end
- # This is needed to be able to stub_env the CI variable
- ::Gitlab::Database::QueryAnalyzer.instance.begin!([described_class])
+ shared_examples_for 'an enabled query recorder' do
+ it 'logs queries to a file' do
+ allow(FileUtils).to receive(:mkdir_p)
+ .with(log_path)
+ expect(File).to receive(:write)
+ .with(log_file, /^{"sql":"#{query}/, mode: 'a')
+ expect(described_class).to receive(:analyze).with(/^#{query}/).and_call_original
+
+ expect { ApplicationRecord.connection.execute(query) }.not_to raise_error
+ end
end
- after do
- ::Gitlab::Database::QueryAnalyzer.instance.end!([described_class])
+ context 'on default branch' do
+ before do
+ stub_env('CI_MERGE_REQUEST_LABELS', nil)
+ stub_env('CI_DEFAULT_BRANCH', 'default_branch_name')
+ stub_env('CI_COMMIT_REF_NAME', 'default_branch_name')
+
+ # This is needed to be able to stub_env the CI variable
+ ::Gitlab::Database::QueryAnalyzer.instance.begin!([described_class])
+ end
+
+ it_behaves_like 'an enabled query recorder'
+ end
+
+ context 'on database merge requests' do
+ before do
+ stub_env('CI_MERGE_REQUEST_LABELS', 'database')
+
+ # This is needed to be able to stub_env the CI variable
+ ::Gitlab::Database::QueryAnalyzer.instance.begin!([described_class])
+ end
+
+ it_behaves_like 'an enabled query recorder'
+ end
+ end
+
+ describe '.log_file' do
+ let(:folder) { 'query_recorder' }
+ let(:extension) { 'ndjson' }
+ let(:default_name) { 'rspec' }
+ let(:job_name) { 'test-job-1' }
+
+ subject { described_class.log_file.to_s }
+
+ context 'when in CI' do
+ before do
+ stub_env('CI_JOB_NAME_SLUG', job_name)
+ end
+
+ it { is_expected.to include("#{folder}/#{job_name}.#{extension}") }
+ it { is_expected.not_to include("#{folder}/#{default_name}.#{extension}") }
end
- it 'logs queries to a file' do
- allow(FileUtils).to receive(:mkdir_p)
- .with(File.dirname(log_path))
- expect(File).to receive(:write)
- .with(log_path, /^{"sql":"#{query}/, mode: 'a')
- expect(described_class).to receive(:analyze).with(/^#{query}/).and_call_original
+ context 'when not in CI' do
+ before do
+ stub_env('CI_JOB_NAME_SLUG', nil)
+ end
- expect { ApplicationRecord.connection.execute(query) }.not_to raise_error
+ it { is_expected.to include("#{folder}/#{default_name}.#{extension}") }
+ it { is_expected.not_to include("#{folder}/#{job_name}.#{extension}") }
end
end
end