File: generator_helpers.rb

package info (click to toggle)
ruby-beaker-hostgenerator 1.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 3,552 kB
  • sloc: ruby: 3,059; makefile: 3
file content (157 lines) | stat: -rw-r--r-- 4,836 bytes parent folder | download | duplicates (4)
1
2
3
4
5
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require "yaml"

require 'beaker-hostgenerator'
require 'beaker-hostgenerator/data'

module GeneratorTestHelpers
  include BeakerHostGenerator::Data

  def run_cli_with_options(options=[])
    cli = BeakerHostGenerator::CLI.new(options)
    yaml_string = cli.execute

    hash = YAML.load(yaml_string)
    return hash
  end

  def open_file(path)
    # Takes a list of path elements.
    # Returns a file object where the basename of the file is the last path
    # element.
    dirname = File.join(path[1, path.length - 2]) # wtf
    filename = File.join(path)
    FileUtils.mkdir_p(dirname)
    return File.open(filename, "w")
  end

  def generate_fixture(relative_path, options, spec, environment_variables={})
    specopts = options + [spec]
    arguments_string = specopts.join(" ")

    environment_variables.each do |key, value| 
      ENV[key] = value
    end
    generated_hash = run_cli_with_options(specopts)
    environment_variables.each_key do |key|
      ENV[key] = nil
    end

    fixture_hash = {
      "arguments_string" => arguments_string,
      "environment_variables" => environment_variables,
      "expected_hash" => generated_hash,
      "expected_exception" => nil,
    }
    fixture_yaml = fixture_hash.to_yaml

    fixture_file = open_file([Dir.pwd, @fixture_root] + relative_path + [spec])
    fixture_file.write(fixture_yaml)
  end

  def generate_fixtures_using_osinfo(relative_path,
                                     role_enumerator,
                                     options=[],
                                     bhg_version=0)
    platforms = get_platforms(bhg_version)
    platforms.each do |platform_info|
      role = role_enumerator.next
      spec = "#{platform_info}" + role
      generate_fixture(relative_path, options, spec)
    end
  end

end

class FixtureGenerator
  include GeneratorTestHelpers

  def initialize
    @fixture_root = 'test/fixtures/generated/'
    @simple_roles = ["a", "u", "l", "c", "d", "f", "m", "aulcdfm"]
  end

  def generate
    # Validates single-host scenarios using all short-form role aliases with no
    # optional flags'
    generate_fixtures_using_osinfo(['default'], @simple_roles.cycle, [])

    # Validates the use of environment variables to set various pe options.
    [
      {'case_name' => 'pe_version_and_pe_family',
       'environment_variables' => {
         'pe_version' => '6.6.6',
         'pe_upgrade_version' => '6.6.6',
         'pe_family' => '6.6.6',
         'pe_upgrade_family' => '6.6.6',
       }
      },
      {'case_name' => 'pe_version_and_pe_family_upgrade_only',
       'environment_variables' => {
        'pe_upgrade_version' => '6.6.6',
        'pe_upgrade_family' => '6.6.6',
       }
      },
      {'case_name' => 'pe_version_and_pe_family_no_upgrade',
       'environment_variables' => {
         'pe_version' => '6.6.6',
         'pe_family' => '6.6.6',
       }
      },
    ].each do |fixture_hash|
      case_name = fixture_hash['case_name']
      environment_variables = fixture_hash['case_name']
      generate_fixture(['environment_variable_tests', fixture_hash['case_name']],
                       [],
                       'centos6-32a',
                       fixture_hash['environment_variables'])
    end

    # Validates single-host scenarios using all short-form role aliases with the
    # addition of the --osinfo-version flag to indicate which BHG version to
    # generate host configs for.
    [0, 1].each do |bhg_version|
      generate_fixtures_using_osinfo(["osinfo-version-#{bhg_version}"],
                                     @simple_roles.cycle,
                                     ["--osinfo-version", "#{bhg_version}"],
                                     bhg_version)
    end

    # Validates the use of the pe ver/dir type options.
    [
      {
        'path' => ['pe_upgrade_ver'],
        'options' => ['--pe_upgrade_ver', '2020.7.3'],
        'spec' => 'centos6-64mdc'
        },
      {
        'path' => ['pe_ver'],
        'options' => ['--pe_ver', '2020.7'],
        'spec' => 'centos6-64mdc'
        },
      {
        'path' => ['pe_upgrade_dir'],
        'options' => ['--pe_upgrade_dir', '/opt/whatever'],
        'spec' => 'centos6-64mdc'
        },
      {
        'path' => ['pe_dir'],
        'options' => ['--pe_dir', '/opt/hello'],
        'spec' => 'centos6-64mdc'
        }
      ].each do |fixture_info|
      generate_fixture(fixture_info['path'],
                       fixture_info['options'],
                       fixture_info['spec'])
    end

    # Validates multi-platform specs
    get_platforms(0).zip(
      get_platforms(1).reverse,
      get_platforms(0),
      @simple_roles.cycle,
      @simple_roles.reverse.cycle
    ) do |p1, p2, p3, r1, r2|
      generate_fixture(["multiplatform"], [], "#{p1}#{r1}-#{p2}-#{p3}#{r2}")
    end
  end
end