~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to test/language/collector.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env ruby
2
 
 
3
 
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
4
 
 
5
 
require 'puppet/rails'
6
 
require 'puppettest'
7
 
require 'puppettest/parsertesting'
8
 
require 'puppettest/resourcetesting'
9
 
require 'puppettest/railstesting'
10
 
 
11
 
class TestCollector < Test::Unit::TestCase
12
 
        include PuppetTest
13
 
    include PuppetTest::ParserTesting
14
 
    include PuppetTest::ResourceTesting
15
 
    include PuppetTest::RailsTesting
16
 
    Parser = Puppet::Parser
17
 
    AST = Parser::AST
18
 
 
19
 
    def setup
20
 
        super
21
 
        Puppet[:trace] = false
22
 
        @interp, @scope, @source = mkclassframing
23
 
    end
24
 
 
25
 
    # Test just collecting a specific resource.  This is used by the 'realize'
26
 
    # function, and it's much faster than iterating over all of the resources.
27
 
    def test_collect_resource
28
 
        # Make a couple of virtual resources
29
 
        one = mkresource(:type => "file", :title => "/tmp/virtual1",
30
 
            :virtual => true, :params => {:owner => "root"})
31
 
        two = mkresource(:type => "file", :title => "/tmp/virtual2",
32
 
            :virtual => true, :params => {:owner => "root"})
33
 
        @scope.setresource one
34
 
        @scope.setresource two
35
 
 
36
 
        # Now make a collector
37
 
        coll = nil
38
 
        assert_nothing_raised do
39
 
            coll = Puppet::Parser::Collector.new(@scope, "file", nil, nil, :virtual)
40
 
        end
41
 
 
42
 
        # Now set the resource in the collector
43
 
        assert_nothing_raised do 
44
 
            coll.resources = one.ref
45
 
        end
46
 
 
47
 
        # Now run the collector
48
 
        assert_nothing_raised do
49
 
            coll.evaluate
50
 
        end
51
 
 
52
 
        # And make sure the resource is no longer virtual
53
 
        assert(! one.virtual?,
54
 
            "Resource is still virtual")
55
 
 
56
 
        # But the other still is
57
 
        assert(two.virtual?,
58
 
            "Resource got realized")
59
 
    end
60
 
 
61
 
    def test_virtual
62
 
        # Make a virtual resource
63
 
        virtual = mkresource(:type => "file", :title => "/tmp/virtual",
64
 
            :virtual => true, :params => {:owner => "root"})
65
 
        @scope.setresource virtual
66
 
 
67
 
        # And a non-virtual
68
 
        real = mkresource(:type => "file", :title => "/tmp/real",
69
 
            :params => {:owner => "root"})
70
 
        @scope.setresource real
71
 
 
72
 
        # Now make a collector
73
 
        coll = nil
74
 
 
75
 
        # Make a fake query
76
 
        code = proc do |res|
77
 
            true
78
 
        end
79
 
        assert_nothing_raised do
80
 
            coll = Puppet::Parser::Collector.new(@scope, "file", nil, code, :virtual)
81
 
        end
82
 
 
83
 
        # Set it in our scope
84
 
        @scope.newcollection(coll)
85
 
 
86
 
        # Make sure it's in the collections
87
 
        assert_equal([coll], @scope.collections)
88
 
 
89
 
        # And try to collect the virtual resources.
90
 
        ret = nil
91
 
        assert_nothing_raised do
92
 
            ret = coll.collect_virtual
93
 
        end
94
 
 
95
 
        assert_equal([virtual], ret)
96
 
 
97
 
        # Now make sure evaluate does the right thing.
98
 
        assert_nothing_raised do
99
 
            ret = coll.evaluate
100
 
        end
101
 
 
102
 
        # Make sure it got deleted from the collection list
103
 
        assert_equal([], @scope.collections)
104
 
 
105
 
        # And make sure our virtual object is no longer virtual
106
 
        assert(! virtual.virtual?, "Virtual object did not get realized")
107
 
 
108
 
        # Now make a new collector of a different type and make sure it
109
 
        # finds nothing.
110
 
        assert_nothing_raised do
111
 
            coll = Puppet::Parser::Collector.new(@scope, "exec", nil, nil, :virtual)
112
 
        end
113
 
 
114
 
        # Remark this as virtual
115
 
        virtual.virtual = true
116
 
 
117
 
        assert_nothing_raised do
118
 
            ret = coll.evaluate
119
 
        end
120
 
 
121
 
        assert_equal([], ret)
122
 
    end
123
 
 
124
 
    if defined? ActiveRecord::Base
125
 
    def test_collect_exported
