inspec 2.2.112 → 2.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +8 -2
- data/CHANGELOG.md +42 -19
- data/README.md +1 -1
- data/Rakefile +16 -3
- data/docs/dev/integration-testing.md +31 -0
- data/docs/dev/plugins.md +4 -2
- data/docs/dsl_inspec.md +104 -4
- data/docs/plugins.md +57 -0
- data/docs/resources/aws_ebs_volume.md.erb +76 -0
- data/docs/resources/aws_ebs_volumes.md.erb +86 -0
- data/docs/style.md +178 -0
- data/examples/plugins/inspec-resource-lister/Gemfile +12 -0
- data/examples/plugins/inspec-resource-lister/LICENSE +13 -0
- data/examples/plugins/inspec-resource-lister/README.md +62 -0
- data/examples/plugins/inspec-resource-lister/Rakefile +40 -0
- data/examples/plugins/inspec-resource-lister/inspec-resource-lister.gemspec +45 -0
- data/examples/plugins/inspec-resource-lister/lib/inspec-resource-lister.rb +16 -0
- data/examples/plugins/inspec-resource-lister/lib/inspec-resource-lister/cli_command.rb +70 -0
- data/examples/plugins/inspec-resource-lister/lib/inspec-resource-lister/plugin.rb +55 -0
- data/examples/plugins/inspec-resource-lister/lib/inspec-resource-lister/version.rb +10 -0
- data/examples/plugins/inspec-resource-lister/test/fixtures/README.md +24 -0
- data/examples/plugins/inspec-resource-lister/test/functional/README.md +18 -0
- data/examples/plugins/inspec-resource-lister/test/functional/inspec_resource_lister_test.rb +110 -0
- data/examples/plugins/inspec-resource-lister/test/helper.rb +26 -0
- data/examples/plugins/inspec-resource-lister/test/unit/README.md +17 -0
- data/examples/plugins/inspec-resource-lister/test/unit/cli_args_test.rb +64 -0
- data/examples/plugins/inspec-resource-lister/test/unit/plugin_def_test.rb +51 -0
- data/examples/profile/controls/example.rb +9 -8
- data/inspec.gemspec +2 -1
- data/lib/inspec/attribute_registry.rb +1 -1
- data/lib/inspec/globals.rb +4 -0
- data/lib/inspec/objects/control.rb +18 -3
- data/lib/inspec/plugin/v2.rb +14 -3
- data/lib/inspec/plugin/v2/activator.rb +7 -2
- data/lib/inspec/plugin/v2/installer.rb +426 -0
- data/lib/inspec/plugin/v2/loader.rb +137 -30
- data/lib/inspec/plugin/v2/registry.rb +13 -4
- data/lib/inspec/profile.rb +2 -1
- data/lib/inspec/reporters/json.rb +11 -1
- data/lib/inspec/resource.rb +6 -15
- data/lib/inspec/rule.rb +18 -9
- data/lib/inspec/runner_rspec.rb +1 -1
- data/lib/inspec/schema.rb +1 -0
- data/lib/inspec/version.rb +1 -1
- data/lib/plugins/inspec-plugin-manager-cli/README.md +6 -0
- data/lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli.rb +18 -0
- data/lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli/cli_command.rb +420 -0
- data/lib/plugins/inspec-plugin-manager-cli/lib/inspec-plugin-manager-cli/plugin.rb +12 -0
- data/lib/plugins/inspec-plugin-manager-cli/test/fixtures/config_dirs/empty/.gitkeep +0 -0
- data/lib/plugins/inspec-plugin-manager-cli/test/fixtures/plugins/inspec-egg-white-omelette/lib/inspec-egg-white-omelette.rb +2 -0
- data/lib/plugins/inspec-plugin-manager-cli/test/fixtures/plugins/inspec-egg-white-omelette/lib/inspec-egg-white-omelette/.gitkeep +0 -0
- data/lib/plugins/inspec-plugin-manager-cli/test/fixtures/plugins/inspec-wrong-structure/.gitkeep +0 -0
- data/lib/plugins/inspec-plugin-manager-cli/test/fixtures/plugins/wrong-name/lib/wrong-name.rb +1 -0
- data/lib/plugins/inspec-plugin-manager-cli/test/fixtures/plugins/wrong-name/lib/wrong-name/.gitkeep +0 -0
- data/lib/plugins/inspec-plugin-manager-cli/test/functional/inspec-plugin_test.rb +651 -0
- data/lib/plugins/inspec-plugin-manager-cli/test/unit/cli_args_test.rb +71 -0
- data/lib/plugins/inspec-plugin-manager-cli/test/unit/plugin_def_test.rb +20 -0
- data/lib/plugins/shared/core_plugin_test_helper.rb +101 -2
- data/lib/plugins/things-for-train-integration.rb +14 -0
- data/lib/resource_support/aws.rb +2 -0
- data/lib/resources/aws/aws_ebs_volume.rb +122 -0
- data/lib/resources/aws/aws_ebs_volumes.rb +63 -0
- data/lib/resources/port.rb +10 -6
- metadata +56 -11
- data/docs/ruby_usage.md +0 -204
@@ -0,0 +1,70 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'inspec/resource'
|
4
|
+
|
5
|
+
module InspecPlugins::ResourceLister
|
6
|
+
# This class will provide the actual CLI implementation.
|
7
|
+
# Its superclass is provided by another call to Inspec.plugin,
|
8
|
+
# this time with two args. The first arg specifies we are requesting
|
9
|
+
# version 2 of the Plugins API. The second says we are making a CLI
|
10
|
+
# Command plugin component, so please make available any DSL needed
|
11
|
+
# for that.
|
12
|
+
# In fact, aside from a some housekeeping DSL methods, most of the
|
13
|
+
# DSL provided is that of Thor. Inspec.plugin(2, :cli_command)
|
14
|
+
# promises to return a class that is a subclass of Thor. So, to add
|
15
|
+
# commands, usage information, and options, use the Thor documentation.
|
16
|
+
class CliCommand < Inspec.plugin(2, :cli_command)
|
17
|
+
# This isn't provided by Thor, but is needed by InSpec so that it can
|
18
|
+
# register the subcommand. Args are a usage message, and a short decription.
|
19
|
+
# These will appear when someone has installed the plugin, and then they
|
20
|
+
# run `inspec help`.
|
21
|
+
subcommand_desc 'list-resources [COMMAND]', 'List resources that InSpec finds.'
|
22
|
+
|
23
|
+
# The usual rhythm for a Thor CLI file is description, options, command method.
|
24
|
+
# Thor just has you call DSL methods in sequence prior to each command.
|
25
|
+
# Let's make a command, 'core', that lists all of the resources included with InSpec.
|
26
|
+
|
27
|
+
# First, provide a usage / description. This will appear in `inspec help list-resources`.
|
28
|
+
desc 'core [OPTIONS]', 'List resources that are included with InSpec.'
|
29
|
+
|
30
|
+
# Let's include an option, -s, to summarize the list.
|
31
|
+
# Refer to the Thors docs; there is a lot you can do here.
|
32
|
+
option :summary, desc: 'Include a total at the bottom', \
|
33
|
+
type: :boolean, default: true, aliases: [:s]
|
34
|
+
|
35
|
+
# OK, now the actual method itself. If you provide params, you're telling Thor that
|
36
|
+
# you accept CLI arguments after all options have been consumed. Let's accept a
|
37
|
+
# pattern, assumed to be a wildcard substring. If we provide a default, the CLI arg becomes optional.
|
38
|
+
def core(pattern = /.+/)
|
39
|
+
# The code here will *only* be executed if someone actually runs
|
40
|
+
# `inspec list-resources core`. So, we can lazily wait to load
|
41
|
+
# expensive things here. However, InSpec has in fact already
|
42
|
+
# loaded the Resources, so we don't have anything to load.
|
43
|
+
|
44
|
+
# If we were passed a CLI arg, wrap the arg in Regexp matchers so
|
45
|
+
# we will match anywhere in the name.
|
46
|
+
unless pattern == /.+/
|
47
|
+
pattern = Regexp.new('.*' + pattern + '.*')
|
48
|
+
end
|
49
|
+
|
50
|
+
# This gets a bit into InSpec innards; but this is simply a Hash.
|
51
|
+
registry = Inspec::Resource.default_registry
|
52
|
+
resource_names = registry.keys.grep(pattern).sort
|
53
|
+
|
54
|
+
# One day we'll have nice UI support.
|
55
|
+
resource_names.each { |name| puts name }
|
56
|
+
|
57
|
+
if options[:summary]
|
58
|
+
puts '-' * 30
|
59
|
+
puts "#{resource_names.count} resources total"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# A neat idea for future work would be to add another command, perhaps
|
64
|
+
# 'resource-pack', which examines a possibly remote resource pack and
|
65
|
+
# enumerates the resources it defines.
|
66
|
+
|
67
|
+
# Another idea might be to fetch a profile, and list the resources actually
|
68
|
+
# used in the controls of the profile, along with counts.
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# Plugin Definition file
|
4
|
+
# The purpose of this file is to declare to InSpec what plugin_types (capabilities)
|
5
|
+
# are included in this plugin, and provide hooks that will load them as needed.
|
6
|
+
|
7
|
+
# It is important that this file load successfully and *quickly*.
|
8
|
+
# Your plugin's functionality may never be used on this InSpec run; so we keep things
|
9
|
+
# fast and light by only loading heavy things when they are needed.
|
10
|
+
|
11
|
+
# Presumably this is light
|
12
|
+
require 'inspec-resource-lister/version'
|
13
|
+
|
14
|
+
# The InspecPlugins namespace is where all plugins should declare themselves.
|
15
|
+
# The 'Inspec' capitalization is used throughout the InSpec source code; yes, it's
|
16
|
+
# strange.
|
17
|
+
module InspecPlugins
|
18
|
+
# Pick a reasonable namespace here for your plugin. A reasonable choice
|
19
|
+
# would be the CamelCase version of your plugin gem name.
|
20
|
+
# inspec-resource-lister => ResourceLister
|
21
|
+
module ResourceLister
|
22
|
+
# This simple class handles the plugin definition, so calling it simply Plugin is OK.
|
23
|
+
# Inspec.plugin returns various Classes, intended to be superclasses for various
|
24
|
+
# plugin components. Here, the one-arg form gives you the Plugin Definition superclass,
|
25
|
+
# which mainly gives you access to the hook / plugin_type DSL.
|
26
|
+
# The number '2' says you are asking for version 2 of the plugin API. If there are
|
27
|
+
# future versions, InSpec promises plugin API v2 will work for at least two more InSpec
|
28
|
+
# major versions.
|
29
|
+
class Plugin < ::Inspec.plugin(2)
|
30
|
+
# Internal machine name of the plugin. InSpec will use this in errors, etc.
|
31
|
+
plugin_name :'inspec-resource-lister'
|
32
|
+
|
33
|
+
# Define a new CLI subcommand.
|
34
|
+
# The argument here will be used to match against the command line args,
|
35
|
+
# and if the user said `inspec list-resources`, this hook will get called.
|
36
|
+
# Notice that you can define multiple hooks with different names, and they
|
37
|
+
# don't have to match the plugin name.
|
38
|
+
|
39
|
+
# We'd like this to be list-resources, but Thor does not support hyphens
|
40
|
+
# see https://github.com/erikhuda/thor/pull/613
|
41
|
+
cli_command :listresources do
|
42
|
+
# Calling this hook doesn't mean list-resources is being executed - just
|
43
|
+
# that we should be ready to do so. So, load the file that defines the
|
44
|
+
# functionality.
|
45
|
+
# For example, InSpec will activate this hook when `inspec help` is
|
46
|
+
# executed, so that this plugin's usage message will be included in the help.
|
47
|
+
require 'inspec-resource-lister/cli_command'
|
48
|
+
|
49
|
+
# Having loaded our functionality, return a class that will let the
|
50
|
+
# CLI engine tap into it.
|
51
|
+
InspecPlugins::ResourceLister::CliCommand
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# This file simply makes it easier for CI engines to update
|
4
|
+
# the version stamp, and provide a clean way for the gemspec
|
5
|
+
# to learn the current version.
|
6
|
+
module InspecPlugins
|
7
|
+
module ResourceLister
|
8
|
+
VERSION = '0.1.0'.freeze
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Test Fixtures Area
|
2
|
+
|
3
|
+
In this directory, you would place things that you need during testing. For example, if you were making a plugin that counts the number of controls in a profile, you might have a directory tree like this:
|
4
|
+
|
5
|
+
```
|
6
|
+
fixtures/
|
7
|
+
profiles/
|
8
|
+
zero-controls/
|
9
|
+
inspec.yml
|
10
|
+
controls/
|
11
|
+
twelve-controls/
|
12
|
+
inspec.yml
|
13
|
+
controls/
|
14
|
+
nine.rb
|
15
|
+
three.rb
|
16
|
+
```
|
17
|
+
|
18
|
+
When writing your functional tests, you can point InSpec at the various test fixture profiles, and know that when it points at the zero-controls profile, it should find no controls; and when pointed at the twelve-controls profile, it should find 12.
|
19
|
+
|
20
|
+
## Using test fixtures provided with core inspec
|
21
|
+
|
22
|
+
InSpec itself ships with many test fixtures - not just profiles, but attribute files, configuration directories, and more. Examine them at [the fixtures directory](https://github.com/inspec/inspec/tree/master/test/unit/mock)
|
23
|
+
|
24
|
+
To use them, see the helper.rb file included in the example at test/helper.rb .
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Functional Testing Area for Example Plugins
|
2
|
+
|
3
|
+
## What example tests are provided?
|
4
|
+
|
5
|
+
Here, since this is a CliCommand plugin, we provide one set of functional tests:
|
6
|
+
|
7
|
+
* inspec_resource_lister_test.rb - Runs `inspec resource-lister` in several circumstances, and verifies the output from the process.
|
8
|
+
|
9
|
+
## What are functional tests?
|
10
|
+
|
11
|
+
Functional tests are tests that verify that your plugin works _as would be seen by a user_. Functional tests generally do not have inside knowledge about the inner workings of the plugin. However a functional test is very interested in changes that you plugin make to the outside world: exit codes, command output, changes to files on the filesystem, etc.
|
12
|
+
|
13
|
+
To be picked up by the Rake tasks as tests, each test file should end in `_test.rb`.
|
14
|
+
|
15
|
+
## Unit vs Functional Tests
|
16
|
+
|
17
|
+
A practical difference between unit tests and functional tests is that unit tests all run within one process, while functional tests might exercise a CLI plugin by shelling out to an inspec command in a subprocess, and examining the results.
|
18
|
+
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# Functional Tests for Example Plugin, Resource Lister
|
2
|
+
|
3
|
+
# Functional tests are used to verify the behavior of the plugin are as expected, to a user.
|
4
|
+
# Functional tests generally do not have inside knowledge of how the plugin works.
|
5
|
+
|
6
|
+
# Include our test harness
|
7
|
+
require_relative '../helper'
|
8
|
+
|
9
|
+
# Because InSpec is a Spec-style test suite, we're going to use MiniTest::Spec
|
10
|
+
# here, for familiar look and feel. However, this isn't InSpec (or RSpec) code.
|
11
|
+
describe 'inspec list-resources core' do
|
12
|
+
# Our helper.rb locates this library from the InSpec install that
|
13
|
+
# Bundler installed for us. If we want its methods, we still must
|
14
|
+
# import it. Including it here will make it available in all child
|
15
|
+
# 'describe' blocks.
|
16
|
+
include CorePluginFunctionalHelper
|
17
|
+
|
18
|
+
# When thinking up scenarios to test, start with the simplest.
|
19
|
+
# Then think of each major feature, and exercise them.
|
20
|
+
# Running combinations of features makes sense if it is very likely,
|
21
|
+
# or a difficult / dangerous case. You can always add more tests
|
22
|
+
# here as users find subtle problems. In fact, having a user submit
|
23
|
+
# a PR that creates a failing functional test is a great way to
|
24
|
+
# capture the reproduction case.
|
25
|
+
# The simplest case:
|
26
|
+
describe "when run without an argument" do
|
27
|
+
|
28
|
+
# run_inspec_process_with_this_plugin is a helper provided by
|
29
|
+
# CoreFunctionalHelper. It makes the InSpec that Bundler installed
|
30
|
+
# think that this plugin we are currently testing is installed as a
|
31
|
+
# user plugin, by writing a plugin config file in a temp dir.
|
32
|
+
# To use it, just provide a command line, minus the word `inspec`.
|
33
|
+
let (:outcome) { run_inspec_process_with_this_plugin('listresources core') }
|
34
|
+
|
35
|
+
# Some tests through here use minitest Expectations, which attach to all
|
36
|
+
# Objects, and begin with 'must' (positive) or 'wont' (negative)
|
37
|
+
# See https://ruby-doc.org/stdlib-2.1.0/libdoc/minitest/rdoc/MiniTest/Expectations.html
|
38
|
+
it("should exit successfully") { outcome.exit_status.must_equal(0) }
|
39
|
+
it("should be silent on stderr") { outcome.stderr.must_be_empty }
|
40
|
+
|
41
|
+
# A selection of core resources, just spot checking.
|
42
|
+
# This is an example of using Ruby to define sets of tests.
|
43
|
+
['process', 'service', 'user', 'file'].each do |resource_name|
|
44
|
+
it "should mention the '#{resource_name}' resource" do
|
45
|
+
outcome.stdout.must_include(resource_name)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Check for the summary
|
50
|
+
it "should mention the summary" do
|
51
|
+
outcome.stdout.must_include('resources total')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Test the search pattern feature, in a couple of ways.
|
56
|
+
describe "when run with a search pattern that matches things" do
|
57
|
+
# Notice that the command line is changed here:
|
58
|
+
# "list all resources that have the word user in them"
|
59
|
+
let (:outcome) { run_inspec_process_with_this_plugin('listresources core user') }
|
60
|
+
|
61
|
+
# Should be well-behaved...
|
62
|
+
it("should exit successfully") { outcome.exit_status.must_equal(0) }
|
63
|
+
it("should be silent on stderr") { outcome.stderr.must_be_empty }
|
64
|
+
|
65
|
+
# Here, we want to know it DID match some things, and NOT some others.
|
66
|
+
['user', 'users'].each do |resource_name|
|
67
|
+
it "should mention the '#{resource_name}' resource" do
|
68
|
+
outcome.stdout.must_include(resource_name)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
['process', 'service', 'file'].each do |resource_name|
|
72
|
+
it "should NOT mention the '#{resource_name}' resource" do
|
73
|
+
outcome.stdout.wont_include(resource_name)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
describe "when run with a search pattern that matches nothing" do
|
78
|
+
# Unlikely we'll have a resource with the string 'autogyro' in it.
|
79
|
+
let (:outcome) { run_inspec_process_with_this_plugin('listresources core autogyro') }
|
80
|
+
|
81
|
+
# Should be well-behaved...
|
82
|
+
it("should exit successfully") { outcome.exit_status.must_equal(0) }
|
83
|
+
it("should be silent on stderr") { outcome.stderr.must_be_empty }
|
84
|
+
|
85
|
+
# Output lines should be just two, for the summary.
|
86
|
+
it "should only have two output lines" do
|
87
|
+
outcome.stdout.split("\n").count.must_equal(2)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Check for the summary
|
91
|
+
it "should mention a zero-resource summary" do
|
92
|
+
outcome.stdout.must_include('0 resources total')
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# Exercise the summary option, which defaults to 'true'.
|
97
|
+
describe "when run with the no-summary flag" do
|
98
|
+
# Alter the command string to include the no-summary option
|
99
|
+
let(:outcome) { run_inspec_process_with_this_plugin('listresources core --no-summary') }
|
100
|
+
|
101
|
+
# Should be well-behaved...
|
102
|
+
it("should exit successfully") { outcome.exit_status.must_equal(0) }
|
103
|
+
it("should be silent on stderr") { outcome.stderr.must_be_empty }
|
104
|
+
|
105
|
+
# Check for the summary
|
106
|
+
it "should NOT mention summary" do
|
107
|
+
outcome.stdout.wont_include('0 resources total')
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Test helper file for example plugins
|
2
|
+
|
3
|
+
# This file's job is to collect any libraries needed for testing, as well as provide
|
4
|
+
# any utilities to make testing a plugin easier.
|
5
|
+
|
6
|
+
# InSpec core provides a number of such libraries and facilities, in the file
|
7
|
+
# lib/pligins/shared/core_plugin_test_helper.rb . So, one job in this file is
|
8
|
+
# to locate and load that file.
|
9
|
+
require 'inspec/../plugins/shared/core_plugin_test_helper'
|
10
|
+
|
11
|
+
# Also load the InSpec plugin system. We need this so we can unit-test the plugin
|
12
|
+
# classes, which will rely on the plugin system.
|
13
|
+
require 'inspec/plugin/v2'
|
14
|
+
|
15
|
+
# Caution: loading all of InSpec (i.e. require 'inspec') may cause interference with
|
16
|
+
# minitest/spec; one symptom would be appearing to have no tests.
|
17
|
+
# See https://github.com/inspec/inspec/issues/3380
|
18
|
+
|
19
|
+
# You can select from a number of test harnesses. Since InSpec uses Spec-style controls
|
20
|
+
# in profile code, you will probably want to use something like minitest/spec, which provides
|
21
|
+
# Spec-style tests.
|
22
|
+
require 'minitest/spec'
|
23
|
+
require 'minitest/autorun'
|
24
|
+
|
25
|
+
# You might want to put some debugging tools here. We run tests to find bugs, after all.
|
26
|
+
require 'byebug'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Unit Testing Area for Example Plugins
|
2
|
+
|
3
|
+
## What Example Tests are Provided?
|
4
|
+
|
5
|
+
Here, since this is a CliCommand plugin, we provide two sets of unit tests:
|
6
|
+
|
7
|
+
* plugin_def_test.rb - Would be useful in any plugin. Verifies that the plugin is properly detected and registered.
|
8
|
+
* cli_args_test.rb - Verifies that the expected commands are present, and that they have the expected options and args.
|
9
|
+
|
10
|
+
## What are Unit Tests?
|
11
|
+
|
12
|
+
Unit tests are tests that verify that the individual components of your plugin work as intended. To be picked up by the Rake tasks as tests, each test file should end in `_test.rb`.
|
13
|
+
|
14
|
+
## Unit vs Functional Tests
|
15
|
+
|
16
|
+
A practical difference between unit tests and functional tests is that unit tests all run within one process, while functional tests might exercise a CLI plugin by shelling out to an inspec command in a subprocess, and examining the results.
|
17
|
+
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# This unit test performs some tests to verify that the command line options for
|
2
|
+
# inspec-resource-lister are correct.
|
3
|
+
|
4
|
+
# Include our test harness
|
5
|
+
require_relative '../helper'
|
6
|
+
|
7
|
+
# Load the class under test, the CliCommand definition.
|
8
|
+
require 'inspec-resource-lister/cli_command'
|
9
|
+
|
10
|
+
# Because InSpec is a Spec-style test suite, we're going to use MiniTest::Spec
|
11
|
+
# here, for familiar look and feel. However, this isn't InSpec (or RSpec) code.
|
12
|
+
describe InspecPlugins::ResourceLister::CliCommand do
|
13
|
+
|
14
|
+
# When writing tests, you can use `let` to create variables that you
|
15
|
+
# can reference easily.
|
16
|
+
|
17
|
+
# This is the CLI Command implementation class.
|
18
|
+
# It is a subclass of Thor, which is a CLI framework.
|
19
|
+
# This unit test file is mostly about verifying the Thor settings.
|
20
|
+
let(:cli_class) { InspecPlugins::ResourceLister::CliCommand }
|
21
|
+
|
22
|
+
# This is a Hash of Structs that tells us details of options for the 'core' subcommand.
|
23
|
+
let(:core_options) { cli_class.all_commands['core'].options }
|
24
|
+
|
25
|
+
# To group tests together, you can nest 'describe' in minitest/spec
|
26
|
+
# (that is discouraged in InSpec control code.)
|
27
|
+
describe 'the core command' do
|
28
|
+
|
29
|
+
# Some tests through here use minitest Expectations, which attach to all
|
30
|
+
# Objects, and begin with 'must' (positive) or 'wont' (negative)
|
31
|
+
# See https://ruby-doc.org/stdlib-2.1.0/libdoc/minitest/rdoc/MiniTest/Expectations.html
|
32
|
+
|
33
|
+
# Option count OK?
|
34
|
+
it "should take one option" do
|
35
|
+
core_options.count.must_equal(1)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Summary option
|
39
|
+
describe "the summary option" do
|
40
|
+
it "should be present" do
|
41
|
+
core_options.keys.must_include(:summary)
|
42
|
+
end
|
43
|
+
it "should have a description" do
|
44
|
+
core_options[:summary].description.wont_be_nil
|
45
|
+
end
|
46
|
+
it "should not be required" do
|
47
|
+
core_options[:summary].required.wont_equal(true)
|
48
|
+
end
|
49
|
+
it "should have a single-letter alias" do
|
50
|
+
core_options[:summary].aliases.must_include(:s)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Argument count
|
55
|
+
# The 'core' command takes one optional argument. According to the
|
56
|
+
# metaprogramming rules of Ruby, the core() method should thus have an
|
57
|
+
# arity of -1. See http://ruby-doc.org/core-2.5.1/Method.html#method-i-arity
|
58
|
+
# for how that number is caclulated.
|
59
|
+
it "should take one optional argument" do
|
60
|
+
cli_class.instance_method(:core).arity.must_equal(-1)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# This unit test performs some tests to verify that
|
2
|
+
# the inspec-resource-lister plugin is configured correctly.
|
3
|
+
|
4
|
+
# Include our test harness
|
5
|
+
require_relative '../helper'
|
6
|
+
|
7
|
+
# Load the class under test, the Plugin definition.
|
8
|
+
require 'inspec-resource-lister/plugin'
|
9
|
+
|
10
|
+
# Because InSpec is a Spec-style test suite, we're going to use MiniTest::Spec
|
11
|
+
# here, for familiar look and feel. However, this isn't InSpec (or RSpec) code.
|
12
|
+
|
13
|
+
describe InspecPlugins::ResourceLister::Plugin do
|
14
|
+
|
15
|
+
# When writing tests, you can use `let` to create variables that you
|
16
|
+
# can reference easily.
|
17
|
+
|
18
|
+
# Internally, plugins are always known by a Symbol name. Convert here.
|
19
|
+
let(:plugin_name) { :'inspec-resource-lister' }
|
20
|
+
|
21
|
+
# The Registry knows about all plugins that ship with InSpec by
|
22
|
+
# default, as well as any that are installed by the user. When a
|
23
|
+
# plugin definition is loaded, it will also self-register.
|
24
|
+
let(:registry) { Inspec::Plugin::V2::Registry.instance }
|
25
|
+
|
26
|
+
# The plugin status record tells us what the Registry knows.
|
27
|
+
# Note that you can use previously-defined 'let's.
|
28
|
+
let(:status) { registry[plugin_name] }
|
29
|
+
|
30
|
+
# OK, actual tests!
|
31
|
+
|
32
|
+
# Does the Registry know about us at all?
|
33
|
+
it "should be registered" do
|
34
|
+
registry.known_plugin?(plugin_name)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Some tests through here use minitest Expectations, which attach to all
|
38
|
+
# Objects, and begin with 'must' (positive) or 'wont' (negative)
|
39
|
+
# See https://ruby-doc.org/stdlib-2.1.0/libdoc/minitest/rdoc/MiniTest/Expectations.html
|
40
|
+
|
41
|
+
# The plugin system had an undocumented v1 API; this should be a v2 example.
|
42
|
+
it "should be an api-v2 plugin" do
|
43
|
+
status.api_generation.must_equal(2)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Plugins can support several different activator hooks, each of which has a type.
|
47
|
+
# Since this is (primarily) a CliCommand plugin, we'd expect to see that among our types.
|
48
|
+
it "should include a cli_command activator hook" do
|
49
|
+
status.plugin_types.must_include(:cli_command)
|
50
|
+
end
|
51
|
+
end
|