Top Level Namespace

Defined Under Namespace

Modules: Serverspec, Voxpupuli

Instance Method Summary collapse

Instance Method Details

#configure_beaker(modules: :metadata, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/voxpupuli/acceptance/spec_helper_acceptance.rb', line 6

def configure_beaker(modules: :metadata, &block)
  collection = ENV['BEAKER_PUPPET_COLLECTION'] || 'puppet'
  ENV['BEAKER_DEBUG'] ||= 'true'
  ENV['BEAKER_HYPERVISOR'] ||= 'docker'

  # On Ruby 3 this doesn't appear to matter but on Ruby 2 beaker-hiera must be
  # included before beaker-rspec so Beaker::DSL is final
  require 'beaker-hiera'
  require 'beaker_puppet_helpers'
  require 'beaker-rspec'

  require_relative 'fixtures' if modules == :fixtures

  unless ENV['BEAKER_PROVISION'] == 'no'
    block_on hosts, run_in_parallel: true do |host|
      if collection != 'preinstalled'
        if collection != 'none'
          BeakerPuppetHelpers::InstallUtils.install_puppet_release_repo_on(host, collection)
        end

        package_name = ENV.fetch('BEAKER_PUPPET_PACKAGE_NAME') do
          BeakerPuppetHelpers::InstallUtils.collection2packagename(host, collection)
        end
        host.install_package(package_name)
      end

      # by default, puppet-agent creates /etc/profile.d/puppet-agent.sh which adds /opt/puppetlabs/bin to PATH
      # in our non-interactive ssh sessions we manipulate PATH in ~/.ssh/environment, we need to do this step here as well
      host.add_env_var('PATH', '/opt/puppetlabs/bin')
    end
  end

  RSpec.configure do |c|
    # Readable test descriptions
    c.formatter = :documentation

    # Configure all nodes in nodeset
    c.before :suite do
      case modules
      when :metadata
        install_local_module_on(hosts)
      when :fixtures
        fixture_modules = File.join(Dir.pwd, 'spec', 'fixtures', 'modules')
        Voxpupuli::Acceptance::Fixtures.install_fixture_modules_on(hosts, fixture_modules)
      end

      if RSpec.configuration.suite_configure_facts_from_env
        require_relative 'facts'
        Voxpupuli::Acceptance::Facts.write_beaker_facts_on(hosts)
      end

      if RSpec.configuration.suite_hiera?
        hiera_data_dir = RSpec.configuration.suite_hiera_data_dir

        if Dir.exist?(hiera_data_dir)
          write_hiera_config_on(hosts, RSpec.configuration.suite_hiera_hierachy)
          copy_hiera_data_to(hosts, hiera_data_dir)
        end
      end

      local_setup = RSpec.configuration.setup_acceptance_node
      local_setup_content = File.exist?(local_setup) ? File.read(local_setup) : nil
      hosts.each do |host|
        yield host if block

        if local_setup_content
          puts "Configuring #{host} by applying #{local_setup}"
          apply_manifest_on(host, local_setup_content, catch_failures: true)
        end
      end
    end
  end
end