From 3e8bd4cb9a6a8228cd0dbe599051908cbc9033c1 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Wed, 30 Sep 2020 22:32:16 +0200 Subject: [PATCH] Add support to store environment variables as facts This code converts BEAKER_FACTER_* env vars to facts on target machines. This allows testing variations. While this is similar to tiers, it's not really the same. For example, a tier can be single node vs multi node, but those can both be run with multiple versions. --- README.md | 38 +++++++++++++++++++ .../acceptance/spec_helper_acceptance.rb | 31 ++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9123bf8..0f759a0 100644 --- a/README.md +++ b/README.md @@ -90,3 +90,41 @@ It's also possible to skip module installation altogether, giving the module dev ```ruby configure_beaker(modules: nil) ``` + +## Environment variables to facts + +It can be useful to provide facts via environment variables. A possible use is run the test suite with version 1.0 and 1.1. Often it's much easier to run the entire suite with version 1.0 and run it with 1.1 in a complete standalone fashion. + +Voxpupuli-acceptance converts all environment variables starting with `BEAKER_FACTER_` and stores them in `/etc/facter/facts.d/voxpupuli-acceptance-env.json` on the target machine. All environment variables are converted to lowercase. + +Given following `spec_helper_acceptance.rb` is used: + +```ruby +require 'voxpupuli/acceptance/spec_helper_acceptance' + +MANIFEST = < fact('mymodule_version'), +} +PUPPET + +configure_beaker do |host| + apply_manifest_on(host, MANIFEST, catch_failures: true) +end +``` + +Then it can be tested with: +```bash +BEAKER_FACTER_MYMODULE_VERSION=1.0 bundle exec rake beaker +BEAKER_FACTER_MYMODULE_VERSION=1.1 bundle exec rake beaker +``` + +Many CI systems make it easy to build a matrix with this. + +If no environment variables are present, the file is removed. It is not possible to store structured facts. + +This behavior can be disabled altogether: + +```ruby +configure_beaker(configure_facts_from_env: false) +``` diff --git a/lib/voxpupuli/acceptance/spec_helper_acceptance.rb b/lib/voxpupuli/acceptance/spec_helper_acceptance.rb index b093979..cf2e8df 100644 --- a/lib/voxpupuli/acceptance/spec_helper_acceptance.rb +++ b/lib/voxpupuli/acceptance/spec_helper_acceptance.rb @@ -1,4 +1,31 @@ -def configure_beaker(modules: :metadata, &block) +ENV_VAR_PREFIX = 'BEAKER_FACTER_' +FACT_FILE = '/etc/facter/facts.d/voxpupuli-acceptance-env.json' + +def beaker_facts_from_env + facts = {} + + ENV.each do |var, value| + next unless var.start_with?(ENV_VAR_PREFIX) + + fact = var.sub(ENV_VAR_PREFIX, '').downcase + facts[fact] = value + end + + facts +end + +def write_beaker_facts_on(hosts) + beaker_facts = beaker_facts_from_env + + if beaker_facts.any? + require 'json' + on(hosts, "mkdir -p #{File.dirname(FACT_FILE)} && cat < #{FACT_FILE}\n#{beaker_facts.to_json}\nVOXPUPULI_BEAKER_ENV_VARS") + else + on(hosts, "rm -f #{FACT_FILE}") + end +end + +def configure_beaker(modules: :metadata, configure_facts_from_env: true, &block) ENV['PUPPET_INSTALL_TYPE'] ||= 'agent' ENV['BEAKER_PUPPET_COLLECTION'] ||= 'puppet6' ENV['BEAKER_debug'] ||= 'true' @@ -33,6 +60,8 @@ def configure_beaker(modules: :metadata, &block) Voxpupupli::Acceptance::Fixtures.install_fixture_modules_on(hosts, fixture_modules) end + write_beaker_facts_on(hosts) if configure_facts_from_env + if block hosts.each do |host| yield host