2020-01-28 10:08:36 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'fast_spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::SidekiqConfig::CliMethods do
|
2020-01-28 10:08:36 -05:00
|
|
|
let(:dummy_root) { '/tmp/' }
|
|
|
|
|
|
|
|
describe '.worker_queues' do
|
|
|
|
def expand_path(path)
|
|
|
|
File.join(dummy_root, path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_exists(exists: true)
|
2021-11-08 10:13:35 -05:00
|
|
|
['app/workers/all_queues.yml', 'ee/app/workers/all_queues.yml', 'jh/app/workers/all_queues.yml'].each do |path|
|
2020-01-28 10:08:36 -05:00
|
|
|
allow(File).to receive(:exist?).with(expand_path(path)).and_return(exists)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-08 10:13:35 -05:00
|
|
|
def stub_contents(foss_queues, ee_queues, jh_queues)
|
2020-01-28 10:08:36 -05:00
|
|
|
allow(YAML).to receive(:load_file)
|
|
|
|
.with(expand_path('app/workers/all_queues.yml'))
|
|
|
|
.and_return(foss_queues)
|
|
|
|
|
|
|
|
allow(YAML).to receive(:load_file)
|
|
|
|
.with(expand_path('ee/app/workers/all_queues.yml'))
|
|
|
|
.and_return(ee_queues)
|
2021-11-08 10:13:35 -05:00
|
|
|
|
|
|
|
allow(YAML).to receive(:load_file)
|
|
|
|
.with(expand_path('jh/app/workers/all_queues.yml'))
|
|
|
|
.and_return(jh_queues)
|
2020-01-28 10:08:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
described_class.clear_memoization!
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the file exists' do
|
|
|
|
before do
|
|
|
|
stub_exists(exists: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'valid file contents' do
|
|
|
|
it 'memoizes the result' do
|
|
|
|
result = described_class.worker_queues(dummy_root)
|
|
|
|
|
|
|
|
stub_exists(exists: false)
|
|
|
|
|
|
|
|
expect(described_class.worker_queues(dummy_root)).to eq(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'flattens and joins the contents' do
|
2021-11-08 10:13:35 -05:00
|
|
|
expected_queues = %w[queue_a]
|
|
|
|
expected_queues << 'queue_b' if Gitlab.ee?
|
|
|
|
expected_queues << 'queue_c' if Gitlab.jh?
|
2020-01-28 10:08:36 -05:00
|
|
|
|
|
|
|
expect(described_class.worker_queues(dummy_root))
|
|
|
|
.to match_array(expected_queues)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the file contains an array of hashes' do
|
|
|
|
before do
|
2021-11-08 10:13:35 -05:00
|
|
|
stub_contents([{ name: 'queue_a' }], [{ name: 'queue_b' }], [{ name: 'queue_c' }])
|
2020-01-28 10:08:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'valid file contents'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the file does not exist' do
|
|
|
|
before do
|
|
|
|
stub_exists(exists: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty array' do
|
|
|
|
expect(described_class.worker_queues(dummy_root)).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.expand_queues' do
|
2020-02-14 04:08:43 -05:00
|
|
|
let(:worker_queues) do
|
2020-05-29 14:08:26 -04:00
|
|
|
[
|
2020-06-08 14:08:27 -04:00
|
|
|
'cronjob:import_stuck_project_import_jobs',
|
2020-05-29 14:08:26 -04:00
|
|
|
'cronjob:jira_import_stuck_jira_import_jobs',
|
|
|
|
'cronjob:stuck_merge_jobs',
|
|
|
|
'post_receive'
|
|
|
|
]
|
2020-01-28 10:08:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults the value of the second argument to .worker_queues' do
|
|
|
|
allow(described_class).to receive(:worker_queues).and_return([])
|
|
|
|
|
|
|
|
expect(described_class.expand_queues(['cronjob']))
|
|
|
|
.to contain_exactly('cronjob')
|
|
|
|
|
2020-02-14 04:08:43 -05:00
|
|
|
allow(described_class).to receive(:worker_queues).and_return(worker_queues)
|
2020-01-28 10:08:36 -05:00
|
|
|
|
|
|
|
expect(described_class.expand_queues(['cronjob']))
|
2020-06-08 14:08:27 -04:00
|
|
|
.to contain_exactly(
|
|
|
|
'cronjob',
|
|
|
|
'cronjob:import_stuck_project_import_jobs',
|
|
|
|
'cronjob:jira_import_stuck_jira_import_jobs',
|
|
|
|
'cronjob:stuck_merge_jobs'
|
|
|
|
)
|
2020-01-28 10:08:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'expands queue namespaces to concrete queue names' do
|
2020-02-14 04:08:43 -05:00
|
|
|
expect(described_class.expand_queues(['cronjob'], worker_queues))
|
2020-06-08 14:08:27 -04:00
|
|
|
.to contain_exactly(
|
|
|
|
'cronjob',
|
|
|
|
'cronjob:import_stuck_project_import_jobs',
|
|
|
|
'cronjob:jira_import_stuck_jira_import_jobs',
|
|
|
|
'cronjob:stuck_merge_jobs'
|
|
|
|
)
|
2020-01-28 10:08:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'lets concrete queue names pass through' do
|
2020-02-14 04:08:43 -05:00
|
|
|
expect(described_class.expand_queues(['post_receive'], worker_queues))
|
2020-01-28 10:08:36 -05:00
|
|
|
.to contain_exactly('post_receive')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'lets unknown queues pass through' do
|
2020-02-14 04:08:43 -05:00
|
|
|
expect(described_class.expand_queues(['unknown'], worker_queues))
|
2020-01-28 10:08:36 -05:00
|
|
|
.to contain_exactly('unknown')
|
|
|
|
end
|
|
|
|
end
|
2020-02-14 04:08:43 -05:00
|
|
|
|
2021-04-19 08:09:04 -04:00
|
|
|
describe '.query_queues' do
|
|
|
|
let(:worker_metadatas) do
|
2020-02-14 04:08:43 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
name: 'a',
|
|
|
|
feature_category: :category_a,
|
|
|
|
has_external_dependencies: false,
|
2020-03-12 14:09:28 -04:00
|
|
|
urgency: :low,
|
2020-05-25 23:08:02 -04:00
|
|
|
resource_boundary: :cpu,
|
|
|
|
tags: [:no_disk_io, :git_access]
|
2020-02-14 04:08:43 -05:00
|
|
|
},
|
|
|
|
{
|
2020-02-25 13:09:02 -05:00
|
|
|
name: 'a:2',
|
2020-02-14 04:08:43 -05:00
|
|
|
feature_category: :category_a,
|
|
|
|
has_external_dependencies: false,
|
2020-03-02 13:07:42 -05:00
|
|
|
urgency: :high,
|
2020-05-25 23:08:02 -04:00
|
|
|
resource_boundary: :none,
|
|
|
|
tags: [:git_access]
|
2020-02-14 04:08:43 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'b',
|
|
|
|
feature_category: :category_b,
|
|
|
|
has_external_dependencies: true,
|
2020-03-02 13:07:42 -05:00
|
|
|
urgency: :high,
|
2020-05-25 23:08:02 -04:00
|
|
|
resource_boundary: :memory,
|
|
|
|
tags: [:no_disk_io]
|
2020-02-14 04:08:43 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'c',
|
|
|
|
feature_category: :category_c,
|
|
|
|
has_external_dependencies: false,
|
2020-03-12 14:09:28 -04:00
|
|
|
urgency: :throttled,
|
2020-05-25 23:08:02 -04:00
|
|
|
resource_boundary: :memory,
|
|
|
|
tags: []
|
2020-02-14 04:08:43 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2021-04-19 08:09:04 -04:00
|
|
|
let(:worker_matcher) { double(:WorkerMatcher) }
|
|
|
|
let(:query) { 'feature_category=category_a,category_c' }
|
2020-02-14 04:08:43 -05:00
|
|
|
|
2021-04-19 08:09:04 -04:00
|
|
|
before do
|
|
|
|
allow(::Gitlab::SidekiqConfig::WorkerMatcher).to receive(:new).with(query).and_return(worker_matcher)
|
|
|
|
allow(worker_matcher).to receive(:match?).and_return(true, true, false, true)
|
2020-02-14 04:08:43 -05:00
|
|
|
end
|
|
|
|
|
2021-04-19 08:09:04 -04:00
|
|
|
it 'returns the queue names of matched workers' do
|
|
|
|
expect(described_class.query_queues(query, worker_metadatas)).to match(%w(a a:2 c))
|
2020-02-14 04:08:43 -05:00
|
|
|
end
|
|
|
|
end
|
2020-01-28 10:08:36 -05:00
|
|
|
end
|