Class: TestRunner::TestEvaluator

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/test-runner/test_evaluator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test, scripts, system: NixCli::DEFAULT_SYSTEM, test_config_path: nil, **opts) ⇒ TestEvaluator

Returns a new instance of TestEvaluator.

Parameters:

Options Hash (**opts):

  • :default_timeout (Integer)
  • :destructive (Boolean)
  • :recreate_disks (Boolean)
  • :state_dir (String)
  • :sock_dir (String)


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
# File 'lib/test-runner/test_evaluator.rb', line 28

def initialize(test, scripts, system: NixCli::DEFAULT_SYSTEM, test_config_path: nil, **opts)
  scripts.each do |s|
    next if s.test == test

    raise ArgumentError, "script #{s.name} is not of test #{test.path}"
  end

  @test = test
  @scripts = scripts
  @opts = opts
  @config = TestConfig.build(
    test,
    system:,
    test_config_path:,
    config_path: File.join(@opts.fetch(:state_dir), 'config.json')
  )
  @machines = {}
  @default_timeout = opts.fetch(:default_timeout)
  @used_container_ids = []

  @config['machines'].each do |name, cfg|
    var = :"@#{name}"

    machine_config = OsVm::MachineConfig.from_config(cfg)

    m = machine_class_for(machine_config).new(
      name,
      machine_config,
      opts[:state_dir],
      opts[:sock_dir],
      default_timeout: opts[:default_timeout],
      hash_base: test.path
    )

    m.destroy_disks if opts[:recreate_disks]

    instance_variable_set(var, m)

    define_singleton_method(name) do
      instance_variable_get(var)
    end

    machines[name] = m
  end
end

Instance Attribute Details

#machinesHash<String, OsVm::Machine> (readonly)

Returns:

  • (Hash<String, OsVm::Machine>)


18
19
20
# File 'lib/test-runner/test_evaluator.rb', line 18

def machines
  @machines
end

Instance Method Details

#after(type, &block) ⇒ Object

Code block executed after suite, context or example

Parameters:

  • type (:suite, :context, :example)


197
198
199
200
201
202
203
204
205
206
# File 'lib/test-runner/test_evaluator.rb', line 197

def after(type, &block)
  if type == :suite
    @after << block
    return
  end

  raise 'Called outside of an example group, use from #describe block' if @group_stack.empty?

  @group_stack.last.add_after(type, block)
end

#before(type, &block) ⇒ Object

Code block executed before suite, context or example

Parameters:

  • type (:suite, :context, :example)


184
185
186
187
188
189
190
191
192
193
# File 'lib/test-runner/test_evaluator.rb', line 184

def before(type, &block)
  if type == :suite
    @before << block
    return
  end

  raise 'Called outside of an example group, use from #describe block' if @group_stack.empty?

  @group_stack.last.add_before(type, block)
end

#breakpointObject

Invoke interactive shell from within a test



144
145
146
# File 'lib/test-runner/test_evaluator.rb', line 144

def breakpoint
  binding.pry # rubocop:disable Lint/Debugger
end

#build_test_result(script_results, elapsed_time) ⇒ Object (protected)



404
405
406
407
408
409
410
411
412
413
414
# File 'lib/test-runner/test_evaluator.rb', line 404

def build_test_result(script_results, elapsed_time)
  test_success = script_results.all?(&:expected_result?)

  TestResult.new(
    @test,
    script_results,
    test_success,
    elapsed_time,
    @opts[:state_dir]
  )
end

#call_after_test_run_hook(test_result) ⇒ Object (protected)



378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/test-runner/test_evaluator.rb', line 378

def call_after_test_run_hook(test_result)
  Hook.call(
    :after_test_run,
    kwargs: {
      test: @test,
      test_result:,
      scripts: @scripts,
      machines:,
      state_dir: @opts[:state_dir]
    }
  )
end

#call_after_test_script_hook(script_result) ⇒ Object (protected)



391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/test-runner/test_evaluator.rb', line 391

