Skip to content

Commit

Permalink
Test kitchen setup to test detection of audit-cookbook
Browse files Browse the repository at this point in the history
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
  • Loading branch information
clintoncwolfe committed May 26, 2020
1 parent 43a5fd7 commit d5be4c2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
20 changes: 20 additions & 0 deletions kitchen.run_context.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ verifier:
lifecycle:
# This pre_create hook, along with the install_inspec cookbook, ensures that
# the VM has a version of InSpec that matches that built from source.
# Note that audit cookbook will ignore this.
pre_create:
- local: cd inspec-bin && gem build inspec-core-bin.gemspec --output ../test/kitchen/cookbooks/install_inspec/files/inspec-core-bin.gem
- local: gem build inspec-core.gemspec --output test/kitchen/cookbooks/install_inspec/files/inspec-core.gem
Expand All @@ -27,3 +28,22 @@ suites:
- name: run-context-tk
run_list:
- recipe[install_inspec]

# This test suite uses a pair of profiles to check the Telemetry Run Context
# detection system under audit cookbook.
- name: run-context-after-audit
run_list:
- recipe[install_inspec]
- recipe[audit]
attributes:
audit:
profiles:
# This actually runs during converge time, and performs the stack
# probe that we care about. It writes a JSON copy of the stack that
# it saw to /tmp/audit_stack.json . run-context-after-audit examines
# that JSON file and runs it through the context probe to see what it gets.
run-context-during-audit:
# This should work but doesn't :-(
# path: <%= Dir.pwd %>/test/integration/run-context-during-audit/
url: https://github.com/inspec/inspec-test-profile-run-context-audit/archive/v0.3.2.zip

4 changes: 2 additions & 2 deletions lib/inspec/utils/telemetry/run_context_probe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Telemetry
# All stack values here are determined experimentally

class RunContextProbe
def self.guess_run_context
stack = caller_locations
def self.guess_run_context(stack = nil)
stack ||= caller_locations
return "test-kitchen" if kitchen?(stack)
return "cli" if run_by_thor?(stack)
return "audit-cookbook" if audit_cookbook?(stack)
Expand Down
16 changes: 16 additions & 0 deletions test/integration/run-context-after-audit/controls/after-audit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

# run-context-during-audit should have left a file with a JSON
# representation of the stack as experienced by the audit cookbook.

raw_data = JSON.parse(file("/tmp/audit_stack.json").content)
# These aren't really stack frames, so we do some duck typing
Frame = Struct.new(:absolute_path, :label)
reconstructed_stack = raw_data.map{ |f| Frame.new(f["absolute_path"], f["label"]) }

require "inspec/utils/telemetry/run_context_probe"

control "run-context" do
describe Inspec::Telemetry::RunContextProbe.guess_run_context(reconstructed_stack) do
it { should eq "audit-cookbook" }
end
end
10 changes: 10 additions & 0 deletions test/integration/run-context-after-audit/inspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: run-context-after-audit
title: InSpec Profile
maintainer: InSpec Engineering
copyright: Chef Software, Inc.
copyright_email: inspec@chef.io
license: Apache-2.0
summary: An InSpec Compliance Profile to test inspec run context detection under audit-cookbook
version: 0.1.0
supports:
platform: os

0 comments on commit d5be4c2

Please sign in to comment.