From 491e7018c8809e0bbaa8c855a0c896c1875b90da Mon Sep 17 00:00:00 2001 From: Adam Spiers Date: Fri, 21 Mar 2014 18:01:36 +0000 Subject: [PATCH] implement Pacemaker::Constraint::Location --- libraries/pacemaker/constraint/location.rb | 31 +++++++++ spec/fixtures/location_constraint.rb | 15 +++++ .../pacemaker/constraint/location_spec.rb | 66 +++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 libraries/pacemaker/constraint/location.rb create mode 100644 spec/fixtures/location_constraint.rb create mode 100644 spec/libraries/pacemaker/constraint/location_spec.rb diff --git a/libraries/pacemaker/constraint/location.rb b/libraries/pacemaker/constraint/location.rb new file mode 100644 index 0000000..5e43691 --- /dev/null +++ b/libraries/pacemaker/constraint/location.rb @@ -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 diff --git a/spec/fixtures/location_constraint.rb b/spec/fixtures/location_constraint.rb new file mode 100644 index 0000000..faea088 --- /dev/null +++ b/spec/fixtures/location_constraint.rb @@ -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 diff --git a/spec/libraries/pacemaker/constraint/location_spec.rb b/spec/libraries/pacemaker/constraint/location_spec.rb new file mode 100644 index 0000000..c6a6572 --- /dev/null +++ b/spec/libraries/pacemaker/constraint/location_spec.rb @@ -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