Class: Voxpupuli::Acceptance::Fixtures

Inherits:
Object
  • Object
show all
Defined in:
lib/voxpupuli/acceptance/fixtures.rb

Class Method Summary collapse

Class Method Details

.build_modules(modulepath, destination, logger = nil) ⇒ Hash<String=>String>

Build modules in the given sources.

Parameters:

  • modulepath (Array<String>, String)

    A modulepath as Puppet uses it

  • destination (String)

    The target directory to build modules into

  • logger (Logger, nil) (defaults to: nil)

    An optional logger

Returns:

  • (Hash<String=>String>)

    A mapping of module names and the path to their tarball



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
# File 'lib/voxpupuli/acceptance/fixtures.rb', line 37

def build_modules(modulepath, destination, logger = nil)
  modulepath = [modulepath] if modulepath.is_a?(String)

  modules = {}

  modulepath.each do |dir|
    Dir[File.join(dir, '*', 'metadata.json')].each do ||
      path = File.dirname()
      name = File.basename(path)
      next if modules.include?(name)

      builder = Puppet::Modulebuilder::Builder.new(File.realpath(path), destination, logger)

      unless valid_directory_name?(name, builder.['name'])
        warning = "Directory name of #{path} doesn't match metadata name #{builder.['name']}"
        if logger
          logger.warn(warning)
        else
          STDERR.puts(warning)
        end
      end

      modules[name] = builder.build
    end
  end

  modules
end

.install_fixture_modules_on(hosts, modulepath, logger = nil) ⇒ Object

Install fixture modules on the given hosts

Parameters:

  • hosts (Host, Array<Host>, String, Symbol)

    The beaker hosts to run on

  • modulepath (Array<String>, String)

    A modulepath as Puppet uses it. Typically spec/fixtures/modules

  • logger (Logger, nil) (defaults to: nil)

    An optional logger



16
17
18
19
20
21
22
23
24
# File 'lib/voxpupuli/acceptance/fixtures.rb', line 16

def install_fixture_modules_on(hosts, modulepath, logger = nil)
  Dir.mktmpdir do |destination|
    modules = build_modules(modulepath, destination, logger)

    install_modules_on(hosts, modules, logger)
  end

  nil
end

.install_modules_on(hosts, modules, logger = nil) ⇒ Object

Install modules on a number of hosts

This is done by iterating over every host. On that host a temporary directory is created. Each module is copied there and installed by force.

Parameters:

  • hosts (Host, Array<Host>, String, Symbol)

    The beaker hosts to run on

  • modules (Hash<String=>String>)

    A mapping between module names and their tarball

  • logger (Logger, nil) (defaults to: nil)

    An optional logger



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/voxpupuli/acceptance/fixtures.rb', line 78

def install_modules_on(hosts, modules, logger = nil)
  hosts.each do |host|
    logger.debug("Installing modules on #{host}") if logger

    temp_dir_on(host) do |target_dir|
      modules.each do |name, source_path|
        target_file = File.join(target_dir, File.basename(source_path))
        logger.debug("Copying module #{name} from #{source_path} to #{target_file}") if logger

        scp_to(host, source_path, target_file)
        on host, "puppet module install --force --ignore-dependencies '#{target_file}'"
      end
    end
  end

  nil
end

.temp_dir_on(host, &block) ⇒ Object

Create a temporary directory on the host, similar to Dir.mktmpdir but on a remote host.

Examples:

temp_dir_on(host) { |dir| puts dir }

Parameters:

  • host (Host, String, Symbol)

    The beaker host to run on



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/voxpupuli/acceptance/fixtures.rb', line 104

def temp_dir_on(host, &block)
  result = on host, "mktemp -d"
  raise "Could not create directory" unless result.success?

  dir = result.stdout.strip

  begin
    yield dir
  ensure
    on host, "rm -rf #{dir}"
  end

  nil
end

.valid_directory_name?(directory_name, metadata_name) ⇒ Boolean

Validate a directory name matches its metadata name.

This is useful to detect misconfigurations in fixture set ups.

Examples:

assert valid_directory_name?('tftp', 'theforeman-tftp')
refute valid_directory_name?('puppet-tftp', 'theforeman-tftp')

Parameters:

  • directory_name (String)

    Name of the directory. Not the full path

  • metadata_name (String)

    Name as it shows up in the metadata. It is normalized and accepts both / and - as separators.

Returns:

  • (Boolean)

    Wether the directory name is valid with the given metadata name



136
137
138
139
# File 'lib/voxpupuli/acceptance/fixtures.rb', line 136

def valid_directory_name?(directory_name, )
  normalized = .tr('/', '-').split('-').last
  normalized == directory_name
end