Skip to content

Commit

Permalink
(maint) Introduce BEAKER_HYPERVISOR
Browse files Browse the repository at this point in the history
Beaker already knows how to generate hosts on the fly using
beaker-hostgenrator. The problem is that there's no way to choose the
default hypervisor. This means you always specify the long form. Using
an environment variable we can simplify configs.
  • Loading branch information
ekohl committed Oct 25, 2018
1 parent 4227347 commit 5b42a3c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ git logs & PR history.

### Added
- `--preserve-state` flag will preserve a given host options hash across subcommand runs(BKR-1541)
- `BEAKER_HYPERVISOR` environment variable to choose the beaker-hostgenerator hypervisor

### Changed

Expand Down
20 changes: 20 additions & 0 deletions docs/tutorials/lets_write_a_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ What needs to happen in this test:

This command will generate a host file for our system under test (SUT). It will use vmpooler as hypervisor for the host. Please check out [this](https://github.com/puppetlabs/beaker/tree/master/docs/how_to/hypervisors) doc to learn more about hypervisors for beaker.

## Creating the host configuration on at runtime

When you don't want to store a static file, you can also let [beaker-hostgenerator](https://github.com/puppetlabs/beaker-hostgenerator) generate it on the fly. The filename is used as a host specification which comes down to the environment variable `BEAKER_setfile`. A simple example is:

```
BEAKER_setfile=centos7-64
```

It's also possible to set the hypervisor and hostname:

```
BEAKER_setfile=centos7-64{hypervisor=docker\,hostname=centos7-64.example.com}
```

An alternative way to set the hypervisor is `BEAKER_HYPERVISOR`:

```
BEAKER_setfile=centos7-64 BEAKER_HYPERVISOR=docker
```

## Install and run HTTPD

Make a file named `install.rb` and put the following code into it:
Expand Down
5 changes: 4 additions & 1 deletion lib/beaker/options/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,11 @@ def parse_hosts_options
$stdout.puts dne_message
require 'beaker-hostgenerator'

host_generator_options = [ @options[:hosts_file] ]
host_generator_options += [ '--hypervisor', ENV['BEAKER_HYPERVISOR'] ] if ENV['BEAKER_HYPERVISOR']

hosts_file_content = begin
bhg_cli = BeakerHostGenerator::CLI.new( [ @options[:hosts_file] ] )
bhg_cli = BeakerHostGenerator::CLI.new(host_generator_options)
bhg_cli.execute
rescue BeakerHostGenerator::Exceptions::Error,
BeakerHostGenerator::Exceptions::InvalidNodeSpecError => error
Expand Down
33 changes: 33 additions & 0 deletions spec/beaker/options/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,46 @@ def mock_out_parsing
).and_return( cli_execute_return )
expect( BeakerHostGenerator::CLI ).to receive(
:new
).with(
[ 'notafile.yml' ]
).and_return( mock_beaker_hostgenerator_cli )
allow( Beaker::Options::HostsFileParser ).to receive(
:parse_hosts_string
).with( cli_execute_return )
parser.parse_hosts_options
end

it 'calls beaker-hostgenerator to get hosts information with a default hypervisor' do
old_beaker_hypervisor = ENV['BEAKER_HYPERVISOR']
begin
ENV['BEAKER_HYPERVISOR'] = 'docker'

parser.instance_variable_set( :@options, {
:hosts_file => 'notafile.yml'
} )
allow( Beaker::Options::HostsFileParser ).to receive(
:parse_hosts_file
).and_raise( Errno::ENOENT )

mock_beaker_hostgenerator_cli = Object.new
cli_execute_return = 'job150865'
expect( mock_beaker_hostgenerator_cli ).to receive(
:execute
).and_return( cli_execute_return )
expect( BeakerHostGenerator::CLI ).to receive(
:new
).with(
[ 'notafile.yml', '--hypervisor', 'docker' ]
).and_return( mock_beaker_hostgenerator_cli )
allow( Beaker::Options::HostsFileParser ).to receive(
:parse_hosts_string
).with( cli_execute_return )
parser.parse_hosts_options
ensure
ENV['BEAKER_HYPERVISOR'] = old_beaker_hypervisor
end
end

it 'sets the :hosts_file_generated flag to signal others when needed' do
options_test = {
:hosts_file => 'not_a_file.yml'
Expand Down

0 comments on commit 5b42a3c

Please sign in to comment.