Repository navigation
Functest: Support Dynamic tests #1237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module AutoHCK | ||
| module Functest | ||
| # Namespace for dynamic test class factories. Each dynamic test's .rb file | ||
| # defines a method here named after the JSON basename (e.g. vm_pause_resume) | ||
| # that returns the DynamicTest subclass to instantiate. | ||
| module DynamicTests | ||
| end | ||
|
|
||
| # Base class for Ruby-driven functest cases. JSON still supplies metadata | ||
| # (name, clients, snapshots, timeout, …); when `"dynamic": true`, AutoHCK | ||
| # loads the sibling .rb and delegates pre/steps/cleanup here. | ||
| # | ||
| # Subclasses receive the same runtime handles TestExecutor uses: | ||
| # @project, @clients, @tools, @context, @command_execution_manager, | ||
| # @step_handler, and @logger. | ||
| class DynamicTest | ||
| def initialize(executor) | ||
| @project = executor.project | ||
| @clients = executor.clients | ||
| @tools = executor.tools | ||
| @context = executor.context | ||
| @command_execution_manager = executor.command_execution_manager | ||
| @step_handler = executor.step_handler | ||
| @logger = @project.logger | ||
| @executor = executor | ||
| end | ||
|
|
||
| def run_pre_test_commands(_test); end | ||
|
|
||
| def execute_test_steps(_test, _result) | ||
| raise NotImplementedError, "#{self.class} must implement #execute_test_steps" | ||
| end | ||
|
|
||
| def run_cleanup(_test); end | ||
|
|
||
| protected | ||
|
|
||
| # Runs a CommandInfo step through StepHandler and appends a step result | ||
| # entry, matching JSON-driven step reporting. | ||
| def execute_and_record(step, result) | ||
| index = result[:steps].length | ||
| result[:steps] << @executor.execute_test_step(step, index) | ||
| end | ||
|
|
||
| # Runs via CommandExecutionManager (no StepHandler output checks) and | ||
| # records a step result. Returns the raw execute() hash. | ||
| def execute_raw_and_record(step, result) | ||
| index = result[:steps].length | ||
| desc = @context.substitute_variables(step.desc) | ||
| start_time = Time.now | ||
| step_result = { | ||
| index: index, description: desc, status: 'running', | ||
| start_time: start_time.utc.iso8601 | ||
| } | ||
| outputs = @command_execution_manager.execute(step, replacement: @context.replacement_map) | ||
| finalize_recorded_step(step_result, result, start_time, 'passed') | ||
| outputs | ||
| rescue StandardError => e | ||
| finalize_recorded_step(step_result, result, start_time, 'failed', e.message) | ||
| raise | ||
| end | ||
|
|
||
| def qmp_command(execute, arguments: nil, desc: nil, timeout: 30) | ||
| Models::CommandInfo.new( | ||
| desc: desc || "QMP: #{execute}", | ||
| qmp_command: Models::QmpCommandConfig.new(execute: execute, arguments: arguments), | ||
| timeout: timeout | ||
| ) | ||
| end | ||
|
|
||
| def guest_run(command, desc:, expected_output_contains: nil, timeout: 60) | ||
| Models::CommandInfo.new( | ||
| desc: desc, | ||
| guest_run: command, | ||
| expected_output_contains: expected_output_contains, | ||
| timeout: timeout | ||
| ) | ||
| end | ||
|
|
||
| def primary_qmp_result(outputs, step) | ||
| machine = @command_execution_manager.primary_machine(step) | ||
| outputs.fetch(:qmp_result).fetch(machine) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def finalize_recorded_step(step_result, result, start_time, status, error = nil) | ||
| end_time = Time.now | ||
| step_result[:status] = status | ||
| step_result[:error] = error if error | ||
| step_result[:end_time] = end_time.utc.iso8601 | ||
| step_result[:duration] = end_time - start_time | ||
| log_recorded_step(step_result, status, error) | ||
| result[:steps] << step_result | ||
| end | ||
|
|
||
| def log_recorded_step(step_result, status, error) | ||
| if status == 'passed' | ||
| @logger.info(" PASS: #{step_result[:description]}") | ||
| else | ||
| @logger.error(" FAIL: #{step_result[:description]} - #{error}") | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,8 @@ class TestCase < T::Struct | |
| const :extra_software, T::Array[String], default: [] | ||
| const :pre_test_commands, T::Array[Models::CommandInfo], default: [] | ||
| const :cycles, Integer, default: 1 | ||
| prop :test_steps, T::Array[Models::CommandInfo] | ||
| # Empty for dynamic tests; JSON-driven cases still supply steps. | ||
| prop :test_steps, T::Array[Models::CommandInfo], default: [] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep For ordinary JSON Reject an omitted
|
||
| const :cleanup, T::Array[Models::CommandInfo], default: [] | ||
|
|
||
| # Client ids (e.g. 1, 2) this test case needs booted, by position in | ||
|
|
@@ -32,6 +33,13 @@ class TestCase < T::Struct | |
| # tests can start from via boot_from_snapshot_tag. | ||
| const :save_snapshot_as, T.nilable(String) | ||
|
|
||
| # When true, AutoHCK loads the sibling .rb (see dynamic_path) and | ||
| # delegates pre/steps/cleanup to a DynamicTest subclass. Snapshots, | ||
| # client scoping, and ordering stay under TestExecutor control. | ||
| const :dynamic, T::Boolean, default: false | ||
| # Absolute path to the Ruby implementation; set by TestLoader, not JSON. | ||
| const :dynamic_path, T.nilable(String) | ||
|
|
||
| sig { returns(String) } | ||
| def safe_name | ||
| name.gsub(/[^\w\-.]/, '_').gsub(/(^_|_$)/, '') | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,8 @@ module Functest | |
| class TestExecutor | ||
| include Helper | ||
|
|
||
| attr_reader :results, :context | ||
| attr_reader :results, :context, :project, :clients, :tools, | ||
| :command_execution_manager, :step_handler | ||
|
|
||
| def test_results | ||
| @results.map { |r| Models::TestResult.from_functest(r) } | ||
|
|
@@ -90,6 +91,19 @@ def summary | |
| passed_with_known_issue: passed_with_known_issue, results: @results } | ||
| end | ||
|
|
||
| # Public so DynamicTest#execute_and_record can share step result formatting. | ||
| def execute_test_step(step, index) | ||
| desc = @context.substitute_variables(step.desc) | ||
| start_time = Time.now | ||
| step_result = { index: index, description: desc, status: 'running', start_time: start_time.utc.iso8601 } | ||
| run_step(step, step_result, desc) | ||
| step_result | ||
| ensure | ||
| end_time = Time.now | ||
| step_result[:end_time] = end_time.utc.iso8601 | ||
| step_result[:duration] = end_time - start_time | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def summary_results_entries | ||
|
|
@@ -224,10 +238,13 @@ def run_test_steps(test, result) | |
| # clients, then fails the test right away if any step (including | ||
| # pre_test_commands, cleanup, and parallel branches) targets an | ||
| # undeclared client or violates a parallel execution constraint. | ||
| # Dynamic tests own their steps in Ruby, so JSON step validation is skipped. | ||
| def prepare_test_clients!(test) | ||
| raise EngineError, "Test '#{test.name}' has an empty clients list" if test.clients.empty? | ||
|
|
||
| @command_execution_manager.scope_to_test(test.clients) | ||
| return if test.dynamic | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reject dynamic-test step targets outside the test's declared clients. JSON tests reject any step that targets a client outside Apply the same declared-client rule to Ruby-created steps before dispatch, including parallel branch targets. Reject an explicit target outside the test's scoped clients, as the JSON path does. Keep an empty step-level Impact is limited to authoring errors in in-repo dynamic tests. It does not expose untrusted input.
|
||
|
|
||
| validate_step_clients!(test) | ||
| validate_parallel_blocks!(test) | ||
| end | ||
|
|
@@ -369,6 +386,8 @@ def capture_output_owners(branches) | |
| end | ||
|
|
||
| def execute_test_steps(test, result) | ||
| return dynamic_impl_for(test).execute_test_steps(test, result) if test.dynamic | ||
|
|
||
| test.test_steps.each_with_index { |step, index| result[:steps] << execute_test_step(step, index) } | ||
| end | ||
|
|
||
|
|
@@ -408,18 +427,6 @@ def save_snapshot_if_needed(test) | |
| @created_tags << tag | ||
| end | ||
|
|
||
| def execute_test_step(step, index) | ||
| desc = @context.substitute_variables(step.desc) | ||
| start_time = Time.now | ||
| step_result = { index: index, description: desc, status: 'running', start_time: start_time.utc.iso8601 } | ||
| run_step(step, step_result, desc) | ||
| step_result | ||
| ensure | ||
| end_time = Time.now | ||
| step_result[:end_time] = end_time.utc.iso8601 | ||
| step_result[:duration] = end_time - start_time | ||
| end | ||
|
|
||
| def run_step(step, step_result, desc) | ||
| @step_handler.execute_step(step) | ||
| step_result[:status] = 'passed' | ||
|
|
@@ -441,6 +448,7 @@ def log_section(title) | |
| end | ||
|
|
||
| def run_pre_test_commands(test) | ||
| return dynamic_impl_for(test).run_pre_test_commands(test) if test.dynamic | ||
| return if test.pre_test_commands.empty? | ||
|
|
||
| @logger.info('Running pre-test commands...') | ||
|
|
@@ -455,6 +463,7 @@ def run_pre_test_commands(test) | |
| end | ||
|
|
||
| def run_cleanup(test) | ||
| return run_dynamic_cleanup(test) if test.dynamic | ||
| return if test.cleanup.empty? | ||
|
|
||
| @logger.info('Running cleanup steps...') | ||
|
|
@@ -465,6 +474,18 @@ def run_cleanup(test) | |
| end | ||
| end | ||
|
|
||
| def run_dynamic_cleanup(test) | ||
| @logger.info('Running dynamic test cleanup...') | ||
| dynamic_impl_for(test).run_cleanup(test) | ||
| rescue StandardError => e | ||
| @logger.warn("Dynamic cleanup failed (ignoring): #{e.message}") | ||
| end | ||
|
|
||
| def dynamic_impl_for(test) | ||
| @dynamic_impls ||= {} | ||
| @dynamic_impls[test.name] ||= TestLoader.load_dynamic_test_class(test).new(self) | ||
| end | ||
|
|
||
| def collect_memory_dumps(test_name) | ||
| id = test_name.gsub(/\W/, '_') | ||
| MemoryDumpCollector.new(@tools, @machine_names, @project.workspace_path, @logger).collect(id) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ def load_test(test_name) | |
| @logger.info("Loading test case: #{test_path}") | ||
| definition = AutoHCK::Helper::Json.read_json(test_path, @logger) | ||
| resolve_test_paths(definition) | ||
| attach_dynamic_path!(definition, test_path) | ||
| TestCase.from_hash(definition) | ||
| end | ||
|
|
||
|
|
@@ -39,8 +40,48 @@ def load_suite_tests(suite) | |
| suite.tests.map { |test_name| load_test(test_name) } | ||
| end | ||
|
|
||
| # Resolves and validates the DynamicTest subclass for a dynamic case. | ||
| # Named after the JSON basename (e.g. vm_pause_resume), matching how | ||
| # QemuHCK dynamic devices expose factory methods. | ||
| def self.load_dynamic_test_class(test) | ||
| raise EngineError, "Test '#{test.name}' is not dynamic" unless test.dynamic | ||
|
|
||
| path = test.dynamic_path | ||
| raise InvalidConfigFile, "Dynamic test path missing for '#{test.name}'" if path.nil? || path.empty? | ||
|
|
||
| require path | ||
|
|
||
| method_name = File.basename(path, '.rb') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bind each factory to its implementation path. Cases Use path-bound factory registration and lookup. Do not identify implementations by basename alone.
|
||
| unless DynamicTests.respond_to?(method_name) | ||
| raise InvalidConfigFile, "DynamicTests.#{method_name} not defined in #{path}" | ||
| end | ||
|
|
||
| klass = DynamicTests.public_send(method_name) | ||
| unless klass.is_a?(Class) && klass < DynamicTest | ||
| raise InvalidConfigFile, "DynamicTests.#{method_name} must return a DynamicTest subclass" | ||
| end | ||
|
|
||
| klass | ||
| end | ||
|
|
||
| def load_dynamic_test_class(test) | ||
| self.class.load_dynamic_test_class(test) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def attach_dynamic_path!(definition, test_path) | ||
| return unless definition['dynamic'] | ||
|
|
||
| ruby_path = test_path.sub(/\.json\z/, '.rb') | ||
| unless File.exist?(ruby_path) | ||
| raise InvalidConfigFile, "Dynamic test '#{definition['name']}' requires #{ruby_path}" | ||
| end | ||
|
|
||
| definition['dynamic_path'] = File.expand_path(ruby_path) | ||
| @logger.info("Dynamic test Ruby implementation: #{definition['dynamic_path']}") | ||
| end | ||
|
|
||
| def resolve_test_paths(definition) | ||
| STEP_COLLECTIONS.each do |collection| | ||
| definition.fetch(collection, []).each { |step| resolve_step_paths(step) } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "vm_pause_resume", | ||
| "description": "Pause and resume the VM via QMP, assert query-status transitions, then verify the guest is still responsive. Works with any driver.", | ||
| "test_system_ref": "", | ||
| "timeout": 180, | ||
| "dynamic": true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module AutoHCK | ||
| module Functest | ||
| module DynamicTests | ||
| def self.vm_pause_resume | ||
| VmPauseResume | ||
| end | ||
|
|
||
| # Ruby rewrite of the QMP stop/cont pattern used by JSON cases such as | ||
| # pvpanic/pvpanic_with_stop_continue, plus query-status checks that are | ||
| # awkward to express as plain JSON expected-output rules. | ||
| class VmPauseResume < DynamicTest | ||
| def execute_test_steps(_test, result) | ||
| assert_status!(result, 'running', 'before pause') | ||
|
|
||
| execute_and_record(qmp_command('stop', desc: 'QMP: stop (pause) the VM'), result) | ||
| assert_status!(result, 'paused', 'while paused') | ||
|
|
||
| execute_and_record(qmp_command('cont', desc: 'QMP: cont (resume) the VM'), result) | ||
| assert_status!(result, 'running', 'after resume') | ||
|
|
||
| execute_and_record( | ||
| guest_run( | ||
| "Write-Output 'PASS: guest alive after pause/resume'", | ||
| desc: 'Verify guest is responsive after pause/resume', | ||
| expected_output_contains: 'PASS:', | ||
| timeout: 60 | ||
| ), | ||
| result | ||
| ) | ||
| end | ||
|
|
||
| def run_cleanup(_test) | ||
| return unless query_status_value == 'paused' | ||
|
|
||
| @logger.warn('VM still paused after test; issuing QMP cont during cleanup') | ||
| @step_handler.execute_step(qmp_command('cont', desc: 'QMP cleanup: resume VM')) | ||
| rescue StandardError => e | ||
| @logger.warn("vm_pause_resume cleanup could not resume VM: #{e.message}") | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def assert_status!(result, expected, label) | ||
| step = qmp_command('query-status', desc: "QMP: query-status #{label}") | ||
| response = primary_qmp_result(execute_raw_and_record(step, result), step) | ||
| status = status_from(response) | ||
| return if status == expected | ||
|
|
||
| raise EngineError, "Expected VM status '#{expected}' #{label}, got '#{status.inspect}'" | ||
| end | ||
|
|
||
| def query_status_value | ||
| step = qmp_command('query-status') | ||
| status_from(primary_qmp_result( | ||
| @command_execution_manager.execute(step, replacement: @context.replacement_map), | ||
| step | ||
| )) | ||
| end | ||
|
|
||
| def status_from(response) | ||
| response.is_a?(Hash) ? response['status'] : response | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Raise an exception that the executor handles.
The loader accepts subclasses that inherit this method. For such a subclass,
NotImplementedErrorbypassesrun_test_stepsbecause that handler rescues onlyStandardError. Ruby 3.3 placesNotImplementedErrorunderScriptError, notStandardError. (docs.ruby-lang.org)The exception interrupts
execute_testsand leaves this result without a failed-status assignment. RaiseEngineErrorinstead. The existing handler can then fail this test and continue the suite.