debian-mirror-gitlab/spec/lib/marginalia_spec.rb

155 lines
4.1 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe 'Marginalia spec' do
2021-04-29 21:17:54 +05:30
class MarginaliaTestController < ApplicationController
skip_before_action :authenticate_user!, :check_two_factor_requirement
2020-01-01 13:55:28 +05:30
def first_user
User.first
render body: nil
end
2021-04-29 21:17:54 +05:30
2022-08-27 11:52:29 +05:30
def first_ci_pipeline
Ci::Pipeline.first
render body: nil
end
2021-04-29 21:17:54 +05:30
private
[:auth_user, :current_user, :set_experimentation_subject_id_cookie, :signed_in?].each do |method|
2022-08-27 11:52:29 +05:30
define_method(method) {}
2021-04-29 21:17:54 +05:30
end
2020-01-01 13:55:28 +05:30
end
class MarginaliaTestJob
include Sidekiq::Worker
def perform
2021-04-29 21:17:54 +05:30
Gitlab::ApplicationContext.with_context(caller_id: self.class.name) do
User.first
end
2020-01-01 13:55:28 +05:30
end
end
2020-03-13 15:44:24 +05:30
class MarginaliaTestMailer < ApplicationMailer
2020-01-01 13:55:28 +05:30
def first_user
User.first
end
end
describe 'For rails web requests' do
let(:correlation_id) { SecureRandom.uuid }
2022-08-27 11:52:29 +05:30
let(:recorded) { ActiveRecord::QueryRecorder.new { make_request(correlation_id, :first_user) } }
2020-01-01 13:55:28 +05:30
let(:component_map) do
{
2022-10-11 01:57:18 +05:30
"application" => "test",
"endpoint_id" => "MarginaliaTestController#first_user",
2021-11-11 11:23:49 +05:30
"correlation_id" => correlation_id,
"db_config_name" => "main"
2020-01-01 13:55:28 +05:30
}
end
2021-04-17 20:07:23 +05:30
it 'generates a query that includes the component and value' do
component_map.each do |component, value|
expect(recorded.log.last).to include("#{component}:#{value}")
2020-01-01 13:55:28 +05:30
end
end
2021-11-11 11:23:49 +05:30
context 'when using CI database' do
2022-08-27 11:52:29 +05:30
let(:recorded) { ActiveRecord::QueryRecorder.new { make_request(correlation_id, :first_ci_pipeline) } }
2021-11-11 11:23:49 +05:30
let(:component_map) do
{
2022-10-11 01:57:18 +05:30
"application" => "test",
"endpoint_id" => "MarginaliaTestController#first_ci_pipeline",
2021-11-11 11:23:49 +05:30
"correlation_id" => correlation_id,
2022-07-23 23:45:48 +05:30
"db_config_name" => 'ci'
2021-11-11 11:23:49 +05:30
}
end
2021-12-11 22:18:48 +05:30
before do
2023-04-23 21:23:45 +05:30
skip_if_multiple_databases_not_setup(:ci)
2021-11-11 11:23:49 +05:30
end
it 'generates a query that includes the component and value' do
component_map.each do |component, value|
expect(recorded.log.last).to include("#{component}:#{value}")
end
end
end
2020-01-01 13:55:28 +05:30
end
describe 'for Sidekiq worker jobs' do
2020-04-22 19:07:51 +05:30
around do |example|
with_sidekiq_server_middleware do |chain|
2021-04-29 21:17:54 +05:30
chain.add Labkit::Middleware::Sidekiq::Context::Server
2020-04-22 19:07:51 +05:30
chain.add Marginalia::SidekiqInstrumentation::Middleware
Marginalia.application_name = "sidekiq"
example.run
end
2020-01-01 13:55:28 +05:30
end
after(:all) do
MarginaliaTestJob.clear
end
before do
MarginaliaTestJob.perform_async
end
let(:sidekiq_job) { MarginaliaTestJob.jobs.first }
let(:recorded) { ActiveRecord::QueryRecorder.new { MarginaliaTestJob.drain } }
let(:component_map) do
{
2022-10-11 01:57:18 +05:30
"application" => "sidekiq",
"endpoint_id" => "MarginaliaTestJob",
2021-04-29 21:17:54 +05:30
"correlation_id" => sidekiq_job['correlation_id'],
2022-10-11 01:57:18 +05:30
"jid" => sidekiq_job['jid'],
2021-11-11 11:23:49 +05:30
"db_config_name" => "main"
2020-01-01 13:55:28 +05:30
}
end
2021-04-17 20:07:23 +05:30
it 'generates a query that includes the component and value' do
component_map.each do |component, value|
expect(recorded.log.last).to include("#{component}:#{value}")
2020-01-01 13:55:28 +05:30
end
2021-04-17 20:07:23 +05:30
end
2020-01-01 13:55:28 +05:30
2021-09-30 23:02:18 +05:30
describe 'for ActionMailer delivery jobs', :sidekiq_mailers do
2021-04-17 20:07:23 +05:30
let(:delivery_job) { MarginaliaTestMailer.first_user.deliver_later }
2020-01-01 13:55:28 +05:30
2021-04-17 20:07:23 +05:30
let(:recorded) do
ActiveRecord::QueryRecorder.new do
2021-04-29 21:17:54 +05:30
Sidekiq::Worker.drain_all
2020-01-01 13:55:28 +05:30
end
end
2021-04-17 20:07:23 +05:30
let(:component_map) do
{
2022-10-11 01:57:18 +05:30
"application" => "sidekiq",
"endpoint_id" => "ActionMailer::MailDeliveryJob",
"jid" => delivery_job.job_id,
2021-11-11 11:23:49 +05:30
"db_config_name" => "main"
2021-04-17 20:07:23 +05:30
}
2020-01-01 13:55:28 +05:30
end
2021-04-17 20:07:23 +05:30
it 'generates a query that includes the component and value' do
component_map.each do |component, value|
expect(recorded.log.last).to include("#{component}:#{value}")
end
2020-01-01 13:55:28 +05:30
end
end
end
2021-01-03 14:25:43 +05:30
2022-08-27 11:52:29 +05:30
def make_request(correlation_id, action_name)
2021-01-03 14:25:43 +05:30
request_env = Rack::MockRequest.env_for('/')
::Labkit::Correlation::CorrelationId.use_id(correlation_id) do
2022-08-27 11:52:29 +05:30
MarginaliaTestController.action(action_name).call(request_env)
2021-01-03 14:25:43 +05:30
end
end
2020-01-01 13:55:28 +05:30
end