Please review pull request #618: (#9167) Do not send email when nothing changes opened by (kelseyhightower)
Description:
Without this patch the tagmail report sends an email even when there are
no changes or resources out of sync. This has the undesired effect of
sending emails after every Puppet run.
When there are no changes or out of sync resources, tagmail logs a
notice stating the fact and skips processing logs and sending emails.
This patch includes new spec tests for the changes in behavior.
- Opened: Mon Apr 02 21:35:22 UTC 2012
- Based on: puppetlabs:2.7.x (d88f3e7c387eff270101604f2fec2e086f88ebdd)
- Requested merge: kelseyhightower:kelseyhightower/ticket/2.7.x/9167_tagmail_report_sends_email_when_nothing_happens (3252476b1f8940edb4fe4c2269d2f14db6fd0701)
Diff follows:
diff --git a/lib/puppet/reports/tagmail.rb b/lib/puppet/reports/tagmail.rb
index c37341e..4e88ecf 100644
--- a/lib/puppet/reports/tagmail.rb
+++ b/lib/puppet/reports/tagmail.rb
@@ -113,6 +113,16 @@ def process
return
end
+ # Do not send an email if there are no changes or resources out of sync.
+ changes = ['out_of_sync', 'changed'].inject(0) do |sum, metric|
+ sum + raw_summary['resources'][metric]
+ end
+
+ unless changes > 0
+ Puppet.notice "Not sending tagmail report; no changes"
+ return
+ end
+
taglists = parse(File.read(Puppet[:tagmap]))
# Now find any appropriately tagged messages.
diff --git a/spec/fixtures/unit/reports/tagmail/tagmail_email.conf b/spec/fixtures/unit/reports/tagmail/tagmail_email.conf
new file mode 100644
index 0000000..94efca4
--- /dev/null
+++ b/spec/fixtures/unit/reports/tagmail/tagmail_email.conf
@@ -0,0 +1,2 @@
+secure: [email protected]
+
diff --git a/spec/unit/reports/tagmail_spec.rb b/spec/unit/reports/tagmail_spec.rb
index a53d119..757505f 100755
--- a/spec/unit/reports/tagmail_spec.rb
+++ b/spec/unit/reports/tagmail_spec.rb
@@ -88,4 +88,69 @@ def match(pos = [], neg = [])
results.should be_nil
end
end
+
+ describe "the behavior of tagmail.process" do
+ before do
+ Puppet[:tagmap] = my_fixture "tagmail_email.conf"
+ end
+
+ let(:processor) do
+ processor = Puppet::Transaction::Report.new("apply")
+ processor.extend(Puppet::Reports.report(:tagmail))
+ processor
+ end
+
+ context "when any messages match a positive tag" do
+ before do
+ processor << log_entry
+ end
+
+ let(:log_entry) do
+ Puppet::Util::Log.new(
+ :level => :notice, :message => "Secure change", :tags => %w{secure})
+ end
+
+ let(:message) do
+ "#{log_entry.time} Puppet (notice): Secure change"
+ end
+
+ it "should send email if there are changes" do
+ processor.expects(:send).with([[['[email protected]'], message]])
+ processor.expects(:raw_summary).with().times(2).returns({
+ "resources" => { "changed" => 1, "out_of_sync" => 0 }
+ })
+
+ processor.process
+ end
+
+ it "should send email if there are resources out of sync" do
+ processor.expects(:send).with([[['[email protected]'], message]])
+ processor.expects(:raw_summary).with().times(2).returns({
+ "resources" => { "changed" => 0, "out_of_sync" => 1 }
+ })
+
+ processor.process
+ end
+
+ it "should not send email if no changes or resources out of sync" do
+ processor.expects(:send).never
+ processor.expects(:raw_summary).with().times(2).returns({
+ "resources" => { "changed" => 0, "out_of_sync" => 0 }
+ })
+
+ processor.process
+ end
+
+ it "should log a message if no changes or resources out of sync" do
+ processor.expects(:send).never
+ processor.expects(:raw_summary).with().times(2).returns({
+ "resources" => { "changed" => 0, "out_of_sync" => 0 }
+ })
+
+ Puppet.expects(:notice).with("Not sending tagmail report; no changes")
+ processor.process
+ end
+ end
+ end
end
+
-- You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
