add tests for cib_objects.rb
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
Gemfile.lock
 | 
			
		||||
							
								
								
									
										6
									
								
								Gemfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								Gemfile
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
source 'https://rubygems.org'
 | 
			
		||||
 | 
			
		||||
#gem 'berkshelf',  '~> 2.0'
 | 
			
		||||
gem 'chefspec',   '~> 3.0'
 | 
			
		||||
gem 'foodcritic', '~> 3.0'
 | 
			
		||||
gem 'rubocop'
 | 
			
		||||
							
								
								
									
										124
									
								
								spec/cib_object_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										124
									
								
								spec/cib_object_spec.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,124 @@
 | 
			
		||||
require 'spec_helper'
 | 
			
		||||
require_relative File.join(%w(.. libraries cib_objects))
 | 
			
		||||
 | 
			
		||||
describe Chef::Libraries::Pacemaker::CIBObjects do
 | 
			
		||||
  include Chef::Libraries::Pacemaker::CIBObjects
 | 
			
		||||
 | 
			
		||||
  shared_context "shellout stubs" do
 | 
			
		||||
    before(:each) do
 | 
			
		||||
      Mixlib::ShellOut.any_instance.stub(:run_command)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  shared_context "keystone config" do
 | 
			
		||||
    before(:all) do
 | 
			
		||||
      @params = {
 | 
			
		||||
        "os_password"    => "adminpw",
 | 
			
		||||
        "os_auth_url"    => "http://node1:5000/v2.0",
 | 
			
		||||
        "os_username"    => "admin",
 | 
			
		||||
        "os_tenant_name" => "openstack"
 | 
			
		||||
      }
 | 
			
		||||
      params_string = @params.map { |k,v| %'#{k}="#{v}"' }.join(" ")
 | 
			
		||||
      @config = <<EOF
 | 
			
		||||
primitive keystone ocf:openstack:keystone \\
 | 
			
		||||
        params #{params_string} \\
 | 
			
		||||
        meta target-role="Started" is-managed="true" \\
 | 
			
		||||
        op monitor interval="10s"
 | 
			
		||||
EOF
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  shared_context "keystone primitive" do
 | 
			
		||||
    include_context "shellout stubs"
 | 
			
		||||
    include_context "keystone config"
 | 
			
		||||
 | 
			
		||||
    before(:each) do
 | 
			
		||||
      Mixlib::ShellOut.any_instance.stub(:error!)
 | 
			
		||||
      expect_any_instance_of(Mixlib::ShellOut) \
 | 
			
		||||
        .to receive(:stdout) \
 | 
			
		||||
        .and_return(@config)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  shared_context "no keystone primitive" do
 | 
			
		||||
    include_context "shellout stubs"
 | 
			
		||||
    before(:each) do
 | 
			
		||||
      expect_any_instance_of(Mixlib::ShellOut) \
 | 
			
		||||
        .to receive(:error!) \
 | 
			
		||||
        .and_raise(RuntimeError)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "#get_cib_object_definition" do
 | 
			
		||||
    include_context "keystone primitive"
 | 
			
		||||
 | 
			
		||||
    it "should retrieve cluster config" do
 | 
			
		||||
      expect(get_cib_object_definition("keystone")).to eq(@config)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "#get_cib_object_definition" do
 | 
			
		||||
    include_context "no keystone primitive"
 | 
			
		||||
 | 
			
		||||
    it "should return nil cluster config" do
 | 
			
		||||
      expect(get_cib_object_definition("keystone")).to eq(nil)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "#cib_object_exists?" do
 | 
			
		||||
    include_context "keystone primitive"
 | 
			
		||||
 | 
			
		||||
    it "should return true" do
 | 
			
		||||
      expect(cib_object_exists?("keystone")).to be(true)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "#cib_object_exists?" do
 | 
			
		||||
    include_context "no keystone primitive"
 | 
			
		||||
 | 
			
		||||
    it "should return false" do
 | 
			
		||||
      expect(cib_object_exists?("keystone")).to be(false)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "#cib_object_type" do
 | 
			
		||||
    include_context "keystone config"
 | 
			
		||||
 | 
			
		||||
    it "should return primitive" do
 | 
			
		||||
      expect(cib_object_type(@config)).to eq("primitive")
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "should raise an error without valid config" do
 | 
			
		||||
      expect { cib_object_type("nonsense") }.to raise_error
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "#pacemaker_resource_running?" do
 | 
			
		||||
    before(:each) do
 | 
			
		||||
      @cmd = double(Mixlib::ShellOut)
 | 
			
		||||
      expect(self).to receive(:shell_out!) \
 | 
			
		||||
        .with(*%w(crm resource status keystone)).ordered \
 | 
			
		||||
        .and_return(@cmd)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "should return true" do
 | 
			
		||||
      expect(@cmd).to receive(:stdout).at_least(:once).and_return("resource keystone is running")
 | 
			
		||||
      expect(pacemaker_resource_running?("keystone")).to be(true)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "should return false" do
 | 
			
		||||
      expect(@cmd).to receive(:stdout).at_least(:once).and_return("resource keystone is not running")
 | 
			
		||||
      expect(pacemaker_resource_running?("keystone")).to be(false)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "#extract_hash" do
 | 
			
		||||
    include_context "keystone config"
 | 
			
		||||
 | 
			
		||||
    it "should extract a hash from config" do
 | 
			
		||||
      expect(extract_hash("keystone", @config, "params")).to eq(@params)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										30
									
								
								spec/spec_helper.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								spec/spec_helper.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
require 'chefspec'
 | 
			
		||||
 | 
			
		||||
RSpec.configure do |config|
 | 
			
		||||
  # config.mock_with :rspec do |mocks|
 | 
			
		||||
  #   # This option should be set when all dependencies are being loaded
 | 
			
		||||
  #   # before a spec run, as is the case in a typical spec helper. It will
 | 
			
		||||
  #   # cause any verifying double instantiation for a class that does not
 | 
			
		||||
  #   # exist to raise, protecting against incorrectly spelt names.
 | 
			
		||||
  #   mocks.verify_doubled_constant_names = true
 | 
			
		||||
  # end
 | 
			
		||||
 | 
			
		||||
  # Specify the path for Chef Solo to find cookbooks (default: [inferred from
 | 
			
		||||
  # the location of the calling spec file])
 | 
			
		||||
  #config.cookbook_path = '/var/cookbooks'
 | 
			
		||||
 | 
			
		||||
  # Specify the path for Chef Solo to find roles (default: [ascending search])
 | 
			
		||||
  #config.role_path = '/var/roles'
 | 
			
		||||
 | 
			
		||||
  # Specify the Chef log_level (default: :warn)
 | 
			
		||||
  #config.log_level = :debug
 | 
			
		||||
 | 
			
		||||
  # Specify the path to a local JSON file with Ohai data (default: nil)
 | 
			
		||||
  #config.path = 'ohai.json'
 | 
			
		||||
 | 
			
		||||
  # Specify the operating platform to mock Ohai data from (default: nil)
 | 
			
		||||
  config.platform = 'suse'
 | 
			
		||||
 | 
			
		||||
  # Specify the operating version to mock Ohai data from (default: nil)
 | 
			
		||||
  config.version = '11.03'
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user