From 602df4e57a611952ddbec995d85bf1f290c610e6 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Fri, 18 Apr 2014 00:18:24 +0400 Subject: [PATCH] #376: added specs --- app/jobs/restart_nodes_job.rb | 2 +- spec/factories/node_instructions.rb | 6 +++++ spec/factories/rpm_build_nodes.rb | 6 +++++ spec/factories/users.rb | 4 +++ spec/jobs/restart_nodes_job_spec.rb | 40 ++++++++++++++++++++++++++++ spec/models/node_instruction_spec.rb | 9 +++++++ 6 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 spec/factories/node_instructions.rb create mode 100644 spec/factories/rpm_build_nodes.rb create mode 100644 spec/jobs/restart_nodes_job_spec.rb create mode 100644 spec/models/node_instruction_spec.rb diff --git a/app/jobs/restart_nodes_job.rb b/app/jobs/restart_nodes_job.rb index b0063d497..107b042fc 100644 --- a/app/jobs/restart_nodes_job.rb +++ b/app/jobs/restart_nodes_job.rb @@ -3,7 +3,7 @@ class RestartNodesJob def self.perform return if NodeInstruction.all_locked? - available_nodes = RpmBuildNode.all.map{ |n| n.user_id }.compact.uniq + available_nodes = RpmBuildNode.all.map{ |n| n.user_id if n.user.try(:system?) }.compact.uniq NodeInstruction.where(status: NodeInstruction::READY). where.not(user_id: available_nodes).find_each(&:restart) end diff --git a/spec/factories/node_instructions.rb b/spec/factories/node_instructions.rb new file mode 100644 index 000000000..82a8aed0b --- /dev/null +++ b/spec/factories/node_instructions.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :node_instruction do + association :user, factory: :system_user + instruction { FactoryGirl.generate(:string) } + end +end diff --git a/spec/factories/rpm_build_nodes.rb b/spec/factories/rpm_build_nodes.rb new file mode 100644 index 000000000..6bb155698 --- /dev/null +++ b/spec/factories/rpm_build_nodes.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :rpm_build_node do + id { FactoryGirl.generate(:string) } + user_id { FactoryGirl.create(:user).id } + end +end diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 3bd5fc3a5..86af559fa 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -12,4 +12,8 @@ FactoryGirl.define do factory :admin, parent: :user do role 'admin' end + + factory :system_user, parent: :user do + role 'system' + end end diff --git a/spec/jobs/restart_nodes_job_spec.rb b/spec/jobs/restart_nodes_job_spec.rb new file mode 100644 index 000000000..b84ebc129 --- /dev/null +++ b/spec/jobs/restart_nodes_job_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe RestartNodesJob do + + it 'ensures that not raises error' do + lambda do + RestartNodesJob.perform + end.should_not raise_exception + end + + it 'ensures that do nothing when all instructions disabled' do + NodeInstruction.lock_all + expect(RpmBuildNode).to_not receive(:all) + RestartNodesJob.perform + end + + it 'ensures that creates tasks' do + allow_any_instance_of(NodeInstruction).to receive(:perform_restart) + + # ABF active node + ni1 = FactoryGirl.create(:node_instruction) + FactoryGirl.create(:rpm_build_node, user_id: ni1.user_id) + + # User node + FactoryGirl.create(:rpm_build_node) + + FactoryGirl.create(:node_instruction, status: NodeInstruction::DISABLED) + ni2 = FactoryGirl.create(:node_instruction, status: NodeInstruction::RESTARTING) + FactoryGirl.create(:node_instruction, status: NodeInstruction::FAILED) + + ni3 = FactoryGirl.create(:node_instruction) + + RestartNodesJob.perform + + NodeInstruction.where(status: NodeInstruction::RESTARTING).should have(2).items + NodeInstruction.where(status: NodeInstruction::RESTARTING).should include(ni2, ni3) + NodeInstruction.where(status: NodeInstruction::RESTARTING).should_not include(ni1) + end + +end diff --git a/spec/models/node_instruction_spec.rb b/spec/models/node_instruction_spec.rb new file mode 100644 index 000000000..99d675e22 --- /dev/null +++ b/spec/models/node_instruction_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' + +describe NodeInstruction do + + it 'is valid given valid attributes' do + FactoryGirl.build(:node_instruction).should be_valid + end + +end