summaryrefslogtreecommitdiffstats
path: root/spec/integration/transaction.rb
blob: 1b8b11d481e2258edc7937fa42ee7c4c6d637716 (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
179
180
181
182
183
184
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../spec_helper'

require 'puppet_spec/files'
require 'puppet/transaction'

describe Puppet::Transaction do
    include PuppetSpec::Files

    it "should not apply generated resources if the parent resource fails" do
        catalog = Puppet::Resource::Catalog.new
        resource = Puppet::Type.type(:file).new :path => "/foo/bar", :backup => false
        catalog.add_resource resource

        child_resource = Puppet::Type.type(:file).new :path => "/foo/bar/baz", :backup => false

        resource.expects(:eval_generate).returns([child_resource])

        transaction = Puppet::Transaction.new(catalog)

        resource.expects(:evaluate).raises "this is a failure"

        child_resource.expects(:evaluate).never

        transaction.evaluate
    end

    it "should not apply virtual resources" do
        catalog = Puppet::Resource::Catalog.new
        resource = Puppet::Type.type(:file).new :path => "/foo/bar", :backup => false
        resource.virtual = true
        catalog.add_resource resource

        transaction = Puppet::Transaction.new(catalog)

        resource.expects(:evaluate).never

        transaction.evaluate
    end

    it "should apply exported resources" do
        catalog = Puppet::Resource::Catalog.new
        resource = Puppet::Type.type(:file).new :path => "/foo/bar", :backup => false
        resource.exported = true
        catalog.add_resource resource

        transaction = Puppet::Transaction.new(catalog)

        resource.expects(:evaluate).never

        transaction.evaluate
    end

    it "should not apply virtual exported resources" do
        catalog = Puppet::Resource::Catalog.new
        resource = Puppet::Type.type(:file).new :path => "/foo/bar", :backup => false
        resource.exported = true
        resource.virtual = true
        catalog.add_resource resource

        transaction = Puppet::Transaction.new(catalog)

        resource.expects(:evaluate).never

        transaction.evaluate
    end

    it "should refresh resources that subscribe to changed resources" do
        name = tmpfile("something")
        file = Puppet::Type.type(:file).new(
            :name => name,
            :ensure => "file"
        )
        exec = Puppet::Type.type(:exec).new(
            :name => "echo true",
            :path => "/usr/bin:/bin",
            :refreshonly => true,
            :subscribe => Puppet::Resource::Reference.new(file.class.name, file.name)
        )

        catalog = Puppet::Resource::Catalog.new
        catalog.add_resource file, exec

        exec.expects(:refresh)

        catalog.apply
    end

    it "should not refresh resources that only require changed resources" do
        name = tmpfile("something")
        file = Puppet::Type.type(:file).new(
            :name => name,
            :ensure => "file"
        )
        exec = Puppet::Type.type(:exec).new(
            :name => "echo true",
            :path => "/usr/bin:/bin",
            :refreshonly => true,
            :require => Puppet::Resource::Reference.new(file.class.name, file.name)
        )


        catalog = Puppet::Resource::Catalog.new
        catalog.add_resource file
        catalog.add_resource exec

        exec.expects(:refresh).never

        trans = catalog.apply

        trans.events.length.should == 1
    end

    it "should cascade events such that multiple refreshes result" do
        files = []

        4.times { |i|
            files << Puppet::Type.type(:file).new(
                :name => tmpfile("something"),
                :ensure => "file"
            )
        }

        fname = tmpfile("something")
        exec = Puppet::Type.type(:exec).new(
            :name => "touch %s" % fname,
            :path => "/usr/bin:/bin",
            :refreshonly => true
        )

        exec[:subscribe] = files.collect { |f|
            Puppet::Resource::Reference.new(:file, f.name)
        }

        catalog = Puppet::Resource::Catalog.new
        catalog.add_resource(exec, *files)

        catalog.apply
        FileTest.should be_exist(fname)
    end

    # Make sure refreshing happens mid-transaction, rather than at the end.
    it "should refresh resources as they're encountered rather than all at the end" do
        file = tmpfile("something")

        exec1 = Puppet::Type.type(:exec).new(
            :title => "one",
            :name => "echo one >> %s" % file,
            :path => "/usr/bin:/bin"
        )

        exec2 = Puppet::Type.type(:exec).new(
            :title => "two",
            :name => "echo two >> %s" % file,
            :path => "/usr/bin:/bin",
            :refreshonly => true,
            :subscribe => exec1
        )

        exec3 = Puppet::Type.type(:exec).new(
            :title => "three",
            :name => "echo three >> %s" % file,
            :path => "/usr/bin:/bin",
            :require => exec2
        )
        execs = [exec1, exec2, exec3]

        catalog = Puppet::Resource::Catalog.new
        catalog.add_resource(exec1,exec2,exec3)

        trans = Puppet::Transaction.new(catalog)
        execs.each { |e| catalog.should be_vertex(e) }
        trans.prepare
        execs.each { |e| catalog.should be_vertex(e) }
        reverse = trans.relationship_graph.reversal
        execs.each { |e| reverse.should be_vertex(e) }

        catalog.apply

        FileTest.should be_exist(file)
        File.read(file).should == "one\ntwo\nthree\n"
    end
end