Commit 92b4bfe2 authored by Michael Kozono's avatar Michael Kozono

Merge branch 'e2e-data-cleanup-worker' into 'master'

Add test data cleanup worker

See merge request gitlab-org/gitlab!82551
parents 64364c8e d77a1f2a
......@@ -570,6 +570,15 @@
:weight: 1
:idempotent:
:tags: []
- :name: cronjob:quality_test_data_cleanup
:worker_name: Quality::TestDataCleanupWorker
:feature_category: :quality_management
:has_external_dependencies:
:urgency: :low
:resource_boundary: :unknown
:weight: 1
:idempotent: true
:tags: []
- :name: cronjob:releases_manage_evidence
:worker_name: Releases::ManageEvidenceWorker
:feature_category: :release_evidence
......
# frozen_string_literal: true
module Quality
class TestDataCleanupWorker
include ApplicationWorker
data_consistency :always
feature_category :quality_management
urgency :low
include CronjobQueue
idempotent!
KEEP_RECENT_DATA_DAY = 3
GROUP_PATH_PATTERN = 'test-group-fulfillment'
GROUP_OWNER_EMAIL_PATTERN = %w(test-user- gitlab-qa-user qa-user-).freeze
# Remove test groups generated in E2E tests on gstg
# rubocop: disable CodeReuse/ActiveRecord
def perform
return unless Gitlab.staging?
Group.where('path like ?', "#{GROUP_PATH_PATTERN}%").where('created_at < ?', KEEP_RECENT_DATA_DAY.days.ago).each do |group|
next unless GROUP_OWNER_EMAIL_PATTERN.any? { |pattern| group.owners.first.email.include?(pattern) }
with_context(namespace: group, user: group.owners.first) do
Groups::DestroyService.new(group, group.owners.first).execute
end
end
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
......@@ -118,5 +118,14 @@ FactoryBot.define do
create(:crm_settings, group: group, enabled: true)
end
end
trait :test_group do
path { "test-group-fulfillment#{SecureRandom.hex(4)}" }
created_at { 4.days.ago }
after(:create) do |group|
group.add_owner(create(:user, email: "test-user-#{SecureRandom.hex(4)}@test.com"))
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Quality::TestDataCleanupWorker do
subject { described_class.new }
shared_examples 'successful deletion' do
before do
allow(Gitlab).to receive(:staging?).and_return(true)
end
it 'removes test groups' do
expect { subject.perform }.to change(Group, :count).by(-test_group_count)
end
end
describe "#perform" do
context 'with multiple test groups to remove' do
let(:test_group_count) { 5 }
let!(:groups_to_remove) { create_list(:group, test_group_count, :test_group) }
let!(:group_to_keep) { create(:group, path: 'test-group-fulfillment-keep', created_at: 1.day.ago) }
let!(:non_test_group) { create(:group) }
let(:non_test_owner_group) { create(:group, path: 'test-group-fulfillment1234', created_at: 4.days.ago) }
before do
non_test_owner_group.add_owner(create(:user))
end
it_behaves_like 'successful deletion'
end
context 'with paid groups' do
let(:test_group_count) { 1 }
let!(:paid_group) { create(:group, :test_group) }
before do
allow(paid_group).to receive(:paid?).and_return(true)
end
it_behaves_like 'successful deletion'
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