Commit 511e761b authored by GitLab Bot's avatar GitLab Bot

Add latest changes from gitlab-org/gitlab@master

parent 4247e67b
......@@ -301,7 +301,7 @@ gem 'sentry-raven', '~> 2.9'
gem 'premailer-rails', '~> 1.10.3'
# LabKit: Tracing and Correlation
gem 'gitlab-labkit', '~> 0.5'
gem 'gitlab-labkit', '0.8.0'
# I18n
gem 'ruby_parser', '~> 3.8', require: false
......
......@@ -117,7 +117,7 @@ GEM
activemodel (>= 5.0)
brakeman (4.2.1)
browser (2.5.3)
builder (3.2.3)
builder (3.2.4)
bullet (6.0.2)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
......@@ -362,7 +362,7 @@ GEM
github-markup (1.7.0)
gitlab-chronic (0.10.5)
numerizer (~> 0.2)
gitlab-labkit (0.7.0)
gitlab-labkit (0.8.0)
actionpack (>= 5.0.0, < 6.1.0)
activesupport (>= 5.0.0, < 6.1.0)
grpc (~> 1.19)
......@@ -589,7 +589,7 @@ GEM
activesupport (>= 4)
railties (>= 4)
request_store (~> 1.0)
loofah (2.3.1)
loofah (2.4.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
lumberjack (1.0.13)
......@@ -1204,7 +1204,7 @@ DEPENDENCIES
gitaly (~> 1.73.0)
github-markup (~> 1.7.0)
gitlab-chronic (~> 0.10.5)
gitlab-labkit (~> 0.5)
gitlab-labkit (= 0.8.0)
gitlab-license (~> 1.0)
gitlab-markup (~> 1.7.0)
gitlab-net-dns (~> 0.9.1)
......
......@@ -33,6 +33,7 @@ class ApplicationController < ActionController::Base
before_action :check_impersonation_availability
before_action :required_signup_info
around_action :set_current_context
around_action :set_locale
around_action :set_session_storage
......@@ -448,6 +449,14 @@ class ApplicationController < ActionController::Base
request.base_url
end
def set_current_context(&block)
Gitlab::ApplicationContext.with_context(
user: -> { auth_user },
project: -> { @project },
namespace: -> { @group },
&block)
end
def set_locale(&block)
Gitlab::I18n.with_user_locale(current_user, &block)
end
......
......@@ -46,6 +46,8 @@ module Ci
# it could contain many. We only need to know whether it has 1 or more
# artifacts, so fetching the first 2 would be sufficient.
def first_2_metadata_entries_for_artifacts_paths(job)
return [] unless job.artifacts_metadata
job.artifacts_paths
.lazy
.map { |path| job.artifacts_metadata_entry(path, recursive: true) }
......
---
title: Add Index to help Hashed Storage migration on big instances
merge_request: 22391
author:
type: performance
---
title: Performance improvements on milestone burndown chart
merge_request: 22380
author:
type: performance
---
title: Fix bug when trying to expose artifacts and no artifacts are produced by the job
merge_request: 22378
author:
type: fixed
---
title: Gracefully error handle CI lint errors in artifacts section
merge_request: 22388
author:
type: fixed
# frozen_string_literal: true
Rails.application.config.middleware.use(Gitlab::Middleware::CorrelationId)
# frozen_string_literal: true
Rails.application.config.middleware.use(Labkit::Middleware::Rack)
......@@ -2,7 +2,7 @@
if Labkit::Tracing.enabled?
Rails.application.configure do |config|
config.middleware.insert_after Gitlab::Middleware::CorrelationId, ::Labkit::Tracing::RackMiddleware
config.middleware.insert_after Labkit::Middleware::Rack, ::Labkit::Tracing::RackMiddleware
end
# Instrument the Sidekiq client
......
# frozen_string_literal: true
class AddStorageVersionIndexToProjects < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :projects, :id, where: 'storage_version < 2 or storage_version IS NULL', name: 'index_on_id_partial_with_legacy_storage'
end
def down
remove_concurrent_index :projects, :id, where: 'storage_version < 2 or storage_version IS NULL', name: 'index_on_id_partial_with_legacy_storage'
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_12_29_140154) do
ActiveRecord::Schema.define(version: 2020_01_02_170221) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
......@@ -3349,6 +3349,7 @@ ActiveRecord::Schema.define(version: 2019_12_29_140154) do
t.index ["creator_id"], name: "index_projects_on_creator_id"
t.index ["description"], name: "index_projects_on_description_trigram", opclass: :gin_trgm_ops, using: :gin
t.index ["id", "repository_storage", "last_repository_updated_at"], name: "idx_projects_on_repository_storage_last_repository_updated_at"
t.index ["id"], name: "index_on_id_partial_with_legacy_storage", where: "((storage_version < 2) OR (storage_version IS NULL))"
t.index ["id"], name: "index_projects_on_id_partial_for_visibility", unique: true, where: "(visibility_level = ANY (ARRAY[10, 20]))"
t.index ["id"], name: "index_projects_on_mirror_and_mirror_trigger_builds_both_true", where: "((mirror IS TRUE) AND (mirror_trigger_builds IS TRUE))"
t.index ["last_activity_at"], name: "index_projects_on_last_activity_at"
......
......@@ -43,6 +43,14 @@ module API
header['X-Content-Type-Options'] = 'nosniff'
end
before do
Gitlab::ApplicationContext.push(
user: -> { current_user },
project: -> { @project },
namespace: -> { @group }
)
end
# The locale is set to the current user's locale when `current_user` is loaded
after { Gitlab::I18n.use_default_locale }
......
......@@ -71,27 +71,27 @@ module API
ref = params[:ref]
ref ||= pipeline&.ref
ref ||= @project.repository.branch_names_contains(commit.sha).first
ref ||= user_project.repository.branch_names_contains(commit.sha).first
not_found! 'References for commit' unless ref
name = params[:name] || params[:context] || 'default'
unless pipeline
pipeline = @project.ci_pipelines.create!(
pipeline = user_project.ci_pipelines.create!(
source: :external,
sha: commit.sha,
ref: ref,
user: current_user,
protected: @project.protected_for?(ref))
protected: user_project.protected_for?(ref))
end
status = GenericCommitStatus.running_or_pending.find_or_initialize_by(
project: @project,
project: user_project,
pipeline: pipeline,
name: name,
ref: ref,
user: current_user,
protected: @project.protected_for?(ref)
protected: user_project.protected_for?(ref)
)
optional_attributes =
......@@ -117,7 +117,7 @@ module API
render_api_error!('invalid state', 400)
end
MergeRequest.where(source_project: @project, source_branch: ref)
MergeRequest.where(source_project: user_project, source_branch: ref)
.update_all(head_pipeline_id: pipeline.id) if pipeline.latest?
present status, with: Entities::CommitStatus
......
......@@ -107,8 +107,10 @@ module API
if params[:gl_repository]
@project, @repo_type = Gitlab::GlRepository.parse(params[:gl_repository])
@redirected_path = nil
else
elsif params[:project]
@project, @repo_type, @redirected_path = Gitlab::RepoPath.parse(params[:project])
else
@project, @repo_type, @redirected_path = nil, nil, nil
end
end
# rubocop:enable Gitlab/ModuleWithInstanceVariables
......
......@@ -6,6 +6,13 @@ module API
class Base < Grape::API
before { authenticate_by_gitlab_shell_token! }
before do
Gitlab::ApplicationContext.push(
user: -> { actor&.user },
project: -> { project }
)
end
helpers ::API::Helpers::InternalHelpers
UNKNOWN_CHECK_RESULT_ERROR = 'Unknown check result'.freeze
......@@ -205,7 +212,12 @@ module API
status 200
response = Gitlab::InternalPostReceive::Response.new
# Try to load the project and users so we have the application context
# available for logging before we schedule any jobs.
user = actor.user
project
push_options = Gitlab::PushOptions.new(params[:push_options])
response.reference_counter_decreased = Gitlab::ReferenceCounter.new(params[:gl_repository]).decrease
......
......@@ -355,7 +355,7 @@ module API
post ':id/unarchive' do
authorize!(:archive_project, user_project)
::Projects::UpdateService.new(@project, current_user, archived: false).execute
::Projects::UpdateService.new(user_project, current_user, archived: false).execute
present user_project, with: Entities::Project, current_user: current_user
end
......
# frozen_string_literal: true
module Gitlab
# A GitLab-rails specific accessor for `Labkit::Logging::ApplicationContext`
class ApplicationContext
include Gitlab::Utils::LazyAttributes
def self.with_context(args, &block)
application_context = new(**args)
Labkit::Context.with_context(application_context.to_lazy_hash, &block)
end
def self.push(args)
application_context = new(**args)
Labkit::Context.push(application_context.to_lazy_hash)
end
def initialize(user: nil, project: nil, namespace: nil)
@user, @project, @namespace = user, project, namespace
end
def to_lazy_hash
{ user: -> { username },
project: -> { project_path },
root_namespace: -> { root_namespace_path } }
end
private
lazy_attr_reader :user, type: User
lazy_attr_reader :project, type: Project
lazy_attr_reader :namespace, type: Namespace
def project_path
project&.full_path
end
def username
user&.username
end
def root_namespace_path
if namespace
namespace.full_path_components.first
else
project&.full_path_components&.first
end
end
end
end
......@@ -54,6 +54,11 @@ module Gitlab
def expose_as_present?
return false unless Feature.enabled?(:ci_expose_arbitrary_artifacts_in_mr, default_enabled: true)
# This duplicates the `validates :config, type: Hash` above,
# but Validatable currently doesn't halt the validation
# chain if it encounters a validation error.
return false unless @config.is_a?(Hash)
!@config[:expose_as].nil?
end
end
......
# frozen_string_literal: true
# A dumb middleware that steals correlation id
# and sets it as a global context for the request
module Gitlab
module Middleware
class CorrelationId
include ActionView::Helpers::TagHelper
def initialize(app)
@app = app
end
def call(env)
::Labkit::Correlation::CorrelationId.use_id(correlation_id(env)) do
@app.call(env)
end
end
private
def correlation_id(env)
request(env).request_id
end
def request(env)
ActionDispatch::Request.new(env)
end
end
end
end
......@@ -15,7 +15,7 @@ module Gitlab
chain.add Gitlab::SidekiqMiddleware::MemoryKiller if memory_killer
chain.add Gitlab::SidekiqMiddleware::RequestStoreMiddleware if request_store
chain.add Gitlab::SidekiqMiddleware::BatchLoader
chain.add Gitlab::SidekiqMiddleware::CorrelationLogger
chain.add Labkit::Middleware::Sidekiq::Server
chain.add Gitlab::SidekiqMiddleware::InstrumentationLogger
chain.add Gitlab::SidekiqStatus::ServerMiddleware
end
......@@ -27,7 +27,7 @@ module Gitlab
def self.client_configurator
lambda do |chain|
chain.add Gitlab::SidekiqStatus::ClientMiddleware
chain.add Gitlab::SidekiqMiddleware::CorrelationInjector
chain.add Labkit::Middleware::Sidekiq::Client
end
end
end
......
# frozen_string_literal: true
module Gitlab
module SidekiqMiddleware
class CorrelationInjector
def call(worker_class, job, queue, redis_pool)
job[Labkit::Correlation::CorrelationId::LOG_KEY] ||=
Labkit::Correlation::CorrelationId.current_or_new_id
yield
end
end
end
end
# frozen_string_literal: true
module Gitlab
module SidekiqMiddleware
class CorrelationLogger
def call(worker, job, queue)
correlation_id = job[Labkit::Correlation::CorrelationId::LOG_KEY]
Labkit::Correlation::CorrelationId.use_id(correlation_id) do
yield
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Utils
module LazyAttributes
extend ActiveSupport::Concern
include Gitlab::Utils::StrongMemoize
class_methods do
def lazy_attr_reader(*one_or_more_names, type: nil)
names = Array.wrap(one_or_more_names)
names.each { |name| define_lazy_reader(name, type: type) }
end
def lazy_attr_accessor(*one_or_more_names, type: nil)
names = Array.wrap(one_or_more_names)
names.each do |name|
define_lazy_reader(name, type: type)
define_lazy_writer(name)
end
end
private
def define_lazy_reader(name, type:)
define_method(name) do
strong_memoize("#{name}_lazy_loaded") do
value = instance_variable_get("@#{name}")
value = value.call if value.respond_to?(:call)
value = nil if type && !value.is_a?(type)
value
end
end
end
def define_lazy_writer(name)
define_method("#{name}=") do |value|
clear_memoization("#{name}_lazy_loaded")
instance_variable_set("@#{name}", value)
end
end
end
end
end
end
......@@ -7,31 +7,31 @@ set -e
if [[ "$MERGE_TRAIN_TRIGGER_TOKEN" == '' ]]
then
echo 'The variable MERGE_TRAIN_TRIGGER_TOKEN must be set to a non-empy value'
echo 'The variable MERGE_TRAIN_TRIGGER_TOKEN must be set to a non-empty value'
exit 1
fi
if [[ "$MERGE_TRAIN_TRIGGER_URL" == '' ]]
then
echo 'The variable MERGE_TRAIN_TRIGGER_URL must be set to a non-empy value'
echo 'The variable MERGE_TRAIN_TRIGGER_URL must be set to a non-empty value'
exit 1
fi
if [[ "$CI_COMMIT_REF_NAME" == '' ]]
then
echo 'The variable CI_COMMIT_REF_NAME must be set to a non-empy value'
echo 'The variable CI_COMMIT_REF_NAME must be set to a non-empty value'
exit 1
fi
if [[ "$SOURCE_PROJECT" == '' ]]
then
echo 'The variable SOURCE_PROJECT must be set to a non-empy value'
echo 'The variable SOURCE_PROJECT must be set to a non-empty value'
exit 1
fi
if [[ "$TARGET_PROJECT" == '' ]]
then
echo 'The variable TARGET_PROJECT must be set to a non-empy value'
echo 'The variable TARGET_PROJECT must be set to a non-empty value'
exit 1
fi
......
......@@ -895,4 +895,50 @@ describe ApplicationController do
end
end
end
context '#set_current_context' do
controller(described_class) do
def index
Labkit::Context.with_context do |context|
render json: context.to_h
end
end
end
let_it_be(:user) { create(:user) }
before do
sign_in(user)
end
it 'does not break anything when no group or project method is defined' do
get :index
expect(response).to have_gitlab_http_status(:success)
end
it 'sets the username in the context when signed in' do
get :index
expect(json_response['meta.user']).to eq(user.username)
end
it 'sets the group if it was available' do
group = build_stubbed(:group)
controller.instance_variable_set(:@group, group)
get :index, format: :json
expect(json_response['meta.root_namespace']).to eq(group.path)
end
it 'sets the project if one was available' do
project = build_stubbed(:project)
controller.instance_variable_set(:@project, project)
get :index, format: :json
expect(json_response['meta.project']).to eq(project.full_path)
end
end
end
......@@ -6,7 +6,6 @@ import NoteableNote from '~/notes/components/noteable_note.vue';
import PlaceholderNote from '~/vue_shared/components/notes/placeholder_note.vue';
import PlaceholderSystemNote from '~/vue_shared/components/notes/placeholder_system_note.vue';
import SystemNote from '~/vue_shared/components/notes/system_note.vue';
import TimelineEntryItem from '~/vue_shared/components/notes/timeline_entry_item.vue';
import createStore from '~/notes/stores';
import { noteableDataMock, discussionMock, notesDataMock } from '../../notes/mock_data';
......@@ -48,13 +47,13 @@ describe('DiscussionNotes', () => {
it('renders an element for each note in the discussion', () => {
createComponent();
const notesCount = discussionMock.notes.length;
const els = wrapper.findAll(TimelineEntryItem);
const els = wrapper.findAll(NoteableNote);
expect(els.length).toBe(notesCount);
});
it('renders one element if replies groupping is enabled', () => {
createComponent({ shouldGroupReplies: true });
const els = wrapper.findAll(TimelineEntryItem);
const els = wrapper.findAll(NoteableNote);
expect(els.length).toBe(1);
});
......@@ -85,7 +84,7 @@ describe('DiscussionNotes', () => {
];
discussion.notes = notesData;
createComponent({ discussion, shouldRenderDiffs: true });
const notes = wrapper.findAll('.notes > li');
const notes = wrapper.findAll('.notes > *');
expect(notes.at(0).is(PlaceholderSystemNote)).toBe(true);
expect(notes.at(1).is(PlaceholderNote)).toBe(true);
......@@ -111,7 +110,14 @@ describe('DiscussionNotes', () => {
describe('events', () => {
describe('with groupped notes and replies expanded', () => {
const findNoteAtIndex = index => wrapper.find(`.note:nth-of-type(${index + 1}`);
const findNoteAtIndex = index => {
const noteComponents = [NoteableNote, SystemNote, PlaceholderNote, PlaceholderSystemNote];
const allowedNames = noteComponents.map(c => c.name);
return wrapper
.findAll('.notes *')
.filter(w => allowedNames.includes(w.name()))
.at(index);
};
beforeEach(() => {
createComponent({ shouldGroupReplies: true, isExpanded: true });
......@@ -146,7 +152,7 @@ describe('DiscussionNotes', () => {
let note;
beforeEach(() => {
createComponent();
note = wrapper.find('.note');
note = wrapper.find('.notes > *');
});
it('emits deleteNote when first note emits handleDeleteNote', () => {
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::ApplicationContext do
describe '.with_context' do
it 'yields the block' do
expect { |b| described_class.with_context({}, &b) }.to yield_control
end
it 'passes the expected context on to labkit' do
fake_proc = duck_type(:call)
expected_context = hash_including(user: fake_proc, project: fake_proc, root_namespace: fake_proc)
expect(Labkit::Context).to receive(:with_context).with(expected_context)
described_class.with_context(
user: build(:user),
project: build(:project),
namespace: build(:namespace)) {}
end
it 'raises an error when passing invalid options' do
expect { described_class.with_context(no: 'option') {} }.to raise_error(ArgumentError)
end
end
describe '.push' do
it 'passes the expected context on to labkit' do
fake_proc = duck_type(:call)
expected_context = hash_including(user: fake_proc, project: fake_proc, root_namespace: fake_proc)
expect(Labkit::Context).to receive(:push).with(expected_context)
described_class.push(user: build(:user))
end
it 'raises an error when passing invalid options' do
expect { described_class.push(no: 'option')}.to raise_error(ArgumentError)
end
end
describe '#to_lazy_hash' do
let(:user) { build(:user) }
let(:project) { build(:project) }
let(:namespace) { build(:group) }
let(:subgroup) { build(:group, parent: namespace) }
def result(context)
context.to_lazy_hash.transform_values { |v| v.call }
end
it 'does not call the attributes until needed' do
fake_proc = double('Proc')
expect(fake_proc).not_to receive(:call)
described_class.new(user: fake_proc, project: fake_proc, namespace: fake_proc).to_lazy_hash
end
it 'correctly loads the expected values when they are wrapped in a block' do
context = described_class.new(user: -> { user }, project: -> { project }, namespace: -> { subgroup })
expect(result(context))
.to include(user: user.username, project: project.full_path, root_namespace: namespace.full_path)
end
it 'correctly loads the expected values when passed directly' do
context = described_class.new(user: user, project: project, namespace: subgroup)
expect(result(context))
.to include(user: user.username, project: project.full_path, root_namespace: namespace.full_path)
end
it 'falls back to a projects namespace when a project is passed but no namespace' do
context = described_class.new(project: project)
expect(result(context))
.to include(project: project.full_path, root_namespace: project.full_path_components.first)
end
end
end
......@@ -1270,6 +1270,19 @@ module Gitlab
expect(builds.first[:options][:artifacts][:when]).to eq(when_state)
end
end
it "gracefully handles errors in artifacts type" do
config = <<~YAML
test:
script:
- echo "Hello world"
artifacts:
- paths:
- test/
YAML
expect { described_class.new(config) }.to raise_error(described_class::ValidationError)
end
end
describe '#environment' do
......
......@@ -654,13 +654,10 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
let(:user) { create(:user) }
let!(:project) { create(:project, :builds_disabled, :issues_disabled, name: 'project', path: 'project') }
let(:project_tree_restorer) { described_class.new(user: user, shared: shared, project: project) }
let(:correlation_id) { 'my-correlation-id' }
before do
setup_import_export_config('with_invalid_records')
# Import is running from the rake task, `correlation_id` is not assigned
expect(Labkit::Correlation::CorrelationId).to receive(:new_id).and_return(correlation_id)
subject
end
......@@ -682,7 +679,7 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
expect(import_failure.relation_index).to be_present
expect(import_failure.exception_class).to eq('ActiveRecord::RecordInvalid')
expect(import_failure.exception_message).to be_present
expect(import_failure.correlation_id_value).to eq('my-correlation-id')
expect(import_failure.correlation_id_value).not_to be_empty
expect(import_failure.created_at).to be_present
end
end
......
......@@ -84,7 +84,7 @@ describe Gitlab::Profiler do
expect(severity).to eq(Logger::DEBUG)
expect(message).to include('public').and include(described_class::FILTERED_STRING)
expect(message).not_to include(private_token)
end.twice
end.at_least(1) # This spec could be wrapped in more blocks in the future
custom_logger.debug("public #{private_token}")
end
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::SidekiqMiddleware::CorrelationInjector do
class TestWorker
include ApplicationWorker
end
before do |example|
Sidekiq.client_middleware do |chain|
chain.add described_class
end
end
after do |example|
Sidekiq.client_middleware do |chain|
chain.remove described_class
end
Sidekiq::Queues.clear_all
end
around do |example|
Sidekiq::Testing.fake! do
example.run
end
end
it 'injects into payload the correlation id' do
expect_next_instance_of(described_class) do |instance|
expect(instance).to receive(:call).and_call_original
end
Labkit::Correlation::CorrelationId.use_id('new-correlation-id') do
TestWorker.perform_async(1234)
end
expected_job_params = {
"class" => "TestWorker",
"args" => [1234],
"correlation_id" => "new-correlation-id"
}
expect(Sidekiq::Queues.jobs_by_worker).to a_hash_including(
"TestWorker" => a_collection_containing_exactly(
a_hash_including(expected_job_params)))
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::SidekiqMiddleware::CorrelationLogger do
class TestWorker
include ApplicationWorker
end
before do |example|
Sidekiq::Testing.server_middleware do |chain|
chain.add described_class
end
end
after do |example|
Sidekiq::Testing.server_middleware do |chain|
chain.remove described_class
end
end
it 'injects into payload the correlation id', :sidekiq_might_not_need_inline do
expect_any_instance_of(described_class).to receive(:call).and_call_original
expect_any_instance_of(TestWorker).to receive(:perform).with(1234) do
expect(Labkit::Correlation::CorrelationId.current_id).to eq('new-correlation-id')
end
Sidekiq::Client.push(
'queue' => 'test',
'class' => TestWorker,
'args' => [1234],
'correlation_id' => 'new-correlation-id')
end
end
......@@ -38,7 +38,7 @@ describe Gitlab::SidekiqMiddleware do
[
Gitlab::SidekiqMiddleware::Monitor,
Gitlab::SidekiqMiddleware::BatchLoader,
Gitlab::SidekiqMiddleware::CorrelationLogger,
Labkit::Middleware::Sidekiq::Server,
Gitlab::SidekiqMiddleware::InstrumentationLogger,
Gitlab::SidekiqStatus::ServerMiddleware,
Gitlab::SidekiqMiddleware::Metrics,
......@@ -120,7 +120,7 @@ describe Gitlab::SidekiqMiddleware do
# This test ensures that this does not happen
it "invokes the chain" do
expect_any_instance_of(Gitlab::SidekiqStatus::ClientMiddleware).to receive(:call).with(*middleware_expected_args).once.and_call_original
expect_any_instance_of(Gitlab::SidekiqMiddleware::CorrelationInjector).to receive(:call).with(*middleware_expected_args).once.and_call_original
expect_any_instance_of(Labkit::Middleware::Sidekiq::Client).to receive(:call).with(*middleware_expected_args).once.and_call_original
expect { |b| chain.invoke(worker_class_arg, job, queue, redis_pool, &b) }.to yield_control.once
end
......
# frozen_string_literal: true
require 'fast_spec_helper'
require 'active_support/concern'
describe Gitlab::Utils::LazyAttributes do
subject(:klass) do
Class.new do
include Gitlab::Utils::LazyAttributes
lazy_attr_reader :number, type: Numeric
lazy_attr_reader :reader_1, :reader_2
lazy_attr_accessor :incorrect_type, :string_attribute, :accessor_2, type: String
def initialize
@number = -> { 1 }
@reader_1, @reader_2 = 'reader_1', -> { 'reader_2' }
@incorrect_type, @accessor_2 = -> { :incorrect_type }, -> { 'accessor_2' }
end
end
end
context 'class methods' do
it { is_expected.to respond_to(:lazy_attr_reader, :lazy_attr_accessor) }
it { is_expected.not_to respond_to(:define_lazy_reader, :define_lazy_writer) }
end
context 'instance methods' do
subject(:instance) { klass.new }
it do
is_expected.to respond_to(:number, :reader_1, :reader_2, :incorrect_type,
:incorrect_type=, :accessor_2, :accessor_2=,
:string_attribute, :string_attribute=)
end
context 'reading attributes' do
it 'returns the correct values for procs', :aggregate_failures do
expect(instance.number).to eq(1)
expect(instance.reader_2).to eq('reader_2')
expect(instance.accessor_2).to eq('accessor_2')
end
it 'does not return the value if the type did not match what was specified' do
expect(instance.incorrect_type).to be_nil
end
it 'only calls the block once even if it returned `nil`', :aggregate_failures do
expect(instance.instance_variable_get('@number')).to receive(:call).once.and_call_original
expect(instance.instance_variable_get('@accessor_2')).to receive(:call).once.and_call_original
expect(instance.instance_variable_get('@incorrect_type')).to receive(:call).once.and_call_original
2.times do
instance.number
instance.incorrect_type
instance.accessor_2
end
end
end
context 'writing attributes' do
it 'sets the correct values', :aggregate_failures do
instance.string_attribute = -> { 'updated 1' }
instance.accessor_2 = nil
expect(instance.string_attribute).to eq('updated 1')
expect(instance.accessor_2).to be_nil
end
end
end
end
......@@ -389,6 +389,12 @@ describe API::Internal::Base do
end
end
end
it_behaves_like 'storing arguments in the application context' do
let(:expected_params) { { user: key.user.username, project: project.full_path } }
subject { push(key, project) }
end
end
context "access denied" do
......@@ -885,6 +891,12 @@ describe API::Internal::Base do
post api('/internal/post_receive'), params: valid_params
end
it_behaves_like 'storing arguments in the application context' do
let(:expected_params) { { user: user.username, project: project.full_path } }
subject { post api('/internal/post_receive'), params: valid_params }
end
context 'when there are merge_request push options' do
before do
valid_params[:push_options] = ['merge_request.create']
......
......@@ -1626,6 +1626,14 @@ describe API::Projects do
end
end
end
it_behaves_like 'storing arguments in the application context' do
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :public) }
let(:expected_params) { { user: user.username, project: project.full_path } }
subject { get api("/projects/#{project.id}", user) }
end
end
describe 'GET /projects/:id/users' do
......
......@@ -50,10 +50,39 @@ describe Ci::FindExposedArtifactsService do
end
end
shared_examples 'does not find any matches' do
it 'returns empty array' do
expect(subject).to eq []
end
end
let_it_be(:pipeline) { create(:ci_pipeline, project: project) }
subject { described_class.new(project, user).for_pipeline(pipeline) }
context 'with jobs having no exposed artifacts' do
let!(:job) do
create_job_with_artifacts(artifacts: {
paths: ['other_artifacts_0.1.2/doc_sample.txt', 'something-else.html']
})
end
it_behaves_like 'does not find any matches'
end
context 'with jobs having no artifacts (metadata)' do
let!(:job) do
create(:ci_build, pipeline: pipeline, options: {
artifacts: {
expose_as: 'Exposed artifact',
paths: ['other_artifacts_0.1.2/doc_sample.txt', 'something-else.html']
}
})
end
it_behaves_like 'does not find any matches'
end
context 'with jobs having at most 1 matching exposed artifact' do
let!(:job) do
create_job_with_artifacts(artifacts: {
......
......@@ -245,6 +245,12 @@ RSpec.configure do |config|
Rails.cache = caching_store
end
config.around do |example|
# Wrap each example in it's own context to make sure the contexts don't
# leak
Labkit::Context.with_context { example.run }
end
config.around(:each, :clean_gitlab_redis_cache) do |example|
redis_cache_cleanup!
......
# frozen_string_literal: true
RSpec.shared_examples 'storing arguments in the application context' do
around do |example|
Labkit::Context.with_context { example.run }
end
it 'places the expected params in the application context' do
# Stub the clearing of the context so we can validate it later
# The `around` block above makes sure we do clean it up later
allow(Labkit::Context).to receive(:pop)
subject
Labkit::Context.with_context do |context|
expect(context.to_h)
.to include(log_hash(expected_params))
end
end
def log_hash(hash)
hash.transform_keys! { |key| "meta.#{key}" }
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