Permalink
Browse files

Merge branch 'chef-29' of git://github.com/fujin/chef

  • Loading branch information...
2 parents 1b4515b + 15f5633 commit 984ca076ec2e02fc95c9da3c6151ce116a1e4ed9 @adamhjk adamhjk committed Feb 1, 2009
View
20 chef/lib/chef/provider/group/groupadd.rb
@@ -20,26 +20,44 @@ class Chef
class Provider
class Group
class Groupadd < Chef::Provider::Group
+
+ def load_current_resource
+ super
+
+ [ "/usr/sbin/groupadd",
+ "/usr/sbin/groupmod",
+ "/usr/sbin/groupdel",
+ "/usr/bin/gpasswd" ].each do |required_binary|
+ raise Chef::Exception::Group, "Could not find binary #{required_binary} for #{@new_resource}" unless ::File.exists?(required_binary)
+ end
+ end
# Create the group
def create_group
command = "groupadd"
command << set_options
- run_command(:command => command)
+ run_command(:command => command)
+ modify_group_members
end
# Manage the group when it already exists
def manage_group
command = "groupmod"
command << set_options
run_command(:command => command)
+ modify_group_members
end
# Remove the group
def remove_group
run_command(:command => "groupdel #{@new_resource.group_name}")
end
+ def modify_group_members
+ Chef::Log.debug("#{@new_resource}: setting group members to #{@new_resource.members.join(', ')}")
+ run_command(:command => "gpasswd -M #{@new_resource.members.join(',')} #{@new_resource.group_name}")
+ end
+
# Little bit of magic as per Adam's useradd provider to pull the assign the command line flags
#
# ==== Returns
View
7 chef/lib/chef/resource/group.rb
@@ -25,7 +25,7 @@ def initialize(name, collection=nil, node=nil)
@resource_name = :group
@group_name = name
@gid = nil
- @members = nil
+ @members = []
@action = :create
@allowed_actions.push(:create, :remove, :modify, :manage)
end
@@ -47,10 +47,11 @@ def gid(arg=nil)
end
def members(arg=nil)
+ converted_members = arg.is_a?(String) ? [].push(arg) : arg
set_or_return(
:members,
- arg,
- :kind_of => [ String, Array ]
+ converted_members,
+ :kind_of => [ Array ]
)
end
View
65 chef/spec/unit/provider/group/groupadd_spec.rb
@@ -69,27 +69,41 @@
@node = mock("Chef::Node", :null_object => true)
@new_resource = mock("Chef::Resource::Group", :null_object => true)
@provider = Chef::Provider::Group::Groupadd.new(@node, @new_resource)
+ @provider.stub!(:run_command).and_return(true)
@provider.stub!(:set_options).and_return(" monkey")
+ @provider.stub!(:modify_group_members).and_return(true)
end
it "should run groupadd with the return of set_options" do
@provider.should_receive(:run_command).with({ :command => "groupadd monkey" }).and_return(true)
@provider.create_group
end
+
+ it "should modify the group members" do
+ @provider.should_receive(:modify_group_members).and_return(true)
+ @provider.create_group
+ end
end
describe Chef::Provider::Group::Groupadd, "manage_group" do
before do
@node = mock("Chef::Node", :null_object => true)
@new_resource = mock("Chef::Resource::Group", :null_object => true)
@provider = Chef::Provider::Group::Groupadd.new(@node, @new_resource)
+ @provider.stub!(:run_command).and_return(true)
@provider.stub!(:set_options).and_return(" monkey")
+ @provider.stub!(:modify_group_members).and_return(true)
end
it "should run groupmod with the return of set_options" do
@provider.should_receive(:run_command).with({ :command => "groupmod monkey" }).and_return(true)
@provider.manage_group
end
+
+ it "should modify the group members" do
+ @provider.should_receive(:modify_group_members).and_return(true)
+ @provider.manage_group
+ end
end
describe Chef::Provider::Group::Groupadd, "remove_group" do
@@ -100,10 +114,59 @@
:group_name => "aj"
)
@provider = Chef::Provider::Group::Groupadd.new(@node, @new_resource)
+ @provider.stub!(:run_command).and_return(true)
end
it "should run groupdel with the new resources group name" do
- @provider.should_receive(:run_command).with({ :command => "groupdel #{@new_resource.group_name}" }).and_return(true)
+ @provider.should_receive(:run_command).with({ :command => "groupdel aj" }).and_return(true)
@provider.remove_group
end
+end
+
+describe Chef::Provider::Group::Groupadd, "modify_group_members" do
+ before do
+ @node = mock("Chef::Node", :null_object => true)
+ @new_resource = mock("Chef::Resource::Group",
+ :null_object => true,
+ :group_name => "aj",
+ :members => [ "all", "your", "base", "are", "belong", "to", "us" ]
+ )
+ @new_resource.stub!(:to_s).and_return("group[aj]")
+ @provider = Chef::Provider::Group::Groupadd.new(@node, @new_resource)
+ @provider.stub!(:run_command).and_return(true)
+ end
+
+ it "should log an appropriate debug message" do
+ Chef::Log.should_receive(:debug).with("group[aj]: setting group members to all, your, base, are, belong, to, us")
+ @provider.modify_group_members
+ end
+
+ it "should run gpasswd with the members joined by ',' and the target group" do
+ @provider.should_receive(:run_command).with({:command => "gpasswd -M all,your,base,are,belong,to,us aj"})
+ @provider.modify_group_members
+ end
+end
+
+describe Chef::Provider::Group::Groupadd, "load_current_resource" do
+ before do
+ @node = mock("Chef::Node", :null_object => true)
+ @new_resource = mock("Chef::Resource::Group", :null_object => true, :group_name => "aj")
+ @provider = Chef::Provider::Group::Groupadd.new(@node, @new_resource)
+ File.stub!(:exists?).and_return(false)
+ end
+
+ [ "/usr/sbin/groupadd",
+ "/usr/sbin/groupmod",
+ "/usr/sbin/groupdel",
+ "/usr/bin/gpasswd" ].each do |required_binary|
+ it "should raise an error if the required binary #{required_binary} doesn't exist" do
+ File.should_receive(:exists?).with("/usr/sbin/groupadd").and_return(false)
+ lambda { @provider.load_current_resource }.should raise_error(Chef::Exception::Group)
+ end
+ end
+
+ it "shouldn't raise an error if the required binaries exist" do
+ File.stub!(:exists?).and_return(true)
+ lambda { @provider.load_current_resource }.should_not raise_error(Chef::Exception::Group)
+ end
end
View
14 chef/spec/unit/resource/group_spec.rb
@@ -36,10 +36,12 @@
@resource.group_name.should eql("admin")
end
- %w{members gid}.each do |attrib|
- it "should set #{attrib} to nil" do
- @resource.send(attrib).should eql(nil)
- end
+ it "should default gid to nil" do
+ @resource.gid.should eql(nil)
+ end
+
+ it "should default members to an empty array" do
+ @resource.members.should eql([])
end
it "should set action to :create" do
@@ -88,9 +90,9 @@
@resource = Chef::Resource::Group.new("admin")
end
- it "should allow a string" do
+ it "should allow and convert a string" do
@resource.members "aj"
- @resource.members.should eql("aj")
+ @resource.members.should eql(["aj"])
end
it "should allow an array" do

0 comments on commit 984ca07

Please sign in to comment.