implement Pacemaker::Constraint::Location

This commit is contained in:
Adam Spiers 2014-03-21 18:01:36 +00:00
parent f6d054fadc
commit 491e7018c8
3 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,31 @@
require File.expand_path('../constraint', File.dirname(__FILE__))
class Pacemaker::Constraint::Location < Pacemaker::Constraint
TYPE = 'location'
register_type TYPE
attr_accessor :rsc, :score, :node
def self.attrs_to_copy_from_chef
%w(rsc score node)
end
def parse_definition
# FIXME: this is woefully incomplete, and doesn't cope with any of
# the rules syntax. See the crm(8) man page for the official BNF
# grammar.
unless definition =~ /^#{self.class::TYPE} (\S+) (\S+) (\d+|[-+]?inf): (\S+)\s*$/
raise Pacemaker::CIBObject::DefinitionParseError, \
"Couldn't parse definition '#{definition}'"
end
self.name = $1
self.rsc = $2
self.score = $3
self.node = $4
end
def definition_string
"#{self.class::TYPE} #{name} #{rsc} #{score}: #{node}"
end
end

15
spec/fixtures/location_constraint.rb vendored Normal file
View File

@ -0,0 +1,15 @@
require ::File.expand_path('../../libraries/pacemaker/constraint/location',
::File.dirname(__FILE__))
module Chef::RSpec
module Pacemaker
module Config
LOCATION_CONSTRAINT = \
::Pacemaker::Constraint::Location.new('location1')
LOCATION_CONSTRAINT.rsc = 'primitive1'
LOCATION_CONSTRAINT.score = '-inf'
LOCATION_CONSTRAINT.node = 'node1'
LOCATION_CONSTRAINT_DEFINITION = 'location location1 primitive1 -inf: node1'
end
end
end

View File

@ -0,0 +1,66 @@
require 'spec_helper'
require File.expand_path('../../../../libraries/pacemaker/constraint/location',
File.dirname(__FILE__))
require File.expand_path('../../../fixtures/location_constraint', File.dirname(__FILE__))
require File.expand_path('../../../helpers/cib_object', File.dirname(__FILE__))
describe Pacemaker::Constraint::Location do
let(:fixture) { Chef::RSpec::Pacemaker::Config::LOCATION_CONSTRAINT.dup }
let(:fixture_definition) {
Chef::RSpec::Pacemaker::Config::LOCATION_CONSTRAINT_DEFINITION
}
before(:each) do
Mixlib::ShellOut.any_instance.stub(:run_command)
end
def object_type
'location'
end
def pacemaker_object_class
Pacemaker::Constraint::Location
end
def fields
%w(name rsc score node)
end
it_should_behave_like "a CIB object"
describe "#definition_string" do
it "should return the definition string" do
expect(fixture.definition_string).to eq(fixture_definition)
end
it "should return a short definition string" do
location = pacemaker_object_class.new('foo')
location.definition = \
%!location location1 primitive1 -inf: node1!
location.parse_definition
expect(location.definition_string).to eq(<<'EOF'.chomp)
location location1 primitive1 -inf: node1
EOF
end
end
describe "#parse_definition" do
before(:each) do
@parsed = pacemaker_object_class.new(fixture.name)
@parsed.definition = fixture_definition
@parsed.parse_definition
end
it "should parse the rsc" do
expect(@parsed.rsc).to eq(fixture.rsc)
end
it "should parse the score" do
expect(@parsed.score).to eq(fixture.score)
end
it "should parse the node" do
expect(@parsed.node).to eq(fixture.node)
end
end
end