2016-03-25 01:31:19 +01:00
|
|
|
|
# encoding: utf-8
|
|
|
|
|
# author: Dominik Richter
|
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
|
|
|
|
|
|
require 'helper'
|
2018-09-12 18:04:16 -04:00
|
|
|
|
require 'rbconfig'
|
2016-03-25 01:31:19 +01:00
|
|
|
|
|
|
|
|
|
require 'minitest/hell'
|
|
|
|
|
class Minitest::Test
|
|
|
|
|
parallelize_me!
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class Module
|
|
|
|
|
include Minitest::Spec::DSL
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
module FunctionalHelper
|
2018-11-08 12:00:14 -05:00
|
|
|
|
let(:repo_path) do
|
|
|
|
|
path = File.expand_path(File.join( __FILE__, '..', '..', '..'))
|
|
|
|
|
# fix for vagrant repo pathing
|
|
|
|
|
path.gsub!('//vboxsrv', 'C:') if is_windows?
|
|
|
|
|
path
|
|
|
|
|
end
|
2016-03-25 01:31:19 +01:00
|
|
|
|
let(:exec_inspec) { File.join(repo_path, 'bin', 'inspec') }
|
Plugins API v2: Loader, Base API, and Test Harness (#3278)
* Functional tests for userdir option
* Accepts --config-dir CLI option
* Actually loads a config file from the config dir, more cases to test
* Able to load config and verify contents from config-dir
* Functional tests to ensure precedence for config options
* Enable setting config dir via env var
* .inspec, not .inspec.d
* Begin converting PluginCtl to PluginLoader/Registry
* Able to load and partially validate the plugins.json file
* More work on the plugin loader
* Break the world, move next gen stuff to plugin/
* Be sure to require base cli in bundled plugins
* Move test file
* Revert changes to v1 plugin, so we can have a separate one
* Checkpoint commit
* Move v2 plugin work to v2 area
* Move plugins v1 code into an isolated directory
* rubocop fixes
* Rip out the stuff about a user-dir config file, just use a plugin file
* Two psuedocode test file
* Working base API, moock plugin type, and loader.
* Adjust load path to be more welcoming
* Silence circular depencency warning, which was breaking a unit test
* Linting
* Fix plugin type registry, add tests to cover
* Feedback from Jerry
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
2018-08-16 18:16:32 -04:00
|
|
|
|
let(:mock_path) { File.join(repo_path, 'test', 'unit', 'mock') }
|
|
|
|
|
let(:profile_path) { File.join(mock_path, 'profiles') }
|
2016-03-25 01:31:19 +01:00
|
|
|
|
let(:examples_path) { File.join(repo_path, 'examples') }
|
2017-08-15 20:41:24 +02:00
|
|
|
|
let(:integration_test_path) { File.join(repo_path, 'test', 'integration', 'default') }
|
2016-03-25 01:31:19 +01:00
|
|
|
|
|
|
|
|
|
let(:example_profile) { File.join(examples_path, 'profile') }
|
2018-09-06 20:28:08 -07:00
|
|
|
|
let(:meta_profile) { File.join(examples_path, 'meta-profile') }
|
2016-04-20 00:50:18 -04:00
|
|
|
|
let(:example_control) { File.join(example_profile, 'controls', 'example.rb') }
|
2018-09-06 20:28:08 -07:00
|
|
|
|
let(:inheritance_profile) { File.join(examples_path, 'inheritance') }
|
2016-09-01 08:25:56 -04:00
|
|
|
|
let(:failure_control) { File.join(profile_path, 'failures', 'controls', 'failures.rb') }
|
2016-09-01 18:54:03 -04:00
|
|
|
|
let(:simple_inheritance) { File.join(profile_path, 'simple-inheritance') }
|
2017-08-25 16:21:49 -04:00
|
|
|
|
let(:sensitive_profile) { File.join(examples_path, 'profile-sensitive') }
|
Plugins API v2: Loader, Base API, and Test Harness (#3278)
* Functional tests for userdir option
* Accepts --config-dir CLI option
* Actually loads a config file from the config dir, more cases to test
* Able to load config and verify contents from config-dir
* Functional tests to ensure precedence for config options
* Enable setting config dir via env var
* .inspec, not .inspec.d
* Begin converting PluginCtl to PluginLoader/Registry
* Able to load and partially validate the plugins.json file
* More work on the plugin loader
* Break the world, move next gen stuff to plugin/
* Be sure to require base cli in bundled plugins
* Move test file
* Revert changes to v1 plugin, so we can have a separate one
* Checkpoint commit
* Move v2 plugin work to v2 area
* Move plugins v1 code into an isolated directory
* rubocop fixes
* Rip out the stuff about a user-dir config file, just use a plugin file
* Two psuedocode test file
* Working base API, moock plugin type, and loader.
* Adjust load path to be more welcoming
* Silence circular depencency warning, which was breaking a unit test
* Linting
* Fix plugin type registry, add tests to cover
* Feedback from Jerry
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
2018-08-16 18:16:32 -04:00
|
|
|
|
let(:config_dir_path) { File.join(mock_path, 'config_dirs') }
|
2016-03-25 01:31:19 +01:00
|
|
|
|
|
|
|
|
|
let(:dst) {
|
|
|
|
|
# create a temporary path, but we only want an auto-clean helper
|
|
|
|
|
# so remove the file and give back the path
|
|
|
|
|
res = Tempfile.new('inspec-shred')
|
2018-09-13 17:19:02 -07:00
|
|
|
|
res.close
|
2016-03-25 01:31:19 +01:00
|
|
|
|
FileUtils.rm(res.path)
|
|
|
|
|
TMP_CACHE[res.path] = res
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 18:04:16 -04:00
|
|
|
|
def convert_windows_output(text)
|
|
|
|
|
text = text.force_encoding("UTF-8")
|
|
|
|
|
text.gsub!("[PASS]", '✔')
|
|
|
|
|
text.gsub!("\033[0;1;32m", "\033[38;5;41m")
|
|
|
|
|
text.gsub!("[SKIP]", '↺')
|
|
|
|
|
text.gsub!("\033[0;37m", "\033[38;5;247m")
|
|
|
|
|
text.gsub!("[FAIL]", '×')
|
|
|
|
|
text.gsub!("\033[0;1;31m", "\033[38;5;9m")
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-08 12:00:14 -05:00
|
|
|
|
def self.is_windows?
|
2018-09-12 18:04:16 -04:00
|
|
|
|
RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-08 12:00:14 -05:00
|
|
|
|
def is_windows?
|
|
|
|
|
FunctionalHelper.is_windows?
|
|
|
|
|
end
|
|
|
|
|
|
2016-11-09 08:41:48 -06:00
|
|
|
|
def inspec(commandline, prefix = nil)
|
2018-09-12 18:04:16 -04:00
|
|
|
|
if is_windows?
|
|
|
|
|
result = CMD.run_command("cmd /C \"#{prefix} bundle exec ruby #{exec_inspec} #{commandline}\"")
|
|
|
|
|
result.stdout.encode!(universal_newline: true)
|
|
|
|
|
result.stderr.encode!(universal_newline: true)
|
|
|
|
|
convert_windows_output(result.stdout)
|
|
|
|
|
# remove the CLIXML header trash in windows
|
|
|
|
|
result.stderr.gsub!("#< CLIXML\n", '')
|
|
|
|
|
result
|
|
|
|
|
else
|
|
|
|
|
CMD.run_command("#{prefix} #{exec_inspec} #{commandline}")
|
|
|
|
|
end
|
2016-03-25 01:31:19 +01:00
|
|
|
|
end
|
2017-06-12 14:01:26 +02:00
|
|
|
|
|
Plugins API v2: Loader, Base API, and Test Harness (#3278)
* Functional tests for userdir option
* Accepts --config-dir CLI option
* Actually loads a config file from the config dir, more cases to test
* Able to load config and verify contents from config-dir
* Functional tests to ensure precedence for config options
* Enable setting config dir via env var
* .inspec, not .inspec.d
* Begin converting PluginCtl to PluginLoader/Registry
* Able to load and partially validate the plugins.json file
* More work on the plugin loader
* Break the world, move next gen stuff to plugin/
* Be sure to require base cli in bundled plugins
* Move test file
* Revert changes to v1 plugin, so we can have a separate one
* Checkpoint commit
* Move v2 plugin work to v2 area
* Move plugins v1 code into an isolated directory
* rubocop fixes
* Rip out the stuff about a user-dir config file, just use a plugin file
* Two psuedocode test file
* Working base API, moock plugin type, and loader.
* Adjust load path to be more welcoming
* Silence circular depencency warning, which was breaking a unit test
* Linting
* Fix plugin type registry, add tests to cover
* Feedback from Jerry
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
2018-08-16 18:16:32 -04:00
|
|
|
|
def inspec_with_env(commandline, env = {})
|
|
|
|
|
# CMD is a train transport, and does not support anything other than a
|
|
|
|
|
# single param for the command line.
|
|
|
|
|
# TODO: what is the intent of using Train here?
|
|
|
|
|
# HACK: glue together env vars
|
2018-09-12 18:04:16 -04:00
|
|
|
|
if is_windows?
|
|
|
|
|
env_prefix = env.to_a.map { |assignment| "set #{assignment[0]}=#{assignment[1]}" }.join('&& ')
|
|
|
|
|
env_prefix += '&& '
|
|
|
|
|
else
|
|
|
|
|
env_prefix = env.to_a.map { |assignment| "#{assignment[0]}=#{assignment[1]}" }.join(' ')
|
|
|
|
|
env_prefix += ' '
|
|
|
|
|
end
|
|
|
|
|
inspec(commandline, env_prefix)
|
Plugins API v2: Loader, Base API, and Test Harness (#3278)
* Functional tests for userdir option
* Accepts --config-dir CLI option
* Actually loads a config file from the config dir, more cases to test
* Able to load config and verify contents from config-dir
* Functional tests to ensure precedence for config options
* Enable setting config dir via env var
* .inspec, not .inspec.d
* Begin converting PluginCtl to PluginLoader/Registry
* Able to load and partially validate the plugins.json file
* More work on the plugin loader
* Break the world, move next gen stuff to plugin/
* Be sure to require base cli in bundled plugins
* Move test file
* Revert changes to v1 plugin, so we can have a separate one
* Checkpoint commit
* Move v2 plugin work to v2 area
* Move plugins v1 code into an isolated directory
* rubocop fixes
* Rip out the stuff about a user-dir config file, just use a plugin file
* Two psuedocode test file
* Working base API, moock plugin type, and loader.
* Adjust load path to be more welcoming
* Silence circular depencency warning, which was breaking a unit test
* Linting
* Fix plugin type registry, add tests to cover
* Feedback from Jerry
Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
2018-08-16 18:16:32 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-06-12 14:01:26 +02:00
|
|
|
|
# Copy all examples to a temporary directory for functional tests.
|
|
|
|
|
# You can provide an optional directory which will be handed to your
|
|
|
|
|
# test block with its absolute path. If nothing is provided you will
|
|
|
|
|
# get the path of the examples directory in the tmp environment.
|
|
|
|
|
#
|
|
|
|
|
# @param dir = nil [String] optional directory you want to test
|
|
|
|
|
# @param &block [Type] actual test block
|
|
|
|
|
def prepare_examples(dir = nil, &block)
|
|
|
|
|
Dir.mktmpdir do |tmpdir|
|
|
|
|
|
FileUtils.cp_r(examples_path, tmpdir)
|
|
|
|
|
bn = File.basename(examples_path)
|
|
|
|
|
block.call(File.join(tmpdir, bn, dir.to_s))
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-03-25 01:31:19 +01:00
|
|
|
|
end
|