diff options
Diffstat (limited to 'spec/workers/projects')
4 files changed, 377 insertions, 13 deletions
diff --git a/spec/workers/projects/after_import_worker_spec.rb b/spec/workers/projects/after_import_worker_spec.rb new file mode 100644 index 00000000000..332b547bb66 --- /dev/null +++ b/spec/workers/projects/after_import_worker_spec.rb @@ -0,0 +1,131 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Projects::AfterImportWorker do + include GitHelpers + + subject { worker.perform(project.id) } + + let(:worker) { described_class.new } + let(:project) { create(:project, :repository) } + let(:repository) { project.repository } + let(:sha) { project.commit.sha } + let(:housekeeping_service) { double(:housekeeping_service) } + + describe '#execute' do + before do + allow(Repositories::HousekeepingService) + .to receive(:new).with(project).and_return(housekeeping_service) + + allow(housekeeping_service) + .to receive(:execute).and_yield + + allow(housekeeping_service).to receive(:increment!) + end + + it 'performs housekeeping' do + subject + + expect(housekeeping_service).to have_received(:execute) + end + + context 'with some refs in refs/pull/**/*' do + before do + repository.write_ref('refs/pull/1/head', sha) + repository.write_ref('refs/pull/1/merge', sha) + + subject + end + + it 'removes refs/pull/**/*' do + expect(rugged.references.map(&:name)) + .not_to include(%r{\Arefs/pull/}) + end + end + + Repository::RESERVED_REFS_NAMES.each do |name| + context "with a ref in refs/#{name}/tmp" do + before do + repository.write_ref("refs/#{name}/tmp", sha) + + subject + end + + it "does not remove refs/#{name}/tmp" do + expect(rugged.references.map(&:name)) + .to include("refs/#{name}/tmp") + end + end + end + + context 'when after import action throw non-retriable exception' do + let(:exception) { StandardError.new('after import error') } + + before do + allow_next_instance_of(Repository) do |repository| + allow(repository).to receive(:delete_all_refs_except) + .and_raise(exception) + end + end + + it 'throws after import error' do + expect { subject }.to raise_exception('after import error') + end + end + + context 'when housekeeping service lease is taken' do + let(:exception) { Repositories::HousekeepingService::LeaseTaken.new } + + it 'logs the error message' do + allow_next_instance_of(Repositories::HousekeepingService) do |instance| + expect(instance).to receive(:execute).and_raise(exception) + end + + expect(Gitlab::Import::Logger).to receive(:info).with( + { + message: 'Project housekeeping failed', + project_full_path: project.full_path, + project_id: project.id, + 'error.message' => exception.to_s + }).and_call_original + + subject + end + end + + context 'when after import action throw retriable exception one time' do + let(:exception) { GRPC::DeadlineExceeded.new } + + it 'removes refs/pull/**/*' do + subject + + expect(rugged.references.map(&:name)) + .not_to include(%r{\Arefs/pull/}) + end + + it 'records the failures in the database', :aggregate_failures do + expect_next_instance_of(Repository) do |repository| + expect(repository).to receive(:delete_all_refs_except).and_raise(exception) + expect(repository).to receive(:delete_all_refs_except).and_call_original + end + + subject + + import_failure = ImportFailure.last + + expect(import_failure.source).to eq('delete_all_refs') + expect(import_failure.project_id).to eq(project.id) + expect(import_failure.relation_key).to be_nil + expect(import_failure.relation_index).to be_nil + expect(import_failure.exception_class).to eq('GRPC::DeadlineExceeded') + expect(import_failure.exception_message).to be_present + expect(import_failure.correlation_id_value).not_to be_empty + end + end + + def rugged + rugged_repo(repository) + end + end +end diff --git a/spec/workers/projects/inactive_projects_deletion_cron_worker_spec.rb b/spec/workers/projects/inactive_projects_deletion_cron_worker_spec.rb new file mode 100644 index 00000000000..0e7b4ea504c --- /dev/null +++ b/spec/workers/projects/inactive_projects_deletion_cron_worker_spec.rb @@ -0,0 +1,139 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Projects::InactiveProjectsDeletionCronWorker do + include ProjectHelpers + + describe "#perform" do + subject(:worker) { described_class.new } + + let_it_be(:admin_user) { create(:user, :admin) } + let_it_be(:non_admin_user) { create(:user) } + let_it_be(:new_blank_project) do + create_project_with_statistics.tap do |project| + project.update!(last_activity_at: Time.current) + end + end + + let_it_be(:inactive_blank_project) do + create_project_with_statistics.tap do |project| + project.update!(last_activity_at: 13.months.ago) + end + end + + let_it_be(:inactive_large_project) do + create_project_with_statistics(with_data: true, size_multiplier: 2.gigabytes) + .tap { |project| project.update!(last_activity_at: 2.years.ago) } + end + + let_it_be(:active_large_project) do + create_project_with_statistics(with_data: true, size_multiplier: 2.gigabytes) + .tap { |project| project.update!(last_activity_at: 1.month.ago) } + end + + before do + stub_application_setting(inactive_projects_min_size_mb: 5) + stub_application_setting(inactive_projects_send_warning_email_after_months: 12) + stub_application_setting(inactive_projects_delete_after_months: 14) + end + + context 'when delete inactive projects feature is disabled' do + before do + stub_application_setting(delete_inactive_projects: false) + end + + it 'does not invoke Projects::InactiveProjectsDeletionNotificationWorker' do + expect(::Projects::InactiveProjectsDeletionNotificationWorker).not_to receive(:perform_in) + expect(::Projects::DestroyService).not_to receive(:new) + + worker.perform + end + + it 'does not delete the inactive projects' do + worker.perform + + expect(inactive_large_project.reload.pending_delete).to eq(false) + end + end + + context 'when delete inactive projects feature is enabled' do + before do + stub_application_setting(delete_inactive_projects: true) + end + + context 'when feature flag is disabled' do + before do + stub_feature_flags(inactive_projects_deletion: false) + end + + it 'does not invoke Projects::InactiveProjectsDeletionNotificationWorker' do + expect(::Projects::InactiveProjectsDeletionNotificationWorker).not_to receive(:perform_in) + expect(::Projects::DestroyService).not_to receive(:new) + + worker.perform + end + + it 'does not delete the inactive projects' do + worker.perform + + expect(inactive_large_project.reload.pending_delete).to eq(false) + end + end + + context 'when feature flag is enabled', :clean_gitlab_redis_shared_state, :sidekiq_inline do + let_it_be(:delay) { anything } + + before do + stub_feature_flags(inactive_projects_deletion: true) + end + + it 'invokes Projects::InactiveProjectsDeletionNotificationWorker for inactive projects' do + Gitlab::Redis::SharedState.with do |redis| + expect(redis).to receive(:hset).with('inactive_projects_deletion_warning_email_notified', + "project:#{inactive_large_project.id}", Date.current) + end + expect(::Projects::InactiveProjectsDeletionNotificationWorker).to receive(:perform_in).with( + delay, inactive_large_project.id, deletion_date).and_call_original + expect(::Projects::DestroyService).not_to receive(:new) + + worker.perform + end + + it 'does not invoke InactiveProjectsDeletionNotificationWorker for already notified inactive projects' do + Gitlab::Redis::SharedState.with do |redis| + redis.hset('inactive_projects_deletion_warning_email_notified', "project:#{inactive_large_project.id}", + Date.current.to_s) + end + + expect(::Projects::InactiveProjectsDeletionNotificationWorker).not_to receive(:perform_in) + expect(::Projects::DestroyService).not_to receive(:new) + + worker.perform + end + + it 'invokes Projects::DestroyService for projects that are inactive even after being notified' do + Gitlab::Redis::SharedState.with do |redis| + redis.hset('inactive_projects_deletion_warning_email_notified', "project:#{inactive_large_project.id}", + 15.months.ago.to_date.to_s) + end + + expect(::Projects::InactiveProjectsDeletionNotificationWorker).not_to receive(:perform_in) + expect(::Projects::DestroyService).to receive(:new).with(inactive_large_project, admin_user, {}) + .at_least(:once).and_call_original + + worker.perform + + expect(inactive_large_project.reload.pending_delete).to eq(true) + + Gitlab::Redis::SharedState.with do |redis| + expect(redis.hget('inactive_projects_deletion_warning_email_notified', + "project:#{inactive_large_project.id}")).to be_nil + end + end + end + + it_behaves_like 'an idempotent worker' + end + end +end diff --git a/spec/workers/projects/inactive_projects_deletion_notification_worker_spec.rb b/spec/workers/projects/inactive_projects_deletion_notification_worker_spec.rb new file mode 100644 index 00000000000..3ddfec0d346 --- /dev/null +++ b/spec/workers/projects/inactive_projects_deletion_notification_worker_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Projects::InactiveProjectsDeletionNotificationWorker do + describe "#perform" do + subject(:worker) { described_class.new } + + let_it_be(:deletion_date) { Date.current } + let_it_be(:non_existing_project_id) { non_existing_record_id } + let_it_be(:project) { create(:project) } + + it 'invokes NotificationService and calls inactive_project_deletion_warning' do + expect_next_instance_of(NotificationService) do |notification| + expect(notification).to receive(:inactive_project_deletion_warning).with(project, deletion_date) + end + + worker.perform(project.id, deletion_date) + end + + it 'adds the project_id to redis key that tracks the deletion warning emails' do + worker.perform(project.id, deletion_date) + + Gitlab::Redis::SharedState.with do |redis| + expect(redis.hget('inactive_projects_deletion_warning_email_notified', + "project:#{project.id}")).to eq(Date.current.to_s) + end + end + + it 'rescues and logs the exception if project does not exist' do + expect(Gitlab::ErrorTracking).to receive(:log_exception).with(instance_of(ActiveRecord::RecordNotFound), + { project_id: non_existing_project_id }) + + worker.perform(non_existing_project_id, deletion_date) + end + + it_behaves_like 'an idempotent worker' do + let(:job_args) { [project.id, deletion_date] } + end + end +end diff --git a/spec/workers/projects/record_target_platforms_worker_spec.rb b/spec/workers/projects/record_target_platforms_worker_spec.rb index eb53e3f8608..01852f252b7 100644 --- a/spec/workers/projects/record_target_platforms_worker_spec.rb +++ b/spec/workers/projects/record_target_platforms_worker_spec.rb @@ -7,11 +7,11 @@ RSpec.describe Projects::RecordTargetPlatformsWorker do let_it_be(:swift) { create(:programming_language, name: 'Swift') } let_it_be(:objective_c) { create(:programming_language, name: 'Objective-C') } + let_it_be(:java) { create(:programming_language, name: 'Java') } + let_it_be(:kotlin) { create(:programming_language, name: 'Kotlin') } let_it_be(:project) { create(:project, :repository, detected_repository_languages: true) } let(:worker) { described_class.new } - let(:service_result) { %w(ios osx watchos) } - let(:service_double) { instance_double(Projects::RecordTargetPlatformsService, execute: service_result) } let(:lease_key) { "#{described_class.name.underscore}:#{project.id}" } let(:lease_timeout) { described_class::LEASE_TIMEOUT } @@ -21,16 +21,20 @@ RSpec.describe Projects::RecordTargetPlatformsWorker do stub_exclusive_lease(lease_key, timeout: lease_timeout) end - shared_examples 'performs detection' do - it 'creates and executes a Projects::RecordTargetPlatformService instance for the project', :aggregate_failures do - expect(Projects::RecordTargetPlatformsService).to receive(:new).with(project) { service_double } + shared_examples 'performs detection' do |detector_service_class| + let(:service_double) { instance_double(detector_service_class, execute: service_result) } + + it "creates and executes a #{detector_service_class} instance for the project", :aggregate_failures do + expect(Projects::RecordTargetPlatformsService).to receive(:new) + .with(project, detector_service_class) { service_double } expect(service_double).to receive(:execute) perform end it 'logs extra metadata on done', :aggregate_failures do - expect(Projects::RecordTargetPlatformsService).to receive(:new).with(project) { service_double } + expect(Projects::RecordTargetPlatformsService).to receive(:new) + .with(project, detector_service_class) { service_double } expect(worker).to receive(:log_extra_metadata_on_done).with(:target_platforms, service_result) perform @@ -45,19 +49,68 @@ RSpec.describe Projects::RecordTargetPlatformsWorker do end end - context 'when project uses Swift programming language' do - let!(:repository_language) { create(:repository_language, project: project, programming_language: swift) } + def create_language(language) + create(:repository_language, project: project, programming_language: language) + end + + context 'when project uses programming language for Apple platform' do + let(:service_result) { %w(ios osx watchos) } + + context 'when project uses Swift programming language' do + before do + create_language(swift) + end + + it_behaves_like 'performs detection', Projects::AppleTargetPlatformDetectorService + end + + context 'when project uses Objective-C programming language' do + before do + create_language(objective_c) + end - include_examples 'performs detection' + it_behaves_like 'performs detection', Projects::AppleTargetPlatformDetectorService + end end - context 'when project uses Objective-C programming language' do - let!(:repository_language) { create(:repository_language, project: project, programming_language: objective_c) } + context 'when project uses programming language for Android platform' do + let(:feature_enabled) { true } + let(:service_result) { %w(android) } + + before do + stub_feature_flags(detect_android_projects: feature_enabled) + end + + context 'when project uses Java' do + before do + create_language(java) + end + + it_behaves_like 'performs detection', Projects::AndroidTargetPlatformDetectorService + + context 'when feature flag is disabled' do + let(:feature_enabled) { false } + + it_behaves_like 'does nothing' + end + end + + context 'when project uses Kotlin' do + before do + create_language(kotlin) + end + + it_behaves_like 'performs detection', Projects::AndroidTargetPlatformDetectorService - include_examples 'performs detection' + context 'when feature flag is disabled' do + let(:feature_enabled) { false } + + it_behaves_like 'does nothing' + end + end end - context 'when the project does not contain programming languages for Apple platforms' do + context 'when the project does not use programming languages for Apple or Android platforms' do it_behaves_like 'does nothing' end |