3 require File.dirname(__FILE__) + '/../lib/puppettest'
6 require 'puppet/provider'
9 class TestImpl < Test::Unit::TestCase
14 @type = newtype(@method_name.to_s + "type")
16 # But create a new provider for every method.
17 @provider = newprovider(@method_name.to_s + "provider")
21 # First create a fake type
22 return Puppet::Type.newtype(name) {
23 newparam(:name) { isnamevar }
27 def newprovider(name, type = nil)
30 assert_nothing_raised("Could not create provider") do
31 provider = type.provide(name) {}
36 # Just a quick run-through to see if the basics work
38 assert_nothing_raised do
39 @provider.confine :operatingsystem => Facter["operatingsystem"].value
40 @provider.defaultfor :operatingsystem => Facter["operatingsystem"].value
43 assert(@provider.suitable?, "Implementation was not considered suitable")
44 assert(@provider.default?, "Implementation was not considered a default")
46 assert_equal(@provider, @type.defaultprovider,
47 "Did not correctly find default provider")
51 def test_provider_false_confine
52 assert_nothing_raised do
53 @provider.confine :false => false
56 assert(@provider.suitable?, "False did not check correctly")
59 def test_provider_true_confine
60 assert_nothing_raised do
61 @provider.confine :true => true
64 assert(@provider.suitable?, "True did not check correctly")
66 # Now check whether we multiple true things work
67 assert_nothing_raised do
68 @provider.confine :true => false
69 @provider.confine :true => true
71 assert(! @provider.suitable?, "One truth overrode another")
74 def test_provider_exists_confine
77 assert_nothing_raised do
78 @provider.confine :exists => file
81 assert(! @provider.suitable?, "Exists did not check correctly")
82 File.open(file, "w") { |f| f.puts "" }
83 assert(@provider.suitable?, "Exists did not find file correctly")
86 def test_provider_facts_confine
87 # Now check for multiple platforms
88 assert_nothing_raised do
89 @provider.confine :operatingsystem => [Facter["operatingsystem"].value, :yayos]
90 @provider.confine :operatingsystem => [:fakeos, :otheros]
93 assert(@provider.suitable?, "Implementation not considered suitable")
96 def test_provider_default
98 assert_nothing_raised {
99 nondef = newprovider(:nondefault)
102 assert_nothing_raised do
103 @provider.defaultfor :operatingsystem => Facter["operatingsystem"].value
106 assert_equal(@provider.name, @type.defaultprovider.name, "Did not get right provider")
108 @type.suitableprovider
111 def test_subclassconfines
112 parent = newprovider("parentprovider")
114 # Now make a bad confine on the parent
115 parent.confine :exists => "/this/file/definitely/does/not/exist"
118 assert_nothing_raised {
119 child = @type.provide("child", :parent => parent.name) {}
122 assert(child.suitable?, "Parent ruled out child")
126 parent = newprovider("parentprovider")
129 assert_nothing_raised {
130 child = @type.provide("child", :parent => parent.name) {}
133 assert_nothing_raised {
134 child.commands :which => "which"
137 assert(child.command(:which), "Did not find 'which' command")
139 assert(child.command(:which) =~ /^\//,
140 "Command did not become fully qualified")
141 assert(FileTest.exists?(child.command(:which)),
142 "Did not find actual 'which' binary")
144 assert_raise(Puppet::DevError) do
145 child.command(:nosuchcommand)
148 # Now create a parent command
149 assert_nothing_raised {
150 parent.commands :sh => Puppet::Util.binary('sh')
153 assert(parent.command(:sh), "Did not find 'sh' command")
155 assert(child.command(:sh), "Did not find parent's 'sh' command")
157 assert(FileTest.exists?(child.command(:sh)),
158 "Somehow broke path to sh")