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

106 lines
2.9 KiB
Ruby

require 'spec_helper'
require 'mixlib/shellout'
require File.expand_path('../../../libraries/pacemaker', File.dirname(__FILE__))
require File.expand_path('../../fixtures/keystone_primitive', File.dirname(__FILE__))
describe Pacemaker::CIBObject do
before(:each) do
Mixlib::ShellOut.any_instance.stub(:run_command)
end
let(:cib_object) { Chef::RSpec::Pacemaker::Config::KEYSTONE_PRIMITIVE.dup }
#####################################################################
# examples start here
context "no CIB object" do
before(:each) do
expect_any_instance_of(Mixlib::ShellOut) \
.to receive(:error!) \
.and_raise(RuntimeError)
end
describe "#load_definition" do
it "should return nil cluster config" do
cib_object.load_definition
expect(cib_object.definition).to eq(nil)
end
end
describe "#exists?" do
it "should return false" do
cib_object.load_definition
expect(cib_object.exists?).to be(false)
end
end
end
context "keystone primitive resource CIB object" do
before(:each) do
Mixlib::ShellOut.any_instance.stub(:error!)
expect_any_instance_of(Mixlib::ShellOut) \
.to receive(:stdout) \
.and_return(cib_object.definition_string)
end
context "with definition loaded" do
before(:each) do
cib_object.load_definition
end
describe "#exists?" do
it "should return true" do
expect(cib_object.exists?).to be(true)
end
end
describe "#load_definition" do
it "should retrieve cluster config" do
expect(cib_object.definition).to eq(cib_object.definition_string)
end
end
describe "#type" do
it "should return primitive" do
expect(cib_object.type).to eq("primitive")
end
end
end
end
context "CIB object with unregistered type" do
before(:each) do
Mixlib::ShellOut.any_instance.stub(:error!)
end
describe "::from_name" do
it "should refuse to instantiate from any subclass" do
expect_any_instance_of(Mixlib::ShellOut) \
.to receive(:stdout) \
.and_return("unregistered #{cib_object.name} <definition>")
expect {
Pacemaker::CIBObject.from_name(cib_object.name)
}.to raise_error "No subclass of Pacemaker::CIBObject was registered with type 'unregistered'"
end
end
end
context "invalid CIB object definition" do
before(:each) do
Mixlib::ShellOut.any_instance.stub(:error!)
expect_any_instance_of(Mixlib::ShellOut) \
.to receive(:stdout) \
.and_return("nonsense")
end
describe "#type" do
it "should raise an error without a valid definition" do
expect { cib_object.load_definition }.to \
raise_error(RuntimeError, "Couldn't extract CIB object type from 'nonsense'")
end
end
end
end