diff --git a/mugentest.txt b/mugentest.txt new file mode 100644 index 0000000000000000000000000000000000000000..21c5fe5177399ef406b2fdf33eeb5e11fddfc70f --- /dev/null +++ b/mugentest.txt @@ -0,0 +1,157 @@ +import argparse +import os +import subprocess +import logging +import textwrap +import sys + +from oebuild.command import OebuildCommand + +logger = logging.getLogger() + +class MugenTest(OebuildCommand): + name = 'mugentest' + help_msg = 'This command allows you to run Mugen test cases for openEuler Embedded OS.' + description = textwrap.dedent('''\ + Run Mugen test cases for openEuler Embedded systems. + Select the environment (qemu or BSP) and specify the test case from a predefined list. + ''') + + def __init__(self): + super().__init__( + name=self.name, + help_msg=self.help_msg, + description=self.description + ) + + def do_add_parser(self, parser_adder) -> argparse.ArgumentParser: + parser = self._parser( + parser_adder, + usage=textwrap.dedent('''\ + %(prog)s --env --mugen-path [other options] + Then select the test suite from the following options: + 1 -- Tiny Image Test + 2 -- OS Basic Test + 3 -- Embedded Security Config Test + 4 -- Embedded Application Development Test + ''') + ) + + # Add arguments for environment, Mugen path, and remote details for qemu + parser.add_argument('--env', choices=['qemu', 'bsp'], required=True, help='Specify the test environment: qemu or bsp') + parser.add_argument('--mugen-path', required=False, help='Specify the path to the Mugen installation') + + # For QEMU environment: add options for kernel and initrd paths + parser.add_argument('--kernal_img_path', required=False, help='Path to the QEMU kernel image (required for certain tests)') + parser.add_argument('--initrd_path', required=False, help='Path to the QEMU initrd image (required for certain tests)') + + # For QEMU environment remote login details + parser.add_argument('--ip', required=False, help='IP address for remote testing (required for qemu)') + parser.add_argument('--user', required=False, help='Username for remote login (required for qemu)') + parser.add_argument('--password', required=False, help='Password for remote login (required for qemu)') + parser.add_argument('--port', required=False, default=22, help='SSH port (default is 22, required for qemu)') + + return parser + + def do_run(self, args: argparse.Namespace, unknown=None): + # Handle help option + if '-h' in unknown or '--help' in unknown: + self.print_help_msg() + sys.exit(0) + + # Parse additional arguments + args = args.parse_args(unknown) + + # Check Mugen path + mugen_path = self.get_mugen_path(args.mugen_path) + + if not self.is_mugen_installed(mugen_path): + print(f"Mugen not found at {mugen_path}. Please install Mugen first or specify the correct path.") + sys.exit(1) + + # Environment check for qemu + if args.env == "qemu": + if not args.ip or not args.user or not args.password: + logger.error("For qemu environment, --ip, --user, and --password are required.") + return + + # Let user choose test suite + self.select_test_suite(mugen_path, args) + + def get_mugen_path(self, custom_path=None): + if custom_path: + return custom_path + return os.getenv('MUGEN_HOME', os.path.expanduser("~/.local/mugen")) + + def is_mugen_installed(self, mugen_path): + return os.path.exists(mugen_path) + + def select_test_suite(self, mugen_path, args): + # Define test suite options + test_suites = { + 1: "embedded_tiny_image_test", + 2: "embedded_os_basic_test", + 3: "embedded_security_config_test", + 4: "embedded_application_develop_tests" + } + + # Present options to the user + print("Select a test suite to run:") + for i, suite in test_suites.items(): + print(f"{i} -- {suite.replace('_', ' ').capitalize()}") + + # Capture user choice + choice = int(input(f"Enter the number of the test suite to run (1-{len(test_suites)}): ")) + + # Ensure valid input + if choice not in test_suites: + print("Invalid choice. Exiting.") + return + + selected_suite = test_suites[choice] + self.run_mugen_test(mugen_path, selected_suite, args) + + def run_mugen_test(self, mugen_path, suite, args): + try: + print(f"Running {suite} with Mugen...") + # For qemu environment + if args.env == "qemu": + if suite == "embedded_tiny_image_test": + # Tiny image test logic + cmd = f"bash {mugen_path}/mugen.sh -c --ip {args.ip} --password {args.password} --user {args.user} --port {args.port} --put_all --run_remote" + else: + # Other qemu-based tests require starting QEMU with kernel and initrd paths + if not args.kernal_img_path or not args.initrd_path: + logger.error("For this test, --kernal_img_path and --initrd_path are required.") + return + # Start QEMU for OS basic or security or application develop tests + qemu_start_cmd = f"sh qemu_ctl.sh start --put_all --kernal_img_path {args.kernal_img_path} --initrd_path {args.initrd_path}" + if suite == "embedded_os_basic_test" or suite == "embedded_security_config_test" or suite == "embedded_application_develop_tests": + qemu_start_cmd += " --qemu_type arm" # Add this flag if ARM architecture is needed + subprocess.run(qemu_start_cmd, shell=True, check=True) + + # Run the actual test suite + if suite == "embedded_application_develop_tests": + # Compile before running the develop tests + compile_cmd = f"bash {mugen_path}/mugen.sh -b {suite}" + subprocess.run(compile_cmd, shell=True, check=True) + + # Run the selected test suite + cmd = f"bash {mugen_path}/mugen.sh -f {suite} -s" + + elif args.env == "bsp": + # For BSP environment + cmd = f"bash {mugen_path}/mugen.sh -f {suite} -s" + + # Execute the test suite command + subprocess.run(cmd, shell=True, check=True) + print(f"Test suite {suite} completed successfully.") + + # Stop QEMU after the tests for qemu environment + if args.env == "qemu" and suite != "embedded_tiny_image_test": + subprocess.run("sh qemu_ctl.sh stop", shell=True, check=True) + + except subprocess.CalledProcessError as e: + logger.error(f"Failed to run test suite {suite}: {e}") + sys.exit(1) +