Project

General

Profile

Download (5.95 KB) Statistics
| Branch: | Tag: | Revision:
90b83222 Ohad Levy
require 'test_helper'

class OrchestrationTest < ActiveSupport::TestCase
0f759221 Tom Caspy
module Orchestration::TestModule
extend ActiveSupport::Concern

included do
register_rebuild(:rebuild_test, N_('TEST'))
end

def rebuild_test
end
end

module Orchestration::HostTest
extend ActiveSupport::Concern

included do
register_rebuild(:rebuild_host, N_('HOST'))
ff5900e9 Trey Dockendorf
register_rebuild(:rebuild_tftp, N_('TFTP'))
0f759221 Tom Caspy
end

def rebuild_host
end
end

90b83222 Ohad Levy
def test_host_should_have_queue
h = Host.new
assert_respond_to h, :queue
end
d1eb00f3 Greg Sutcliffe
43c4bd72 Marek Hulan
setup do
8c6bc83e Marek Hulan
@host = FactoryBot.create(:host)
@nic = FactoryBot.create(:nic_managed, :host => @host, :ip => '192.168.0.2')
43c4bd72 Marek Hulan
end

d1eb00f3 Greg Sutcliffe
test "test host can call protected queue methods" do
d6bc6b86 Shimon Shtein
class Host::Test1 < Host::Base
d1eb00f3 Greg Sutcliffe
include Orchestration
def test_execute(method)
execute({:action => [self, method]})
end
abd8f1d1 Daniel Lobato
d1eb00f3 Greg Sutcliffe
protected
abd8f1d1 Daniel Lobato
c98eec53 Michael Moll
def setTest
true
end
d1eb00f3 Greg Sutcliffe
end
d6bc6b86 Shimon Shtein
h = Host::Test1.new
d1eb00f3 Greg Sutcliffe
assert h.test_execute(:setTest)
assert_raise Foreman::Exception do
h.test_execute(:noSuchTest)
end
end

fd8bf7f1 Lukas Zapletal
class Host::WithDummyAction < Host::Base
include Orchestration
after_validation :queue_test

def queue_test
queue.create(
:name => 'dummy action',
:priority => 1,
:action => [self, :setAction]
)
end
d6bc6b86 Shimon Shtein
fd8bf7f1 Lukas Zapletal
def setAction
true
d6bc6b86 Shimon Shtein
end
fd8bf7f1 Lukas Zapletal
end

test "test dummy action host can be created and calls update_cache" do
uuid = '710d4a8f-b1b6-47f5-9ef5-5892a19dabcd'
Foreman.stubs(:uuid).returns(uuid)
h = Host::WithDummyAction.new(:name => "test1")
h.stubs(:skip_orchestration?).returns(false)
Rails.cache.expects(:write).with(uuid, any_parameters).at_least_once.returns(true)
h.save!
end
d6bc6b86 Shimon Shtein
fd8bf7f1 Lukas Zapletal
test "test dummy action host compensates queue if active record not saved" do
h = Host::WithDummyAction.new(:name => 'test1')
d6bc6b86 Shimon Shtein
h.stubs(:skip_orchestration?).returns(false)
h.stubs(:update_cache).returns(true)
h.stubs(:_create_record).raises(ActiveRecord::InvalidForeignKey, 'Fake foreign key exception')

# rollback should be called
fd8bf7f1 Lukas Zapletal
h.expects(:delAction).returns(true)
d6bc6b86 Shimon Shtein
assert_raise ActiveRecord::InvalidForeignKey do
h.save!
end
end

041436c8 Timo Goebel
test "parameters can be passed to queue methods" do
class Host::Test < Host::Base
include Orchestration
def test_execute(method)
execute({:action => [self, method, 'abc']})
end

protected

c98eec53 Michael Moll
def setTest(param)
"got #{param}"
end
041436c8 Timo Goebel
end
h = Host::Test.new
h.expects(:setTest).with('abc').returns(true)
assert h.test_execute(:setTest)
end

