mirror of
https://github.com/inspec/inspec
synced 2024-11-10 15:14:23 +00:00
Merge pull request #4025 from inspec/mj/3849
Enable user telemetry opt-in / opt-out on cli
This commit is contained in:
commit
1edfeae5f4
5 changed files with 63 additions and 10 deletions
|
@ -40,6 +40,9 @@ class Inspec::InspecCLI < Inspec::BaseCLI
|
|||
class_option :disable_user_plugins, type: :string, banner: '',
|
||||
desc: 'Disable loading all plugins that the user installed.'
|
||||
|
||||
class_option :enable_telemetry, type: :boolean,
|
||||
desc: 'Allow or disable telemetry', default: false
|
||||
|
||||
require 'license_acceptance/cli_flags/thor'
|
||||
include LicenseAcceptance::CLIFlags::Thor
|
||||
|
||||
|
|
|
@ -67,6 +67,12 @@ module Inspec
|
|||
puts
|
||||
end
|
||||
|
||||
# return all telemetry options from config
|
||||
# @return [Hash]
|
||||
def telemetry_options
|
||||
final_options.select { |key, _| key.include?('telemetry') }
|
||||
end
|
||||
|
||||
#-----------------------------------------------------------------------#
|
||||
# Train Credential Handling
|
||||
#-----------------------------------------------------------------------#
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
require 'inspec/config'
|
||||
require 'inspec/utils/telemetry/data_series'
|
||||
require 'singleton'
|
||||
|
||||
|
@ -6,9 +7,17 @@ module Inspec::Telemetry
|
|||
class Collector
|
||||
include Singleton
|
||||
|
||||
attr_reader :config
|
||||
|
||||
def initialize
|
||||
@data_series = []
|
||||
@enabled = true
|
||||
@telemetry_toggled_off = false
|
||||
load_config
|
||||
end
|
||||
|
||||
# Allow loading a configuration, useful when testing.
|
||||
def load_config(config = Inspec::Config.cached)
|
||||
@config = config
|
||||
end
|
||||
|
||||
# Add a data series to the collection.
|
||||
|
@ -17,17 +26,20 @@ module Inspec::Telemetry
|
|||
@data_series << data_series
|
||||
end
|
||||
|
||||
# Is the Telemetry system enabled or disabled?
|
||||
# Always true until we add configuration parsing.
|
||||
# The loaded configuration should have a option to configure
|
||||
# telemetry, if not default to false.
|
||||
# @return [True, False]
|
||||
def telemetry_enabled?
|
||||
@enabled
|
||||
if @telemetry_toggled_off
|
||||
false
|
||||
else
|
||||
config_telemetry_options.fetch('enable_telemetry', false)
|
||||
end
|
||||
end
|
||||
|
||||
# A way to disable the telemetry system.
|
||||
# @return [True]
|
||||
def disable_telemetry
|
||||
@enabled = false
|
||||
@telemetry_toggled_off = true
|
||||
end
|
||||
|
||||
# The entire data series collection.
|
||||
|
@ -52,9 +64,18 @@ module Inspec::Telemetry
|
|||
end
|
||||
|
||||
# Blanks the contents of the data series collection.
|
||||
# Reset telemetry toggle
|
||||
# @return [True]
|
||||
def reset
|
||||
def reset!
|
||||
@data_series = []
|
||||
@telemetry_toggled_off = false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Minimize exposure of Inspec::Config interface
|
||||
def config_telemetry_options
|
||||
config.telemetry_options
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ require_relative '../../../helper.rb'
|
|||
class TestTelemetryCollector < Minitest::Test
|
||||
def setup
|
||||
@collector = Inspec::Telemetry::Collector.instance
|
||||
@collector.reset
|
||||
@collector.reset!
|
||||
end
|
||||
|
||||
def test_collector_singleton
|
||||
|
@ -37,7 +37,24 @@ class TestTelemetryCollector < Minitest::Test
|
|||
def test_reset_singleton
|
||||
data_series = Inspec::Telemetry::DataSeries.new('/resource/File')
|
||||
@collector.add_data_series(data_series)
|
||||
@collector.reset
|
||||
@collector.reset!
|
||||
assert_equal 0, @collector.list_data_series.count
|
||||
end
|
||||
|
||||
def test_telemetry_enabled
|
||||
@collector.load_config(Inspec::Config.mock('enable_telemetry'=>true))
|
||||
assert @collector.telemetry_enabled?
|
||||
end
|
||||
|
||||
def test_telemetry_disabled
|
||||
@collector.load_config(Inspec::Config.mock('enable_telemetry'=>false))
|
||||
refute @collector.telemetry_enabled?
|
||||
end
|
||||
|
||||
def test_disable_telemetry
|
||||
@collector.load_config(Inspec::Config.mock('enable_telemetry'=>true))
|
||||
assert @collector.telemetry_enabled?
|
||||
@collector.disable_telemetry
|
||||
refute @collector.telemetry_enabled?
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,8 @@ require_relative '../../../helper.rb'
|
|||
class TestTelemetryGlobalMethods < Minitest::Test
|
||||
def setup
|
||||
@collector = Inspec::Telemetry::Collector.instance
|
||||
@collector.reset
|
||||
@collector.load_config(Inspec::Config.mock('enable_telemetry'=>true))
|
||||
@collector.reset!
|
||||
end
|
||||
|
||||
def test_record_telemetry_data
|
||||
|
@ -24,4 +25,9 @@ class TestTelemetryGlobalMethods < Minitest::Test
|
|||
assert_equal ['serverspec_compat'], depgrp.data
|
||||
assert_equal :deprecation_group, depgrp.name
|
||||
end
|
||||
|
||||
def test_telemetry_disabled
|
||||
@collector.load_config(Inspec::Config.mock(telemetry: false))
|
||||
refute Inspec.record_telemetry_data(:deprecation_group, 'serverspec_compat')
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue