implement Pacemaker::Constraint::Order

This commit is contained in:
Adam Spiers 2014-03-22 12:08:04 +00:00
parent e56a821156
commit d00a17fb1e
3 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,31 @@
require File.expand_path('../constraint', File.dirname(__FILE__))
class Pacemaker::Constraint::Order < Pacemaker::Constraint
TYPE = 'order'
register_type TYPE
attr_accessor :score, :ordering
def self.attrs_to_copy_from_chef
%w(score ordering)
end
def parse_definition
# FIXME: add support for symmetrical=<bool>
# Currently we take the easy way out and don't bother parsing the ordering.
# See the crm(8) man page for the official BNF grammar.
score_regexp = %r{\d+|[-+]?inf|Mandatory|Optional|Serialize}
unless definition =~ /^#{self.class::TYPE} (\S+) (#{score_regexp}): (.+?)\s*$/
raise Pacemaker::CIBObject::DefinitionParseError, \
"Couldn't parse definition '#{definition}'"
end
self.name = $1
self.score = $2
self.ordering = $3
end
def definition_string
"#{self.class::TYPE} #{name} #{score}: #{ordering}"
end
end

14
spec/fixtures/order_constraint.rb vendored Normal file
View File

@ -0,0 +1,14 @@
require ::File.expand_path('../../libraries/pacemaker/constraint/order',
File.dirname(__FILE__))
module Chef::RSpec
module Pacemaker
module Config
ORDER_CONSTRAINT = \
::Pacemaker::Constraint::Order.new('order1')
ORDER_CONSTRAINT.score = 'Mandatory'
ORDER_CONSTRAINT.ordering = 'primitive1 clone1'
ORDER_CONSTRAINT_DEFINITION = 'order order1 Mandatory: primitive1 clone1'
end
end
end

View File

@ -0,0 +1,61 @@
require 'spec_helper'
this_dir = File.dirname(__FILE__)
require File.expand_path('../../../../libraries/pacemaker/constraint/order',
this_dir)
require File.expand_path('../../../fixtures/order_constraint', this_dir)
require File.expand_path('../../../helpers/cib_object', this_dir)
describe Pacemaker::Constraint::Order do
let(:fixture) { Chef::RSpec::Pacemaker::Config::ORDER_CONSTRAINT.dup }
let(:fixture_definition) {
Chef::RSpec::Pacemaker::Config::ORDER_CONSTRAINT_DEFINITION
}
def object_type
'order'
end
def pacemaker_object_class
Pacemaker::Constraint::Order
end
def fields
%w(name score ordering)
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
order = pacemaker_object_class.new('foo')
order.definition = \
%!order order1 Mandatory: rsc1 rsc2!
order.parse_definition
expect(order.definition_string).to eq(<<'EOF'.chomp)
order order1 Mandatory: rsc1 rsc2
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 score" do
expect(@parsed.score).to eq(fixture.score)
end
it "should parse the ordering" do
expect(@parsed.ordering).to eq(fixture.ordering)
end
end
end