126
 
        railsinit
127
 
        # make an exported resource
128
 
        exported = mkresource(:type => "file", :title => "/tmp/exported",
129
 
            :exported => true, :params => {:owner => "root"})
130
 
        @scope.setresource exported
131
 
 
132
 
        assert(exported.exported?, "Object was not marked exported")
133
 
        assert(exported.virtual?, "Object was not marked virtual")
134
 
 
135
 
        # And a non-exported
136
 
        real = mkresource(:type => "file", :title => "/tmp/real",
137
 
            :params => {:owner => "root"})
138
 
        @scope.setresource real
139
 
 
140
 
        # Now make a collector
141
 
        coll = nil
142
 
        assert_nothing_raised do
143
 
            coll = Puppet::Parser::Collector.new(@scope, "file", nil, nil, :exported)
144
 
        end
145
 
 
146
 
        # Set it in our scope
147
 
        @scope.newcollection(coll)
148
 
 
149
 
        # Make sure it's in the collections
150
 
        assert_equal([coll], @scope.collections)
151
 
 
152
 
        # And try to collect the virtual resources.
153
 
        ret = nil
154
 
        assert_nothing_raised do
155
 
            ret = coll.collect_exported
156
 
        end
157
 
 
158
 
        assert_equal([exported], ret)
159
 
 
160
 
        # Now make sure evaluate does the right thing.
161
 
        assert_nothing_raised do
162
 
            ret = coll.evaluate
163
 
        end
164
 
 
165
 
        # Make sure it got deleted from the collection list
166
 
        assert_equal([], @scope.collections)
167
 
 
168
 
        # And make sure our exported object is no longer exported
169
 
        assert(! exported.virtual?, "Virtual object did not get realized")
170
 
 
171
 
        # But it should still be marked exported.
172
 
        assert(exported.exported?, "Resource got un-exported")
173
 
 
174
 
        # Now make a new collector of a different type and make sure it
175
 
        # finds nothing.
176
 
        assert_nothing_raised do
177
 
            coll = Puppet::Parser::Collector.new(@scope, "exec", nil, nil, :exported)
178
 
        end
179
 
 
180
 
        # Remark this as virtual
181
 
        exported.virtual = true
182
 
 
183
 
        assert_nothing_raised do
184
 
            ret = coll.evaluate
185
 
        end
186
 
 
187
 
        assert_equal([], ret)
188
 
    end
189
 
 
190
 
    def test_collection_conflicts
191
 
        railsinit
192
 
 
193
 
        # First make a railshost we can conflict with
194
 
        host = Puppet::Rails::Host.new(:name => "myhost")
195
 
 
196
 
        host.rails_resources.build(:title => "/tmp/conflicttest", :restype => "file",
197
 
            :exported => true)
198
 
 
199
 
        host.save
200
 
 
201
 
        # Now make a normal resource
202
 
        normal = mkresource(:type => "file", :title => "/tmp/conflicttest",
203
 
            :params => {:owner => "root"})
204
 
        @scope.setresource normal
205
 
 
206
 
        # Now make a collector
207
 
        coll = nil
208
 
        assert_nothing_raised do
209
 
            coll = Puppet::Parser::Collector.new(@scope, "file", nil, nil, :exported)
210
 
        end
211
 
 
212
 
        # And try to collect the virtual resources.
213
 
        assert_raise(Puppet::ParseError) do
214
 
            ret = coll.collect_exported
215
 
        end
216
 
    end
217
 
 
218
 
    # Make sure we do not collect resources from the host we're on
219
 
    def test_no_resources_from_me
220
 
        railsinit
221
 
 
222
 
        # Make our configuration
223
 
        host = Puppet::Rails::Host.new(:name => "myhost")
224
 
 
225
 
        host.rails_resources.build(:title => "/tmp/hosttest", :restype => "file",
226
 
            :exported => true)
227
 
 
228
 
        host.save
229
 
 
230
 
        @scope.host = "myhost"
231
 
 
232
 
        # Now make a collector
233
 
        coll = nil
234
 
        assert_nothing_raised do
235
 
            coll = Puppet::Parser::Collector.new(@scope, "file", nil, nil, :exported)
236
 
        end
237
 
 
238
 
        # And make sure we get nada back
239
 
        ret = nil
240
 
        assert_nothing_raised do
241
 
            ret = coll.collect_exported
242
 
        end
243
 
 
244
 
        assert(ret.empty?, "Found exports from our own host")
245
 
    end
246
 
    end
247
 
end
248
 
 
249
 
# $Id: collector.rb 1810 2006-10-18 06:01:18Z luke $