https://github.com/burtlo/inspec-spec
https://github.com/burtlo/inspec-spec
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/burtlo/inspec-spec
- Owner: burtlo
- Created: 2019-05-25T22:45:14.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-06-02T06:56:11.000Z (about 6 years ago)
- Last Synced: 2025-02-10T20:27:45.061Z (4 months ago)
- Language: Ruby
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# InSpec Spec
This repository explores adding testing to the InSpec controls
that you define within your InSpe Resource Pack.> A Resource Pack is an InSpec profile that defines its own
> resource without any controls that exercise it.InSpec resource tests are written in RSpec with additional
helper content found in the `spec/spec_helper.rb` file to
make the tests easier to compose. The tests are run through
the RSpec binary `rspec`.## Installation
To run the tests you will have to have the RSpec test runner
available to you. RSpec is installed alongside InSpec. You can
run it from where it embedded within your InSpec installation.```bash
$ /opt/inspec/embedded/bin/rspec
```Alternatively, if you have a ruby development environment you can install the
gem version of InSpec using bundler.**Note:** This will install several gems into your ruby environment which can
conflict with a standlone install of InSpec if you don't use something like
chruby or rvm to mange your `GEM_PATH`s```bash
$ bundle install
$ bundle exec rspec
```## Setup
To setup the testing within your profile you will need to create
a `spec` directory and add the `spec/spec_helper.rb` file found
in this repository.Within your `spec/spec_helper.rb` or in the individual `spec` test files
you will need to require all the InSpec resources defined in the profile's
`libraries` directory.```ruby
require 'inspec'
require 'rspec/its'# To test each of your resources, they will need to be required
# to have the InSpec registry know about it.
# You can choose to add them here for all of the spec test files or
# individually in each of the spec test files.
# require 'libraries/ohai.rb'# ... rest of the spec_helper.rb ...
```Now you will define test file for each of your resources. The file
should exist within the `spec` directory and end with `_spec.rb`.For example, to create a test file for an ohai resource, found in
`libraries/ohai.rb`, I would create a file named `spec/ohai_spec.rb`.At the start of this test file you will want to require the contents
of the `spec_helper` file to ensure it loads all the necessary dependencies,
resources defined in your libraries directory, and the helpers defined
in that file.Second, you want to define a `describe_inspec_resource` example group
that sets the stage for the resource testing. Within that group you can arrange
your tests with `describe` and `context` to create the scenarios and
conditions you want to test.Within any context you can define an `environment` block
which defines the various operations and responses that the
InSpec/Train backend may receive. These environments build
on one another within the nested contexts. This helps you
define layers of conditions that build on one another.Examples can be written out in either `its` or `it`. When defining
the `its`, the symbol specified within the `its` is the message
to send to the resource in the context. Within the `it` block
you can express expectations against the InSpec resource under
test, through the `resource` helper.```ruby
require 'spec_helper'
# To test each of your resources, they will need to be required
# to have the InSpec registry know about it, if not required in the spec_helper
# require 'libraries/ohai.rb'describe_inspec_resource 'ohai' do
context 'relying on the automatic path' do
environment do
command('which ohai').returns(stdout: '/path/to/ohai')
command('/path/to/ohai --version').returns(stdout: "Ohai: 14.8.10\n")
endits(:version) { should eq '14.8.10' }
it 'has a version' do
expect(resource.version).to eq('14.8.10')
endcontext 'with no parameters' do
context 'top-level attributes' do
environment do
command('/path/to/ohai').returns(result: {
stdout: '{ "os": "darwin" }', exit_status: 0
})
endit 'are accessible via dot-notation' do
expect(resource.os).to eq('darwin')
end
end
end
end
end
```