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

- old
+ new

@@ -2,12 +2,13 @@ module Logical module Naf describe Machine do - let(:physical_machine) { FactoryGirl.create(:machine) } - let(:physical_machine_two) { FactoryGirl.create(:machine_two) } + let!(:physical_machine) { FactoryGirl.create(:machine, server_name: 'example.com') } + let!(:physical_machine_two) { FactoryGirl.create(:machine_two) } + let!(:logical_machine) { ::Logical::Naf::Machine.new(physical_machine) } let(:columns) { [:id, :server_name, :server_address, :server_note, :enabled, @@ -16,46 +17,115 @@ :last_seen_alive_at, :log_level, :affinities, :marked_down] } - it "to_hash should return with the specified columns" do - logical_machine = Machine.new(physical_machine) - logical_machine.to_hash.keys.should eql(columns) + describe '#self.all' do + it 'return an array of logical wrappers around machines' do + ::Logical::Naf::Machine.all.map(&:id).should == [physical_machine.id, physical_machine_two.id] + end end - it "should alias the thread_pool_size method, as process_pool_size" do - logical_machine = Machine.new(physical_machine) - logical_machine.should_receive(:thread_pool_size).and_return(5) - logical_machine.process_pool_size + describe '#process_pool_size' do + it 'return the correct value' do + physical_machine.thread_pool_size = 5 + logical_machine.process_pool_size.should == 5 + end end - it "should render last_checked_schedules_at nicely" do - physical_machine.mark_checked_schedule - logical_machine = Machine.new(physical_machine) - logical_machine.should_receive(:time_ago_in_words).and_return("") - logical_machine.last_checked_schedules_at.split(',').first.should =~ /ago$/ + describe '#last_checked_schedules_at' do + it 'render result nicely' do + physical_machine.mark_checked_schedule + logical_machine.should_receive(:time_ago_in_words).and_return('') + logical_machine.last_checked_schedules_at.split(',').first.should =~ /ago$/ + end end - it "should render last_seen_alive_at nicely" do - physical_machine.mark_alive - logical_machine = Machine.new(physical_machine) - logical_machine.should_receive(:time_ago_in_words).and_return("") - logical_machine.last_seen_alive_at.split(',').first.should =~ /ago$/ + describe '#last_seen_alive_at' do + it 'render result nicely' do + physical_machine.mark_alive + logical_machine.should_receive(:time_ago_in_words).and_return('') + logical_machine.last_seen_alive_at.split(',').first.should =~ /ago$/ + end end - context "Class Methods," do - it "all should return an array of logical wrappers around machines" do - machine = physical_machine - machine_two = physical_machine_two + describe '#to_hash' do + it 'return with the specified columns' do + logical_machine.to_hash.keys.should == columns + end + end - logical_machine = Machine.new(machine) - Machine.all.map(&:id).should include(machine.id) - Machine.all.map(&:id).should include(machine_two.id) - Machine.all.should be_a(Array) - Machine.all.should have(2).items + describe '#affinities' do + let!(:slot) { FactoryGirl.create(:normal_machine_affinity_slot, machine: physical_machine) } + + it 'return short_name and affinity_parameter when both are present' do + slot.update_attributes!(affinity_parameter: 5) + logical_machine.affinities.should == 'short_name(5.0)' end + + it 'return short_name when affinity_parameter is not present' do + logical_machine.affinities.should == 'short_name' + end + + it 'return classification and affinity names as last resort' do + slot.affinity.update_attributes!(affinity_short_name: nil) + logical_machine.affinities.should == 'purpose_normal' + end end + + describe '#name' do + it 'return short_name when present' do + physical_machine.short_name = 'short_name1' + logical_machine.name.should == 'short_name1' + end + + it 'return server_name when short_name is not present' do + physical_machine.short_name = nil + logical_machine.name.should == 'example.com' + end + + it 'return server_address when server_name is not present' do + physical_machine.short_name = nil + physical_machine.server_name = nil + logical_machine.name.should == '0.0.0.1' + end + end + + describe '#status' do + let!(:runner) { FactoryGirl.create(:machine_runner, machine: physical_machine) } + let!(:invocation) { FactoryGirl.create(:machine_runner_invocation, machine_runner: runner) } + + it 'report correctly when runner is up' do + hash = { + server_name: logical_machine.name, + status: 'Good', + notes: '' + } + logical_machine.status.should == hash + end + + it 'report correctly when runner is down' do + hash = { + server_name: logical_machine.name, + status: 'Bad', + notes: 'Runner down' + } + invocation.update_attributes!(wind_down_at: Time.zone.now, dead_at: Time.zone.now) + logical_machine.status.should == hash + end + end + + describe '#runner' do + it 'return server_name when present' do + logical_machine.runner.should == 'example.com' + end + + it 'return server_address when server_address is not present' do + physical_machine.server_name = nil + logical_machine.runner.should == '0.0.0.1' + end + end + end end end