extract tests for runnable resources for reuse

The group resource will need these.
This commit is contained in:
Adam Spiers 2014-02-06 10:43:06 +00:00
parent 14b4021a3b
commit 32b36cfb19
2 changed files with 109 additions and 93 deletions

View File

@ -0,0 +1,107 @@
require File.expand_path('cib_object', File.dirname(__FILE__))
module Chef::RSpec
module Pacemaker
module RunnableResource
def expect_running(running)
expect_any_instance_of(cib_object_class) \
.to receive(:running?) \
.and_return(running)
end
end
end
end
shared_examples "a runnable resource" do |fixture|
include Chef::RSpec::Pacemaker::RunnableResource
describe ":delete action" do
it_should_behave_like "action on non-existent resource", \
:delete, "crm configure delete #{fixture.name}", nil
it "should not delete a running resource" do
expect_definition(fixture.definition_string)
expect_running(true)
expected_error = "Cannot delete running #{fixture}"
expect { provider.run_action :delete }.to \
raise_error(RuntimeError, expected_error)
cmd = "crm configure delete '#{fixture.name}'"
expect(@chef_run).not_to run_execute(cmd)
expect(@resource).not_to be_updated
end
it "should delete a non-running resource" do
expect_definition(fixture.definition_string)
expect_running(false)
provider.run_action :delete
cmd = "crm configure delete '#{fixture.name}'"
expect(@chef_run).to run_execute(cmd)
expect(@resource).to be_updated
end
end
describe ":start action" do
it_should_behave_like "action on non-existent resource", \
:start,
"crm resource start #{fixture.name}", \
"Cannot start non-existent #{fixture}"
it "should do nothing to a started resource" do
expect_definition(fixture.definition_string)
expect_running(true)
provider.run_action :start
cmd = "crm resource start #{fixture.name}"
expect(@chef_run).not_to run_execute(cmd)
expect(@resource).not_to be_updated
end
it "should start a stopped resource" do
config = fixture.definition_string.sub("Started", "Stopped")
expect_definition(config)
expect_running(false)
provider.run_action :start
cmd = "crm resource start '#{fixture.name}'"
expect(@chef_run).to run_execute(cmd)
expect(@resource).to be_updated
end
end
describe ":stop action" do
it_should_behave_like "action on non-existent resource", \
:stop,
"crm resource stop #{fixture.name}", \
"Cannot stop non-existent #{fixture}"
it "should do nothing to a stopped resource" do
expect_definition(fixture.definition_string)
expect_running(false)
provider.run_action :stop
cmd = "crm resource start #{fixture.name}"
expect(@chef_run).not_to run_execute(cmd)
expect(@resource).not_to be_updated
end
it "should stop a started resource" do
expect_definition(fixture.definition_string)
expect_running(true)
provider.run_action :stop
cmd = "crm resource stop '#{fixture.name}'"
expect(@chef_run).to run_execute(cmd)
expect(@resource).to be_updated
end
end
end

View File

@ -1,6 +1,7 @@
require 'chef/application'
require File.expand_path('../spec_helper', File.dirname(__FILE__))
require File.expand_path('../helpers/cib_object', File.dirname(__FILE__))
require File.expand_path('../helpers/runnable_resource', File.dirname(__FILE__))
require File.expand_path('../fixtures/keystone_primitive', File.dirname(__FILE__))
describe "Chef::Provider::PacemakerPrimitive" do
@ -33,12 +34,6 @@ describe "Chef::Provider::PacemakerPrimitive" do
include Chef::RSpec::Pacemaker::Common
def expect_running(running)
expect_any_instance_of(cib_object_class) \
.to receive(:running?) \
.and_return(running)
end
describe ":create action" do
def test_modify(expected_cmds)
yield
@ -130,92 +125,6 @@ describe "Chef::Provider::PacemakerPrimitive" do
end
end
describe ":delete action" do
it_should_behave_like "action on non-existent resource", \
:delete, "crm configure delete #{fixture.name}", nil
it "should not delete a running resource" do
expect_definition(fixture.definition_string)
expect_running(true)
expected_error = "Cannot delete running #{fixture}"
expect { provider.run_action :delete }.to \
raise_error(RuntimeError, expected_error)
cmd = "crm configure delete '#{fixture.name}'"
expect(@chef_run).not_to run_execute(cmd)
expect(@resource).not_to be_updated
end
it "should delete a non-running resource" do
expect_definition(fixture.definition_string)
expect_running(false)
provider.run_action :delete
cmd = "crm configure delete '#{fixture.name}'"
expect(@chef_run).to run_execute(cmd)
expect(@resource).to be_updated
end
end
describe ":start action" do
it_should_behave_like "action on non-existent resource", \
:start,
"crm resource start #{fixture.name}", \
"Cannot start non-existent #{fixture}"
it "should do nothing to a started resource" do
expect_definition(fixture.definition_string)
expect_running(true)
provider.run_action :start
cmd = "crm resource start #{fixture.name}"
expect(@chef_run).not_to run_execute(cmd)
expect(@resource).not_to be_updated
end
it "should start a stopped resource" do
config = fixture.definition_string.sub("Started", "Stopped")
expect_definition(config)
expect_running(false)
provider.run_action :start
cmd = "crm resource start '#{fixture.name}'"
expect(@chef_run).to run_execute(cmd)
expect(@resource).to be_updated
end
end
describe ":stop action" do
it_should_behave_like "action on non-existent resource", \
:stop,
"crm resource stop #{fixture.name}", \
"Cannot stop non-existent #{fixture}"
it "should do nothing to a stopped resource" do
expect_definition(fixture.definition_string)
expect_running(false)
provider.run_action :stop
cmd = "crm resource start #{fixture.name}"
expect(@chef_run).not_to run_execute(cmd)
expect(@resource).not_to be_updated
end
it "should stop a started resource" do
expect_definition(fixture.definition_string)
expect_running(true)
provider.run_action :stop
cmd = "crm resource stop '#{fixture.name}'"
expect(@chef_run).to run_execute(cmd)
expect(@resource).to be_updated
end
end
it_should_behave_like "a runnable resource", fixture
end