diff --git a/spec/unit/provider/service/base_spec.rb b/spec/unit/provider/service/base_spec.rb new file mode 100755 index 000000000..9522fd7f8 --- /dev/null +++ b/spec/unit/provider/service/base_spec.rb @@ -0,0 +1,77 @@ +#!/usr/bin/env rspec +require 'spec_helper' +require 'rbconfig' +require 'fileutils' + +provider_class = Puppet::Type.type(:service).provider(:init) + +describe "base service provider" do + include PuppetSpec::Files + + let :type do Puppet::Type.type(:service) end + let :provider do type.provider(:base) end + + subject { provider } + + context "basic operations" do + # Cross-platform file interactions. Fun times. + Ruby = File.join(RbConfig::CONFIG["bindir"], + RbConfig::CONFIG["RUBY_INSTALL_NAME"] + + RbConfig::CONFIG["EXEEXT"]) + + Start = "#{Ruby} -rfileutils -e 'FileUtils.touch(ARGV[0])'" + Status = "#{Ruby} -e 'exit File.file?(ARGV[0])'" + Stop = "#{Ruby} -e 'File.exist?(ARGV[0]) and File.unlink(ARGV[0])'" + + let :flag do tmpfile('base-service-test') end + + subject do + type.new(:name => "test", :provider => :base, + :start => "#{Start} #{flag}", + :status => "#{Status} #{flag}", + :stop => "#{Stop} #{flag}" + ).provider + end + + before :each do + File.unlink(flag) if File.exist?(flag) + end + + it { should be } + + it "should invoke the start command if not running" do + File.should_not be_file flag + subject.start + File.should be_file flag + end + + it "should be stopped before being started" do + subject.status.should == :stopped + end + + it "should be running after being started" do + subject.start + subject.status.should == :running + end + + it "should invoke the stop command when asked" do + subject.start + subject.status.should == :running + subject.stop + subject.status.should == :stopped + File.should_not be_file flag + end + + it "should start again even if already running" do + subject.start + subject.expects(:ucommand).with(:start) + subject.start + end + + it "should stop again even if already stopped" do + subject.stop + subject.expects(:ucommand).with(:stop) + subject.stop + end + end +end diff --git a/test/ral/providers/service/base.rb b/test/ral/providers/service/base.rb deleted file mode 100755 index 1b5a63e7d..000000000 --- a/test/ral/providers/service/base.rb +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/env ruby -require File.expand_path(File.dirname(__FILE__) + '/../../../lib/puppettest') - -require 'puppettest' - -class TestBaseServiceProvider < Test::Unit::TestCase - include PuppetTest - - def test_base - running = tempfile - - commands = {} - %w{touch rm test}.each do |c| - path = %x{which #{c}}.chomp - if path == "" - $stderr.puts "Cannot find '#{c}'; cannot test base service provider" - return - end - commands[c.to_sym] = path - end - - service = Puppet::Type.type(:service).new( - - :name => "yaytest", :provider => :base, - :start => "#{commands[:touch]} #{running}", - :status => "#{commands[:test]} -f #{running}", - - :stop => "#{commands[:rm]} #{running}" - ) - - provider = service.provider - assert(provider, "did not get base provider") - - assert_nothing_raised do - provider.start - end - assert(FileTest.exists?(running), "start was not called correctly") - assert_nothing_raised do - assert_equal(:running, provider.status, "status was not returned correctly") - end - assert_nothing_raised do - provider.stop - end - assert(! FileTest.exists?(running), "stop was not called correctly") - assert_nothing_raised do - assert_equal(:stopped, provider.status, "status was not returned correctly") - end - end - - # Testing #454 - def test_that_failures_propagate - nope = "/no/such/command" - - service = Puppet::Type.type(:service).new( - - :name => "yaytest", :provider => :base, - :start => nope, - :status => nope, - :stop => nope, - - :restart => nope - ) - - provider = service.provider - assert(provider, "did not get base provider") - - # We can't fail well when status is messed up, because we depend on the return code - # of the command for data. - %w{start stop restart}.each do |command| - assert_raise(Puppet::Error, "did not throw error when #{command} failed") do - provider.send(command) - end - end - end -end -