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 Nov 5, 2018
1 parent 0b5a21d commit ae03d2c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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
22 changes: 21 additions & 1 deletion docs/tutorials/lets_write_a_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,27 @@ end
You can now run this with

```console
$ beaker --host redhat7-64ma.yaml --pre-suite install.rb --tests mytest.rb
$ beaker --host redhat7-64ma.yaml --pre-suite install.rb --tests mytest.rb
```

## 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. A simple example is:

```console
$ beaker --host centos7-64 --pre-suite install.rb --tests mytest.rb
```

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

```console
$ beaker --host 'centos7-64{hypervisor=docker,hostname=centos7-64.example.com}' --pre-suite install.rb --tests mytest.rb
```

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

```console
$ BEAKER_HYPERVISOR=docker beaker --host centos7-64 --pre-suite install.rb --tests mytest.rb
```

Next up you may want to look at the [Beaker test for a module](../how_to/write_a_beaker_test_for_a_module.md) page.
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 ae03d2c

Please sign in to comment.