Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

(SIMP-3430) Create Centralized spec_helper.rb #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .travis.yml
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is SIMP_SKIP_NON_SIMPOS_TESTS used by anything? It's not referenced in any other code in the repository.

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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reminder--I'll update this with a Travis variable in a later PR.

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)"
27 changes: 27 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# 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
gem 'bundler'
gem 'rake'

if facterversion = ENV['FACTER_GEM_VERSION']
gem 'facter', facterversion, :require => false
end

group :test do
gem 'rspec'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rspec is already included by the .gemspec file. Unless the Gemfile's gem statement refines a gemspec's gem (like how PUPPET_VERSION refines the puppet gem) there shouldn't be any redundant gems.

gem 'puppetlabs_spec_helper'
gem 'rubocop'
gem 'rubocop-rspec'

end

60 changes: 60 additions & 0 deletions Gemfile.lock
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
178 changes: 178 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,180 @@
# 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
...
```

NOTE: 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:

``` ruby
error during compilation: Evaluation Error: Error while evaluating
a Function Call, undefined method `load_typed' for nil:NilClass
```
### 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this code block should probably go inside the global_spec_helper() method and/or the Simp::SpecHelpers Ruby module. Otherwise, this block would still need to be kept in sync across every module's spec_helper.rb.

require 'simp/rspec-puppet-facts'
include Simp::RspecPuppetFacts
require 'simp/spec_helpers'
include Simp::SpecHelpers
require 'pathname'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pathname isn't required by anything here; it is only used once in lib/simp/spec_helpers/helpers.rb


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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd pass in __FILE__ instead of fixture_path and module_name. Then global_spec_helper() can find all these files, (including spec_helper_local.rb).


# 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



### 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.

### Limitations


55 changes: 55 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
$: << File.expand_path( '../lib/', __FILE__ )

require 'rubygems'
require 'rake/clean'
require 'fileutils'
require 'find'
require 'rspec/core/rake_task'
require 'simp/rake/spec'

@package='simp-spec-helpers'
@rakefile_dir=File.dirname(__FILE__)

Simp::Rake::Spec.new(@rakefile_dir)

CLEAN.include "#{@package}-*.gem"
CLEAN.include 'pkg'
CLEAN.include 'dist'
CLEAN.include '.vendor'
Find.find( @rakefile_dir ) do |path|
if File.directory? path
CLEAN.include path if File.basename(path) == 'tmp'
else
Find.prune
end
end


desc 'Ensure gemspec-safe permissions on all files'
task :chmod do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This task should be defined alongside pkg:gem

gemspec = File.expand_path( "#{@package}.gemspec", @rakefile_dir ).strip
spec = Gem::Specification::load( gemspec )
spec.files.each do |file|
FileUtils.chmod 'go=r', file
end
end

desc 'special notes about these rake commands'
task :help do
puts %Q{
== environment variables ==
SIMP_RPM_BUILD when set, alters the gem produced by pkg:gem to be RPM-safe.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this :help task be defined by whatever provides pkg:gem?

'pkg:gem' sets this automatically.
}
end

desc "Run spec tests"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There aren't any spec tests for this task to run.

RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = ['--color']
t.pattern = 'spec/lib/**/*_spec.rb'
end

desc %q{run all RSpec tests (alias of 'spec')}
task :test => :spec

# vim: syntax=ruby
1 change: 1 addition & 0 deletions lib/simp-spec-helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'simp/spec_helpers'
1 change: 0 additions & 1 deletion lib/simp/rake/spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'rake/clean'
require 'rake/tasklib'
require 'fileutils'
require 'puppetlabs_spec_helper/tasks/beaker'

module Simp; end
module Simp::Rake
Expand Down
18 changes: 9 additions & 9 deletions lib/simp/spec_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require 'bundler'
require 'rspec'

module Simp; end

module Simp::SpecHelpers
require 'puppetlabs_spec_helper/module_spec_helper'
require 'rspec-puppet'

require 'simp/spec_helpers/helpers'
include Simp::SpecHelpers::Helpers

# Stealing this from the Ruby 2.5 Dir::Tmpname workaround from Rails
def self.tmpname
t = Time.new.strftime("%Y%m%d")
"simp-spec-helpers-#{t}-#{$$}-#{rand(0x100000000).to_s(36)}.tmp"
end
require 'simp/spec_helpers/compliance'
include Simp::SpecHelpers::ComplianceMarkup

end


Loading