~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to test/other/relationships.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
require 'puppettest'
7
7
 
8
8
class TestRelationships < Test::Unit::TestCase
9
 
        include PuppetTest
10
 
        def setup
11
 
            super
12
 
            Puppet::Type.type(:exec)
 
9
    include PuppetTest
 
10
    def setup
 
11
        super
 
12
        Puppet::Type.type(:exec)
13
13
    end
14
 
    
 
14
 
15
15
    def newfile
16
16
        assert_nothing_raised() {
17
 
            return Puppet.type(:file).create(
 
17
            return Puppet::Type.type(:file).new(
18
18
                :path => tempfile,
19
19
                :check => [:mode, :owner, :group]
20
20
            )
21
21
        }
22
22
    end
23
 
    
 
23
 
24
24
    def check_relationship(sources, targets, out, refresher)
25
25
        if out
26
26
            deps = sources.builddepends
31
31
        end
32
32
        assert_instance_of(Array, deps)
33
33
        assert(! deps.empty?, "Did not receive any relationships")
34
 
        
 
34
 
35
35
        deps.each do |edge|
36
36
            assert_instance_of(Puppet::Relationship, edge)
37
37
        end
38
 
        
 
38
 
39
39
        sources.each do |source|
40
40
            targets.each do |target|
41
41
                edge = deps.find { |e| e.source == source and e.target == target }
42
42
                assert(edge, "Could not find edge for %s => %s" %
43
43
                    [source.ref, target.ref])
44
 
        
 
44
 
45
45
                if refresher
46
46
                    assert_equal(:ALL_EVENTS, edge.event)
47
47
                    assert_equal(:refresh, edge.callback)
53
53
        end
54
54
    end
55
55
 
56
 
    # Make sure our various metaparams work correctly.  We're just checking
57
 
    # here whether they correctly set up the callbacks and the direction of
58
 
    # the relationship.
59
 
    def test_relationship_metaparams
60
 
        out = {:require => false, :subscribe => false,
61
 
            :notify => true, :before => true}
62
 
        refreshers = [:subscribe, :notify]
63
 
        [:require, :subscribe, :notify, :before].each do |param|
64
 
            # Create three files to generate our events and three
65
 
            # execs to receive them
66
 
            files = []
67
 
            execs = []
68
 
            3.times do |i|
69
 
                files << Puppet::Type.newfile(
70
 
                    :title => "file#{i}",
71
 
                    :path => tempfile(),
72
 
                    :ensure => :file
73
 
                )
74
 
 
75
 
                path = tempfile()
76
 
                execs << Puppet::Type.newexec(
77
 
                    :title => "notifytest#{i}",
78
 
                    :path => "/usr/bin:/bin",
79
 
                    :command => "touch #{path}",
80
 
                    :refreshonly => true
81
 
                )
82
 
            end
83
 
 
84
 
            # Add our first relationship
85
 
            if out[param]
86
 
                files[0][param] = execs[0]
87
 
                sources = files[0]
88
 
                targets = [execs[0]]
89
 
            else
90
 
                execs[0][param] = files[0]
91
 
                sources = [files[0]]
92
 
                targets = execs[0]
93
 
            end
94
 
            check_relationship(sources, targets, out[param], refreshers.include?(param))
95
 
 
96
 
            # Now add another relationship
97
 
            if out[param]
98
 
                files[0][param] = execs[1]
99
 
                targets << execs[1]
100
 
                assert_equal(targets.collect { |t| [t.class.name, t.title]},
101
 
                    files[0][param], "Incorrect target list")
102
 
            else
103
 
                execs[0][param] = files[1]
104
 
                sources << files[1]
105
 
                assert_equal(sources.collect { |t| [t.class.name, t.title]},
106
 
                    execs[0][param], "Incorrect source list")
107
 
            end
108
 
            check_relationship(sources, targets, out[param], refreshers.include?(param))
109
 
 
110
 
            Puppet::Type.allclear
111
 
        end
112
 
    end
113
 
    
114
 
    def test_munge_relationship
115
 
        file = Puppet::Type.newfile :path => tempfile(), :mode => 0755
116
 
        execs = []
117
 
        3.times do |i|
118
 
            execs << Puppet::Type.newexec(:title => "yay#{i}", :command => "/bin/echo yay")
119
 
        end
120
 
        
121
 
        # First try it with one object, specified as a reference and an array
122
 
        result = nil
123
 
        [execs[0], [:exec, "yay0"], ["exec", "yay0"]].each do |target|
124
 
            assert_nothing_raised do
125
 
                result = file.send(:munge_relationship, :require, target)
126
 
            end
127
 
        
128
 
            assert_equal([[:exec, "yay0"]], result)
129
 
        end
130
 
        
131
 
        # Now try it with multiple objects
132
 
        symbols = execs.collect { |e| [e.class.name, e.title] }
133
 
        strings = execs.collect { |e| [e.class.name.to_s, e.title] }
134
 
        [execs, symbols, strings].each do |target|
135
 
            assert_nothing_raised do
136
 
                result = file.send(:munge_relationship, :require, target)
137
 
            end
138
 
        
139
 
            assert_equal(symbols, result)
140
 
        end
141
 
        
142
 
        # Make sure we can mix it up, even though this shouldn't happen
143
 
        assert_nothing_raised do
144
 
            result = file.send(:munge_relationship, :require, [execs[0], [execs[1].class.name, execs[1].title]])
145
 
        end
146
 
        
147
 
        assert_equal([[:exec, "yay0"], [:exec, "yay1"]], result)
148
 
        
149
 
        # Finally, make sure that new results get added to old.  The only way
150
 
        # to get rid of relationships is to delete the parameter.
151
 
        file[:require] = execs[0]
152
 
        
153
 
        assert_nothing_raised do
154
 
            result = file.send(:munge_relationship, :require, [execs[1], execs[2]])
155
 
        end
156
 
        
157
 
        assert_equal(symbols, result)
158
 
    end
159
 
    
160
56
    def test_autorequire
161
57
        # We know that execs autorequire their cwd, so we'll use that
162
58
        path = tempfile()
163
 
        
164
 
        file = Puppet::Type.newfile(:title => "myfile", :path => path,
 
59
 
 
60
        file = Puppet::Type.type(:file).new(:title => "myfile", :path => path,
165
61
            :ensure => :directory)
166
62
        exec = Puppet::Type.newexec(:title => "myexec", :cwd => path,
167
63
            :command => "/bin/echo")
168
 
        
 
64
 
 
65
        catalog = mk_catalog(file, exec)
169
66
        reqs = nil
170
67
        assert_nothing_raised do
171
68
            reqs = exec.autorequire
173
70
        assert_instance_of(Puppet::Relationship, reqs[0], "Did not return a relationship edge")
174
71
        assert_equal(file, reqs[0].source, "Did not set the autorequire source correctly")
175
72
        assert_equal(exec, reqs[0].target, "Did not set the autorequire target correctly")
176
 
        
177
 
        # Now make sure that these relationships are added to the 
 
73
 
 
74
        # Now make sure that these relationships are added to the
178
75
        # relationship graph
179
 
        config = mk_catalog(file, exec)
180
 
        config.apply do |trans|
181
 
            assert(config.relationship_graph.edge?(file, exec), "autorequire edge was not created")
 
76
        catalog.apply do |trans|
 
77
            assert(catalog.relationship_graph.edge?(file, exec), "autorequire edge was not created")
182
78
        end
183
79
    end
184
 
    
185
 
    def test_requires?
186
 
        # Test the first direction
187
 
        file1 = Puppet::Type.newfile(:title => "one", :path => tempfile,
188
 
            :ensure => :directory)
189
 
        file2 = Puppet::Type.newfile(:title => "two", :path => tempfile,
190
 
            :ensure => :directory)
191
 
        
192
 
        file1[:require] = file2
193
 
        assert(file1.requires?(file2), "requires? failed to catch :require relationship")
194
 
        file1.delete(:require)
195
 
        assert(! file1.requires?(file2), "did not delete relationship")
196
 
        file1[:subscribe] = file2
197
 
        assert(file1.requires?(file2), "requires? failed to catch :subscribe relationship")
198
 
        file1.delete(:subscribe)
199
 
        assert(! file1.requires?(file2), "did not delete relationship")
200
 
        file2[:before] = file1
201
 
        assert(file1.requires?(file2), "requires? failed to catch :before relationship")
202
 
        file2.delete(:before)
203
 
        assert(! file1.requires?(file2), "did not delete relationship")
204
 
        file2[:notify] = file1
205
 
        assert(file1.requires?(file2), "requires? failed to catch :notify relationship")
206
 
    end
207
 
    
 
80
 
208
81
    # Testing #411.  It was a problem with builddepends.
209
82
    def test_missing_deps
210
 
        file = Puppet::Type.newfile :path => tempfile, :require => ["file", "/no/such/file"]
211
 
        
 
83
        file = Puppet::Type.type(:file).new :path => tempfile, :require => Puppet::Resource::Reference.new("file", "/no/such/file")
 
84
 
212
85
        assert_raise(Puppet::Error) do
213
86
            file.builddepends
214
87
        end