spec/models/logical/naf/job_creator_spec.rb in naf-1.1.4 vs spec/models/logical/naf/job_creator_spec.rb in naf-2.0.0

- old
+ new

@@ -1,32 +1,102 @@ require 'spec_helper' module Logical module Naf describe JobCreator do - context "with regard to queuing from an application_schedule" do - context "a new queued job should get from an application_schedule" do - let(:app_schedule_for_canary) { - tab = FactoryGirl.create(:canary_app_schedule_affinity_tab) - tab.application_schedule - } - let(:job_creator) { JobCreator.new } + let!(:job_creator) { ::Logical::Naf::JobCreator.new } + let!(:historical_job) { FactoryGirl.create(:job) } + let(:application) { FactoryGirl.create(:scheduled_application) } - before do - ::Naf::HistoricalJob.delete_all - ::Naf::HistoricalJobAffinityTab.delete_all - ::Naf::ApplicationSchedule.destroy_all - app_schedule_for_canary.enqueue_backlogs = true - end + describe '#queue_application' do + it 'not queue an application if the group limit is less than number of queued/running jobs' do + historical_job.application_run_group_name = application.application_schedule.application_run_group_name + historical_job.command = application.command + historical_job.save! + job_creator.create_queue_job(historical_job) - it "affinities" do - job_creator.queue_application_schedule(app_schedule_for_canary) - ::Naf::HistoricalJob.first.historical_job_affinity_tabs.map(&:affinity_id). - should == app_schedule_for_canary.application_schedule_affinity_tabs.map(&:affinity_id) - end + job_creator.queue_application(application, + application.application_schedule.application_run_group_restriction, + application.application_schedule.application_run_group_name).should be_nil end + + it 'return a historical_job that has been queued' do + job_creator.queue_application(application, + application.application_schedule.application_run_group_restriction, + application.application_schedule.application_run_group_name).should be_instance_of(::Naf::HistoricalJob) + end + + it 'create affinity tabs when affinities are present' do + job_creator.queue_application(application, + application.application_schedule.application_run_group_restriction, + application.application_schedule.application_run_group_name, + 1, + 0, + [::Naf::Affinity.first]) + ::Naf::HistoricalJobAffinityTab.should have(1).records + end end + + describe '#retrieve_jobs' do + before do + FactoryGirl.create(:queued_job, application_run_group_name: 'Test', + application_run_group_limit: 1) + end + + it 'return the correct records' do + job_creator.retrieve_jobs(::Naf::QueuedJob, '::Naf::QueuedJob.test hello world', 'Test'). + count.to_i.should == 1 + end + end + + describe '#queue_application_schedule' do + it 'raise error if schedule has been queued' do + expect { job_creator.queue_application_schedule(application.application_schedule, [application.application_schedule.id]) }. + to raise_error(::Naf::HistoricalJob::JobPrerequisiteLoop) + end + + it 'return a historical_job that has been queued' do + job_creator.queue_application_schedule(application.application_schedule).should be_instance_of(::Naf::HistoricalJob) + end + end + + describe '#queue_rails_job' do + it 'create and return a historical_job' do + job = job_creator.queue_rails_job(:command) + job.should be_instance_of(::Naf::HistoricalJob) + end + + it 'create affinity tabs when affinites are present' do + job_creator.queue_rails_job(:command, + ::Naf::ApplicationRunGroupRestriction.limited_per_all_machines, + :command, + 1, + 0, + [::Naf::Affinity.first]) + + ::Naf::HistoricalJobAffinityTab.should have(1).records + end + end + + describe '#verify_and_create_prerequisites' do + before do + prereq_job = FactoryGirl.create(:job) + job_creator.verify_and_create_prerequisites(historical_job, [prereq_job]) + end + + it 'insert a row correctly into naf.historical_job_prerequisites' do + ::Naf::HistoricalJobPrerequisite.should have(1).records + end + end + + describe '#create_queue_job' do + it 'insert a row correctly into naf.queued_jobs' do + job_creator.create_queue_job(historical_job) + ::Naf::QueuedJob.should have(1).records + end + end + end end end