Class: Simp::Rake::Beaker
- Inherits:
-
Rake::TaskLib
- Object
- Rake::TaskLib
- Simp::Rake::Beaker
- Defined in:
- lib/simp/rake/beaker.rb
Instance Method Summary collapse
-
#initialize(base_dir) {|_self| ... } ⇒ Beaker
constructor
A new instance of Beaker.
Constructor Details
#initialize(base_dir) {|_self| ... } ⇒ Beaker
Returns a new instance of Beaker.
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/simp/rake/beaker.rb', line 12 def initialize(base_dir) @base_dir = base_dir @clean_list = [] ::CLEAN.include( %{#{@base_dir}/log} ) ::CLEAN.include( %{#{@base_dir}/junit} ) ::CLEAN.include( %{#{@base_dir}/sec_results} ) ::CLEAN.include( %{#{@base_dir}/spec/fixtures/inspec_deps} ) yield self if block_given? ::CLEAN.include( @clean_list ) namespace :beaker do desc " Run a Beaker test against a specific Nodeset\n * :nodeset - The nodeset against which you wish to run\n EOM\n task :run, [:nodeset] do |t,args|\n fail \"You must pass :nodeset to \#{t}\" unless args[:nodeset]\n nodeset = args[:nodeset].strip\n\n old_stdout = $stdout\n nodesets = StringIO.new\n $stdout = nodesets\n\n Rake::Task['beaker_nodes'].invoke\n\n $stdout = old_stdout\n\n nodesets = nodesets.string.split(\"\\n\")\n\n fail \"Nodeset '\#{nodeset}' not found. Valid Nodesets:\\n\#{nodesets.map{|x| x = %( * \#{x})}.join(%(\\n))}\" unless nodesets.include?(nodeset)\n\n ENV['BEAKER_set'] = nodeset\n\n Rake::Task['beaker'].invoke\n end\n\n desc <<-EOM\n Run Beaker test suites.\n * :suite - A specific suite to run\n * If you set this to `ALL`, all suites will be run\n\n * :nodeset - A specific nodeset to run on within a specific suite\n * If you set this to `ALL`, all nodesets will be looped through, starting with 'default'\n * You may also set this to a colon delimited list of short\n nodeset names that will be run in that order\n\n ## Suite Execution\n\n By default the only suite that will be executed is `default`.\n Since each suite is executed in a new environment, spin up can\n take a lot of time. Therefore, the default is to only run the\n default suite.\n\n If there is a suite where the metadata contains `default_run` set\n to the Boolean `true`, then that suite will be part of the\n default suite execution.\n\n You can run all suites by setting the passed suite name to `ALL`\n (case sensitive).\n\n ## Environment Variables\n\n * BEAKER_suite_runall\n * Run all Suites\n\n * BEAKER_suite_basedir\n * The base directory where suites will be defined\n * Default: spec/acceptance\n\n ## Global Suite Configuration\n A file `config.yml` can be placed in the `suites` directory to\n control certain aspects of the suite run.\n\n ### Supported Config:\n\n ```yaml\n ---\n # Fail the entire suite at the first failure\n 'fail_fast' : <true|false> => Default: true\n ```\n ## Individual Suite Configuration\n\n Each suite may contain a YAML file, metadata.yml, which will be\n used to provide information to the suite of tests.\n\n ### Supported Config:\n\n ```yaml\n ---\n 'name' : '<User friendly name for the suite>'\n\n # Run this suite by default\n 'default_run' : <true|false> => Default: false\n ```\n EOM\n task :suites, [:suite, :nodeset] => ['spec_prep'] do |t,args|\n suite = args[:suite]\n nodeset = args[:nodeset]\n\n # Record Tasks That Fail\n # Need to figure out how to capture the errors\n failures = Hash.new\n\n suite_basedir = File.join(@base_dir, 'spec/acceptance/suites')\n\n if ENV['BEAKER_suite_basedir']\n suite_basedir = ENV['BEAKER_suite_basedir']\n end\n\n raise(\"Error: Suites Directory at '\#{suite_basedir}'!\") unless File.directory?(suite_basedir)\n\n if suite && (suite != 'ALL')\n unless File.directory?(File.join(suite_basedir, suite))\n STDERR.puts(\"Error: Could not find suite '\#{suite}'\")\n STDERR.puts(\"Available Suites:\")\n STDERR.puts(' * ' + Dir.glob(\n File.join(suite_basedir, '*')).sort.map{ |x|\n File.basename(x)\n }.join(\"\\n * \")\n )\n exit(1)\n end\n end\n\n suite_config = {\n 'fail_fast' => true\n }\n suite_config_metadata_path = File.join(suite_basedir, 'config.yml')\n if File.file?(suite_config_metadata_path)\n suite_config.merge!(YAML.load_file(suite_config_metadata_path))\n end\n\n suites = Hash.new\n\n if suite && (suite != 'ALL')\n suites[suite] = Hash.new\n # If a suite was set, make sure it runs.\n suites[suite]['default_run'] = true\n else\n Dir.glob(File.join(suite_basedir,'*')) do |file|\n if File.directory?(file)\n suites[File.basename(file)] = Hash.new\n\n if suite == 'ALL'\n suites[File.basename(file)]['default_run'] = true\n end\n end\n end\n end\n\n suites.keys.each do |ste|\n suites[ste]['name'] = ste\n suites[ste]['path'] = File.join(suite_basedir, ste)\n\n metadata_path = File.join(suites[ste]['path'], 'metadata.yml')\n if File.file?(metadata_path)\n suites[ste]['metadata'] = YAML.load_file(metadata_path)\n end\n\n unless File.directory?(File.join(suites[ste]['path'],'nodesets'))\n Dir.chdir(suites[ste]['path']) do\n if File.directory?('../../nodesets')\n FileUtils.ln_s('../../nodesets', 'nodesets')\n end\n end\n end\n\n suites[ste].merge!(suites[ste]['metadata']) if suites[ste]['metadata']\n\n # Ensure that the 'default' suite runs unless explicitly disabled.\n if suites['default']\n if ( suites['default']['default_run'].nil? ) || ( suites['default']['default_run'] == true )\n suites['default']['default_run'] = true\n end\n end\n end\n\n raise(\"Error: No Suites Found in '\#{suite_basedir}'!\") if suites.empty?\n\n # Need to ensure that 'default' is first\n ordered_suites = suites.keys.sort\n default_suite = ordered_suites.delete('default')\n ordered_suites.unshift(default_suite) if default_suite\n\n suite_start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)\n ordered_suites.each do |ste|\n\n next unless (suites[ste]['default_run'] == true)\n\n name = suites[ste]['name']\n\n $stdout.puts(\"\\n\\n=== Suite '\#{name}' Starting ===\\n\\n\")\n\n nodesets = Array.new\n nodeset_path = File.join(suites[ste]['path'],'nodesets')\n\n if nodeset\n if nodeset == 'ALL'\n nodesets = Dir.glob(File.join(nodeset_path, '*.yml'))\n\n # Make sure we run the default set first\n default_set = nodesets.delete(File.join(nodeset_path, 'default.yml'))\n nodesets.unshift(default_set) if default_set\n else\n nodeset.split(':').each do |tgt_nodeset|\n nodesets << File.join(nodeset_path, \"\#{tgt_nodeset.strip}.yml\")\n end\n end\n else\n nodesets << File.join(nodeset_path, 'default.yml')\n end\n\n refined_nodesets = []\n\n nodesets.each do |nodeset_yml|\n parsed_nodeset = ::Beaker::Options::HostsFileParser.parse_hosts_file(nodeset_yml)\n\n # Default to multi-node testing for backwards compatibility\n multi_node = (parsed_nodeset[:multi_node] == false ? false: true)\n\n if multi_node\n refined_nodesets.push(nodeset_yml)\n else\n parsed_nodeset_hosts = parsed_nodeset.delete(:HOSTS)\n\n parsed_nodeset_hosts.each do |k,v|\n\n v[:roles] ||= []\n v[:roles] |= ['default']\n\n tmp_nodeset = {\n :HOSTS => { k => v },\n :CONFIG => parsed_nodeset\n }\n\n tmp_nodeset_file = Tempfile.new(\"nodeset_\#{k}-\")\n tmp_nodeset_file.write(tmp_nodeset.to_yaml)\n tmp_nodeset_file.close\n\n refined_nodesets.push(tmp_nodeset_file.path)\n\n at_exit do\n if tmp_nodeset_file && File.exist?(tmp_nodeset_file.path)\n tmp_nodeset_file.close\n tmp_nodeset_file.unlink\n end\n end\n end\n end\n end\n\n refined_nodesets.sort.each do |nodeset_yml|\n unless File.file?(nodeset_yml)\n # Get here if user has specified a non-existent nodeset or the\n # implied `default` nodeset does not exist.\n if suite_config['fail_fast']\n fail(\"*** Suite \#{name} Nodeset '\#{File.basename(nodeset_yml, '.yml')}' Not Found ***\")\n else\n $stdout.puts(\"=== Suite \#{name} Nodeset '\#{File.basename(nodeset_yml, '.yml')}' Not Found, Skipping ===\")\n next\n end\n end\n\n ENV['BEAKER_setfile'] = nodeset_yml\n\n Rake::Task[:beaker].clear\n RSpec::Core::RakeTask.new(:beaker) do |tsk|\n tsk.rspec_opts = ['--color']\n tsk.pattern = File.join(suites[ste]['path'])\n end\n\n current_suite_task = Rake::Task[:beaker]\n\n if suite_config['fail_fast'] == true\n current_suite_task.execute\n else\n begin\n current_suite_task.execute\n rescue SystemExit\n failures[suites[ste]['name']] = {\n 'path' => suites[ste]['path'],\n 'nodeset' => File.basename(nodeset_yml, '.yml')\n }\n end\n end\n\n $stdout.puts(\"\\n\\n=== Suite '\#{name}' Complete ===\\n\\n\")\n end\n end\n suite_end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)\n\n suite_run_time = ((suite_end_time - suite_start_time)/60).round(2)\n\n $stdout.puts(\"== Total Runtime: \#{suite_run_time} minutes ==\\n\\n\")\n\n unless failures.keys.empty?\n $stdout.puts(\"The following tests had failures:\")\n failures.keys.sort.each do |ste|\n $stdout.puts(\" * \#{ste} => \#{failures[ste]['path']} on \#{failures[ste]['nodeset']}\")\n end\n end\n end\n end\nend\n" |