Skip to content

Commit

Permalink
add basic why-run/assertion structure to group provider
Browse files Browse the repository at this point in the history
  • Loading branch information
marcparadise authored and danielsdeleo committed May 4, 2012
1 parent 5933640 commit e68cce0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
44 changes: 27 additions & 17 deletions chef/lib/chef/provider/group.rb
Expand Up @@ -52,6 +52,14 @@ def load_current_resource

@current_resource
end

def define_resource_requirements
requirements.assert(:modify) do |a|
a.assertion { @group_exists }
a.failure_message(Chef::Exceptions::Group, "Cannot modify #{@new_resource} - group does not exist!")
a.whyrun("Group #{@new_resource} does not exist. Unless it would have been created earlier in this run, this attempt to modify it would fail.")
end
end

# Check to see if a group needs any changes
#
Expand All @@ -76,43 +84,45 @@ def compare_group
def action_create
case @group_exists
when false
create_group
Chef::Log.info("#{@new_resource} created")
@new_resource.updated_by_last_action(true)
converge_by("would create #{@new_resource}") do
create_group
Chef::Log.info("#{@new_resource} created")
end
else
if compare_group
manage_group
Chef::Log.info("#{@new_resource} altered")
@new_resource.updated_by_last_action(true)
converge_by("would alter group #{@new_resource}") do
manage_group
Chef::Log.info("#{@new_resource} altered")
end
end
end
end

def action_remove
if @group_exists
remove_group
@new_resource.updated_by_last_action(true)
Chef::Log.info("#{@new_resource} removed")
converge_by("would remove group #{@new_resource}") do
remove_group
Chef::Log.info("#{@new_resource} removed")
end

end
end

def action_manage
if @group_exists && compare_group
manage_group
@new_resource.updated_by_last_action(true)
Chef::Log.info("#{@new_resource} managed")
converge_by("would manage group #{@new_resource}") do
manage_group
Chef::Log.info("#{@new_resource} managed")
end
end
end

def action_modify
if @group_exists
if compare_group
if compare_group
converge_by("would modify group #{@new_resource}") do
manage_group
@new_resource.updated_by_last_action(true)
Chef::Log.info("#{@new_resource} modified")
end
else
raise Chef::Exceptions::Group, "Cannot modify #{@new_resource} - group does not exist!"
end
end

Expand Down
30 changes: 15 additions & 15 deletions chef/spec/unit/provider/group_spec.rb
Expand Up @@ -114,35 +114,35 @@
it "should call create_group if the group does not exist" do
@provider.group_exists = false
@provider.should_receive(:create_group).and_return(true)
@provider.action_create
@provider.run_action(:create)
end

it "should set the the new_resources updated flag when it creates the group" do
@provider.group_exists = false
@provider.stub!(:create_group)
@provider.action_create
@provider.run_action(:create)
@provider.new_resource.should be_updated
end

it "should check to see if the group has mismatched attributes if the group exists" do
@provider.group_exists = true
@provider.stub!(:compare_group).and_return(false)
@provider.action_create
@provider.run_action(:create)
@provider.new_resource.should_not be_updated
end

it "should call manage_group if the group exists and has mismatched attributes" do
@provider.group_exists = true
@provider.stub!(:compare_group).and_return(true)
@provider.should_receive(:manage_group).and_return(true)
@provider.action_create
@provider.run_action(:create)
end

it "should set the the new_resources updated flag when it creates the group if we call manage_group" do
@provider.group_exists = true
@provider.stub!(:compare_group).and_return(true)
@provider.stub!(:manage_group).and_return(true)
@provider.action_create
@provider.run_action(:create)
@new_resource.should be_updated
end
end
Expand All @@ -152,14 +152,14 @@
it "should not call remove_group if the group does not exist" do
@provider.group_exists = false
@provider.should_not_receive(:remove_group)
@provider.action_remove
@provider.run_action(:remove)
@provider.new_resource.should_not be_updated
end

it "should call remove_group if the group exists" do
@provider.group_exists = true
@provider.should_receive(:remove_group)
@provider.action_remove
@provider.run_action(:remove)
@provider.new_resource.should be_updated
end
end
Expand All @@ -173,26 +173,26 @@
it "should run manage_group if the group exists and has mismatched attributes" do
@provider.should_receive(:compare_group).and_return(true)
@provider.should_receive(:manage_group).and_return(true)
@provider.action_manage
@provider.run_action(:manage)
end

it "should set the new resources updated flag to true if manage_group is called" do
@provider.stub!(:compare_group).and_return(true)
@provider.stub!(:manage_group).and_return(true)
@provider.action_manage
@provider.run_action(:manage)
@new_resource.should be_updated
end

it "should not run manage_group if the group does not exist" do
@provider.group_exists = false
@provider.should_not_receive(:manage_group)
@provider.action_manage
@provider.run_action(:manage)
end

it "should not run manage_group if the group exists but has no differing attributes" do
@provider.should_receive(:compare_group).and_return(false)
@provider.should_not_receive(:manage_group)
@provider.action_manage
@provider.run_action(:manage)
end
end

Expand All @@ -205,25 +205,25 @@
it "should run manage_group if the group exists and has mismatched attributes" do
@provider.should_receive(:compare_group).and_return(true)
@provider.should_receive(:manage_group).and_return(true)
@provider.action_modify
@provider.run_action(:modify)
end

it "should set the new resources updated flag to true if manage_group is called" do
@provider.stub!(:compare_group).and_return(true)
@provider.stub!(:manage_group).and_return(true)
@provider.action_modify
@provider.run_action(:modify)
@new_resource.should be_updated
end

it "should not run manage_group if the group exists but has no differing attributes" do
@provider.should_receive(:compare_group).and_return(false)
@provider.should_not_receive(:manage_group)
@provider.action_modify
@provider.run_action(:modify)
end

it "should raise a Chef::Exceptions::Group if the group doesn't exist" do
@provider.group_exists = false
lambda { @provider.action_modify }.should raise_error(Chef::Exceptions::Group)
lambda { @provider.run_action(:modify) }.should raise_error(Chef::Exceptions::Group)
end
end
end

0 comments on commit e68cce0

Please sign in to comment.