def call_after_test_script_hook(script_result)
  Hook.call(
    :after_test_script_run,
    kwargs: {
      test: @test,
      script: script_result.test_script,
      script_result:,
      machines:,
      state_dir: @opts[:state_dir]
    }
  )
end

#configure_examples {|| ... } ⇒ Object

Configure default settings for example groups

Yield Parameters:



150
151
152
# File 'lib/test-runner/test_evaluator.rb', line 150

def configure_examples
  yield(@example_config)
end

#describe(obj, order: nil) ⇒ Object Also known as: context

Create an example group

Example groups can be nested. Groups are evaluated in the configured order, which defaults to random. Groups contain examples which are defined within the yielded block using #it.

Parameters:

  • obj (#to_s)
  • order (nil, :defined, :rand, Random, Integer) (defaults to: nil)

    order in which examples and subgroups are evaluated



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/test-runner/test_evaluator.rb', line 163

def describe(obj, order: nil, &)
  grp = ExampleGroup.new(obj, parent: @group_stack.last, order:, config: @example_config, &)

  if @group_stack.any?
    @group_stack.last.add_group(grp)
  else
    @example_groups << grp
  end

  @group_stack << grp

  grp.load

  @group_stack = @group_stack[0..-2]
  nil
end

#do_runObject (protected)



506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/test-runner/test_evaluator.rb', line 506

def do_run
  yield

  machines.each_value do |m|
    m.stop if m.running? && m.can_execute?
  end
ensure
  machines.each_value do |m|
    m.kill
    m.destroy if @opts[:destructive]
    m.finalize
    m.cleanup
  end
end

#example(message, pending: false, skip: false, &block) ⇒ Object Also known as: it

Create a test example

#it must be called from within a #describe block. Examples within a group are evaluated in the group’s configured order.

Parameters:

  • message (String)
  • pending (Boolean) (defaults to: false)
  • skip (Boolean) (defaults to: false)


216
217
218
219
220
221
222
# File 'lib/test-runner/test_evaluator.rb', line 216

def example(message, pending: false, skip: false, &block)
  raise 'Called outside of an example group, use from #describe block' if @group_stack.empty?

  grp = @group_stack.last
  grp.add_example(Example.new(grp, message, pending:, skip: skip || block.nil?, &block))
  nil
end

#get_container_id(base = 'testct') ⇒ String

Generate container id that is unique to the test run

Returns:

  • (String)


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/test-runner/test_evaluator.rb', line 250

def get_container_id(base = 'testct')
  10.times do
    new_id = "#{base}-#{SecureRandom.hex(2)}"

    if @used_container_ids.include?(new_id)
      sleep(0.05)
      next
    end

    @used_container_ids << new_id
    return new_id
  end

  raise 'Unable to generate unique container id'
end

#get_example_count(groups: nil) ⇒ Object (protected)



491
492
493
494
495
496
497
498
499
500
# File 'lib/test-runner/test_evaluator.rb', line 491

def get_example_count(groups: nil)
  cnt = 0

  (groups || @example_groups).each do |grp|
    cnt += grp.examples.count(&:evaluate?)
    cnt += get_example_count(groups: grp.groups)
  end

  cnt
end

#interactiveObject

Run interactive shell



132
133
134
135
136
# File 'lib/test-runner/test_evaluator.rb', line 132

def interactive
  do_run do
    binding.pry # rubocop:disable Lint/Debugger
  end
end

#log(msg) ⇒ Object (protected)



502
503
504
# File 'lib/test-runner/test_evaluator.rb', line 502

def log(msg)
  warn "[#{Time.now}] #{msg}"
end

#machine_class_for(config) ⇒ Object (protected)



521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/test-runner/test_evaluator.rb', line 521

def machine_class_for(config)
  klass = Hook.call(:machine_class_for, args: [config])
  return klass if klass

  case config.spin
  when 'vpsadminos'
    OsVm::VpsadminosMachine
  when 'nixos'
    OsVm::NixosMachine
  else
    raise ArgumentError, "Unknown machine spin #{config.spin.inspect}"
  end
end

#pending(message = '', skip: false, &block) ⇒ Object

Create a pending example or mark the currently evaluated example as pending

Parameters:

  • message (String) (defaults to: '')
  • skip (Boolean) (defaults to: false)


229
230
231
232
233
234
235
# File 'lib/test-runner/test_evaluator.rb', line 229

def pending(message = '', skip: false, &block)
  if block || @current_example.nil?
    it(message, pending: true, skip:, &block)
  else
    @current_example.send(:set_pending)
  end
end

#run {|one| ... } ⇒ Hash<String, TestScriptResult>

Run the test scripts

Yield Parameters:

Returns:



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
# File 'lib/test-runner/test_evaluator.rb', line 81

def run
  ret = {}
  script_results = []
  test_started_at = Time.now

  do_run do
    @scripts.each do |script|
      success = false
      t1 = Time.now

      begin
        log "Running script #{script.name}"

        test_script(script.name) do |progress|
          if progress[:type] == :example
            example_result = progress[:result].to_h(
              script: script.name,
              progress: progress[:progress],
              total: progress[:total]
            )

            yield(example_result) if block_given?
          end
        end

        t2 = Time.now
        log "Script #{script.name} finished in #{(t2 - t1).round(2)}s"
      rescue Exception => e # rubocop:disable Lint/RescueException
        t2 = Time.now
        log "Exception occurred while running script #{script.name} in #{(t2 - t1).round(2)}s"
        log e.full_message
      else
        success = true
      end

      script_result = TestScriptResult.new(script, success, t2 - t1)

      ret[script.name] = script_result
      script_results << script_result
      call_after_test_script_hook(script_result)
      yield(script_result) if block_given?
    end

    test_result = build_test_result(script_results, Time.now - test_started_at)
    call_after_test_run_hook(test_result)
  end

  ret
end

#run_examplesObject (protected)



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/test-runner/test_evaluator.rb', line 430

def run_examples
  example_count = get_example_count
  i = 1

  log 'Evaluating examples'

  @before.each(&:call)

  results = ExampleOrdering.sort_by_order(@example_groups, @example_config.default_order).map do |grp|
    grp.evaluate do |type, example_or_result|
      if type == :before
        log "[#{i}/#{example_count}] Evaluating '#{example_or_result.full_message}'"
        @current_example = example_or_result
      else
        result = example_or_result

        status =
          if result.success?
            if result.pending?
              'pending'
            elsif result.skip?
              'skipped'
            else
              'succeeded'
            end
          elsif result.pending?
            'unexpectedly succeeded'
          else
            'failed'
          end

        log "[#{i}/#{example_count}] '#{result.title}' #{status} in #{result.elapsed_time.round(2)}s"

        # No block is given in debug mode
        if block_given?
          yield({ type: :example, progress: i, total: example_count, result: result })
        end

        @current_example = nil

        i += 1
      end
    end
  end.flatten

  @after.each(&:call)

  failed = results.select(&:failure?)
  return if failed.empty?

  warn "\n#{failed.count} examples failed:"

  failed.each do |result|
    warn result.title
    warn result.error
    warn "\n\n"
  end

  raise 'One or more examples failed'
end

#skip(message = '', &block) ⇒ Object

Create a skipped example or mark the currently evaluated example as pending

Parameters:

  • message (String) (defaults to: '')


239
240
241
242
243
244
245
246
# File 'lib/test-runner/test_evaluator.rb', line 239

def skip(message = '', &block)
  if block || @current_example.nil?
    it(message, skip: true, &block)
  else
    @current_example.send(:set_skip)
    throw :skip
  end
end

#start_allObject

Start all machines



139
140
141
# File 'lib/test-runner/test_evaluator.rb', line 139

def start_all
  machines.each_value(&:start)
end

#test_configObject



74
75
76
# File 'lib/test-runner/test_evaluator.rb', line 74

def test_config
  @config.dig('framework', 'testConfig') || {}
end

#test_script(name) ⇒ Object (protected)



416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/test-runner/test_evaluator.rb', line 416

def test_script(name, &)
  @example_config = ExampleConfiguration.new
  @before = []
  @after = []
  @example_groups = []
  @group_stack = []

  binding.eval(@config['testScripts'][name]['script']) # rubocop:disable Security/Eval

  return if @example_groups.empty?

  run_examples(&)
end

#wait_for_block(name:, timeout: @default_timeout) ⇒ any

Wait for block to succeed

Yield until the code block returns a truthy value or a timeout is reached. RSpec expectation failures inside the block are treated as a falsey return value and retried until the timeout.

Parameters:

  • name (String)

    block name for error reporting

  • timeout (Integer) (defaults to: @default_timeout)

Returns:

  • (any)

    yielded value

Raises:

  • (OsVm::TimeoutError)


276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/test-runner/test_evaluator.rb', line 276

def wait_for_block(name:, timeout: @default_timeout)
  t1 = Time.now
  cur_timeout = timeout

  loop do
    ret =
      begin
        yield
      rescue RSpec::Expectations::ExpectationNotMetError
        false
      end
    return ret if ret

    cur_timeout = timeout - (Time.now - t1)

    if cur_timeout <= 0
      raise OsVm::TimeoutError, "Timeout occurred while waiting for #{name}"
    end

    sleep(1)
  end
end

#wait_until_block_fails(name:, timeout: @default_timeout) ⇒ any

Wait for a block that eventually fails

Intended for polling code that is expected to start failing with OsVm::CommandFailed. OsVm::CommandSucceeded is treated as a retryable result, because the block may succeed before it starts failing. If no exception is raised, the method returns once the block yields a falsey value. RSpec expectations are not handled here because they cannot be reliably distinguished.

Parameters:

  • name (String)

    block name for error reporting

  • timeout (Integer) (defaults to: @default_timeout)

Returns:

  • (any)

    block return value or true when failure is signaled with OsVm::CommandFailed

Raises:

  • (OsVm::TimeoutError)

    when the block does not fail in time



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/test-runner/test_evaluator.rb', line 351

def wait_until_block_fails(name:, timeout: @default_timeout)
  t1 = Time.now
  cur_timeout = timeout

  loop do
    begin
      ret = yield
    rescue OsVm::CommandFailed
      return true
    rescue OsVm::CommandSucceeded
      ret = true
    end

    return ret unless ret

    cur_timeout = timeout - (Time.now - t1)

    if cur_timeout <= 0
      raise OsVm::TimeoutError, "Timeout occurred while waiting for #{name} to fail"
    end

    sleep(1)
  end
end

#wait_until_block_succeeds(name:, timeout: @default_timeout) ⇒ any

Wait for a block that eventually succeeds

Intended for polling code that can temporarily fail with OsVm::CommandFailed (treated as a retry) or signal success via OsVm::CommandSucceeded. RSpec expectation failures are also treated as retryable. The method returns as soon as the block yields a truthy value or raises OsVm::CommandSucceeded.

Parameters:

  • name (String)

    block name for error reporting

  • timeout (Integer) (defaults to: @default_timeout)

Returns:

  • (any)

    block return value or true when success is signaled with OsVm::CommandSucceeded

Raises:

  • (OsVm::TimeoutError)

    when the block does not succeed in time



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/test-runner/test_evaluator.rb', line 312

def wait_until_block_succeeds(name:, timeout: @default_timeout)
  t1 = Time.now
  cur_timeout = timeout

  loop do
    ret =
      begin
        yield
      rescue OsVm::CommandFailed, RSpec::Expectations::ExpectationNotMetError
        false
      rescue OsVm::CommandSucceeded
        true
      end
    return ret if ret

    cur_timeout = timeout - (Time.now - t1)

    if cur_timeout <= 0
      raise OsVm::TimeoutError, "Timeout occurred while waiting for #{name} to succeed"
    end

    sleep(1)
  end
end