From 97b9490765b10b096d99295a8865acdad2655a3c Mon Sep 17 00:00:00 2001 From: Lili Deng Date: Thu, 16 Mar 2023 14:19:10 +0800 Subject: [PATCH] use before_case and after_case replace before_suite and after_suite --- examples/testsuites/helloworld.py | 7 ---- examples/testsuites/withscript.py | 2 +- .../testsuites/cloud_hypervisor/ch_tests.py | 13 +++--- microsoft/testsuites/mshv/mshv_root_tests.py | 2 +- selftests/runners/test_lisa_runner.py | 8 ++-- selftests/test_testsuite.py | 40 ------------------- 6 files changed, 11 insertions(+), 61 deletions(-) diff --git a/examples/testsuites/helloworld.py b/examples/testsuites/helloworld.py index ffb073e47..d056791f3 100644 --- a/examples/testsuites/helloworld.py +++ b/examples/testsuites/helloworld.py @@ -67,13 +67,6 @@ class HelloWorld(TestSuite): node.tools.get(Echo) # Ensure echo is in cache assert_that(str(node.tools.echo("bye!"))).is_equal_to("bye!") - def before_suite(self, log: Logger, **kwargs: Any) -> None: - log.info("setup my test suite") - log.info(f"see my code at {__file__}") - - def after_suite(self, log: Logger, **kwargs: Any) -> None: - log.info("clean up my test suite") - def before_case(self, log: Logger, **kwargs: Any) -> None: log.info("before test case") diff --git a/examples/testsuites/withscript.py b/examples/testsuites/withscript.py index 64c6fc705..96edb2861 100644 --- a/examples/testsuites/withscript.py +++ b/examples/testsuites/withscript.py @@ -26,7 +26,7 @@ from lisa import ( """, ) class WithScript(TestSuite): - def before_suite(self, log: Logger, **kwargs: Any) -> None: + def before_case(self, log: Logger, **kwargs: Any) -> None: self._echo_script = CustomScriptBuilder( Path(__file__).parent.joinpath("scripts"), ["echo.sh"] ) diff --git a/microsoft/testsuites/cloud_hypervisor/ch_tests.py b/microsoft/testsuites/cloud_hypervisor/ch_tests.py index 0aec0dedd..3924e463a 100644 --- a/microsoft/testsuites/cloud_hypervisor/ch_tests.py +++ b/microsoft/testsuites/cloud_hypervisor/ch_tests.py @@ -30,22 +30,19 @@ from microsoft.testsuites.cloud_hypervisor.ch_tests_tool import CloudHypervisorT """, ) class CloudHypervisorTestSuite(TestSuite): - def before_suite(self, log: Logger, **kwargs: Any) -> None: - node = kwargs["node"] - node.tools[Modprobe].load("openvswitch") - - def after_suite(self, log: Logger, **kwargs: Any) -> None: - node = kwargs["node"] - node.tools[Modprobe].remove(["openvswitch"]) - def before_case(self, log: Logger, **kwargs: Any) -> None: node = kwargs["node"] if not isinstance(node.os, (CBLMariner, Ubuntu)): raise SkippedException( f"Cloud Hypervisor tests are not implemented in LISA for {node.os.name}" ) + node.tools[Modprobe].load("openvswitch") self._ensure_virtualization_enabled(node) + def after_case(self, log: Logger, **kwargs: Any) -> None: + node = kwargs["node"] + node.tools[Modprobe].remove(["openvswitch"]) + @TestCaseMetadata( description=""" Runs cloud-hypervisor integration tests. diff --git a/microsoft/testsuites/mshv/mshv_root_tests.py b/microsoft/testsuites/mshv/mshv_root_tests.py index c8aeb58e6..2bb9014d1 100644 --- a/microsoft/testsuites/mshv/mshv_root_tests.py +++ b/microsoft/testsuites/mshv/mshv_root_tests.py @@ -39,7 +39,7 @@ class MshvHostTestSuite(TestSuite): HYPERVISOR_FW_NAME = "hypervisor-fw" DISK_IMG_NAME = "vm_disk_img.raw" - def before_suite(self, log: Logger, **kwargs: Any) -> None: + def before_case(self, log: Logger, **kwargs: Any) -> None: node = kwargs["node"] if not node.tools[Ls].path_exists("/dev/mshv", sudo=True): raise SkippedException("This suite is for MSHV root partition only") diff --git a/selftests/runners/test_lisa_runner.py b/selftests/runners/test_lisa_runner.py index 30bddf0aa..300c973c8 100644 --- a/selftests/runners/test_lisa_runner.py +++ b/selftests/runners/test_lisa_runner.py @@ -416,9 +416,9 @@ class RunnerTestCase(TestCase): test_results=test_results, ) - def test_skipped_on_suite_failure(self) -> None: + def test_skipped_on_case_failure(self) -> None: # First two tests were skipped because the setup is made to fail. - test_testsuite.fail_on_before_suite = True + test_testsuite.fail_on_before_case = True test_testsuite.generate_cases_metadata() env_runbook = generate_env_runbook(is_single_env=True, local=True, remote=True) runner = generate_runner(env_runbook) @@ -431,7 +431,7 @@ class RunnerTestCase(TestCase): runner=runner, ) - before_suite_failed = "before_suite: failed" + before_case_failed = "before_case: failed" self.verify_test_results( expected_test_order=["mock_ut1", "mock_ut2", "mock_ut3"], expected_envs=["customized_0", "customized_0", "customized_0"], @@ -440,7 +440,7 @@ class RunnerTestCase(TestCase): TestStatus.SKIPPED, TestStatus.PASSED, ], - expected_message=[before_suite_failed, before_suite_failed, ""], + expected_message=[before_case_failed, before_case_failed, ""], test_results=test_results, ) diff --git a/selftests/test_testsuite.py b/selftests/test_testsuite.py index e1810184c..0c0bf5881 100644 --- a/selftests/test_testsuite.py +++ b/selftests/test_testsuite.py @@ -28,8 +28,6 @@ from lisa.util.logger import Logger from selftests.test_environment import generate_runbook # for other UTs -fail_on_before_suite = False -fail_on_after_suite = False fail_on_before_case = False fail_on_after_case = False partial_pass = False @@ -42,8 +40,6 @@ check_variable = False class MockTestSuite(TestSuite): def __init__(self, *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) - self.fail_on_before_suite = fail_on_before_suite - self.fail_on_after_suite = fail_on_after_suite self.fail_on_before_case = fail_on_before_case self.fail_on_after_case = fail_on_after_case self.partial_pass = partial_pass @@ -55,8 +51,6 @@ class MockTestSuite(TestSuite): def set_fail_phase( self, - fail_on_before_suite: bool = False, - fail_on_after_suite: bool = False, fail_on_before_case: bool = False, fail_on_after_case: bool = False, partial_pass: bool = False, @@ -64,8 +58,6 @@ class MockTestSuite(TestSuite): fail_case_count: int = 0, check_variable: bool = False, ) -> None: - self.fail_on_before_suite = fail_on_before_suite - self.fail_on_after_suite = fail_on_after_suite self.fail_on_before_case = fail_on_before_case self.fail_on_after_case = fail_on_after_case self.partial_pass = partial_pass @@ -73,14 +65,6 @@ class MockTestSuite(TestSuite): self.fail_case_count = fail_case_count self.check_variable = check_variable - def before_suite(self, log: Logger, **kwargs: Any) -> None: - if self.fail_on_before_suite: - raise LisaException("failed") - - def after_suite(self, log: Logger, **kwargs: Any) -> None: - if self.fail_on_after_suite: - raise LisaException("failed") - def before_case(self, log: Logger, **kwargs: Any) -> None: if self.fail_on_before_case: raise LisaException("failed") @@ -226,30 +210,6 @@ class TestSuiteTestCase(TestCase): else: self.assertEqual(False, result.is_queued) - def test_skip_before_suite_failed(self) -> None: - test_suite = self.generate_suite_instance() - test_suite.set_fail_phase(fail_on_before_suite=True) - test_suite.start( - environment=self.default_env, - case_results=self.case_results, - case_variables={}, - ) - for result in self.case_results: - self.assertEqual(TestStatus.SKIPPED, result.status) - self.assertEqual("before_suite: failed", result.message) - - def test_pass_after_suite_failed(self) -> None: - test_suite = self.generate_suite_instance() - test_suite.set_fail_phase(fail_on_after_suite=True) - test_suite.start( - environment=self.default_env, - case_results=self.case_results, - case_variables={}, - ) - for result in self.case_results: - self.assertEqual(TestStatus.PASSED, result.status) - self.assertEqual("", result.message) - def test_variable_exists(self) -> None: test_suite = self.generate_suite_instance() test_suite.set_fail_phase(check_variable=True)