Class: Beaker::CLI
- Inherits:
-
Object
- Object
- Beaker::CLI
- Defined in:
- lib/beaker/cli.rb
Constant Summary collapse
- VERSION_STRING =
" wWWWw |o o| | O | %s! |(\")| / \\X/ \\ | V | | | | "
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#network_manager ⇒ Object
readonly
Returns the value of attribute network_manager.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#build_hosts_preserved_reproducing_command(command, new_hostsfile) ⇒ String
provides a new version of the command given, edited for re-use with a preserved host.
-
#combined_instance_and_options_hosts ⇒ Object
Return a host_hash that is a merging of options host hashes with instance host objects.
-
#configured_options ⇒ Object
Get the list of options that are not equal to presets.
-
#execute! ⇒ Object
Run Beaker tests.
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
-
#initialize_network_manager ⇒ Object
Initialize the network manager so it can initialize hosts for testing for subcommands.
- #parse_options(args = ARGV) ⇒ Object
-
#preserve_hosts_file ⇒ Object
Sets aside the current hosts file for re-use with the –no-provision flag.
-
#print_command_line(log_level = :debug) ⇒ Object
Prints the command line that can be called to reproduce this run (assuming the environment is the same).
-
#print_env_vars_affecting_beaker(log_level) ⇒ Object
Prints Environment variables affecting the beaker run (those that beaker introspects + the ruby env that beaker runs within).
-
#print_reproduction_info(log_level = :debug) ⇒ Object
Prints all information required to reproduce the current run & results to the log.
-
#print_version_and_options ⇒ Object
only call this method after parse_options has been executed.
-
#provision ⇒ Object
Provision, validate and configure all hosts as defined in the hosts file.
-
#run_suite(suite_name, failure_strategy = nil) ⇒ Object
Run the provided test suite.
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
13 14 15 16 17 18 19 20 |
# File 'lib/beaker/cli.rb', line 13 def initialize = Time.now # Initialize a logger object prior to parsing; this should be overwritten whence # the options are parsed and replaced with a new logger based on what is passed # in to configure the logger. @logger = Beaker::Logger.new = {} end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
12 13 14 |
# File 'lib/beaker/cli.rb', line 12 def logger @logger end |
#network_manager ⇒ Object (readonly)
Returns the value of attribute network_manager.
12 13 14 |
# File 'lib/beaker/cli.rb', line 12 def network_manager @network_manager end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/beaker/cli.rb', line 12 def end |
Instance Method Details
#build_hosts_preserved_reproducing_command(command, new_hostsfile) ⇒ String
provides a new version of the command given, edited for re-use with a preserved host. It does this by swapping the hosts file out for the new_hostsfile argument and removing any previously set provisioning flags that it finds (we add :provision => false in the new_hostsfile itself).
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/beaker/cli.rb', line 338 def build_hosts_preserved_reproducing_command(command, new_hostsfile) command_parts = command.split(' ') replace_hosts_file_next = false reproducing_command = [] command_parts.each do |part| if replace_hosts_file_next reproducing_command << new_hostsfile replace_hosts_file_next = false next elsif part == '--provision' || part == '--no-provision' next # skip any provisioning flag. This is handled in the new_hostsfile itself elsif part == '--hosts' replace_hosts_file_next = true end reproducing_command << part end reproducing_command.join(' ') end |
#combined_instance_and_options_hosts ⇒ Object
Return a host_hash that is a merging of options host hashes with instance host objects
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/beaker/cli.rb', line 239 def hosts_yaml = newly_keyed_hosts_entries = {} hosts_yaml['HOSTS'].each do |host_name, file_host_hash| h = Beaker::Options::OptionsHash.new file_host_hash = h.merge(file_host_hash) @hosts.each do |host| if host_name.to_s == host.name.to_s newly_keyed_hosts_entries[host.hostname] = file_host_hash.merge(host.host_hash) break end end end newly_keyed_hosts_entries end |
#configured_options ⇒ Object
Get the list of options that are not equal to presets.
197 198 199 200 201 202 203 204 205 |
# File 'lib/beaker/cli.rb', line 197 def result = Beaker::Options::OptionsHash.new @attribution.each do |attribute, setter| if setter != 'preset' result[attribute] = [attribute] end end result end |
#execute! ⇒ Object
Run Beaker tests.
-
run pre-suite
-
run tests
-
run post-suite
-
cleanup hosts
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/beaker/cli.rb', line 97 def execute! begin trap(:INT) do @logger.warn "Interrupt received; exiting..." exit(1) end # Setup perf monitoring if needed if [:collect_perf_data].to_s =~ /(aggressive)|(normal)/ @perf = Beaker::Perf.new( @hosts, ) end errored = false #pre acceptance phase run_suite(:pre_suite, :fast) #testing phase begin run_suite(:tests, [:fail_mode]) #post acceptance phase rescue => e #post acceptance on failure #run post-suite if we are in fail-slow mode if [:fail_mode].to_s =~ /slow/ run_suite(:post_suite) @perf.print_perf_info if defined? @perf end raise e else #post acceptance on success run_suite(:post_suite) @perf.print_perf_info if defined? @perf end #cleanup phase rescue => e begin run_suite(:pre_cleanup) rescue => e # pre-cleanup failed @logger.error "Failed running the pre-cleanup suite." end #cleanup on error if [:preserve_hosts].to_s =~ /(never)|(onpass)/ @logger.notify "Cleanup: cleaning up after failed run" if @network_manager @network_manager.cleanup end else preserve_hosts_file end print_reproduction_info( :error ) @logger.error "Failed running the test suite." puts '' exit 1 else begin run_suite(:pre_cleanup) rescue => e # pre-cleanup failed @logger.error "Failed running the pre-cleanup suite." end #cleanup on success if [:preserve_hosts].to_s =~ /(never)|(onfail)/ @logger.notify "Cleanup: cleaning up after successful run" if @network_manager @network_manager.cleanup end else preserve_hosts_file end if @logger.is_debug? print_reproduction_info( :debug ) end end end |
#initialize_network_manager ⇒ Object
Initialize the network manager so it can initialize hosts for testing for subcommands
82 83 84 85 86 87 88 89 |
# File 'lib/beaker/cli.rb', line 82 def initialize_network_manager begin @network_manager = Beaker::NetworkManager.new(, @logger) @hosts = @network_manager.provision rescue => e report_and_raise(@logger, e, "CLI.initialize_network_manager") end end |
#parse_options(args = ARGV) ⇒ Object
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 |
# File 'lib/beaker/cli.rb', line 22 def (args = ARGV) = Beaker::Options::Parser.new = .parse_args(args) @attribution = .attribution @logger = Beaker::Logger.new() InParallel::InParallelExecutor.logger = @logger .update_option(:logger, @logger, 'runtime') .update_option(:timestamp, , 'runtime') .update_option(:beaker_version, Beaker::Version::STRING, 'runtime') beaker_version_string = VERSION_STRING % [:beaker_version] # Some flags should exit early if [:help] @logger.notify(.usage) exit(0) end if [:beaker_version_print] @logger.notify(beaker_version_string) exit(0) end if [:parse_only] exit(0) end #add additional paths to the LOAD_PATH if not [:load_path].empty? [:load_path].each do |path| $LOAD_PATH << File.(path) end end [:helper].each do |helper| require File.(helper) end self end |
#preserve_hosts_file ⇒ Object
Sets aside the current hosts file for re-use with the –no-provision flag. This is originally intended for use on a successful tests where the hosts are preserved (the –preserve-hosts option is set accordingly). It copies the current hosts file to the log directory, and rewrites the SUT names to match their names during the finishing run.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/beaker/cli.rb', line 214 def preserve_hosts_file # things that don't belong in the preserved host file dontpreserve = /HOSTS|logger|timestamp|log_prefix|_dated_dir|logger_sut/ # set the pre/post/tests to be none [:pre_suite] = [] [:post_suite] = [] [:tests] = [] [:pre_cleanup] = [] preserved_hosts_filename = File.join([:log_dated_dir], 'hosts_preserved.yml') hosts_yaml = hosts_yaml['HOSTS'] = hosts_yaml['CONFIG'] = Beaker::Options::OptionsHash.new.merge(hosts_yaml['CONFIG'] || {}) # save the rest of the options, excepting the HOSTS that we have already processed hosts_yaml['CONFIG'] = hosts_yaml['CONFIG'].merge(.reject{ |k,v| k =~ dontpreserve }) # remove copy of HOSTS information hosts_yaml['CONFIG']['provision'] = false File.open(preserved_hosts_filename, 'w') do |file| YAML.dump(hosts_yaml, file) end [:hosts_preserved_yaml_file] = preserved_hosts_filename end |
#print_command_line(log_level = :debug) ⇒ Object
Re-use of already provisioned SUTs has been tested against the vmpooler & vagrant boxes. Fusion doesn’t need this, as it has no cleanup steps. Docker is untested at this time. Please contact @electrical or the Puppet QE Team for more info, or for requests to support this.
Prints the command line that can be called to reproduce this run (assuming the environment is the same)
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/beaker/cli.rb', line 305 def print_command_line( log_level = :debug ) @logger.send(log_level, "\nYou can reproduce this run with:\n") @logger.send(log_level, [:command_line]) if [:hosts_preserved_yaml_file] set_docker_warning = false has_supported_hypervisor = false @hosts.each do |host| case host[:hypervisor] when /vagrant|fusion|vmpooler|vcloud/ has_supported_hypervisor = true when /docker/ set_docker_warning = true end end if has_supported_hypervisor reproducing_command = build_hosts_preserved_reproducing_command([:command_line], [:hosts_preserved_yaml_file]) @logger.send(log_level, "\nYou can re-run commands against the already provisioned SUT(s) with:\n") @logger.send(log_level, '(docker support is untested for this feature. please reference the docs for more info)') if set_docker_warning @logger.send(log_level, reproducing_command) end end end |
#print_env_vars_affecting_beaker(log_level) ⇒ Object
Prints Environment variables affecting the beaker run (those that beaker introspects + the ruby env that beaker runs within)
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/beaker/cli.rb', line 272 def print_env_vars_affecting_beaker( log_level ) non_beaker_env_vars = [ 'BUNDLE_PATH', 'BUNDLE_BIN', 'GEM_HOME', 'GEM_PATH', 'RUBYLIB', 'PATH'] env_var_map = non_beaker_env_vars.inject({}) do |memo, possibly_set_vars| set_var = Array(possibly_set_vars).detect {|possible_var| ENV[possible_var] } memo[set_var] = ENV[set_var] if set_var memo end env_var_map = env_var_map.merge(Beaker::Options::Presets.new.env_vars) @logger.send( log_level, "\nImportant ENV variables that may have affected your run:" ) env_var_map.each_pair do |var, value| if value.is_a?(Hash) value.each_pair do | subvar, subvalue | @logger.send( log_level, " #{subvar}\t\t#{subvalue}" ) end else @logger.send( log_level, " #{var}\t\t#{value}" ) end end end |
#print_reproduction_info(log_level = :debug) ⇒ Object
Prints all information required to reproduce the current run & results to the log
260 261 262 263 |
# File 'lib/beaker/cli.rb', line 260 def print_reproduction_info( log_level = :debug ) print_command_line( log_level ) print_env_vars_affecting_beaker( log_level ) end |
#print_version_and_options ⇒ Object
only call this method after parse_options has been executed.
61 62 63 64 65 |
# File 'lib/beaker/cli.rb', line 61 def @logger.info("Beaker!") @logger.info(VERSION_STRING % [:beaker_version]) @logger.info(.dump) end |
#provision ⇒ Object
Provision, validate and configure all hosts as defined in the hosts file
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/beaker/cli.rb', line 68 def provision begin @hosts = [] initialize_network_manager @network_manager.proxy_package_manager @network_manager.validate @network_manager.configure rescue => e report_and_raise(@logger, e, "CLI.provision") end self end |
#run_suite(suite_name, failure_strategy = nil) ⇒ Object
Run the provided test suite
185 186 187 188 189 190 191 192 193 |
# File 'lib/beaker/cli.rb', line 185 def run_suite(suite_name, failure_strategy = nil) if ([suite_name].empty?) @logger.notify("No tests to run for suite '#{suite_name.to_s}'") return end Beaker::TestSuite.new( suite_name, @hosts, , , failure_strategy ).run_and_raise_on_failure end |