diff options
author | Nick Lewis <nick@puppetlabs.com> | 2011-07-26 15:59:33 -0700 |
---|---|---|
committer | Jacob Helwig <jacob@puppetlabs.com> | 2011-08-19 13:52:57 -0700 |
commit | c69baf63bac4e7f6296b81fcb5278eaf95e7f74b (patch) | |
tree | cd998bb3bd829ca4afa2801603878b8c10121d0f | |
parent | 881c3858e88b6cc3d2c0e7bea6785fcbf091e4c0 (diff) | |
download | puppet-c69baf63bac4e7f6296b81fcb5278eaf95e7f74b.tar.gz puppet-c69baf63bac4e7f6296b81fcb5278eaf95e7f74b.tar.xz puppet-c69baf63bac4e7f6296b81fcb5278eaf95e7f74b.zip |
(#8272) Refactor specs for Windows service provider
These were using fake objects when that's not really necessary, so replace them
with real ones. Additionally, many of these specs were doing the same thing
(like creating a resource), so that has been extracted to the before block.
Reviewed-By: Jacob Helwig <jacob@puppetlabs.com>
(cherry picked from commit f5e8dbef9e16bf05e4c05a43407e94027faa2410)
-rwxr-xr-x | spec/unit/provider/service/windows_spec.rb | 90 |
1 files changed, 32 insertions, 58 deletions
diff --git a/spec/unit/provider/service/windows_spec.rb b/spec/unit/provider/service/windows_spec.rb index fff875032..0667d4694 100755 --- a/spec/unit/provider/service/windows_spec.rb +++ b/spec/unit/provider/service/windows_spec.rb @@ -5,42 +5,47 @@ require 'spec_helper' -require 'ostruct' require 'win32/service' if Puppet.features.microsoft_windows? -provider_class = Puppet::Type.type(:service).provider(:windows) - -describe provider_class, :if => Puppet.features.microsoft_windows? do +describe Puppet::Type.type(:service).provider(:windows), :if => Puppet.features.microsoft_windows? do before :each do - @provider = Puppet::Type.type(:service).provider(:windows) - Puppet::Type.type(:service).stubs(:provider).returns(@provider) + @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| OpenStruct.new(:service_name => s)} + list_of_services = ['snmptrap', 'svchost', 'sshd'].map { |s| stub('service', :service_name => s) } Win32::Service.expects(:services).returns(list_of_services) - provider_class.instances.map {|provider| provider.name}.should =~ ['snmptrap', 'svchost', 'sshd'] + 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('snmptrap') - resource = Puppet::Type.type(:service).new(:name => 'snmptrap') - resource.provider.start + @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('snmptrap').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.") ) - resource = Puppet::Type.type(:service).new(:name => 'snmptrap') - expect { resource.provider.start }.to raise_error( + expect { @resource.provider.start }.to raise_error( Puppet::Error, /Cannot start snmptrap, error was: The service cannot be started, either/ ) @@ -55,57 +60,32 @@ describe provider_class, :if => Puppet.features.microsoft_windows? do describe "#status" do ['stopped', 'paused', 'stop pending', 'pause pending'].each do |state| it "should report a #{state} service as stopped" do - Win32::Service.expects(:status).with('snmptrap').returns( - stub( - 'struct_service_status', - :instance_of? => Struct::ServiceStatus, - :current_state => state - ) - ) - resource = Puppet::Type.type(:service).new(:name => 'snmptrap') - - resource.provider.status.should == :stopped + @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 - Win32::Service.expects(:status).with('snmptrap').returns( - stub( - 'struct_service_status', - :instance_of? => Struct::ServiceStatus, - :current_state => state - ) - ) - resource = Puppet::Type.type(:service).new(:name => 'snmptrap') - resource.provider.status.should == :running + @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 - Win32::Service.expects(:config_info).with('snmptrap').returns( - stub( - 'struct_config_info', - :instance_of? => Struct::ServiceConfigInfo, - :start_type => Win32::Service.get_start_type(Win32::Service::SERVICE_DEMAND_START) - ) - ) - resource = Puppet::Type.type(:service).new(:name => 'snmptrap') - resource.provider.enabled?.should == :manual + @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 - Win32::Service.expects(:config_info).with('snmptrap').returns( - stub( - 'struct_config_info', - :instance_of? => Struct::ServiceConfigInfo, - :start_type => Win32::Service.get_start_type(Win32::Service::SERVICE_DISABLED) - ) - ) - resource = Puppet::Type.type(:service).new(:name => 'snmptrap') - resource.provider.enabled?.should == :false + @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 @@ -114,15 +94,9 @@ describe provider_class, :if => Puppet.features.microsoft_windows? do [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 - Win32::Service.expects(:config_info).with('snmptrap').returns( - stub( - 'struct_config_info', - :instance_of? => Struct::ServiceConfigInfo, - :start_type => start_type - ) - ) - resource = Puppet::Type.type(:service).new(:name => 'snmptrap') - resource.provider.enabled?.should == :true + @config.start_type = start_type + + @resource.provider.enabled?.should == :true end end end |