summaryrefslogtreecommitdiffstats
path: root/test/other/propertychange.rb
blob: 34669771b204c37c04cf20ae60092ade8a2a80f8 (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
#!/usr/bin/env ruby
#
#  Created by Luke A. Kanies on 2006-12-21.
#  Copyright (c) 2006. All rights reserved.

$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/

require 'puppettest'

class TestPropertyChange < Test::Unit::TestCase
	include PuppetTest
	class FakeProperty < Puppet::Property
	    attr_accessor :is, :should, :resource
	    attr_reader :noop
	    def change_to_s(currentvalue, newvalue)
	        "fake change"
        end
	    def insync?(is)
	        is == @should
        end
        def log(msg)
            Puppet::Util::Log.create(
                :level => :info,
                :source => self,
                :message => msg
            )
        end
        def noop
            if defined? @noop
                @noop
            else
                false
            end
        end
        def path
            "fakechange"
        end
        def should_to_s(newvalue)
            newvalue.to_s
        end
        def sync
            if insync?(@is)
                return nil
            else
                @is = @should
                return :fake_change
            end
        end
        def to_s
            path
        end
    end
    
    def mkchange
        property = FakeProperty.new :resource => "fakeparent"
        property.is = :start
        property.should = :finish
        property.resource = :parent
        change = nil
        assert_nothing_raised do
            change = Puppet::PropertyChange.new(property, :start)
        end
        change.transaction = :trans
        
        assert_equal(:start, change.is, "@is did not get copied")
        assert_equal(:finish, change.should, "@should did not get copied")
        assert_equal(%w{fakechange change}, change.path, "path did not get set correctly")
        
        assert(! change.changed?, "change defaulted to already changed")
        
        return change
    end
    
	def test_go
	    change = mkchange
	    
	    coll = logcollector()
	    
	    events = nil
	    # First make sure we get an immediate return 
	    assert_nothing_raised do
    	    events = change.go
	    end
	    assert_instance_of(Array, events, "events were not returned in an array")
	    assert_instance_of(Puppet::Event, events[0], "event array did not contain events")

	    event = events.shift
	    {:event => :fake_change, :transaction => :trans, :source => :parent}.each do |method, val|
	        assert_equal(val, event.send(method), "Event did not set %s correctly" % method)
        end
	    
        # Disabled, because it fails when running the whole suite at once.
        #assert(coll.detect { |l| l.message == "fake change" }, "Did not log change")
	    assert_equal(change.property.is, change.property.should, "did not call sync method")
	    
	    # Now make sure that proxy sources can be set.
	    assert_nothing_raised do
    	    change.proxy = :other
	    end
	    # Reset, so we change again
	    change.property.is = :start
	    change.is = :start
	    assert_nothing_raised do
	        events = change.go
        end
        
	    assert_instance_of(Array, events, "events were not returned in an array")
	    assert_instance_of(Puppet::Event, events[0], "event array did not contain events")

	    event = events.shift
	    {:event => :fake_change, :transaction => :trans, :source => :other}.each do |method, val|
	        assert_equal(val, event.send(method), "Event did not set %s correctly" % method)
        end
	    
	    #assert(coll.detect { |l| l.message == "fake change" }, "Did not log change")
	    assert_equal(change.property.is, change.property.should, "did not call sync method")
    end

    # Related to #542.  Make sure changes in noop mode produce the :noop event.
    def test_noop_event
        change = mkchange

        assert(! change.skip?, "Change is already being skipped")

        Puppet[:noop] = true

        change.property.noop = true
        assert(change.noop, "did not set noop")
        assert(change.skip?, "setting noop did not mark change for skipping")

        event = nil
        assert_nothing_raised("Could not generate noop event") do
            event = change.forward
        end

        assert_equal(1, event.length, "got wrong number of events")
        assert_equal(:noop, event[0].event, "did not generate noop mode when in noop")
    end
end