Permalink
Browse files

Added rake task and supporting files for RSpec examples.

  • Loading branch information...
1 parent 857e304 commit d4e2e5df444dd670271c7d61671dfeb440b33c48 mosen committed Feb 21, 2012
View
@@ -42,7 +42,7 @@ __Example__
uri => "lpd://localhost/printer_a",
description => "This is the printer description",
location => "Main office",
- ppd => "/Library/Printers/PPDs/Printer.ppd",
+ ppd => "/Library/Printers/PPDs/Printer.ppd", # PPD file will be autorequired
enabled => true, # Enabled by default
shared => false, # Disabled by default
options => {}, # Not yet supported: Hash of PPD options ( name => value )
View
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rspec/core/rake_task'
+
+task :default do
+ sh %{rake -T}
+end
+
+# Aliases for spec. The (s) versions are used by rvm specs/tests.
+task :test => [:spec]
+task :tests => [:spec]
+task :specs => [:spec]
+
+desc 'Run all RSpec tests'
+RSpec::Core::RakeTask.new(:spec) do |t|
+ t.rspec_opts = ['--color']
+end
+
+desc 'Generate code coverage'
+RSpec::Core::RakeTask.new(:coverage) do |t|
+ t.rcov = true
+ t.rcov_opts = ['--exclude', 'spec']
+end
File renamed without changes.
@@ -95,6 +95,7 @@ def printer_uris
uris
end
+ # TODO: unify prefetch/instances
def prefetch(resources)
prefetched_uris = self.printer_uris
@@ -111,7 +112,18 @@ def prefetch(resources)
end
def instances
- []
+ prefetched_uris = self.printer_uris
+
+ prefetched_long = self.printers_long.collect { |p|
+
+ p[:options] = self.printer_options(p[:name])
+ p[:provider] = :cups
+ p[:uri] = prefetched_uris[p] if prefetched_uris.key? p
+
+ new(p)
+ }
+
+ prefetched_long
end
end
View
@@ -1,12 +0,0 @@
-class puppet-cups {
- notify { $printers: }
- #printer { "test":
- # ensure => present,
-
- printer { "test":
- ensure => present,
-# uri => "/dev/null",
- location => "Non existent",
- description => "Test printer for puppet-cups module",
- }
-}
@@ -0,0 +1,27 @@
+#!/usr/bin/env rspec
+
+require 'spec_helper'
+
+describe 'cups provider' do
+ let(:provider) { Puppet::Type.type(:printer).provider(:cups) }
+ let(:resource) {
+ Puppet::Type.type(:printer).new({
+ :name => 'RSpec_Test_Printer',
+ :uri => 'file:///',
+ })
+ }
+
+ before :each do
+ Puppet::Type::Printer.stubs(:defaultprovider).returns provider
+ provider.stubs(:command).with(:lpadmin).returns "/usr/sbin/lpadmin"
+ end
+
+ it 'should be able to get a list of existing printers' do
+ provider.expects(:execute).with(['/usr/sbin/lpadmin']).returns("")
+
+ provider.instances.each do |p|
+ p.should be_instance_of(provider)
+ p.properties[:provider].to_s.should == provider.name.to_s
+ end
+ end
+end
@@ -1,47 +1,99 @@
#!/usr/bin/env rspec
-# TODO: cant require spec_helper outside of puppet tree?
+require 'spec_helper'
printer = Puppet::Type.type(:printer)
describe printer do
before do
- #ENV["PATH"] += File::PATH_SEPARATOR + "/usr/sbin" unless ENV["PATH"].split(File::PATH_SEPARATOR).include?("/usr/sbin")
+ @class = printer
@provider = stub 'provider'
- #@resource = stub 'resource', :resource => nil, :provider => @provider, :line => nil, :file => nil
+ @provider.stubs(:name).returns(:cups)
+ Puppet::Type::Printer.stubs(:defaultprovider).returns @provider
+
+ @resource = @class.new({:name => 'RSpec_Test_Printer'})
end
- it "should have a default provider inheriting from Puppet::Provider" do
- printer.defaultprovider.ancestors.should be_include(Puppet::Provider)
+ it 'should have :name be its namevar' do
+ @class.key_attributes.should == [:name]
end
- it "should be able to create a instance" do
- printer.new(:name => "foo").should_not be_nil
+ describe ':name' do
+ it 'should accept a name' do
+ @resource[:name] = 'RSpec_Test_Printer'
+ @resource[:name].should == 'RSpec_Test_Printer'
+ end
+
+ # CUPS naming requirements
+
+ it 'should not accept a name with spaces' do
+ lambda { @resource[:name] = ' test' }.should raise_error(Puppet::Error)
+ end
+
+ it 'should not accept a name with tabs' do
+ lambda { @resource[:name] = "\ttest" }.should raise_error(Puppet::Error)
+ end
+
+ it 'should not accept a name with forward slash' do
+ lambda { @resource[:name] = "test/printer" }.should raise_error(Puppet::Error)
+ end
+
+ it 'should not accept a name with the pound sign' do
+ lambda { @resource[:name] = "test#printer" }.should raise_error(Puppet::Error)
+ end
end
- describe "instances" do
- it "should have a valid provider" do
- printer.new(:name => "foo").provider.class.ancestors.should be_include(Puppet::Provider)
+ describe ':uri' do
+ it 'should accept a string' do
+ @resource[:uri] = "test"
end
+ end
- it "should delegate existence questions to its provider" do
- instance = printer.new(:name => "foo")
- instance.provider.expects(:exists?).returns "eh"
- instance.exists?.should == "eh"
+ describe ':description' do
+ it 'should accept a string' do
+ @resource[:description] = "test"
end
end
- properties = [:ensure, :name, :uri, :description, :location, :ppd, :enabled, :shared, :options]
+ describe ':location' do
+ it 'should accept a string' do
+ @resource[:location] = "test"
+ end
+ end
+
+ describe ':ppd' do
+ it 'should accept a string' do
+ @resource[:location] = "test"
+ end
+ end
- properties.each do |property|
- it "should have a #{property} property" do
- printer.attrclass(property).ancestors.should be_include(Puppet::Property)
+ describe ':enabled' do
+ it 'should fail if not supplied a boolean value' do
+ lambda { @resource[:enabled] = "test" }.should raise_error(Puppet::Error)
end
- it "should have documentation for its #{property} property" do
- printer.attrclass(property).doc.should be_instance_of(String)
+ [true, false].each do |v|
+ it "should accept boolean value of #{v}" do
+ @resource[:enabled] = v
+ end
end
end
+ describe ':shared' do
+ it 'should fail if not supplied a boolean value' do
+ lambda { @resource[:shared] = "test" }.should raise_error(Puppet::Error)
+ end
+ [true, false].each do |v|
+ it "should accept boolean value of #{v}" do
+ @resource[:enabled] = v
+ end
+ end
+ end
+
+ describe ':options' do
+ it 'should fail if not supplied a hash value' do
+ lambda { @resource[:shared] = "test" }.should raise_error(Puppet::Error)
+ end
+ end
end

0 comments on commit d4e2e5d

Please sign in to comment.