summaryrefslogtreecommitdiff
path: root/lib/chef/audit/control_group_data.rb
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2015-02-19 15:17:05 -0800
committerJay Mundrawala <jdmundrawala@gmail.com>2015-02-19 15:17:05 -0800
commit864f9ac95063c7833235c8ed50dcb89653eda03f (patch)
tree93da186fd522bdfc5de6414d011f4c0dd49aeb40 /lib/chef/audit/control_group_data.rb
parent194f49bdb7737e0591271ba95021997e90379c5d (diff)
parenta7f5c92960aedf8d5bfc71abbce430ab075e016a (diff)
downloadchef-jdm/merge-into-12-stable.tar.gz
Merge remote-tracking branch 'origin/master' into HEADjdm/merge-into-12-stable
* origin/master: (642 commits) Remove Chef 12 release notes Update Changelog for Chef 12.1.0 Chef 12.1.0.rc.0 Group spec needs to respond to shell_out fix dpkg regression fix Lint/BlockAlignment whitespaces fixes fix Lint/AmbiguousRegexpLiteral fix Lint/LiteralInCondition fix Lint/Loop style Make tests pass on Windows remove unreachable code Fix unit specs for PR #2934 dont raise exceptions in load_current_resource when checking current status update changelog fix typo in msi provider Added spec for #2914 fix virtual package logic in check_package_state use scalar pkg not array package convert is_virtual_package to hash ... Conflicts: .travis.yml CHANGELOG.md DOC_CHANGES.md RELEASE_NOTES.md appveyor.yml lib/chef/application.rb lib/chef/dsl/recipe.rb lib/chef/knife/bootstrap.rb lib/chef/knife/core/bootstrap_context.rb lib/chef/node/attribute.rb lib/chef/node/attribute_collections.rb lib/chef/node/immutable_collections.rb lib/chef/resource.rb lib/chef/run_context.rb lib/chef/version.rb spec/functional/dsl/reboot_pending_spec.rb spec/functional/event_loggers/windows_eventlog_spec.rb spec/functional/resource/link_spec.rb spec/support/platform_helpers.rb spec/unit/knife_spec.rb spec/unit/mixin/deep_merge_spec.rb spec/unit/mixin/shell_out_spec.rb spec/unit/node/attribute_spec.rb spec/unit/node_spec.rb spec/unit/provider/package/apt_spec.rb spec/unit/provider/service/systemd_service_spec.rb spec/unit/provider_resolver_spec.rb spec/unit/recipe_spec.rb spec/unit/resource/resource_notification_spec.rb spec/unit/run_context_spec.rb
Diffstat (limited to 'lib/chef/audit/control_group_data.rb')
-rw-r--r--lib/chef/audit/control_group_data.rb140
1 files changed, 140 insertions, 0 deletions
diff --git a/lib/chef/audit/control_group_data.rb b/lib/chef/audit/control_group_data.rb
new file mode 100644
index 0000000000..204d7f8070
--- /dev/null
+++ b/lib/chef/audit/control_group_data.rb
@@ -0,0 +1,140 @@
+#
+# Author:: Tyler Ball (<tball@chef.io>)
+#
+# Copyright:: Copyright (c) 2014 Chef Software, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'securerandom'
+
+class Chef
+ class Audit
+ class AuditData
+ attr_reader :node_name, :run_id, :control_groups
+ attr_accessor :start_time, :end_time
+
+ def initialize(node_name, run_id)
+ @node_name = node_name
+ @run_id = run_id
+ @control_groups = []
+ end
+
+ def add_control_group(control_group)
+ control_groups << control_group
+ end
+
+ def to_hash
+ {
+ :node_name => node_name,
+ :run_id => run_id,
+ :start_time => start_time,
+ :end_time => end_time,
+ :control_groups => control_groups.collect { |c| c.to_hash }
+ }
+ end
+ end
+
+ class ControlGroupData
+ attr_reader :name, :status, :number_succeeded, :number_failed, :controls, :metadata
+
+ def initialize(name, metadata={})
+ @status = "success"
+ @controls = []
+ @number_succeeded = 0
+ @number_failed = 0
+ @name = name
+ @metadata = metadata
+ end
+
+
+ def example_success(control_data)
+ @number_succeeded += 1
+ control = create_control(control_data)
+ control.status = "success"
+ controls << control
+ control
+ end
+
+ def example_failure(control_data, details)
+ @number_failed += 1
+ @status = "failure"
+ control = create_control(control_data)
+ control.details = details if details
+ control.status = "failure"
+ controls << control
+ control
+ end
+
+ def to_hash
+ # We sort it so the examples appear in the output in the same order
+ # they appeared in the recipe
+ controls.sort! {|x,y| x.line_number <=> y.line_number}
+ h = {
+ :name => name,
+ :status => status,
+ :number_succeeded => number_succeeded,
+ :number_failed => number_failed,
+ :controls => controls.collect { |c| c.to_hash }
+ }
+ # If there is a duplicate key, metadata will overwrite it
+ add_display_only_data(h).merge(metadata)
+ end
+
+ private
+
+ def create_control(control_data)
+ ControlData.new(control_data)
+ end
+
+ # The id and control sequence number are ephemeral data - they are not needed
+ # to be persisted and can be regenerated at will. They are only needed
+ # for display purposes.
+ def add_display_only_data(group)
+ group[:id] = SecureRandom.uuid
+ group[:controls].collect!.with_index do |c, i|
+ # i is zero-indexed, and we want the display one-indexed
+ c[:sequence_number] = i+1
+ c
+ end
+ group
+ end
+
+ end
+
+ class ControlData
+ attr_reader :name, :resource_type, :resource_name, :context, :line_number
+ attr_accessor :status, :details
+
+ def initialize(control_data={})
+ control_data.each do |k, v|
+ self.instance_variable_set("@#{k}", v)
+ end
+ end
+
+ def to_hash
+ h = {
+ :name => name,
+ :status => status,
+ :details => details,
+ :resource_type => resource_type,
+ :resource_name => resource_name
+ }
+ h[:context] = context || []
+ h
+ end
+ end
+
+ end
+end