43c4bd72 Marek Hulan
test "orchestration can clone object with type attribute" do
@nic.ip = '192.168.0.1'
93d1e467 Daniel Lobato
@nic.type = 'Nic::Managed'
43c4bd72 Marek Hulan
clone = @nic.send :setup_object_clone, @nic
refute_equal @nic.object_id, clone.object_id
refute_equal @nic.updated_at, clone.updated_at
assert_equal '192.168.0.2', clone.ip
assert_equal 'Nic::Managed', clone.type
end

test "orchestration can clone object and execute block if given before old attributes are assigned" do
@nic.ip = '192.168.0.1'
2d8b4fef Daniel Lobato
clone = @nic.send(:setup_object_clone, @nic) { |c| c.mac, c.ip = 'AA:AA:AA:AA:AA:AA', 'override this' }
43c4bd72 Marek Hulan
refute_equal @nic.object_id, clone.object_id
assert_equal 'AA:AA:AA:AA:AA:AA', clone.mac
assert_equal '192.168.0.2', clone.ip
end

test "orchestration can clone object with belongs_to associations by updating association id" do
# in rails 2 we had to reload associations, this tests prevents regressions after we dropped it in rails 3
8c6bc83e Marek Hulan
@host2 = FactoryBot.create(:host)
43c4bd72 Marek Hulan
@nic.host_id = @host2.id
clone = @nic.send(:setup_object_clone, @nic)
refute_equal @nic.object_id, clone.object_id
assert_equal @host, clone.host
assert_equal @host2, @nic.host
end
0f5c60f2 Marek Hulan
test '#valid? does not trigger cloning in !unattended mode' do
original, SETTINGS[:unattended] = SETTINGS[:unattended], false
@nic.expects(:setup_clone).never
@nic.valid?
SETTINGS[:unattended] = original
end
2fba6ad7 Ondrej Prazak
edd5310f Dominic Cleal
context "when registering orchestration rebuild methods" do
2fba6ad7 Ondrej Prazak
setup do
596cf7cf Timo Goebel
@klass = Class.new(ApplicationRecord) do
edd5310f Dominic Cleal
include Orchestration
include Orchestration::TestModule
end
2fba6ad7 Ondrej Prazak
end

test "register_rebuild can register methods" do
edd5310f Dominic Cleal
assert @klass.respond_to? :register_rebuild
assert @klass.ancestors.include? Orchestration::TestModule
2fba6ad7 Ondrej Prazak
end

test "we can retrieve registered methods" do
e0910b7e Michael Moll
assert @klass.rebuild_methods.key?(:rebuild_test)
2fba6ad7 Ondrej Prazak
end

test "we cannot override already subscribed methods" do
edd5310f Dominic Cleal
@klass.send(:include, Orchestration::HostTest)

2fba6ad7 Ondrej Prazak
module Orchestration::HostTest2
extend ActiveSupport::Concern

included do
register_rebuild(:rebuild_host, N_('HOST'))
end

def rebuild_host
end
end
edd5310f Dominic Cleal
assert_raises(RuntimeError) { @klass.send :include, Orchestration::HostTest2 }
2fba6ad7 Ondrej Prazak
end
end
b7cc39f8 Dominic Cleal
ff5900e9 Trey Dockendorf
context "when getting orchestration methods" do
test "get all from rebuild_methods_for all" do
assert_equal @host.class.rebuild_methods.values, @host.class.rebuild_methods_for.values
end

test "get all from rebuild_methods_for empty" do
assert_equal @host.class.rebuild_methods.values, @host.class.rebuild_methods_for([]).values
end

test "get one from rebuild_methods_for" do
edd5310f Dominic Cleal
assert_equal ['TFTP'], @nic.class.rebuild_methods_for(['TFTP']).values
ff5900e9 Trey Dockendorf
end
end

b7cc39f8 Dominic Cleal
describe '#attr_equivalent?' do
test 'two identical strings are equal' do
assert_equal true, @host.send(:attr_equivalent?, 'test', 'test')
end

test 'nil and an empty string are equal' do
assert_equal true, @host.send(:attr_equivalent?, '', nil)
end

test 'two different strings are not equal' do
assert_equal false, @host.send(:attr_equivalent?, 'test', 'different')
end

test 'nil and a string are not equal' do
assert_equal false, @host.send(:attr_equivalent?, 'test', nil)
end
end
90b83222 Ohad Levy
end