diff options
Diffstat (limited to 'spec/unit/provider')
-rwxr-xr-x | spec/unit/provider/exec/shell_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/provider/group/windows_adsi_spec.rb | 79 | ||||
-rwxr-xr-x | spec/unit/provider/macauthorization_spec.rb | 5 | ||||
-rwxr-xr-x | spec/unit/provider/mount/parsed_spec.rb | 2 | ||||
-rwxr-xr-x | spec/unit/provider/service/smf_spec.rb | 1 | ||||
-rwxr-xr-x | spec/unit/provider/service/windows_spec.rb | 166 | ||||
-rwxr-xr-x | spec/unit/provider/ssh_authorized_key/parsed_spec.rb | 4 | ||||
-rwxr-xr-x | spec/unit/provider/user/user_role_add_spec.rb | 2 | ||||
-rwxr-xr-x | spec/unit/provider/user/useradd_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/provider/user/windows_adsi_spec.rb | 110 |
10 files changed, 367 insertions, 6 deletions
diff --git a/spec/unit/provider/exec/shell_spec.rb b/spec/unit/provider/exec/shell_spec.rb index 90047b9d6..62036a79c 100755 --- a/spec/unit/provider/exec/shell_spec.rb +++ b/spec/unit/provider/exec/shell_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' provider_class = Puppet::Type.type(:exec).provider(:shell) -describe provider_class do +describe provider_class, :unless => Puppet.features.microsoft_windows? do before :each do @resource = Puppet::Resource.new(:exec, 'foo') @provider = provider_class.new(@resource) diff --git a/spec/unit/provider/group/windows_adsi_spec.rb b/spec/unit/provider/group/windows_adsi_spec.rb new file mode 100644 index 000000000..7faaa1a8c --- /dev/null +++ b/spec/unit/provider/group/windows_adsi_spec.rb @@ -0,0 +1,79 @@ +#!/usr/bin/env ruby + +require 'spec_helper' + +describe Puppet::Type.type(:group).provider(:windows_adsi) do + let(:resource) do + Puppet::Type.type(:group).new( + :title => 'testers', + :provider => :windows_adsi + ) + end + + let(:provider) { resource.provider } + + let(:connection) { stub 'connection' } + + before :each do + Puppet::Util::ADSI.stubs(:computer_name).returns('testcomputername') + Puppet::Util::ADSI.stubs(:connect).returns connection + end + + describe ".instances" do + it "should enumerate all groups" do + names = ['group1', 'group2', 'group3'] + stub_groups = names.map{|n| stub(:name => n)} + + connection.stubs(:execquery).with("select * from win32_group").returns stub_groups + + described_class.instances.map(&:name).should =~ names + end + end + + describe "when managing members" do + it "should be able to provide a list of members" do + provider.group.stubs(:members).returns ['user1', 'user2', 'user3'] + + provider.members.should =~ ['user1', 'user2', 'user3'] + end + + it "should be able to set group members" do + provider.group.stubs(:members).returns ['user1', 'user2'] + + provider.group.expects(:remove_members).with('user1') + provider.group.expects(:add_members).with('user3') + + provider.members = ['user2', 'user3'] + end + end + + it "should be able to create a group" do + resource[:members] = ['user1', 'user2'] + + group = stub 'group' + Puppet::Util::ADSI::Group.expects(:create).with('testers').returns group + + group.expects(:set_members).with(['user1', 'user2']) + + provider.create + end + + it "should be able to test whether a group exists" do + Puppet::Util::ADSI.stubs(:connect).returns stub('connection') + provider.should be_exists + + Puppet::Util::ADSI.stubs(:connect).returns nil + provider.should_not be_exists + end + + it "should be able to delete a group" do + connection.expects(:Delete).with('group', 'testers') + + provider.delete + end + + it "should warn when trying to manage the gid property" do + provider.expects(:warning).with { |msg| msg =~ /No support for managing property gid/ } + provider.send(:gid=, 500) + end +end diff --git a/spec/unit/provider/macauthorization_spec.rb b/spec/unit/provider/macauthorization_spec.rb index a76f917f7..dbe36a04b 100755 --- a/spec/unit/provider/macauthorization_spec.rb +++ b/spec/unit/provider/macauthorization_spec.rb @@ -106,6 +106,11 @@ describe provider_class do end it "should call the internal method set_right" do + @provider.expects(:execute).with { |cmds, args| + cmds.include?("read") and + cmds.include?(@authname) and + args[:combine] == false + }.once @provider.expects(:set_right) @provider.flush end diff --git a/spec/unit/provider/mount/parsed_spec.rb b/spec/unit/provider/mount/parsed_spec.rb index e812e3359..c86525707 100755 --- a/spec/unit/provider/mount/parsed_spec.rb +++ b/spec/unit/provider/mount/parsed_spec.rb @@ -4,7 +4,7 @@ require 'shared_behaviours/all_parsedfile_providers' provider_class = Puppet::Type.type(:mount).provider(:parsed) -describe provider_class do +describe provider_class, :fails_on_windows => true do before :each do @mount_class = Puppet::Type.type(:mount) diff --git a/spec/unit/provider/service/smf_spec.rb b/spec/unit/provider/service/smf_spec.rb index 5212d540a..fd7d50e3a 100755 --- a/spec/unit/provider/service/smf_spec.rb +++ b/spec/unit/provider/service/smf_spec.rb @@ -111,6 +111,7 @@ describe provider_class do it "should import the manifest if service is missing" do @provider.expects(:svccfg).with(:import, "/tmp/myservice.xml") @provider.expects(:texecute).with(:start, ["/usr/sbin/svcadm", :enable, "/system/myservice"], true) + @provider.expects(:svcs).with('-H', '-o', 'state,nstate', "/system/myservice").returns("online\t-") @provider.start end diff --git a/spec/unit/provider/service/windows_spec.rb b/spec/unit/provider/service/windows_spec.rb new file mode 100755 index 000000000..07e4f4d1a --- /dev/null +++ b/spec/unit/provider/service/windows_spec.rb @@ -0,0 +1,166 @@ +#!/usr/bin/env rspec +# +# Unit testing for the Windows service Provider +# + +require 'spec_helper' + +require 'win32/service' if Puppet.features.microsoft_windows? + +describe Puppet::Type.type(:service).provider(:windows), :if => Puppet.features.microsoft_windows? do + + before :each do + @resource = Puppet::Type.type(:service).new(:name => 'snmptrap', :provider => :windows) + + @config = Struct::ServiceConfigInfo.new + + @status = Struct::ServiceStatus.new + + Win32::Service.stubs(:config_info).with(@resource[:name]).returns(@config) + Win32::Service.stubs(:status).with(@resource[:name]).returns(@status) + end + + describe ".instances" do + it "should enumerate all services" do + list_of_services = ['snmptrap', 'svchost', 'sshd'].map { |s| stub('service', :service_name => s) } + Win32::Service.expects(:services).returns(list_of_services) + + described_class.instances.map(&:name).should =~ ['snmptrap', 'svchost', 'sshd'] + end + end + + describe "#start" do + it "should call out to the Win32::Service API to start the service" do + @config.start_type = Win32::Service.get_start_type(Win32::Service::SERVICE_AUTO_START) + + Win32::Service.expects(:start).with( @resource[:name] ) + + @resource.provider.start + end + + it "should handle when Win32::Service.start raises a Win32::Service::Error" do + @config.start_type = Win32::Service.get_start_type(Win32::Service::SERVICE_AUTO_START) + + Win32::Service.expects(:start).with( @resource[:name] ).raises( + Win32::Service::Error.new("The service cannot be started, either because it is disabled or because it has no enabled devices associated with it.") + ) + + expect { @resource.provider.start }.to raise_error( + Puppet::Error, + /Cannot start .*, error was: The service cannot be started, either/ + ) + end + + describe "when the service is disabled" do + before :each do + @config.start_type = Win32::Service.get_start_type(Win32::Service::SERVICE_DISABLED) + Win32::Service.stubs(:start).with(@resource[:name]) + end + + it "should refuse to start if not managing enable" do + expect { @resource.provider.start }.to raise_error(Puppet::Error, /Will not start disabled service/) + end + + it "should enable if managing enable and enable is true" do + @resource[:enable] = :true + + Win32::Service.expects(:configure).with('service_name' => @resource[:name], 'start_type' => Win32::Service::SERVICE_AUTO_START).returns(Win32::Service) + + @resource.provider.start + end + + it "should manual start if managing enable and enable is false" do + @resource[:enable] = :false + + Win32::Service.expects(:configure).with('service_name' => @resource[:name], 'start_type' => Win32::Service::SERVICE_DEMAND_START).returns(Win32::Service) + + @resource.provider.start + end + end + end + + describe "#stop" do + it "should call out to the Win32::Service API to stop the service" do + Win32::Service.expects(:stop).with( @resource[:name] ) + @resource.provider.stop + end + + it "should handle when Win32::Service.stop raises a Win32::Service::Error" do + Win32::Service.expects(:stop).with( @resource[:name] ).raises( + Win32::Service::Error.new("should not try to stop an already stopped service.") + ) + + expect { @resource.provider.stop }.to raise_error( + Puppet::Error, + /Cannot stop .*, error was: should not try to stop an already stopped service/ + ) + end + end + + describe "#status" do + ['stopped', 'paused', 'stop pending', 'pause pending'].each do |state| + it "should report a #{state} service as stopped" do + @status.current_state = state + + @resource.provider.status.should == :stopped + end + end + + ["running", "continue pending", "start pending" ].each do |state| + it "should report a #{state} service as running" do + @status.current_state = state + + @resource.provider.status.should == :running + end + end + end + + describe "#enabled?" do + it "should report a service with a startup type of manual as manual" do + @config.start_type = Win32::Service.get_start_type(Win32::Service::SERVICE_DEMAND_START) + + @resource.provider.enabled?.should == :manual + end + + it "should report a service with a startup type of disabled as false" do + @config.start_type = Win32::Service.get_start_type(Win32::Service::SERVICE_DISABLED) + + @resource.provider.enabled?.should == :false + end + + # We need to guard this section explicitly since rspec will always + # construct all examples, even if it isn't going to run them. + if Puppet.features.microsoft_windows? + [Win32::Service::SERVICE_AUTO_START, Win32::Service::SERVICE_BOOT_START, Win32::Service::SERVICE_SYSTEM_START].each do |start_type_const| + start_type = Win32::Service.get_start_type(start_type_const) + it "should report a service with a startup type of '#{start_type}' as true" do + @config.start_type = start_type + + @resource.provider.enabled?.should == :true + end + end + end + end + + describe "#enable" do + it "should set service start type to Service_Auto_Start when enabled" do + Win32::Service.expects(:configure).with('service_name' => @resource[:name], 'start_type' => Win32::Service::SERVICE_AUTO_START).returns(Win32::Service) + @resource.provider.enable + end + end + + describe "#disable" do + it "should set service start type to Service_Disabled when disabled" do + Win32::Service.expects(:configure).with('service_name' => @resource[:name], 'start_type' => Win32::Service::SERVICE_DISABLED).returns(Win32::Service) + @resource.provider.disable + end + end + + describe "#manual_start" do + it "should set service start type to Service_Demand_Start (manual) when manual" do + Win32::Service.expects(:configure).with('service_name' => @resource[:name], 'start_type' => Win32::Service::SERVICE_DEMAND_START).returns(Win32::Service) + @resource.provider.manual_start + end + end + +end diff --git a/spec/unit/provider/ssh_authorized_key/parsed_spec.rb b/spec/unit/provider/ssh_authorized_key/parsed_spec.rb index bd5e55a9e..a7798be54 100755 --- a/spec/unit/provider/ssh_authorized_key/parsed_spec.rb +++ b/spec/unit/provider/ssh_authorized_key/parsed_spec.rb @@ -5,7 +5,7 @@ require 'puppet_spec/files' provider_class = Puppet::Type.type(:ssh_authorized_key).provider(:parsed) -describe provider_class do +describe provider_class, :unless => Puppet.features.microsoft_windows? do include PuppetSpec::Files before :each do @@ -77,7 +77,7 @@ describe provider_class do end end -describe provider_class do +describe provider_class, :unless => Puppet.features.microsoft_windows? do before :each do @resource = Puppet::Type.type(:ssh_authorized_key).new(:name => "foo", :user => "random_bob") diff --git a/spec/unit/provider/user/user_role_add_spec.rb b/spec/unit/provider/user/user_role_add_spec.rb index 5f2fc306e..c44fc5a65 100755 --- a/spec/unit/provider/user/user_role_add_spec.rb +++ b/spec/unit/provider/user/user_role_add_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' provider_class = Puppet::Type.type(:user).provider(:user_role_add) -describe provider_class do +describe provider_class, :fails_on_windows => true do before do @resource = stub("resource", :name => "myuser", :managehome? => nil) @resource.stubs(:should).returns "fakeval" diff --git a/spec/unit/provider/user/useradd_spec.rb b/spec/unit/provider/user/useradd_spec.rb index 724fc12c0..4265ee3a0 100755 --- a/spec/unit/provider/user/useradd_spec.rb +++ b/spec/unit/provider/user/useradd_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' provider_class = Puppet::Type.type(:user).provider(:useradd) -describe provider_class do +describe provider_class, :fails_on_windows => true do before do @resource = stub("resource", :name => "myuser", :managehome? => nil) @resource.stubs(:should).returns "fakeval" diff --git a/spec/unit/provider/user/windows_adsi_spec.rb b/spec/unit/provider/user/windows_adsi_spec.rb new file mode 100644 index 000000000..073a3d328 --- /dev/null +++ b/spec/unit/provider/user/windows_adsi_spec.rb @@ -0,0 +1,110 @@ +#!/usr/bin/env ruby + +require 'spec_helper' + +describe Puppet::Type.type(:user).provider(:windows_adsi) do + let(:resource) do + Puppet::Type.type(:user).new( + :title => 'testuser', + :comment => 'Test J. User', + :provider => :windows_adsi + ) + end + + let(:provider) { resource.provider } + + let(:connection) { stub 'connection' } + + before :each do + Puppet::Util::ADSI.stubs(:computer_name).returns('testcomputername') + Puppet::Util::ADSI.stubs(:connect).returns connection + end + + describe ".instances" do + it "should enumerate all users" do + names = ['user1', 'user2', 'user3'] + stub_users = names.map{|n| stub(:name => n)} + + connection.stubs(:execquery).with("select * from win32_useraccount").returns(stub_users) + + described_class.instances.map(&:name).should =~ names + end + end + + it "should provide access to a Puppet::Util::ADSI::User object" do + provider.user.should be_a(Puppet::Util::ADSI::User) + end + + describe "when managing groups" do + it 'should return the list of groups as a comma-separated list' do + provider.user.stubs(:groups).returns ['group1', 'group2', 'group3'] + + provider.groups.should == 'group1,group2,group3' + end + + it "should return absent if there are no groups" do + provider.user.stubs(:groups).returns [] + + provider.groups.should == '' + end + + it 'should be able to add a user to a set of groups' do + resource[:membership] = :minimum + provider.user.expects(:set_groups).with('group1,group2', true) + + provider.groups = 'group1,group2' + + resource[:membership] = :inclusive + provider.user.expects(:set_groups).with('group1,group2', false) + + provider.groups = 'group1,group2' + end + end + + describe "when creating a user" do + it "should create the user on the system and set its other properties" do + resource[:groups] = ['group1', 'group2'] + resource[:membership] = :inclusive + resource[:comment] = 'a test user' + resource[:home] = 'C:\Users\testuser' + + user = stub 'user' + Puppet::Util::ADSI::User.expects(:create).with('testuser').returns user + + user.stubs(:groups).returns(['group2', 'group3']) + + user.expects(:set_groups).with('group1,group2', false) + user.expects(:[]=).with('Description', 'a test user') + user.expects(:[]=).with('HomeDirectory', 'C:\Users\testuser') + + provider.create + end + end + + it 'should be able to test whether a user exists' do + Puppet::Util::ADSI.stubs(:connect).returns stub('connection') + provider.should be_exists + + Puppet::Util::ADSI.stubs(:connect).returns nil + provider.should_not be_exists + end + + it 'should be able to delete a user' do + connection.expects(:Delete).with('user', 'testuser') + + provider.delete + end + + it "should commit the user when flushed" do + provider.user.expects(:commit) + + provider.flush + end + + [:uid, :gid, :shell].each do |prop| + it "should warn when trying to manage the #{prop} property" do + provider.expects(:warning).with { |msg| msg =~ /No support for managing property #{prop}/ } + provider.send("#{prop}=", 'foo') + end + end +end |