summaryrefslogtreecommitdiff
path: root/spec/functional/resource/group_spec.rb
diff options
context:
space:
mode:
authorsersut <serdar@opscode.com>2013-11-07 12:34:25 -0800
committersersut <serdar@opscode.com>2013-11-11 11:27:48 -0800
commit1db8f468a15632eb129b30367babd001ee2d0ace (patch)
tree91bbc68ec638c48b071fb374372a66b6886366e9 /spec/functional/resource/group_spec.rb
parentecbc917ac5496f3138b798332ea66f477c33f8ba (diff)
downloadchef-1db8f468a15632eb129b30367babd001ee2d0ace.tar.gz
Specs for CHEF-3297. TDD rules.
Diffstat (limited to 'spec/functional/resource/group_spec.rb')
-rw-r--r--spec/functional/resource/group_spec.rb329
1 files changed, 212 insertions, 117 deletions
diff --git a/spec/functional/resource/group_spec.rb b/spec/functional/resource/group_spec.rb
index 2c9a568979..e2829ea9f0 100644
--- a/spec/functional/resource/group_spec.rb
+++ b/spec/functional/resource/group_spec.rb
@@ -21,32 +21,31 @@ require 'spec_helper'
require 'functional/resource/base'
describe Chef::Resource::Group, :requires_root_or_running_windows do
-
- def group_should_exist(resource)
+ def group_should_exist(group)
case ohai[:platform_family]
when "debian", "fedora", "rhel", "suse", "gentoo", "slackware", "arch"
- expect { Etc::getgrnam(resource.name) }.to_not raise_error(ArgumentError, "can't find group for #{resource.name}")
- expect(resource.name).to eq(Etc::getgrnam(resource.name).name)
+ expect { Etc::getgrnam(group) }.to_not raise_error(ArgumentError, "can't find group for #{group}")
+ expect(group).to eq(Etc::getgrnam(group).name)
when "windows"
- expect { Chef::Util::Windows::NetGroup.new(resource.group_name).local_get_members }.to_not raise_error(ArgumentError, "The group name could not be found.")
+ expect { Chef::Util::Windows::NetGroup.new(group).local_get_members }.to_not raise_error(ArgumentError, "The group name could not be found.")
end
end
- def user_exist_in_group?(resource, user)
+ def user_exist_in_group?(user)
case ohai[:platform_family]
- when "debian", "fedora", "rhel", "suse", "gentoo", "slackware", "arch"
- Etc::getgrnam(resource.name).mem.include?(user)
when "windows"
- Chef::Util::Windows::NetGroup.new(resource.group_name).local_get_members.include?(user)
+ Chef::Util::Windows::NetGroup.new(group_name).local_get_members.include?(user)
+ else
+ Etc::getgrnam(group_name).mem.include?(user)
end
end
- def group_should_not_exist(resource)
+ def group_should_not_exist(group)
case ohai[:platform_family]
when "debian", "fedora", "rhel", "suse", "gentoo", "slackware", "arch"
- expect { Etc::getgrnam(resource.name) }.to raise_error(ArgumentError, "can't find group for #{resource.name}")
+ expect { Etc::getgrnam(group) }.to raise_error(ArgumentError, "can't find group for #{group}")
when "windows"
- expect { Chef::Util::Windows::NetGroup.new(resource.group_name).local_get_members }.to raise_error(ArgumentError, "The group name could not be found.")
+ expect { Chef::Util::Windows::NetGroup.new(group).local_get_members }.to raise_error(ArgumentError, "The group name could not be found.")
end
end
@@ -54,151 +53,247 @@ describe Chef::Resource::Group, :requires_root_or_running_windows do
return resource.gid == Etc::getgrnam(resource.name).gid if unix?
end
- def get_user_resource(username)
+ def user(username)
usr = Chef::Resource::User.new("#{username}", run_context)
- usr.password("Jetsream123!")
usr
end
def create_user(username)
- get_user_resource(username).run_action(:create)
+ user(username).run_action(:create)
+ # TODO: User shouldn't exist
end
def remove_user(username)
- get_user_resource(username).run_action(:remove)
+ user(username).run_action(:remove)
+ # TODO: User shouldn't exist
end
- before do
- @grp_resource = Chef::Resource::Group.new("test-group-#{SecureRandom.random_number(9999)}", run_context)
- end
+ shared_examples_for "correct group management" do
+ def add_members_to_group(members)
+ temp_resource = group_resource.dup
+ temp_resource.members(members)
+ temp_resource.excluded_members([ ])
+ temp_resource.append(true)
+ temp_resource.run_action(:modify)
+ members.each do |member|
+ user_exist_in_group?(member).should == true
+ end
+ end
+
+ def create_group
+ temp_resource = group_resource.dup
+ temp_resource.members([ ])
+ temp_resource.excluded_members([ ])
+ temp_resource.run_action(:create)
+ group_should_exist(group_name)
+ included_members.each do |member|
+ user_exist_in_group?(member).should == false
+ end
+ end
+
+ before(:each) do
+ create_group
+ end
- context "group create action" do
after(:each) do
- @grp_resource.run_action(:remove)
+ group_resource.run_action(:remove)
+ group_should_not_exist(group_name)
end
- it "create a group" do
- @grp_resource.run_action(:create)
- group_should_exist(@grp_resource)
+ describe "when append is not set" do
+ let(:included_members) { ["group-spec-Eric"] }
+
+ before do
+ create_user("group-spec-Gordon")
+ add_members_to_group(["group-spec-Gordon"])
+ end
+
+ it "should remove the existing users and add the new users to the group" do
+ group_resource.run_action(tested_action)
+
+ user_exist_in_group?("group-spec-Eric").should == true
+ user_exist_in_group?("group-spec-Gordon").should == false
+ end
end
- context "group name with 256 characters", :windows_only do
+ describe "when append is set" do
before(:each) do
- grp_name = "theoldmanwalkingdownthestreetalwayshadagoodsmileonhisfacetheoldmanwalkingdownthestreetalwayshadagoodsmileonhisfacetheoldmanwalkingdownthestreetalwayshadagoodsmileonhisfacetheoldmanwalkingdownthestreetalwayshadagoodsmileonhisfacetheoldmanwalkingdownthestree"
- @new_grp = Chef::Resource::Group.new(grp_name, run_context)
+ group_resource.append(true)
end
- after do
- @new_grp.run_action(:remove)
+
+ describe "when the users exist" do
+ before do
+ (included_members + excluded_members).each do |member|
+ create_user(member)
+ end
+ end
+
+ after do
+ (included_members + excluded_members).each do |member|
+ remove_user(member)
+ end
+ end
+
+ it "should add included members to the group" do
+ group_resource.run_action(tested_action)
+
+ included_members.each do |member|
+ user_exist_in_group?(member).should == true
+ end
+ excluded_members.each do |member|
+ user_exist_in_group?(member).should == false
+ end
+ end
+
+ describe "when group contains some users" do
+ before(:each) do
+ add_members_to_group([ "group-spec-Gordon", "group-spec-Anthony" ])
+ end
+
+ it "should add the included users and remove excluded users" do
+ group_resource.run_action(tested_action)
+
+ included_members.each do |member|
+ user_exist_in_group?(member).should == true
+ end
+ excluded_members.each do |member|
+ user_exist_in_group?(member).should == false
+ end
+ end
+ end
end
- it " create a group" do
- @new_grp.run_action(:create)
- group_should_exist(@new_grp)
+
+ describe "when the users doesn't exist" do
+ describe "when append is not set" do
+ it "should raise an error" do
+ lambda { @grp_resource.run_action(tested_action) }.should raise_error
+ end
+ end
+
+ describe "when append is set" do
+ it "should raise an error" do
+ lambda { @grp_resource.run_action(tested_action) }.should raise_error
+ end
+ end
end
end
- context "group name with more than 256 characters", :windows_only do
+ end
+
+ let(:group_name) { "test-group-#{SecureRandom.random_number(9999)}" }
+ let(:included_members) { nil }
+ let(:excluded_members) { nil }
+ let(:group_resource) {
+ group = Chef::Resource::Group.new(group_name, run_context)
+ group.members(included_members)
+ group.excluded_members(excluded_members)
+ group
+ }
+
+ it "append should be false by default" do
+ group_resource.append.should == false
+ end
+
+ describe "group create action" do
+ after(:each) do
+ group_resource.run_action(:remove)
+ group_should_not_exist(group_name)
+ end
+
+ it "should create a group" do
+ group_resource.run_action(:create)
+ group_should_exist(group_name)
+ end
+
+ describe "when group name is length 256", :windows_only do
before(:each) do
- grp_name = "theoldmanwalkingdownthestreetalwayshadagoodsmileonhisfacetheoldmanwalkingdownthestreetalwayshadagoodsmileonhisfacetheoldmanwalkingdownthestreetalwayshadagoodsmileonhisfacetheoldmanwalkingdownthestreetalwayshadagoodsmileonhisfacetheoldmanwalkingdownthestreeQQQQQQQQQQQQQQQQQ"
- @new_grp = Chef::Resource::Group.new(grp_name, run_context)
+ let!(:group_name) { "theoldmanwalkingdownthestreetalwayshadagood\
+smileonhisfacetheoldmanwalkingdownthestreetalwayshadagoodsmileonhisface\
+theoldmanwalkingdownthestreetalwayshadagoodsmileonhisfacetheoldmanwalking\
+downthestreetalwayshadagoodsmileonhisfacetheoldmanwalkingdownthestree" }
end
- it " not create a group" do
- expect { @new_grp.run_action(:create) }.to raise_error
- group_should_not_exist(@new_grp)
+
+ it "should create a group" do
+ group_resource.run_action(:create)
+ group_should_exist(group_name)
end
end
- end
- context "group remove action" do
- before(:each) do
- @grp_resource.run_action(:create)
+ describe "when group name length is more than 256", :windows_only do
+ before(:each) do
+ let!(:group_name) { "theoldmanwalkingdownthestreetalwayshadagood\
+smileonhisfacetheoldmanwalkingdownthestreetalwayshadagoodsmileonhisface\
+theoldmanwalkingdownthestreetalwayshadagoodsmileonhisfacetheoldmanwalking\
+downthestreetalwayshadagoodsmileonhisfacetheoldmanwalkingdownthestreeQQQQQQ" }
+ end
+
+ it "should not create a group" do
+ lambda { group_resource.run_action(:create) }.should raise_error
+ group_should_not_exist(group_name)
+ end
end
- it "remove a group" do
- @grp_resource.run_action(:remove)
- group_should_not_exist(@grp_resource)
+ describe "should raise an error when same member is included in the members and excluded_members" do
+ it "should raise an error" do
+ invalid_resource = group_resource.dup
+ invalid_resource.members(["Jack"])
+ invalid_resource.excluded_members(["Jack"])
+ lambda { invalid_resource.run_action(:create)}.should raise_error(Chef::Exceptions::ConflictingMembersInGroup)
+ end
end
end
- context "group modify action", :unsupported_group_provider_platform do
- before(:each) do
- @grp_resource.run_action(:create)
+ describe "group remove action" do
+ describe "when there is a group" do
+ before do
+ group_resource.run_action(:create)
+ group_should_exist(group_name)
+ end
+
+ it "should remove a group" do
+ group_resource.run_action(:remove)
+ group_should_not_exist(group_name)
+ end
end
- after(:each) do
- @grp_resource.run_action(:remove)
- end
-
- it "add user to group" do
- user1 = "user1-#{SecureRandom.random_number(9999)}"
- user2 = "user2-#{SecureRandom.random_number(9999)}"
-
- create_user(user1)
- @grp_resource.members(user1)
- expect(user_exist_in_group?(@grp_resource, user1)).to be_false
- @grp_resource.run_action(:modify)
- group_should_exist(@grp_resource)
- expect(user_exist_in_group?(@grp_resource, user1)).to be_true
-
- create_user(user2)
- expect(user_exist_in_group?(@grp_resource, user2)).to be_false
- @grp_resource.members(user2)
- @grp_resource.run_action(:modify)
- group_should_exist(@grp_resource)
-
- #default append is false, so modify action remove old member user1 from group and add new member user2
- expect(user_exist_in_group?(@grp_resource, user1)).to be_false
- expect(user_exist_in_group?(@grp_resource, user2)).to be_true
- remove_user(user1)
- remove_user(user2)
- end
-
-
- it "append user to a group" do
- user1 = "user1-#{SecureRandom.random_number(9999)}"
- user2 = "user2-#{SecureRandom.random_number(9999)}"
- create_user(user1)
- @grp_resource.members(user1)
- expect(user_exist_in_group?(@grp_resource, user1)).to be_false
- #default append attribute is false
- @grp_resource.run_action(:modify)
- group_should_exist(@grp_resource)
- expect(user_exist_in_group?(@grp_resource, user1)).to be_true
- #set append attribute to true
- @grp_resource.append(true)
- create_user(user2)
- expect(user_exist_in_group?(@grp_resource, user2)).to be_false
- @grp_resource.members(user2)
- @grp_resource.run_action(:modify)
- group_should_exist(@grp_resource)
- expect(user_exist_in_group?(@grp_resource, user1)).to be_true
- expect(user_exist_in_group?(@grp_resource, user2)).to be_true
- remove_user(user1)
- remove_user(user2)
- end
-
- it "raise error on add non-existent user to group" do
- user1 = "user1-#{SecureRandom.random_number(9999)}"
- @grp_resource.members(user1)
- @grp_resource.append(true)
- expect(user_exist_in_group?(@grp_resource, user1)).to be_false
- expect { @grp_resource.run_action(:modify) }.to raise_error
+ describe "when there is no group" do
+ it "should be no-op" do
+ group_resource.run_action(:remove)
+ group_should_not_exist(group_name)
+ end
end
end
- context "group manage action", :unix_only, :unsupported_group_provider_platform do
- before(:each) do
- @grp_resource.run_action(:create)
+ describe "group modify action" do
+ let(:included_members) { ["group-spec-Gordon", "group-spec-Eric"] }
+ let(:excluded_members) { ["group-spec-Anthony"] }
+ let(:tested_action) { :modify }
+
+ describe "when there is no group" do
+ it "should raise an error" do
+ lambda { group_resource.run_action(:modify) }.should raise_error
+ end
end
- after(:each) do
- @grp_resource.run_action(:remove)
+ describe "when there is a group" do
+ it_behaves_like "correct group management"
end
+ end
- it "change gid of the group" do
- grp_id = 1234567890
- @grp_resource.gid(grp_id)
- @grp_resource.run_action(:manage)
- group_should_exist(@grp_resource)
- expect(compare_gid(@grp_resource, grp_id)).to be_true
+ describe "group manage action" do
+ let(:included_members) { ["group-spec-Gordon", "group-spec-Eric"] }
+ let(:excluded_members) { ["group-spec-Anthony"] }
+ let(:tested_action) { :manage }
+
+ describe "when there is no group" do
+ it "should raise an error" do
+ lambda { group_resource.run_action(:manage) }.should_not raise_error
+ group_should_not_exist(group_name)
+ end
+ end
+
+ describe "when there is a group" do
+ it_behaves_like "correct group management"
end
end
end
+