stack probe for test-kitchen

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2020-02-10 15:41:58 -05:00
parent e96ddbf2cb
commit 5be4ee913f

View file

@ -1,21 +1,27 @@
module Inspec module Inspec
module Telemetry module Telemetry
# Guesses the run context of InSpec - how were we invoked? # Guesses the run context of InSpec - how were we invoked?
# All stack values here are determined experimentally
class RunContextProbe class RunContextProbe
def self.guess_run_context def self.guess_run_context
stack = caller_locations(4) stack = caller_locations(4)
return "test-kitchen" if kitchen?(stack)
return "cli" if run_by_thor?(stack) return "cli" if run_by_thor?(stack)
# audit-cookbook # audit-cookbook
# kitchen-inspec "unknown"
# "unknown"
end end
def self.run_by_thor?(stack) def self.run_by_thor?(stack)
stack_match(stack: stack[-14..-9], path: "thor/command", label: "run") && stack_match(stack: stack[-14..-9], path: "thor/command", label: "run") &&
stack_match(stack: stack[-14..-9], path: "thor/invocation", label: "invoke_command") stack_match(stack: stack[-14..-9], path: "thor/invocation", label: "invoke_command")
end end
def self.kitchen?(stack)
stack_match(stack: stack[-21..-11], path: "kitchen/instance", label: "verify_action") &&
stack_match(stack: stack[-14..-4], path: "kitchen/instance", label: "verify")
end
def self.stack_match(stack: nil, label: nil, path: nil) def self.stack_match(stack: nil, label: nil, path: nil)
stack.any? do |frame| stack.any? do |frame|
if label && path if label && path