extract standard CIB object examples for reuse
This commit is contained in:
@@ -16,6 +16,7 @@ group :rspec do
|
|||||||
watch(%r{^Gemfile$}) { all_specs }
|
watch(%r{^Gemfile$}) { all_specs }
|
||||||
watch(%r{^Gemfile.lock$}) { all_specs }
|
watch(%r{^Gemfile.lock$}) { all_specs }
|
||||||
watch(%r{^spec/spec_helper\.rb$}) { all_specs }
|
watch(%r{^spec/spec_helper\.rb$}) { all_specs }
|
||||||
|
watch(%r{^spec/helpers/(.+)\.rb$}) { all_specs }
|
||||||
watch(%r{^spec/fixtures/(.+)\.rb$}) { all_specs }
|
watch(%r{^spec/fixtures/(.+)\.rb$}) { all_specs }
|
||||||
watch(%r{^libraries/pacemaker\.rb$}) { all_specs }
|
watch(%r{^libraries/pacemaker\.rb$}) { all_specs }
|
||||||
watch(%r{^spec/.+_spec\.rb$})
|
watch(%r{^spec/.+_spec\.rb$})
|
||||||
|
37
spec/helpers/common_object_examples.rb
Normal file
37
spec/helpers/common_object_examples.rb
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
require 'mixlib/shellout'
|
||||||
|
require_relative File.join(%w(.. .. libraries pacemaker cib_object))
|
||||||
|
|
||||||
|
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
|
@@ -1,141 +1,124 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
require_relative File.join(%w(.. .. .. .. libraries pacemaker resource primitive))
|
require_relative File.join(%w(.. .. .. .. libraries pacemaker resource primitive))
|
||||||
require_relative File.join(%w(.. .. .. fixtures keystone_primitive))
|
require_relative File.join(%w(.. .. .. fixtures keystone_primitive))
|
||||||
|
require_relative File.join(%w(.. .. .. helpers common_object_examples))
|
||||||
|
|
||||||
describe Pacemaker::Resource::Primitive do
|
describe Pacemaker::Resource::Primitive do
|
||||||
|
let(:fixture) { Chef::RSpec::Pacemaker::Config::KEYSTONE_PRIMITIVE.dup }
|
||||||
|
|
||||||
before(:each) do
|
before(:each) do
|
||||||
@primitive = Chef::RSpec::Pacemaker::Config::KEYSTONE_PRIMITIVE.dup
|
|
||||||
Mixlib::ShellOut.any_instance.stub(:run_command)
|
Mixlib::ShellOut.any_instance.stub(:run_command)
|
||||||
end
|
end
|
||||||
|
|
||||||
def expect_to_match_fixture(obj)
|
def object_type
|
||||||
expect(obj.is_a? Pacemaker::Resource::Primitive).to be_true
|
'primitive'
|
||||||
%w(name agent params_string meta_string op_string).each do |field|
|
|
||||||
method = field.to_sym
|
|
||||||
expect(obj.send(method)).to eq(@primitive.send(method))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be instantiated via Pacemaker::CIBObject.from_name" do
|
def pacemaker_object_class
|
||||||
Mixlib::ShellOut.any_instance.stub(:error!)
|
Pacemaker::Resource::Primitive
|
||||||
expect_any_instance_of(Mixlib::ShellOut) \
|
|
||||||
.to receive(:stdout) \
|
|
||||||
.and_return(@primitive.definition_string)
|
|
||||||
|
|
||||||
obj = Pacemaker::CIBObject.from_name(@primitive.name)
|
|
||||||
expect_to_match_fixture(obj)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be instantiated via Pacemaker::CIBObject.from_definition" do
|
def fields
|
||||||
obj = Pacemaker::CIBObject.from_definition(@primitive.definition_string)
|
%w(name agent params_string meta_string op_string)
|
||||||
expect_to_match_fixture(obj)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should barf if the loaded definition's type is not primitive" do
|
it_should_behave_like "a CIB object"
|
||||||
Mixlib::ShellOut.any_instance.stub(:error!)
|
|
||||||
expect_any_instance_of(Mixlib::ShellOut) \
|
|
||||||
.to receive(:stdout) \
|
|
||||||
.and_return("clone foo blah blah")
|
|
||||||
expect { @primitive.load_definition }.to \
|
|
||||||
raise_error(Pacemaker::CIBObject::TypeMismatch,
|
|
||||||
"Expected primitive type but loaded definition was type clone")
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#params_string" do
|
describe "#params_string" do
|
||||||
it "should return empty string with nil params" do
|
it "should return empty string with nil params" do
|
||||||
@primitive.params = nil
|
fixture.params = nil
|
||||||
expect(@primitive.params_string).to eq("")
|
expect(fixture.params_string).to eq("")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return empty string with empty params" do
|
it "should return empty string with empty params" do
|
||||||
@primitive.params = {}
|
fixture.params = {}
|
||||||
expect(@primitive.params_string).to eq("")
|
expect(fixture.params_string).to eq("")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return a resource params string" do
|
it "should return a resource params string" do
|
||||||
@primitive.params = {
|
fixture.params = {
|
||||||
"foo" => "bar",
|
"foo" => "bar",
|
||||||
"baz" => "qux",
|
"baz" => "qux",
|
||||||
}
|
}
|
||||||
expect(@primitive.params_string).to eq(%'params baz="qux" foo="bar"')
|
expect(fixture.params_string).to eq(%'params baz="qux" foo="bar"')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#meta_string" do
|
describe "#meta_string" do
|
||||||
it "should return empty string with nil meta" do
|
it "should return empty string with nil meta" do
|
||||||
@primitive.meta = nil
|
fixture.meta = nil
|
||||||
expect(@primitive.meta_string).to eq("")
|
expect(fixture.meta_string).to eq("")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return empty string with empty meta" do
|
it "should return empty string with empty meta" do
|
||||||
@primitive.meta = {}
|
fixture.meta = {}
|
||||||
expect(@primitive.meta_string).to eq("")
|
expect(fixture.meta_string).to eq("")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return a resource meta string" do
|
it "should return a resource meta string" do
|
||||||
@primitive.meta = {
|
fixture.meta = {
|
||||||
"foo" => "bar",
|
"foo" => "bar",
|
||||||
"baz" => "qux",
|
"baz" => "qux",
|
||||||
}
|
}
|
||||||
expect(@primitive.meta_string).to eq(%'meta baz="qux" foo="bar"')
|
expect(fixture.meta_string).to eq(%'meta baz="qux" foo="bar"')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#op_string" do
|
describe "#op_string" do
|
||||||
it "should return empty string with nil op" do
|
it "should return empty string with nil op" do
|
||||||
@primitive.op = nil
|
fixture.op = nil
|
||||||
expect(@primitive.op_string).to eq("")
|
expect(fixture.op_string).to eq("")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return empty string with empty op" do
|
it "should return empty string with empty op" do
|
||||||
@primitive.op = {}
|
fixture.op = {}
|
||||||
expect(@primitive.op_string).to eq("")
|
expect(fixture.op_string).to eq("")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return a resource op string" do
|
it "should return a resource op string" do
|
||||||
@primitive.op = {
|
fixture.op = {
|
||||||
"monitor" => {
|
"monitor" => {
|
||||||
"foo" => "bar",
|
"foo" => "bar",
|
||||||
"baz" => "qux",
|
"baz" => "qux",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expect(@primitive.op_string).to eq(%'op monitor baz="qux" foo="bar"')
|
expect(fixture.op_string).to eq(%'op monitor baz="qux" foo="bar"')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "::extract_hash" do
|
describe "::extract_hash" do
|
||||||
it "should extract a params hash from config" do
|
it "should extract a params hash from config" do
|
||||||
expect(@primitive.class.extract_hash(@primitive.definition_string, "params")).to \
|
expect(fixture.class.extract_hash(fixture.definition_string, "params")).to \
|
||||||
eq(Hash[@primitive.params])
|
eq(Hash[fixture.params])
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should extract an op start hash from config" do
|
it "should extract an op start hash from config" do
|
||||||
expect(@primitive.class.extract_hash(@primitive.definition_string, 'op start')).to \
|
expect(fixture.class.extract_hash(fixture.definition_string, 'op start')).to \
|
||||||
eq(Hash[@primitive.op]['start'])
|
eq(Hash[fixture.op]['start'])
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should extract an op monitor hash from config" do
|
it "should extract an op monitor hash from config" do
|
||||||
expect(@primitive.class.extract_hash(@primitive.definition_string, 'op monitor')).to \
|
expect(fixture.class.extract_hash(fixture.definition_string, 'op monitor')).to \
|
||||||
eq(Hash[@primitive.op]['monitor'])
|
eq(Hash[fixture.op]['monitor'])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#definition_string" do
|
describe "#definition_string" do
|
||||||
it "should return the definition string" do
|
it "should return the definition string" do
|
||||||
expect(@primitive.definition_string).to \
|
expect(fixture.definition_string).to \
|
||||||
eq(Chef::RSpec::Pacemaker::Config::KEYSTONE_PRIMITIVE_DEFINITION)
|
eq(Chef::RSpec::Pacemaker::Config::KEYSTONE_PRIMITIVE_DEFINITION)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#parse_definition" do
|
describe "#parse_definition" do
|
||||||
before(:each) do
|
before(:each) do
|
||||||
@parsed = Pacemaker::Resource::Primitive.new(@primitive.name)
|
@parsed = Pacemaker::Resource::Primitive.new(fixture.name)
|
||||||
@parsed.definition = Chef::RSpec::Pacemaker::Config::KEYSTONE_PRIMITIVE_DEFINITION
|
@parsed.definition = Chef::RSpec::Pacemaker::Config::KEYSTONE_PRIMITIVE_DEFINITION
|
||||||
@parsed.parse_definition
|
@parsed.parse_definition
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should parse the agent" do
|
it "should parse the agent" do
|
||||||
expect(@parsed.agent).to eq(@primitive.agent)
|
expect(@parsed.agent).to eq(fixture.agent)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user