summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Ball <tyleraball@gmail.com>2014-12-08 14:51:20 -0800
committerTyler Ball <tyleraball@gmail.com>2014-12-08 14:51:20 -0800
commitc6e5654a18f236dd75bb59472e80887c27310d99 (patch)
treedec361fcc9e502ad4d3d16db048c875725684018
parent9c654e43e87089816dbcaa3b4effe6c47eb8e6be (diff)
parent07020a416f9f3883b46c7d7aa612426e2c20fa06 (diff)
downloadchef-c6e5654a18f236dd75bb59472e80887c27310d99.tar.gz
Merge pull request #2528 from opscode/tball/line-number
Adding cookbook and recipe location information to JSON analytics payload
-rw-r--r--lib/chef/audit/audit_event_proxy.rb2
-rw-r--r--lib/chef/audit/audit_reporter.rb9
-rw-r--r--lib/chef/audit/control_group_data.rb33
-rw-r--r--lib/chef/audit/runner.rb2
-rw-r--r--lib/chef/dsl/audit.rb13
5 files changed, 33 insertions, 26 deletions
diff --git a/lib/chef/audit/audit_event_proxy.rb b/lib/chef/audit/audit_event_proxy.rb
index 6d5591d943..36160db9bb 100644
--- a/lib/chef/audit/audit_event_proxy.rb
+++ b/lib/chef/audit/audit_event_proxy.rb
@@ -43,7 +43,7 @@ class Chef
described_class = example.metadata[:described_class]
if described_class
resource_type = described_class.class.name.split(':')[-1]
- # TODO submit github PR to expose this
+ # TODO https://github.com/serverspec/serverspec/pull/493
resource_name = described_class.instance_variable_get(:@name)
end
diff --git a/lib/chef/audit/audit_reporter.rb b/lib/chef/audit/audit_reporter.rb
index 21ffb62829..d022ac0c47 100644
--- a/lib/chef/audit/audit_reporter.rb
+++ b/lib/chef/audit/audit_reporter.rb
@@ -28,7 +28,7 @@ class Chef
attr_reader :rest_client, :audit_data, :ordered_control_groups, :run_status
private :rest_client, :audit_data, :ordered_control_groups, :run_status
- PROTOCOL_VERSION = '0.1.0'
+ PROTOCOL_VERSION = '0.1.1'
def initialize(rest_client)
@rest_client = rest_client
@@ -36,6 +36,10 @@ class Chef
@ordered_control_groups = Hash.new
end
+ def run_context
+ run_status.run_context
+ end
+
def audit_phase_start(run_status)
Chef::Log.debug("Audit Reporter starting")
@audit_data = AuditData.new(run_status.node.name, run_status.run_id)
@@ -71,7 +75,8 @@ class Chef
if ordered_control_groups.has_key?(name)
raise Chef::Exceptions::AuditControlGroupDuplicate.new(name)
end
- ordered_control_groups.store(name, ControlGroupData.new(name))
+ metadata = run_context.audits[name].metadata
+ ordered_control_groups.store(name, ControlGroupData.new(name, metadata))
end
def control_example_success(control_group_name, example_data)
diff --git a/lib/chef/audit/control_group_data.rb b/lib/chef/audit/control_group_data.rb
index e221ae94cc..42a91ef5a7 100644
--- a/lib/chef/audit/control_group_data.rb
+++ b/lib/chef/audit/control_group_data.rb
@@ -28,19 +28,20 @@ class Chef
end
class ControlGroupData
- attr_reader :name, :status, :number_success, :number_failed, :controls
+ attr_reader :name, :status, :number_succeeded, :number_failed, :controls, :metadata
- def initialize(name)
+ def initialize(name, metadata={})
@status = "success"
@controls = []
- @number_success = 0
+ @number_succeeded = 0
@number_failed = 0
@name = name
+ @metadata = metadata
end
def example_success(control_data)
- @number_success += 1
+ @number_succeeded += 1
control = create_control(control_data)
control.status = "success"
controls << control
@@ -64,24 +65,18 @@ class Chef
h = {
:name => name,
:status => status,
- :number_success => number_success,
+ :number_succeeded => number_succeeded,
:number_failed => number_failed,
:controls => controls.collect { |c| c.to_hash }
}
- add_display_only_data(h)
+ # If there is a duplicate key, metadata will overwrite it
+ add_display_only_data(h).merge(metadata)
end
private
def create_control(control_data)
- name = control_data[:name]
- resource_type = control_data[:resource_type]
- resource_name = control_data[:resource_name]
- context = control_data[:context]
- line_number = control_data[:line_number]
- # TODO make this smarter with splat arguments so if we start passing in more control_data
- # I don't have to modify code in multiple places
- ControlData.new(name, resource_type, resource_name, context, line_number)
+ ControlData.new(control_data)
end
# The id and control sequence number are ephemeral data - they are not needed
@@ -103,12 +98,10 @@ class Chef
attr_reader :name, :resource_type, :resource_name, :context, :line_number
attr_accessor :status, :details
- def initialize(name, resource_type, resource_name, context, line_number)
- @context = context
- @name = name
- @resource_type = resource_type
- @resource_name = resource_name
- @line_number = line_number
+ def initialize(control_data={})
+ control_data.each do |k, v|
+ self.instance_variable_set("@#{k}", v)
+ end
end
def to_hash
diff --git a/lib/chef/audit/runner.rb b/lib/chef/audit/runner.rb
index 306212989a..51c007d1d0 100644
--- a/lib/chef/audit/runner.rb
+++ b/lib/chef/audit/runner.rb
@@ -142,7 +142,7 @@ class Chef
def register_controls
add_example_group_methods
run_context.audits.each do |name, group|
- ctl_grp = RSpec::Core::ExampleGroup.__controls__(*group[:args], &group[:block])
+ ctl_grp = RSpec::Core::ExampleGroup.__controls__(*group.args, &group.block)
RSpec.world.register(ctl_grp)
end
end
diff --git a/lib/chef/dsl/audit.rb b/lib/chef/dsl/audit.rb
index a11d9039ef..022bbcce01 100644
--- a/lib/chef/dsl/audit.rb
+++ b/lib/chef/dsl/audit.rb
@@ -30,11 +30,20 @@ class Chef
name = args[0]
if name.nil? || name.empty?
raise Chef::Exceptions::AuditNameMissing
- elsif run_context.controls.has_key?(name)
+ elsif run_context.audits.has_key?(name)
raise Chef::Exceptions::AuditControlGroupDuplicate.new(name)
end
- run_context.audits[name] = { :args => args, :block => block }
+ # This DSL will only work in the Recipe class because that exposes the cookbook_name
+ cookbook_name = self.cookbook_name
+ metadata = {
+ cookbook_name: cookbook_name,
+ cookbook_version: self.run_context.cookbook_collection[cookbook_name].version,
+ recipe_name: self.recipe_name,
+ line_number: block.source_location[1]
+ }
+
+ run_context.audits[name] = Struct.new(:args, :block, :metadata).new(args, block, metadata)
end
end