Class: TestRunner::TestEvaluator
- Inherits:
-
Object
- Object
- TestRunner::TestEvaluator
- Defined in:
- lib/test-runner/test_evaluator.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
protected
Returns the value of attribute config.
- #machines ⇒ Hash<String, OsVm::Machine> readonly
-
#opts ⇒ Object
readonly
protected
Returns the value of attribute opts.
-
#scripts ⇒ Object
readonly
protected
Returns the value of attribute scripts.
-
#test ⇒ Object
readonly
protected
Returns the value of attribute test.
Instance Method Summary collapse
-
#breakpoint ⇒ Object
Invoke interactive shell from within a test.
- #do_run ⇒ Object protected
-
#get_container_id(base = 'testct') ⇒ String
Generate container id that is unique to the test run.
-
#initialize(test, scripts, **opts) ⇒ TestEvaluator
constructor
A new instance of TestEvaluator.
-
#interactive ⇒ Object
Run interactive shell.
-
#run {|one| ... } ⇒ Hash<String, Boolean>
Run the test scripts.
-
#start_all ⇒ Object
Start all machines.
- #test_script(name) ⇒ Object protected
Constructor Details
#initialize(test, scripts, **opts) ⇒ TestEvaluator
Returns a new instance of TestEvaluator.
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
#config ⇒ Object (readonly, protected)
Returns the value of attribute config.
123 124 125 |
# File 'lib/test-runner/test_evaluator.rb', line 123 def config @config end |
#machines ⇒ Hash<String, OsVm::Machine> (readonly)
7 8 9 |
# File 'lib/test-runner/test_evaluator.rb', line 7 def machines @machines end |
#opts ⇒ Object (readonly, protected)
Returns the value of attribute opts.
123 124 125 |
# File 'lib/test-runner/test_evaluator.rb', line 123 def opts @opts end |
#scripts ⇒ Object (readonly, protected)
Returns the value of attribute scripts.
123 124 125 |
# File 'lib/test-runner/test_evaluator.rb', line 123 def scripts @scripts end |
#test ⇒ Object (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
#breakpoint ⇒ Object
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_run ⇒ Object (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
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 |
#interactive ⇒ Object
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
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. 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_all ⇒ Object
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 |