Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telemetry - determine run context from stack introspection #4907

Merged
merged 9 commits into from Jun 17, 2020

Conversation

clintoncwolfe
Copy link
Contributor

@clintoncwolfe clintoncwolfe commented Feb 11, 2020

Description

Provides ability to determine how InSpec is being run - whether via the CLI, via the Audit cookbook, or via kitchen-inspec, all via looking at the call stack.

Here is some of the stack data captured :

During an audit run under chef-solo (manipulated to include indices from the outermost frame):

"-29:[/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/inspec-core-4.18.85/lib/inspec/profile.rb:208:in `block in collect_tests'",
 "-28:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/inspec-core-4.18.85/lib/inspec/profile.rb:204:in `each'",
 "-27:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/inspec-core-4.18.85/lib/inspec/profile.rb:204:in `collect_tests'",
 "-26:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/inspec-core-4.18.85/lib/inspec/runner.rb:118:in`block in load'",
 "-25:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/inspec-core-4.18.85/lib/inspec/runner.rb:101:in `each'",
 "-24:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/inspec-core-4.18.85/lib/inspec/runner.rb:101:in `load'",
 "-23:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/inspec-core-4.18.85/lib/inspec/runner.rb:129:in `run'", "-22:/tmp/kitchen/cache/cookbooks/audit/files/default/handler/audit_report.rb:174:in `call'", "-21:/tmp/kitchen/cache/cookbooks/audit/files/default/handler/audit_report.rb:82:in `report'",
 "-20:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/handler.rb:256:in `run_report_unsafe'", "-19:/tmp/kitchen/cache/cookbooks/audit/files/default/handler/audit_report.rb:100:in `run_report_safely'",
 "-18:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/handler.rb:123:in `block in run_report_handlers'",
 "-17:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/handler.rb:121:in `each'",
 "-16:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/handler.rb:121:in `run_report_handlers'",
 "-15:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/handler.rb:133:in `block in <class:Handler>'",
 "-14:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/client.rb:417:in `block in run_completed_successfully'",
 "-13:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/client.rb:416:in `each'",
 "-12:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/client.rb:416:in `run_completed_successfully'",
 "-11:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/client.rb:292:in `run'",
 "-10:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/application.rb:320:in `run_with_graceful_exit_option'",
 "-9:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/application.rb:296:in `block in run_chef_client'",
 "-8:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/local_mode.rb:42:in `with_server_connectivity'",
 "-7:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/application.rb:279:in `run_chef_client'",
 "-6:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/application/base.rb:330:in `run_application'",
 "-5:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/application.rb:69:in`run'",
 "-4:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-15.8.23/lib/chef/application/solo.rb:59:in `run'",
 "-3:/opt/chef/embedded/lib/ruby/gems/2.6.0/gems/chef-bin-15.8.23/bin/chef-solo:24:in `<top (required)>'",
 "-2:/opt/chef/bin/chef-solo:177:in `load'",
 "-1:/opt/chef/bin/chef-solo:177:in `<main>'"

Related Issue

Needed for #4900

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New content (non-breaking change)
  • Breaking change (a content change which would break existing functionality or processes)

Checklist:

  • I have read the CONTRIBUTING document.

Aha! Link: https://chef.aha.io/features/SH-2163

@codeclimate
Copy link

codeclimate bot commented Feb 11, 2020

Code Climate has analyzed commit 6051618 and detected 5 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 5

View more on Code Climate.

@zenspider zenspider requested review from a team and zenspider and removed request for zenspider and a team February 12, 2020 19:40

def self.audit_cookbook?(stack)
stack_match(stack: stack[-21..-11], path: "chef/handler", label: "run_report_handlers") &&
stack_match(stack: stack[-26..-16], path: "handler/audit_report", label: "report")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These fixed numbers like -26 feel likely to change and break, is that true? What happens if they do? What are the consequences of false positives/negatives?

At the least I think we'd want unit tests that run against a captured stack trace embedded in the tests. That would make it easier to understand what is being picked out by seeing the same. Of course that wouldn't detect any changes to the stack, so we'd probably want an integration tests for each of these, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the fixed frame ranges. I agree they would be brittle.

The consequence of a false positive would be miscategorizing a run in the telemetry data. A false negative would likely result in an "unknown" result.

I've added integration tests that exercise each of the three outcomes.

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
…tKitchen

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
@clintoncwolfe
Copy link
Contributor Author

@james-stocks @Schwad Two of the tests on this require test-kitchen. We don't currently run any test-kitchen suites automatically, AFAIK, so I didn't add anything to BuildKite or Rake to enable that; let me know how you'd like that handled.

@@ -387,11 +393,6 @@ def version
end
map %w{-v --version} => :version

desc "nothing", "does nothing"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be best to delete these lines in a separate commit.

@james-stocks
Copy link
Contributor

Regarding test-kitchen, I'm not comfortable with the general approach of adding a test command to the CLI and having the integration suite depend on both kitchen and the audit cookbook.
It feels like this is somewhat jumping the gun on properly defining and making a start on end-to-end testing.

I'd prefer that the unit and integration tests stick to static fixtures and mocking; and exercise only the code contained in this project.

@clintoncwolfe
Copy link
Contributor Author

@james-stocks, in order for this code to be integration tested, which @btm stated was required, it must run in its real environment, which means running under test-kitchen and audit cookbook.

Copy link
Contributor

@james-stocks james-stocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the confusion @clintoncwolfe - this looks good to me - as discussed I'll raise an issue for us to get test/integration running in buildkite

@Schwad Schwad merged commit dca57b6 into master Jun 17, 2020
@Schwad Schwad deleted the cw/telemetry-run-context branch June 17, 2020 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants