Skip to content

Commit

Permalink
Add support to store environment variables as facts
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ekohl committed Oct 6, 2020
1 parent aada484 commit 3e8bd4c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<PUPPET
class { 'mymodule':
version => 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)
```
31 changes: 30 additions & 1 deletion lib/voxpupuli/acceptance/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
@@ -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 <<VOXPUPULI_BEAKER_ENV_VARS > #{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'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e8bd4c

Please sign in to comment.