Sha256: a03e3677af224da620392ae4d62a573dc492e386baae75793ae4927d11a20c5b

Contents?: true

Size: 1.48 KB

Versions: 58

Compression:

Stored size: 1.48 KB

Contents

module Inspec
  module Telemetry
    # Guesses the run context of InSpec - how were we invoked?
    # All stack values here are determined experimentally

    class RunContextProbe
      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)

        "unknown"
      end

      def self.run_by_thor?(stack)
        stack_match(stack: stack, path: "thor/command", label: "run") &&
          stack_match(stack: stack, path: "thor/invocation", label: "invoke_command")
      end

      def self.kitchen?(stack)
        stack_match(stack: stack, path: "kitchen/instance", label: "verify_action") &&
          stack_match(stack: stack, path: "kitchen/instance", label: "verify")
      end

      def self.audit_cookbook?(stack)
        stack_match(stack: stack, path: "chef/handler", label: "run_report_handlers") &&
          stack_match(stack: stack, path: "handler/audit_report", label: "report")
      end

      def self.stack_match(stack: [], label: nil, path: nil)
        return false if stack.nil?

        stack.any? do |frame|
          if label && path
            frame.label == label && frame.absolute_path.include?(path)
          elsif label
            frame.label == label
          elsif path
            frame.absolute_path.include?(path)
          else
            false
          end
        end
      end
    end
  end
end

Version data entries

58 entries across 58 versions & 1 rubygems

Version Path
inspec-core-5.22.58 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-5.22.55 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-5.22.40 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-6.6.0 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-5.22.36 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-5.22.29 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-4.56.58 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-5.22.3 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-5.21.29 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-5.18.14 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-5.17.4 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-5.14.0 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-4.56.20 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-5.12.2 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-5.10.5 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-4.56.19 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-5.7.9 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-4.56.17 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-4.52.9 lib/inspec/utils/telemetry/run_context_probe.rb
inspec-core-4.50.3 lib/inspec/utils/telemetry/run_context_probe.rb