This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(SIMP-3430) Create Centralize spec_helpers.rb
- created baseline for spec_helper.rb to do basc RSPEC configuration for SIMP module unit tests. - added some basic methods that were also in spec_helpers - added compliance markup methods for use in compliance markup unit tests. - note that you need compliance_markup in the fixtures file for these helpers to work. SIMP-3430 #comment added simp-spec-helpers
- Loading branch information
1 parent
1c225e0
commit 43691aa
Showing
9 changed files
with
499 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
language: ruby | ||
cache: bundler | ||
sudo: false | ||
before_install: | ||
- rm Gemfile.lock || true | ||
bundler_args: "--without development --path .vendor" | ||
notifications: | ||
email: false | ||
rvm: | ||
- 2.4.5 | ||
env: | ||
- SIMP_SKIP_NON_SIMPOS_TESTS=1 | ||
script: | ||
- bundle exec rake spec | ||
before_deploy: | ||
- bundle exec rake clobber | ||
- "export GEM_VERSION=`ruby -r ./lib/simp/spec_helpers/version.rb -e 'puts Simp::SpecHelpers::VERSION'`" | ||
- '[[ $TRAVIS_TAG =~ ^${GEM_VERSION}$ ]]' | ||
deploy: | ||
- provider: rubygems | ||
gemspec: simp-spec-helpers.gemspec | ||
gem: simp-spec-helpers | ||
api_key: | ||
secure: "need key" | ||
on: | ||
tags: true | ||
rvm: 2.4.5 | ||
condition: "($SKIP_PUBLISH != true)" | ||
- provider: releases | ||
api_key: | ||
secure: "need key" | ||
on: | ||
tags: true | ||
rvm: 2.4.5 | ||
condition: "($SKIP_PUBLISH != true)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# | ||
# SIMP_GEM_SERVERS | a space/comma delimited list of rubygem servers | ||
# PUPPET_VERSION | specifies the version of the puppet gem to load | ||
# FACTER_GEM_VERSION | specifies the version of the facter to load | ||
puppetversion = ENV.fetch('PUPPET_VERSION', '~> 5.5') | ||
gem_sources = ENV.key?('SIMP_GEM_SERVERS') ? ENV['SIMP_GEM_SERVERS'].split(/[, ]+/) : ['https://rubygems.org'] | ||
|
||
gem_sources.each { |gem_source| source gem_source } | ||
|
||
gemspec | ||
|
||
gem 'puppet', puppetversion, :require => false | ||
|
||
if facterversion = ENV['FACTER_GEM_VERSION'] | ||
gem 'facter', facterversion, :require => false | ||
end | ||
|
||
group :test do | ||
gem 'rspec' | ||
gem 'puppetlabs_spec_helper' | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
simp-spec-helpers (0.0.2) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
diff-lcs (1.3) | ||
facter (2.5.7) | ||
fast_gettext (1.1.2) | ||
hiera (3.6.0) | ||
locale (2.1.2) | ||
mocha (1.11.2) | ||
multi_json (1.14.1) | ||
pathspec (0.2.1) | ||
puppet (5.5.18) | ||
facter (> 2.0.1, < 4) | ||
fast_gettext (~> 1.1.2) | ||
hiera (>= 3.2.1, < 4) | ||
locale (~> 2.1) | ||
multi_json (~> 1.10) | ||
puppet-lint (2.4.2) | ||
puppet-syntax (2.6.1) | ||
puppet (>= 5) | ||
rake | ||
puppetlabs_spec_helper (2.14.1) | ||
mocha (~> 1.0) | ||
pathspec (~> 0.2.1) | ||
puppet-lint (~> 2.0) | ||
puppet-syntax (~> 2.0) | ||
rspec-puppet (~> 2.0) | ||
rake (13.0.1) | ||
rspec (3.9.0) | ||
rspec-core (~> 3.9.0) | ||
rspec-expectations (~> 3.9.0) | ||
rspec-mocks (~> 3.9.0) | ||
rspec-core (3.9.1) | ||
rspec-support (~> 3.9.1) | ||
rspec-expectations (3.9.0) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.9.0) | ||
rspec-mocks (3.9.1) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.9.0) | ||
rspec-puppet (2.7.8) | ||
rspec | ||
rspec-support (3.9.2) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
puppet (~> 5.5) | ||
puppetlabs_spec_helper | ||
rspec | ||
simp-spec-helpers! | ||
|
||
BUNDLED WITH | ||
1.17.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,186 @@ | ||
# rubygem-simp-spec-helpers | ||
|
||
rspec-puppet helper methods for SIMP module testing | ||
|
||
[![License](http://img.shields.io/:license-apache-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0.html) | ||
[![Build Status](https://travis-ci.org/simp/rubygem-simp-spec-helpers.svg?branch=master)](https://travis-ci.org/simp/rubygem-simp-spec-helpers) | ||
[![Gem](https://img.shields.io/gem/v/simp-spec-helpers.svg)](https://rubygems.org/gems/simp-spec-helpers) | ||
[![Gem_Downloads](https://img.shields.io/gem/dt/simp-spec-helpers.svg)](https://rubygems.org/gems/simp-spec-helpers) | ||
|
||
#### Table of Contents | ||
|
||
<!-- vim-markdown-toc GFM --> | ||
|
||
* [Overview](#overview) | ||
* [This gem is part of SIMP](#this-gem-is-part-of-simp) | ||
* [Features](#features) | ||
* [Setup](#setup) | ||
* [Gemfile](#gemfile) | ||
* [Fixtures](#fixtures) | ||
* [Usage](#usage) | ||
* [In a Unit Test](#in-a-unit-test) | ||
* [Environment Variables](#environment-variables) | ||
* [Reference](#reference) | ||
* [Limitations](#limitations) | ||
* [Some versions of bundler fail on FIPS-enabled Systems](#some-versions-of-bundler-fail-on-fips-enabled-systems) | ||
* [Development](#development) | ||
* [License](#license) | ||
* [History](#history) | ||
|
||
<!-- vim-markdown-toc --> | ||
|
||
## Overview | ||
|
||
The `simp-spec-helpers` gem provides common rspec tasks to support the SIMP unit testing process. | ||
|
||
### This gem is part of SIMP | ||
|
||
This gem is part of (the build tooling for) the [System Integrity Management Platform](https://github.com/NationalSecurityAgency/SIMP), a compliance-management framework built on [Puppet](https://puppetlabs.com/). | ||
|
||
|
||
### Features | ||
|
||
* Customizable RPM packaging based on a Puppet module's [`metadata.json`][metadata.json] | ||
* RPM signing | ||
* Rubygem packaging | ||
|
||
## Setup | ||
|
||
### .fixtures.yml | ||
|
||
You must include the compliance_markup module in your .fixtures.yml: | ||
|
||
``` yaml | ||
--- | ||
fixtures: | ||
repositories: | ||
... | ||
compliance_markup: https://github.com/simp/pupmod-simp-compliance_markup | ||
... | ||
``` | ||
|
||
### Gemfile | ||
|
||
The Gemfile for your puppet module should have the following included (with updated | ||
versions where necessary): | ||
|
||
``` ruby | ||
group :test do | ||
gem 'puppet', 'puppet', ENV.fetch('PUPPET_VERSION', '~> 5.5') | ||
gem 'rspec' | ||
gem 'rspec-puppet' | ||
gem 'hiera-puppet-helper' | ||
gem 'puppetlabs_spec_helper' | ||
gem 'metadata-json-lint' | ||
gem 'puppet-strings' | ||
gem 'simp-spec-helpers' | ||
gem 'simp-rspec-puppet-facts', ENV.fetch('SIMP_RSPEC_PUPPET_FACTS_VERSION', '~> 2.2') | ||
gem 'simp-rake-helpers', ENV.fetch('SIMP_RAKE_HELPERS_VERSION', '~> 5.9') | ||
gem 'facterdb' | ||
end | ||
``` | ||
|
||
## Usage | ||
|
||
### In a Unit Test | ||
|
||
In your puppet module, create a file spec/spec_helper.rb with the following contents: | ||
|
||
``` ruby | ||
require 'simp/rspec-puppet-facts' | ||
include Simp::RspecPuppetFacts | ||
require 'simp/spec_helpers' | ||
include Simp::SpecHelpers | ||
require 'pathname' | ||
|
||
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures')) | ||
module_name = File.basename(File.expand_path(File.join(__FILE__,'../..'))) | ||
|
||
global_spec_helper(fixture_path, module_name) | ||
|
||
# Get any local methods in spec_helper_local.rb | ||
local_spec_helper = File.join(File.dirname(File.expand_path(__FILE__)),"spec_helper_local.rb") | ||
require_relative 'spec_helper_local' if File.exists?(local_spec_helper) | ||
|
||
``` | ||
Require the spec helper at the top of each unit test file. | ||
|
||
```ruby | ||
require 'spec_helper' | ||
``` | ||
Place any module specific configurations or overrides in a file spec/spec_helper_local.rb. | ||
|
||
For example to use RSPEC instead of MOCHA to mock the environment place the following | ||
in spec/spec_helper_local.rb: | ||
|
||
``` ruby | ||
RSpec.configure do |c| | ||
c.mock_framework = :rspec | ||
c.mock_with :rspec | ||
end | ||
``` | ||
|
||
### Environment Variables | ||
|
||
PUPPET_DEBUG set to anything will enable debug | ||
|
||
### Limitations | ||
|
||
Just some more information on why you need to include compliance_markup: | ||
|
||
The helper module sets up the hiera.yml to include the compliance | ||
module. It should just ignore this if compliance_markup is not included | ||
but, because of a quirk in the testing framework, it doesn't. When it | ||
tries to compile you will get an error like: | ||
If you do not it will barf with an error that looks like | ||
``` ruby | ||
error during compilation: Evaluation Error: Error while evaluating a Function Call, undefined method `load_typed' for nil:NilClass | ||
``` | ||
if compliance_markup is not in the fixtures file. | ||
|
||
|
||
|
||
### Available methods | ||
|
||
Besides the global_spec_helper that is used to set up RSPEC the following | ||
methods are installed and can be used in your tests: | ||
|
||
|
||
#### set_hieradata(hieradata) | ||
This can be used from inside your spec tests to load custom hieradata within | ||
any context. | ||
|
||
Example: | ||
|
||
describe 'some::class' do | ||
context 'with version 10' do | ||
let(:hieradata){ "#{class_name}_v10" } | ||
... | ||
end | ||
end | ||
|
||
Then, create a YAML file at spec/fixtures/hieradata/some__class_v10.yaml. | ||
|
||
Hiera will use this file as it's base of information stacked on top of | ||
'default.yaml' and <module_name>.yaml per the defaults above. | ||
Note: Any colons (:) are replaced with underscores (_) in the class name. | ||
#### set_environment | ||
This can be used from inside your spec tests to set the testable environment. | ||
You can use this to stub out an ENC. | ||
Example: | ||
context 'in the :foo environment' do | ||
let(:environment){:foo} | ||
... | ||
end | ||
#### normalize_compliance_results | ||
Can be used to removed expected errors in compliance data due to test configuration. | ||
See pupmod-simp-pupmod for example. | ||
]s |
Oops, something went wrong.