pairing with luke, nagios_command provider skeleton
[vinpup.git] / test / other / events.rb
blob9d6bd2bb7768b087fb3337aa0a1d97d6b005b70a
1 #!/usr/bin/env ruby
3 require File.dirname(__FILE__) + '/../lib/puppettest'
5 require 'puppet'
6 require 'puppettest'
9 class TestEvents < Test::Unit::TestCase
10         include PuppetTest
12     def test_simplesubscribe
13         name = tempfile()
14         file = Puppet.type(:file).create(
15             :name => name,
16             :ensure => "file"
17         )
18         exec = Puppet.type(:exec).create(
19             :name => "echo true",
20             :path => "/usr/bin:/bin",
21             :refreshonly => true,
22             :subscribe => [[file.class.name, file.name]] 
23         )
25         comp = mk_catalog("eventtesting", file, exec)
27         trans = assert_events([:file_created, :triggered], comp)
29         assert_equal(1, trans.triggered?(exec, :refresh))
30     end
32     def test_simplerequire
33         name = tempfile()
34         file = Puppet.type(:file).create(
35             :name => name,
36             :ensure => "file"
37         )
38         exec = Puppet.type(:exec).create(
39             :name => "echo true",
40             :path => "/usr/bin:/bin",
41             :refreshonly => true,
42             :require => [[file.class.name, file.name]] 
43         )
46         config = mk_catalog
47         config.add_resource file
48         config.add_resource exec
49         trans = config.apply
51         assert_equal(1, trans.events.length)
53         assert_equal(0, trans.triggered?(exec, :refresh))
54     end
56     def test_multiplerefreshes
57         files = []
59         4.times { |i|
60             files << Puppet.type(:file).create(
61                 :name => tempfile(),
62                 :ensure => "file"
63             )
64         }
66         fname = tempfile()
67         exec = Puppet.type(:exec).create(
68             :name => "touch %s" % fname,
69             :path => "/usr/bin:/bin",
70             :refreshonly => true
71         )
73         exec[:subscribe] = files.collect { |f|
74             ["file", f.name]
75         }
77         comp = mk_catalog(exec, *files)
79         assert_apply(comp)
80         assert(FileTest.exists?(fname), "Exec file did not get created")
81     end
83     # Make sure refreshing happens mid-transaction, rather than at the end.
84     def test_refreshordering
85         file = tempfile()
87         exec1 = Puppet.type(:exec).create(
88             :title => "one",
89             :name => "echo one >> %s" % file,
90             :path => "/usr/bin:/bin"
91         )
93         exec2 = Puppet.type(:exec).create(
94             :title => "two",
95             :name => "echo two >> %s" % file,
96             :path => "/usr/bin:/bin",
97             :refreshonly => true,
98             :subscribe => exec1
99         )
101         exec3 = Puppet.type(:exec).create(
102             :title => "three",
103             :name => "echo three >> %s" % file,
104             :path => "/usr/bin:/bin",
105             :require => exec2
106         )
107         execs = [exec1, exec2, exec3]
109         config = mk_catalog(exec1,exec2,exec3)
110         
111         trans = Puppet::Transaction.new(config)
112         execs.each do |e| assert(config.vertex?(e), "%s is not in graph" % e.title) end
113         trans.prepare
114         execs.each do |e| assert(config.vertex?(e), "%s is not in relgraph" % e.title) end
115         reverse = trans.relationship_graph.reversal
116         execs.each do |e| assert(reverse.vertex?(e), "%s is not in reversed graph" % e.title) end
117         
118         config.apply
120         assert(FileTest.exists?(file), "File does not exist")
122         assert_equal("one\ntwo\nthree\n", File.read(file))
123     end