cookbook-pacemaker/spec/helpers/common_object_examples.rb
Adam Spiers fe5616cfca stop libraries being required multiple times
The previous use of require caused File.join on several occasions to
calculate different paths to the same library, depending on which
__FILE__ the library was being calculated as relative to; e.g.
/some/path/prefix/spec/one/bar.rb would do the equivalent of:

  require '/some/path/prefix/spec/one/../../libraries/foo/mylib.rb'

and /some/path/prefix/spec/two/baz.rb would do the equivalent of:

  require '/some/path/prefix/spec/two/../../libraries/foo/mylib.rb'

This would result in mylib.rb being loaded multiple times, causing
warnings from constants being redefined, and worse, multiple objects
representing the same class hierarchy (@@foo) variables.  The latter
actually broke the @@subclasses registration mechanism in
Pacemaker::CIBObject.

By switching to File.expand_path, we ensure we always refer to each
library using a single absolute path, which means Ruby's require
mechanism works as it should, only loading the code the first time
round.
2014-02-06 15:44:14 +00:00

39 lines
1.3 KiB
Ruby

require 'mixlib/shellout'
require File.expand_path('../../libraries/pacemaker/cib_object',
File.dirname(__FILE__))
shared_examples "a CIB object" do
def expect_to_match_fixture(obj)
expect(obj.is_a? pacemaker_object_class).to eq(true)
fields.each do |field|
method = field.to_sym
expect(obj.send(method)).to eq(fixture.send(method))
end
end
it "should be instantiated via Pacemaker::CIBObject.from_name" do
Mixlib::ShellOut.any_instance.stub(:error!)
expect_any_instance_of(Mixlib::ShellOut) \
.to receive(:stdout) \
.and_return(fixture.definition_string)
obj = Pacemaker::CIBObject.from_name(fixture.name)
expect_to_match_fixture(obj)
end
it "should instantiate by parsing a definition" do
obj = Pacemaker::CIBObject.from_definition(fixture.definition_string)
expect_to_match_fixture(obj)
end
it "should barf if the loaded definition's type is not colocation" do
Mixlib::ShellOut.any_instance.stub(:error!)
expect_any_instance_of(Mixlib::ShellOut) \
.to receive(:stdout) \
.and_return("clone foo blah blah")
expect { fixture.load_definition }.to \
raise_error(Pacemaker::CIBObject::TypeMismatch,
"Expected #{object_type} type but loaded definition was type clone")
end
end