Skip to content

Commit

Permalink
(SIMP-4897) Updated Puppet installation methods (#68)
Browse files Browse the repository at this point in the history
* Updated README
* Changed acceptance tests to use `beaker:suites`
* Removed all Puppet 5+ mappings and updated the install method to
  figure out what to use based on the available gems so that everything
  is now consistent
* Fix version handling and add unit tests
  • Loading branch information
trevor-vaughan authored and Nick Miller committed May 15, 2018
1 parent 9c7d8d0 commit fa21279
Show file tree
Hide file tree
Showing 18 changed files with 535 additions and 63 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 1.10.7 / 2018-05-11
* Updated README
* Changed acceptance tests to use `beaker:suites`
* Removed all Puppet 5+ mappings and updated the install method to figure out
what to use based on the available gems so that everything is now consistent

### 1.10.6 / 2018-05-07
* Added Simp::BeakerHelpers.tmpname method to work around the removal of
Dir::Tmpname in Ruby 2.5
Expand Down
111 changes: 81 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,48 @@
Methods to assist beaker acceptance tests for SIMP.

#### Table of Contents
1. [Overview](#overview)
2. [Setup](#setup)
* [Beginning with simp-beaker-helpers](#beginning-with-simp-beaker-helpers)
3. [General Enhancements](#general-enhancements)
* [Suites](#suites)
4. [Nodeset Enhancements](#nodeset-enhancements)
* [YUM Repo Support](#yum_repo_support)
5. [Methods](#methods)
* [`copy_fixture_modules_to`](#copy_fixture_modules_to)

<!-- vim-markdown-toc GFM -->

* [Overview](#overview)
* [Setup](#setup)
* [Beginning with simp-beaker-helpers](#beginning-with-simp-beaker-helpers)
* [Rake Tasks](#rake-tasks)
* [`rake beaker:suites`](#rake-beakersuites)
* [Suite Execution](#suite-execution)
* [Environment Variables](#environment-variables)
* [Global Suite Configuration](#global-suite-configuration)
* [Supported Config:](#supported-config)
* [Individual Suite Configuration](#individual-suite-configuration)
* [Supported Config:](#supported-config-1)
* [Nodeset Enhancements](#nodeset-enhancements)
* [YUM Repo Support](#yum-repo-support)
* [Methods](#methods)
* [`copy_to`](#copy_to)
* [`copy_fixture_modules_to`](#copy_fixture_modules_to)
* [`fix_errata_on`](#fix_errata_on)
* PKI
* [`run_fake_pki_ca_on`](#run_fake_pki_ca_on)
* [`copy_pki_to`](#copy_pki_to)
* [`copy_keydist_to`](#copy_keydist_to)
* Custom facts
* [`pfact_on`](#pfact_on)
* [`pluginsync_on`](#pluginsync_on)
* Hiera
* [`write_hieradata_to`](#write_hieradata_to)
* [`set_hieradata_on`](#set_hieradata_on)
* [`clear_temp_hieradata`](#clear_temp_hieradata)
6. [Environment variables](#environment-variables)
* [`run_fake_pki_ca_on`](#run_fake_pki_ca_on)
* [`copy_pki_to`](#copy_pki_to)
* [`copy_keydist_to`](#copy_keydist_to)
* [`pfact_on`](#pfact_on)
* [`pluginsync_on`](#pluginsync_on)
* [`write_hieradata_to`](#write_hieradata_to)
* [`set_hieradata_on`](#set_hieradata_on)
* [`clear_temp_hieradata`](#clear_temp_hieradata)
* [`latest_puppet_agent_version_for(puppet_version)`](#latest_puppet_agent_version_forpuppet_version)
* [`install_puppet`](#install_puppet)
* [Environment variables](#environment-variables-1)
* [`BEAKER_fips`](#beaker_fips)
* [`BEAKER_spec_prep`](#beaker_spec_prep)
* [`BEAKER_stringify_facts`](#beaker_stringify_facts)
* [`BEAKER_use_fixtures_dir_for_modules`](#beaker_use_fixtures_dir_for_modules)
* [`PUPPET_VERSION`](#puppet_version)
7. [Examples](#examples)
* [Prep OS, Generate and copy PKI certs to each SUT](#prep-os-generate-and-copy-pki-certs-to-each-sut)
* [Specify the version of Puppet to run in the SUTs](#specify-the-version-of-puppet-to-run-in-the-suts)
8. [License](#license)
* [PUPPET_VERSION](#puppet_version)
* [Examples](#examples)
* [Prep OS, Generate and copy PKI certs to each SUT](#prep-os-generate-and-copy-pki-certs-to-each-sut)
* [Specify the version of Puppet to run in the SUTs](#specify-the-version-of-puppet-to-run-in-the-suts)
* [License](#license)

<!-- vim-markdown-toc -->

## Overview

Expand All @@ -53,9 +63,19 @@ Add this to your project's `spec/spec_helper_acceptance.rb`:
require 'simp/beaker_helpers'
include Simp::BeakerHelpers
```
## General Enhancements

### Suites
## Rake Tasks

New `rake` tasks are available to help you use `beaker` more effectively.

These can be included in your `Rakefile` by adding the following:

```
require 'simp/rake/beaker'
Simp::Rake::Beaker.new(File.dirname(__FILE__))
```

### `rake beaker:suites`

The 'beaker:suites' rake task provides the ability to run isolated test sets
with a full reset of the Beaker environment.
Expand All @@ -64,7 +84,7 @@ These are entirely isolated runs of Beaker and have been designed to be used
for situations where you need to eliminate all of the cruft from your previous
runs to perform a new type of test.

#### Suite Execution
### Suite Execution

By default the only suite that will be executed is `default`. Since each suite
is executed in a new environment, spin up can take a lot of time. Therefore,
Expand Down Expand Up @@ -278,8 +298,33 @@ Clean up all temporary hiera data files; meant to be called from `after(:all)`

`clear_temp_hieradata`

#### `latest_puppet_agent_version_for(puppet_version)`

Finds the latest `puppet-agent` version based on the passed gem version and can
accept the usual Gem comparison syntax (e.g., '4.0', '=4.2', '~> 4.3.1', '5')

Returns the `puppet-agent` package version or `nil` if not found.

#### `install_puppet`

Performs an assessment of all set parameters and installs the correct
`puppet-agent` based on those parameters based on the following logic.

If the environment variable `BEAKER_PUPPET_AGENT_VERSION` or
`PUPPET_INSTALL_VERSION` or `PUPPET_VERSION` is set, it will use that value
to determine the `puppet-agent` version to install.

If it is unable to determine the `puppet-agent` version from any `*VERSION`
environment variables and the environment variable `BEAKER_PUPPET_COLLECTION`
is set, it will use this to determine which puppet collection to install from.
(Presently, this only works with Puppet 5.x and is set as `puppet5`.)

If it cannot determinte the `puppet-agent` version from any environment
variables, it will default the version to the value of
Simp::BeakerHelpers::DEFAULT_PUPPET_AGENT_VERSION, which is currently '1.10.4'.

## Environment variables

#### `BEAKER_fips`

_(Default: `no`)_ When set to `yes`, Beaker will enable [FIPS mode](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Security_Guide/sect-Security_Guide-Federal_Standards_And_Regulations-Federal_Information_Processing_Standard.html) on all SUTs before running tests.
Expand All @@ -304,6 +349,9 @@ The `PUPPET_VERSION` environment variable will install the latest
`puppet-agent` package that provides that version of Puppet. This honors
`Gemfile`-style expressions like `"~> 4.8.0"`

`BEAKER_PUPPET_AGENT_VERSION` and `PUPPET_INSTALL_VERSION` are synonyms of
`PUPPET_VERSION`.

## Examples


Expand Down Expand Up @@ -361,7 +409,10 @@ PUPPET_VERSION="~> 4.8.0" bundle exec rake beaker:suites
# puppet-agent 1.9.2 will be installed in VMs
PUPPET_INSTALL_VERSION=1.9.2 bundle exec rake beaker:suites

# puppet-agent 1.8.3 will be installed in VMs
# The latest puppet 5 will be installed in VMs
PUPPET_VERSION="5" bundle exec rake beaker:suites

# puppet-agent 1.10.4 will be installed in VMs
bundle exec rake beaker:suites
```

Expand Down
16 changes: 11 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ require 'rake/clean'
require 'fileutils'
require 'find'
require 'rspec/core/rake_task'
require 'puppetlabs_spec_helper/rake_tasks'
require 'simp/rake/beaker'

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

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

['spec','syntax','syntax:hiera','syntax:manifests','syntax:templates','lint','metadata'].each do |task|
Rake::Task[task].clear
end

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


CLEAN.include "#{@package}-*.gem"
CLEAN.include 'pkg'
CLEAN.include 'dist'
Expand Down Expand Up @@ -46,6 +46,12 @@ SIMP_RPM_BUILD when set, alters the gem produced by pkg:gem to be RPM-safe.
}
end

desc "Run spec tests"
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

Expand Down
15 changes: 4 additions & 11 deletions files/puppet-agent-versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@
# - https://docs.puppet.com/puppet/latest/about_agent.html
#
version_mappings:
'5.4.0': '5.4.0'
'5.3.5': '5.3.5'
'5.3.4': '5.3.4'
'5.3.3': '5.3.3'
'5.3.2': '5.3.2'
'5.3.1': '5.3.1'
'5.3.0': '5.3.0'
'5.2.0': '5.2.0'
'5.1.0': '5.1.0'
'5.0.1': '5.0.1'
'5.0.0': '5.0.0'
'4.10.11': '1.10.12'
'4.10.10': '1.10.10'
'4.10.9': '1.10.9'
'4.10.8': '1.10.8'
'4.10.7': '1.10.7'
'4.10.6': '1.10.6'
'4.10.5': '1.10.5'
Expand Down
103 changes: 87 additions & 16 deletions lib/simp/beaker_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def self.tmpname
#
# This is done so that we know if some new thing that we're using breaks the
# oldest system that we support.
DEFAULT_PUPPET_AGENT_VERSION = '1.7.1'
DEFAULT_PUPPET_AGENT_VERSION = '1.10.4'

# We can't cache this because it may change during a run
def fips_enabled(sut)
Expand Down Expand Up @@ -687,48 +687,119 @@ def pluginsync_on( suts = hosts )
# Looks up latest `puppet-agent` version by the version of its `puppet` gem
#
# @param puppet_version [String] target Puppet gem version. Works with
# Gemfile comparison syntax (e.g., '4.0', '= 4.2', '~> 4.3.1')
# Gemfile comparison syntax (e.g., '4.0', '= 4.2', '~> 4.3.1', '> 5.1, < 5.5')
#
# @return [String,Nil] the `puppet-agent` version or nil
#
def latest_puppet_agent_version_for( puppet_version )
return nil if puppet_version.nil?

require 'rubygems/requirement'
require 'rubygems/version'
require 'yaml'

_puppet_version = puppet_version.strip.split(',')


@agent_version_table ||= YAML.load_file(
File.expand_path(
'../../files/puppet-agent-versions.yaml',
File.dirname(__FILE__)
)).fetch('version_mappings')
_pair = @agent_version_table.find do |k,v|
Gem::Requirement.new(puppet_version).satisfied_by?(Gem::Version.new(k))
Gem::Requirement.new(_puppet_version).satisfied_by?(Gem::Version.new(k))
end
result = _pair ? _pair.last : nil

# If we didn't get a match, go look for published rubygems
unless result
puppet_gems = nil

Bundler.with_clean_env do
puppet_gems = %x(gem search -ra -e puppet).match(/\((.+)\)/)
end

if puppet_gems
puppet_gems = puppet_gems[1].split(/,?\s+/).select{|x| x =~ /^\d/}

# If we don't have a full version string, we need to massage it for the
# match.
begin
if _puppet_version.size == 1
Gem::Version.new(_puppet_version[0])
if _puppet_version[0].count('.') < 2
_puppet_version = "~> #{_puppet_version[0]}"
end
end
rescue ArgumentError
# this means _puppet_version is not just a version, but a version
# specifier such as "= 5.2.3", "<= 5.1", "> 4", "~> 4.10.7"
end

result = puppet_gems.find do |ver|
Gem::Requirement.new(_puppet_version).satisfied_by?(Gem::Version.new(ver))
end
end
end

return result
end

# returns hash with :puppet_install_version, :beaker_puppet_collection,
# and :puppet_install_type keys determined from environment variables,
# host settings, and/or defaults
#
# NOTE: BEAKER_PUPPET_AGENT_VERSION or PUPPET_INSTALL_VERSION or
# PUPPET_VERSION takes precedence over BEAKER_PUPPET_COLLECTION
# or host.options['puppet_collection'], when both a puppet
# install version and a puppet collection are specified. This is
# because the puppet install version can specify more precise
# version information than is available from a puppet collection.
def get_puppet_install_info
# The first match is internal Beaker and the second is legacy SIMP
puppet_install_version = ENV['BEAKER_PUPPET_AGENT_VERSION'] || ENV['PUPPET_INSTALL_VERSION'] || ENV['PUPPET_VERSION']

if puppet_install_version and !puppet_install_version.strip.empty?
puppet_agent_version = latest_puppet_agent_version_for(puppet_install_version.strip)
end

if puppet_agent_version.nil?
if puppet_collection = (ENV['BEAKER_PUPPET_COLLECTION'] || host.options['puppet_collection'])
if puppet_collection =~ /puppet(\d+)/
puppet_install_version = "~> #{$1}"
puppet_agent_version = latest_puppet_agent_version_for(puppet_install_version)
else
raise("Error: Puppet Collection '#{puppet_collection}' must match /puppet(\\d+)/")
end
else
puppet_agent_version = DEFAULT_PUPPET_AGENT_VERSION
end
end

if puppet_collection.nil?
base_version = puppet_agent_version.to_i
puppet_collection = "puppet#{base_version}" if base_version >= 5
end

{ :puppet_install_version => puppet_agent_version,
:beaker_puppet_collection => puppet_collection,
:puppet_install_type => ENV.fetch('PUPPET_INSTALL_TYPE', 'agent')
}
end


# Replacement for `install_puppet` in spec_helper_acceptance.rb
def install_puppet
puppet_install_type = ENV.fetch('PUPPET_INSTALL_TYPE', 'agent')
install_info = get_puppet_install_info

puppet_agent_version = ENV.fetch('PUPPET_INSTALL_VERSION', nil)

unless puppet_agent_version
if host.options['puppet_collection'] && host.options['puppet_collection'] =~ /puppet(\d+)/
puppet_agent_version = latest_puppet_agent_version_for("~> #{$1}")
else
puppet_agent_version = (
latest_puppet_agent_version_for(ENV.fetch('PUPPET_VERSION',nil)) ||
DEFAULT_PUPPET_AGENT_VERSION
)
end
# In case Beaker needs this info internally
ENV['PUPPET_INSTALL_VERSION'] = install_info[:puppet_install_version]
unless install_info[:puppet_collection].nil?
ENV['BEAKER_PUPPET_COLLECTION'] = install_info[:puppet_collection]
end

require 'beaker/puppet_install_helper'

run_puppet_install_helper(puppet_install_type, puppet_agent_version)
run_puppet_install_helper(install_info[:puppet_install_type], install_info[:puppet_install_version])
end
end
2 changes: 1 addition & 1 deletion lib/simp/beaker_helpers/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Simp; end

module Simp::BeakerHelpers
VERSION = '1.10.6'
VERSION = '1.10.7'
end
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions spec/acceptance/suites/default/nodesets
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit fa21279

Please sign in to comment.