summaryrefslogtreecommitdiffstats
path: root/test/language/collector.rb
blob: 30949c06130bcc07476338f474ef2f72f4629533 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../lib/puppettest'

require 'puppettest'
require 'puppettest/parsertesting'
require 'puppettest/resourcetesting'

class TestCollector < Test::Unit::TestCase
	include PuppetTest
    include PuppetTest::ParserTesting
    include PuppetTest::ResourceTesting
    Parser = Puppet::Parser
    AST = Parser::AST

    def setup
        super
        Puppet[:trace] = false
        @scope = mkscope
        @compile = @scope.compile
    end

    # Test just collecting a specific resource.  This is used by the 'realize'
    # function, and it's much faster than iterating over all of the resources.
    def test_collect_resource
        # Make a collector
        coll = nil
        assert_nothing_raised do
            coll = Puppet::Parser::Collector.new(@scope, "file", nil, nil, :virtual)
        end

        # Now set the resource in the collector
        assert_nothing_raised do 
            coll.resources = ["File[/tmp/virtual1]", "File[/tmp/virtual3]"]
        end
        @compile.add_collection(coll)

        # Evaluate the collector and make sure it doesn't fail with no resources
        # found yet
        assert_nothing_raised("Resource collection with no results failed") do
            assert_equal(false, coll.evaluate)
        end

        # Make a couple of virtual resources
        one = mkresource(:type => "file", :title => "/tmp/virtual1",
            :virtual => true, :params => {:owner => "root"})
        two = mkresource(:type => "file", :title => "/tmp/virtual2",
            :virtual => true, :params => {:owner => "root"})
        @scope.compile.store_resource @scope, one
        @scope.compile.store_resource @scope, two

        # Now run the collector again and make sure it finds our resource
        assert_nothing_raised do
            assert_equal([one], coll.evaluate, "did not find resource")
        end

        # And make sure the resource is no longer virtual
        assert(! one.virtual?,
            "Resource is still virtual")

        # But the other still is
        assert(two.virtual?,
            "Resource got realized")

        # Make sure that the collection is still there
        assert(@compile.collections.include?(coll), "collection was deleted too soon")

        # Now add our third resource
        three = mkresource(:type => "file", :title => "/tmp/virtual3",
            :virtual => true, :params => {:owner => "root"})
        @scope.compile.store_resource @scope, three

        # Run the collection
        assert_nothing_raised do
            assert_equal([three], coll.evaluate, "did not find resource")
        end
        assert(! three.virtual?, "three is still virtual")

        # And make sure that the collection got deleted from the scope's list
        assert(@compile.collections.empty?, "collection was not deleted")
    end

    def test_virtual
        # Make a virtual resource
        virtual = mkresource(:type => "file", :title => "/tmp/virtual",
            :virtual => true, :params => {:owner => "root"})
        @scope.compile.store_resource @scope, virtual

        # And a non-virtual
        real = mkresource(:type => "file", :title => "/tmp/real",
            :params => {:owner => "root"})
        @scope.compile.store_resource @scope, real

        # Now make a collector
        coll = nil

        # Make a fake query
        code = proc do |res|
            true
        end
        assert_nothing_raised do
            coll = Puppet::Parser::Collector.new(@scope, "file", nil, code, :virtual)
        end

        # Set it in our scope
        @compile.add_collection(coll)

        # Make sure it's in the collections
        assert(@compile.collections.include?(coll), "collection was not added")

        # And try to collect the virtual resources.
        ret = nil
        assert_nothing_raised do
            ret = coll.collect_virtual
        end

        assert_equal([virtual], ret)

        # Now make sure evaluate does the right thing.
        assert_nothing_raised do
            ret = coll.evaluate
        end

        # And make sure our virtual object is no longer virtual
        assert(! virtual.virtual?, "Virtual object did not get realized")

        # Now make a new collector of a different type and make sure it
        # finds nothing.
        assert_nothing_raised do
            coll = Puppet::Parser::Collector.new(@scope, "exec", nil, nil, :virtual)
        end

        # Remark this as virtual
        virtual.virtual = true

        assert_nothing_raised do
            ret = coll.evaluate
        end

        assert_equal(false, ret)
    end

    # Collections that specify resources should be deleted when they succeed,
    # but others should remain until the very end.
    def test_normal_collections_remain
        # Make a collector
        coll = nil
        assert_nothing_raised do
            coll = Puppet::Parser::Collector.new(@scope, "file", nil, nil, :virtual)
        end

        @compile.add_collection(coll)

        # run the collection and make sure it doesn't get deleted, since it
        # didn't return anything
        assert_nothing_raised do
            assert_equal(false, coll.evaluate,
                "Evaluate returned incorrect value")
        end

        assert_equal([coll], @compile.collections, "Collection was deleted")

        # Make a resource
        one = mkresource(:type => "file", :title => "/tmp/virtual1",
            :virtual => true, :params => {:owner => "root"})
        @scope.compile.store_resource @scope, one

        # Now perform the collection again, and it should still be there
        assert_nothing_raised do
            assert_equal([one], coll.evaluate,
                "Evaluate returned incorrect value")
        end

        assert_equal([coll], @compile.collections, "Collection was deleted")

        assert_equal(false, one.virtual?, "One was not realized")
    end
end