summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/property.rb38
-rw-r--r--lib/puppet/transaction/change.rb58
-rw-r--r--lib/puppet/transaction/event.rb26
-rwxr-xr-xspec/unit/property.rb108
-rwxr-xr-xspec/unit/transaction/change.rb54
-rwxr-xr-xspec/unit/transaction/event.rb23
6 files changed, 197 insertions, 110 deletions
diff --git a/lib/puppet/property.rb b/lib/puppet/property.rb
index e246eb702..23b9d780a 100644
--- a/lib/puppet/property.rb
+++ b/lib/puppet/property.rb
@@ -152,31 +152,25 @@ class Puppet::Property < Puppet::Parameter
end
# Figure out which event to return.
- def default_event_name(value, event = nil)
- if value_event = self.class.value_option(value, :event)
- return value_event
- end
+ def event_name
+ value = self.should
- if event and event.is_a?(Symbol)
- if event == :nochange
- return nil
- else
- return event
- end
- end
+ event_name = self.class.value_option(value, :event) and return event_name
- if self.class.name == :ensure
- event = case self.should
- when :present; (@resource.class.name.to_s + "_created").intern
- when :absent; (@resource.class.name.to_s + "_removed").intern
- else
- (@resource.class.name.to_s + "_changed").intern
- end
+ name == :ensure or return (name.to_s + "_changed").to_sym
+
+ return (resource.type.to_s + case value
+ when :present; "_created"
+ when :absent; "_removed"
else
- event = (@resource.class.name.to_s + "_changed").intern
- end
+ "_changed"
+ end).to_sym
+ end
- return event
+ # Create our event object.
+ def event
+ Puppet::Transaction::Event.new(:name => event_name, :resource => resource.ref, :desired_value => should,
+ :file => file, :line => line, :tags => tags, :property => name, :version => version)
end
attr_reader :shadow
@@ -314,8 +308,6 @@ class Puppet::Property < Puppet::Parameter
# do so in the block.
devfail "Cannot use obsolete :call value '%s' for property '%s'" % [call, self.class.name]
end
-
- return default_event_name(name, event)
end
# If there's a shadowing metaparam, instantiate it now.
diff --git a/lib/puppet/transaction/change.rb b/lib/puppet/transaction/change.rb
index 5e1aff106..49569d6e3 100644
--- a/lib/puppet/transaction/change.rb
+++ b/lib/puppet/transaction/change.rb
@@ -16,17 +16,11 @@ class Puppet::Transaction::Change
end
# Create our event object.
- def event(event_name)
- event_name ||= property.default_event_name(should)
-
- # default to a simple event type
- unless event_name.is_a?(Symbol)
- @property.warning "Property '#{property.class}' returned invalid event '#{event_name}'; resetting to default"
-
- event_name = property.default_event_name(should)
- end
-
- Puppet::Transaction::Event.new(event_name, resource.ref, property.name, is, should)
+ def event
+ result = property.event
+ result.previous_value = is
+ result.desired_value = should
+ result
end
def initialize(property, currentvalue)
@@ -41,32 +35,23 @@ class Puppet::Transaction::Change
# Perform the actual change. This method can go either forward or
# backward, and produces an event.
def go
- if self.noop?
- @property.log "is %s, should be %s (noop)" % [property.is_to_s(@is), property.should_to_s(@should)]
- return event(:noop)
- end
-
- # The transaction catches any exceptions here.
- event_name = @property.sync
-
- # Use the first event only, if multiple are provided.
- # This might result in the event_name being nil,
- # which is fine.
- event_name = event_name.shift if event_name.is_a?(Array)
-
- event = event(event_name)
- event.log = @property.notice @property.change_to_s(@is, @should)
- event.status = "success"
- event
+ return noop_event if noop?
+
+ property.sync
+
+ result = event()
+ result.log = property.notice property.change_to_s(is, should)
+ result.status = "success"
+ result
rescue => detail
puts detail.backtrace if Puppet[:trace]
- event = event(nil)
- event.status = "failure"
+ result = event()
+ result.status = "failure"
is = property.is_to_s(is)
should = property.should_to_s(should)
- event.log = property.err "change from #{is} to #{should} failed: #{detail}"
- event
+ result.log = property.err "change from #{is} to #{should} failed: #{detail}"
+ result
end
def forward
@@ -90,4 +75,13 @@ class Puppet::Transaction::Change
def to_s
return "change %s" % @property.change_to_s(@is, @should)
end
+
+ private
+
+ def noop_event
+ result = event
+ result.log = property.log "is #{property.is_to_s(is)}, should be #{property.should_to_s(should)} (noop)"
+ result.status = "noop"
+ return result
+ end
end
diff --git a/lib/puppet/transaction/event.rb b/lib/puppet/transaction/event.rb
index 5d422f93d..abd5c8041 100644
--- a/lib/puppet/transaction/event.rb
+++ b/lib/puppet/transaction/event.rb
@@ -1,7 +1,29 @@
-require 'puppet'
+require 'puppet/transaction'
+require 'puppet/util/tagging'
# A simple struct for storing what happens on the system.
-Puppet::Transaction::Event = Struct.new(:name, :resource, :property, :result, :log, :previous_value, :desired_value) do
+class Puppet::Transaction::Event
+ include Puppet::Util::Tagging
+
+ ATTRIBUTES = [:name, :resource, :property, :previous_value, :desired_value, :status, :log, :node, :version, :file, :line]
+ attr_accessor *ATTRIBUTES
+ attr_writer :tags
+ attr_accessor :time
+
+ EVENT_STATUSES = %w{noop success failure}
+
+ def initialize(*args)
+ options = args.last.is_a?(Hash) ? args.pop : ATTRIBUTES.inject({}) { |hash, attr| hash[attr] = args.pop; hash }
+ options.each { |attr, value| send(attr.to_s + "=", value) unless value.nil? }
+
+ @time = Time.now
+ end
+
+ def status=(value)
+ raise ArgumentError, "Event status can only be #{EVENT_STATUSES.join(', ')}" unless EVENT_STATUSES.include?(value)
+ @status = value
+ end
+
def to_s
log
end
diff --git a/spec/unit/property.rb b/spec/unit/property.rb
index 03b848b15..6d3871d7b 100755
--- a/spec/unit/property.rb
+++ b/spec/unit/property.rb
@@ -45,6 +45,101 @@ describe Puppet::Property do
@property.must respond_to(:shadow)
end
+ describe "when returning the default event name" do
+ before do
+ @resource = stub 'resource'
+ @instance = @class.new(:resource => @resource)
+ @instance.stubs(:should).returns "myval"
+ end
+
+ it "should use the current 'should' value to pick the event name" do
+ @instance.expects(:should).returns "myvalue"
+ @class.expects(:value_option).with('myvalue', :event).returns :event_name
+
+ @instance.event_name
+ end
+
+ it "should return any event defined with the specified value" do
+ @instance.expects(:should).returns :myval
+ @class.expects(:value_option).with(:myval, :event).returns :event_name
+
+ @instance.event_name.should == :event_name
+ end
+
+ describe "and the property is 'ensure'" do
+ before do
+ @instance.stubs(:name).returns :ensure
+ @resource.expects(:type).returns :mytype
+ end
+
+ it "should use <type>_created if the 'should' value is 'present'" do
+ @instance.expects(:should).returns :present
+ @instance.event_name.should == :mytype_created
+ end
+
+ it "should use <type>_removed if the 'should' value is 'absent'" do
+ @instance.expects(:should).returns :absent
+ @instance.event_name.should == :mytype_removed
+ end
+
+ it "should use <type>_changed if the 'should' value is not 'absent' or 'present'" do
+ @instance.expects(:should).returns :foo
+ @instance.event_name.should == :mytype_changed
+ end
+
+ it "should use <type>_changed if the 'should value is nil" do
+ @instance.expects(:should).returns nil
+ @instance.event_name.should == :mytype_changed
+ end
+ end
+
+ it "should use <property>_changed if the property is not 'ensure'" do
+ @instance.stubs(:name).returns :myparam
+ @instance.expects(:should).returns :foo
+ @instance.event_name.should == :myparam_changed
+ end
+
+ it "should use <property>_changed if no 'should' value is set" do
+ @instance.stubs(:name).returns :myparam
+ @instance.expects(:should).returns nil
+ @instance.event_name.should == :myparam_changed
+ end
+ end
+
+ describe "when creating an event" do
+ before do
+ @resource = stub 'resource', :ref => "File[/foo]", :file => "/my/file", :line => 50, :tags => %w{foo bar}, :version => 42
+ @instance = @class.new(:resource => @resource)
+ @instance.stubs(:should).returns "myval"
+ end
+
+ it "should have the default event name" do
+ @instance.expects(:event_name).returns :my_event
+ @instance.event.name.should == :my_event
+ end
+
+ it "should have the resource's reference as the resource" do
+ @resource.stubs(:ref).returns "File[/yay]"
+ @instance.event.resource.should == "File[/yay]"
+ end
+
+ it "should have the property's name" do
+ @instance.event.property.should == @instance.name
+ end
+
+ it "should have the 'should' value set" do
+ @instance.stubs(:should).returns "foo"
+ @instance.event.desired_value.should == "foo"
+ end
+
+ {:file => "/my/file", :line => 50, :tags => %{foo bar}, :version => 50}.each do |attr, value|
+ it "should set the #{attr}" do
+ @instance.stubs(attr).returns value
+ @instance.event.send(attr).should == value
+ end
+ end
+ end
+
describe "when shadowing metaparameters" do
before do
@shadow_class = Class.new(Puppet::Property) do
@@ -273,13 +368,6 @@ describe Puppet::Property do
@provider.expects(:foo=).with :bar
@property.set(:bar)
end
-
- it "should return any specified event" do
- @class.newvalue(:bar, :event => :whatever)
- @property.should = :bar
- @provider.expects(:foo=).with :bar
- @property.set(:bar).should == :whatever
- end
end
describe "that was defined with a block" do
@@ -294,12 +382,6 @@ describe Puppet::Property do
@property.expects(:test)
@property.set("foo")
end
-
- it "should return any specified event" do
- @class.newvalue(:bar, :event => :myevent) {}
- @property.expects(:set_bar)
- @property.set(:bar).should == :myevent
- end
end
end
end
diff --git a/spec/unit/transaction/change.rb b/spec/unit/transaction/change.rb
index b42f0691b..4c22c8327 100755
--- a/spec/unit/transaction/change.rb
+++ b/spec/unit/transaction/change.rb
@@ -54,48 +54,23 @@ describe Puppet::Transaction::Change do
describe "and creating an event" do
before do
@resource = stub 'resource', :ref => "My[resource]"
- @property.stubs(:resource).returns @resource
- @property.stubs(:name).returns :myprop
- end
-
- it "should set the event name to the provided name" do
- @change.event(:foo).name.should == :foo
+ @event = stub 'event', :previous_value= => nil, :desired_value= => nil
+ @property.stubs(:event).returns @event
end
- it "should use the property's default event if the event name is nil" do
- @property.expects(:default_event_name).with(@change.should).returns :myevent
- @change.event(nil).name.should == :myevent
- end
-
- it "should produce a warning if the event name is not a symbol" do
- @property.expects(:warning)
- @property.stubs(:default_event_name).returns :myevent
- @change.event("a string")
- end
-
- it "should use the property to generate the event name if the provided name is not a symbol" do
- @property.stubs(:warning)
- @property.expects(:default_event_name).with(@change.should).returns :myevent
-
- @change.event("a string").name.should == :myevent
- end
-
- it "should set the resource to the resource reference" do
- @change.resource.expects(:ref).returns "Foo[bar]"
- @change.event(:foo).resource.should == "Foo[bar]"
- end
-
- it "should set the property to the property name" do
- @change.property.expects(:name).returns :myprop
- @change.event(:foo).property.should == :myprop
+ it "should use the property to create the event" do
+ @property.expects(:event).returns @event
+ @change.event.should equal(@event)
end
it "should set 'previous_value' from the change's 'is'" do
- @change.event(:foo).previous_value.should == @change.is
+ @event.expects(:previous_value=).with(@change.is)
+ @change.event
end
it "should set 'desired_value' from the change's 'should'" do
- @change.event(:foo).desired_value.should == @change.should
+ @event.expects(:desired_value=).with(@change.should)
+ @change.event
end
end
@@ -103,7 +78,7 @@ describe Puppet::Transaction::Change do
before do
@event = Puppet::Transaction::Event.new(:myevent)
@change.stubs(:noop?).returns false
- @change.stubs(:event).returns @event
+ @property.stubs(:event).returns @event
@property.stub_everything
@property.stubs(:resource).returns "myresource"
@@ -116,18 +91,19 @@ describe Puppet::Transaction::Change do
it "should log that it is in noop" do
@property.expects(:is_to_s)
@property.expects(:should_to_s)
- @property.expects(:log)
+ @property.expects(:log).returns "my log"
+
+ @event.expects(:log=).with("my log")
- @change.stubs :event
@change.forward
end
it "should produce a :noop event and return" do
@property.stub_everything
- @change.expects(:event).with(:noop).returns :noop_event
+ @event.expects(:status=).with("noop")
- @change.forward.should == :noop_event
+ @change.forward.should == @event
end
end
diff --git a/spec/unit/transaction/event.rb b/spec/unit/transaction/event.rb
index b7bd179f6..7bdd0898e 100755
--- a/spec/unit/transaction/event.rb
+++ b/spec/unit/transaction/event.rb
@@ -5,7 +5,7 @@ require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/transaction/event'
describe Puppet::Transaction::Event do
- [:log, :previous_value, :desired_value, :property, :resource, :name, :result].each do |attr|
+ [:log, :previous_value, :desired_value, :property, :resource, :name, :log, :node, :version, :file, :line, :tags].each do |attr|
it "should support #{attr}" do
event = Puppet::Transaction::Event.new
event.send(attr.to_s + "=", "foo")
@@ -18,4 +18,25 @@ describe Puppet::Transaction::Event do
event.expects(:log).returns "my log"
event.to_s.should == "my log"
end
+
+ it "should support 'status'" do
+ event = Puppet::Transaction::Event.new
+ event.status = "success"
+ event.status.should == "success"
+ end
+
+ it "should fail if the status is not to 'noop', 'success', or 'failure" do
+ event = Puppet::Transaction::Event.new
+ lambda { event.status = "foo" }.should raise_error(ArgumentError)
+ end
+
+ it "should support tags" do
+ Puppet::Transaction::Event.ancestors.should include(Puppet::Util::Tagging)
+ end
+
+ it "should be able to send logs"
+
+ it "should create a timestamp at its creation time" do
+ Puppet::Transaction::Event.new.time.should be_instance_of(Time)
+ end
end