Class: TestRunner::TestEvaluator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test, scripts, **opts) ⇒ TestEvaluator

Returns a new instance of TestEvaluator.

Parameters:

Options Hash (**opts):

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


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

def initialize(test, scripts, **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
  @config = TestConfig.build(test)
  @opts = opts
  @machines = {}
  @used_container_ids = []

  config['machines'].each do |name, cfg|
    var = :"@#{name}"
    m = OsVm::Machine.new(
      name,
      OsVm::MachineConfig.new(cfg),
      opts[:state_dir],
      opts[:sock_dir],
      default_timeout: opts[:default_timeout],
      hash_base: test.path
    )
    instance_variable_set(var, m)

    define_singleton_method(name) do
      instance_variable_get(var)
    end

    machines[name] = m
  end
end

Instance Attribute Details

#configObject (readonly, protected)

Returns the value of attribute config.



123
124
125
# File 'lib/test-runner/test_evaluator.rb', line 123

def config
  @config
end

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

Returns:

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


7
8
9
# File 'lib/test-runner/test_evaluator.rb', line 7

def machines
  @machines
end

#optsObject (readonly, protected)

Returns the value of attribute opts.



123
124
125
# File 'lib/test-runner/test_evaluator.rb', line 123

def opts
  @opts
end

#scriptsObject (readonly, protected)

Returns the value of attribute scripts.



123
124
125
# File 'lib/test-runner/test_evaluator.rb', line 123

def scripts
  @scripts
end

#testObject (readonly, protected)

Returns the value of attribute test.



123
124
125
# File 'lib/test-runner/test_evaluator.rb', line 123

def test
  @test
end

Instance Method Details

#breakpointObject

Invoke interactive shell from within a test



99
100
101
# File 'lib/test-runner/test_evaluator.rb', line 99

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

#do_runObject (protected)



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/test-runner/test_evaluator.rb', line 129

def do_run
  yield

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

#get_container_id(base = 'testct') ⇒ String

Generate container id that is unique to the test run

Returns:

  • (String)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/test-runner/test_evaluator.rb', line 105

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

#interactiveObject

Run interactive shell



87
88
89
90
91
# File 'lib/test-runner/test_evaluator.rb', line 87

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

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

Run the test scripts

Yield Parameters:

  • one (Hash)

    result per test script

Returns:

  • (Hash<String, Boolean>)

    script name => result



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

def run
  ret = {}

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

      begin
        warn "Running script #{script.name}"
        test_script(script.name)
        warn "Script #{script.name} finished"
      rescue Exception => e # rubocop:disable Lint/RescueException
        warn "Exception occurred while running script #{script.name}"
        warn e.full_message
      else
        success = true
      end

      result = {
        script: script.name,
        success:,
        elapsed_time: Time.now - t1
      }

      ret[script.name] = result
      yield(result) if block_given?
    end
  end

  ret
end

#start_allObject

Start all machines



94
95
96
# File 'lib/test-runner/test_evaluator.rb', line 94

def start_all
  machines.each(&:start)
end

#test_script(name) ⇒ Object (protected)



125
126
127
# File 'lib/test-runner/test_evaluator.rb', line 125

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