summaryrefslogtreecommitdiffstats
path: root/test/other/relationships.rb
blob: 976d6d8941fc58d8a3d3f7e43d586f3768bdac35 (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
#!/usr/bin/env ruby

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

require 'puppet'
require 'puppettest'

class TestRelationships < Test::Unit::TestCase
    include PuppetTest
    def setup
        super
        Puppet::Type.type(:exec)
    end
    
    def newfile
        assert_nothing_raised() {
            return Puppet::Type.type(:file).new(
                :path => tempfile,
                :check => [:mode, :owner, :group]
            )
        }
    end
    
    def check_relationship(sources, targets, out, refresher)
        if out
            deps = sources.builddepends
            sources = [sources]
        else
            deps = targets.builddepends
            targets = [targets]
        end
        assert_instance_of(Array, deps)
        assert(! deps.empty?, "Did not receive any relationships")
        
        deps.each do |edge|
            assert_instance_of(Puppet::Relationship, edge)
        end
        
        sources.each do |source|
            targets.each do |target|
                edge = deps.find { |e| e.source == source and e.target == target }
                assert(edge, "Could not find edge for %s => %s" %
                    [source.ref, target.ref])
        
                if refresher
                    assert_equal(:ALL_EVENTS, edge.event)
                    assert_equal(:refresh, edge.callback)
                else
                    assert_nil(edge.event)
                    assert_nil(edge.callback, "Got a callback with no events")
                end
            end
        end
    end

    def test_autorequire
        # We know that execs autorequire their cwd, so we'll use that
        path = tempfile()
        
        file = Puppet::Type.type(:file).new(:title => "myfile", :path => path,
            :ensure => :directory)
        exec = Puppet::Type.newexec(:title => "myexec", :cwd => path,
            :command => "/bin/echo")
        
        catalog = mk_catalog(file, exec)
        reqs = nil
        assert_nothing_raised do
            reqs = exec.autorequire
        end
        assert_instance_of(Puppet::Relationship, reqs[0], "Did not return a relationship edge")
        assert_equal(file, reqs[0].source, "Did not set the autorequire source correctly")
        assert_equal(exec, reqs[0].target, "Did not set the autorequire target correctly")
        
        # Now make sure that these relationships are added to the 
        # relationship graph
        catalog.apply do |trans|
            assert(catalog.relationship_graph.edge?(file, exec), "autorequire edge was not created")
        end
    end
    
    # Testing #411.  It was a problem with builddepends.
    def test_missing_deps
        file = Puppet::Type.type(:file).new :path => tempfile, :require => Puppet::Resource::Reference.new("file", "/no/such/file")
        
        assert_raise(Puppet::Error) do
            file.builddepends
        end
    end
end