summaryrefslogtreecommitdiffstats
path: root/test/other/provider.rb
blob: fc04b9926c0604757e9d7ec03607e91c38b333bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
if __FILE__ == $0
    $:.unshift '..'
    $:.unshift '../../lib'
    $puppetbase = "../.."
end

require 'puppet'
require 'puppet/provider'
require 'puppettest'
require 'test/unit'

class TestImpl < Test::Unit::TestCase
    include TestPuppet

    def setup
        super
        @type = newtype(@method_name.to_s + "type")

        # But create a new provider for every method.
        @provider = newprovider(@method_name.to_s + "provider")
    end

    def newtype(name)
        # First create a fake type
        return Puppet::Type.newtype(name) {
            newparam(:name) { isnamevar }
        }
    end

    def newprovider(name, type = nil)
        type ||= @type
        provider = nil
        assert_nothing_raised("Could not create provider") do
            provider = type.provide(name) {}
        end
        return provider
    end

    # Just a quick run-through to see if the basics work
    def test_newprovider
        assert_nothing_raised do
            @provider.confine :operatingsystem => Facter["operatingsystem"].value
            @provider.defaultfor :operatingsystem => Facter["operatingsystem"].value
        end

        assert(@provider.suitable?, "Implementation was not considered suitable")
        assert(@provider.default?, "Implementation was not considered a default")

        assert_equal(@provider, @type.defaultprovider,
                     "Did not correctly find default provider")

    end

    def test_provider_false_confine
        assert_nothing_raised do
            @provider.confine :false => false
        end

        assert(@provider.suitable?, "False did not check correctly")
    end

    def test_provider_true_confine
        assert_nothing_raised do
            @provider.confine :true => true
        end

        assert(@provider.suitable?, "True did not check correctly")

        # Now check whether we multiple true things work
        assert_nothing_raised do
            @provider.confine :true => false
            @provider.confine :true => true
        end
        assert(! @provider.suitable?, "One truth overrode another")
    end

    def test_provider_exists_confine
        file = tempfile()

        assert_nothing_raised do
            @provider.confine :exists => file
        end

        assert(! @provider.suitable?, "Exists did not check correctly")
        File.open(file, "w") { |f| f.puts "" }
        assert(@provider.suitable?, "Exists did not find file correctly")
    end

    def test_provider_facts_confine
        # Now check for multiple platforms
        assert_nothing_raised do
            @provider.confine :operatingsystem => [Facter["operatingsystem"].value, :yayos]
            @provider.confine :operatingsystem => [:fakeos, :otheros]
        end

        assert(@provider.suitable?, "Implementation not considered suitable")
    end

    def test_provider_default
        nondef = nil
        assert_nothing_raised {
            nondef = newprovider(:nondefault)
        }

        assert_nothing_raised do
            @provider.defaultfor :operatingsystem => Facter["operatingsystem"].value
        end

        assert_equal(@provider.name, @type.defaultprovider.name, "Did not get right provider")

        @type.suitableprovider
    end

    def test_subclassconfines
        parent = newprovider("parentprovider")

        # Now make a bad confine on the parent
        parent.confine :exists => "/this/file/definitely/does/not/exist"

        child = nil
        assert_nothing_raised {
            child = @type.provide("child", :parent => parent.name) {}
        }

        assert(child.suitable?, "Parent ruled out child")
    end

    def test_commands
        parent = newprovider("parentprovider")

        child = nil
        assert_nothing_raised {
            child = @type.provide("child", :parent => parent.name) {}
        }

        assert_nothing_raised {
            child.commands :which => "which"
        }

        assert(child.command(:which), "Did not find 'which' command")

        assert(child.command(:which) =~ /^\//,
                "Command did not become fully qualified")
        assert(FileTest.exists?(child.command(:which)),
                                "Did not find actual 'which' binary")

        assert_raise(Puppet::DevError) do
            child.command(:nosuchcommand)
        end

        # Now create a parent command
        assert_nothing_raised {
            parent.commands :sh => Puppet::Util.binary('sh')
        }

        assert(parent.command(:sh), "Did not find 'sh' command")

        assert(child.command(:sh), "Did not find parent's 'sh' command")

        assert(FileTest.exists?(child.command(:sh)),
                                "Somehow broke path to sh")
    end
end

# $Id$