Commit d19de7e0 authored by Kerri Miller's avatar Kerri Miller

Merge branch 'update-quality-cleanup-work-prod' into 'master'

Create a worker to handle test groups created on production

See merge request gitlab-org/gitlab!83585
parents caf1c66a bcc945d2
...@@ -390,6 +390,15 @@ ...@@ -390,6 +390,15 @@
:weight: 1 :weight: 1
:idempotent: :idempotent:
:tags: [] :tags: []
- :name: cronjob:quality_test_group_cleanup_prod
:worker_name: Quality::TestGroupCleanupProdWorker
:feature_category: :quality_management
:has_external_dependencies:
:urgency: :low
:resource_boundary: :unknown
:weight: 1
:idempotent: true
:tags: []
- :name: cronjob:security_create_orchestration_policy - :name: cronjob:security_create_orchestration_policy
:worker_name: Security::CreateOrchestrationPolicyWorker :worker_name: Security::CreateOrchestrationPolicyWorker
:feature_category: :security_orchestration :feature_category: :security_orchestration
......
# frozen_string_literal: true
module Quality
class TestGroupCleanupProdWorker
include ApplicationWorker
data_consistency :always
feature_category :quality_management
urgency :low
include CronjobQueue
idempotent!
QA_USER_IN_PRODUCTION = 'gitlab-qa'
# This is a temporary workaround for issue https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82692
# Consolidates the logic with quality::test_data_cleanup_worker once we enforce the naming schema for test data
# rubocop: disable CodeReuse/ActiveRecord
def perform
return unless Gitlab.com?
return unless (qa_user_id = User.find_by(username: QA_USER_IN_PRODUCTION)&.id).present?
Group.joins(:all_group_members).where(members: { user_id: qa_user_id }).find_each do |group|
with_context(namespace: group, user: group.owners.first) do
Groups::DestroyService.new(group, group.owners.first).execute if group.marked_for_deletion?
end
end
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Quality::TestGroupCleanupProdWorker do
let(:qa_user) { create(:user, username: 'test-user') }
let!(:groups_to_remove) { create_list(:group, 3) }
let!(:group_to_keep) { create(:group, :test_group) }
let!(:non_test_group) { create(:group) }
subject { described_class.new }
describe "#perform" do
before do
stub_licensed_features(adjourned_deletion_for_projects_and_groups: true)
groups_to_remove.each do |group|
group.add_owner(create(:user))
create(:group_member, :developer, group: group, user: qa_user)
create(:group_deletion_schedule, group: group, marked_for_deletion_on: 1.day.ago)
end
group_to_keep.add_owner(create(:user))
end
context 'when not in production environment' do
before do
allow(Gitlab).to receive(:com?).and_return(false)
stub_const("Quality::TestGroupCleanupProdWorker::QA_USER_IN_PRODUCTION", 'test-user')
end
it 'does not remove any groups' do
expect { subject.perform }.to not_change(Group, :count)
end
end
context 'when qa user does not exist' do
before do
allow(Gitlab).to receive(:com?).and_return(true)
stub_const("Quality::TestGroupCleanupProdWorker::QA_USER_IN_PRODUCTION", 'other-test-user')
end
it 'does not remove any groups' do
expect { subject.perform }.to not_change(Group, :count)
end
end
context 'with multiple groups to remove' do
before do
allow(Gitlab).to receive(:com?).and_return(true)
stub_const("Quality::TestGroupCleanupProdWorker::QA_USER_IN_PRODUCTION", 'test-user')
end
it 'successfully removes test groups' do
expect { subject.perform }.to change(Group, :count).by(-3)
end
end
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