Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 1 | import asyncio |
| 2 | import os |
| 3 | import pathlib |
| 4 | import subprocess |
| 5 | import tempfile |
| 6 | import unittest.mock |
| 7 | |
| 8 | from oeqa.selftest.case import OESelftestTestCase |
| 9 | |
| 10 | runfvp = pathlib.Path(__file__).parents[5] / "scripts" / "runfvp" |
| 11 | testdir = pathlib.Path(__file__).parent / "tests" |
| 12 | |
| 13 | class RunFVPTests(OESelftestTestCase): |
| 14 | def setUpLocal(self): |
| 15 | self.assertTrue(runfvp.exists()) |
| 16 | |
| 17 | def run_fvp(self, *args, should_succeed=True): |
| 18 | """ |
| 19 | Call runfvp passing any arguments. If check is True verify return stdout |
| 20 | on exit code 0 or fail the test, otherwise return the CompletedProcess |
| 21 | instance. |
| 22 | """ |
| 23 | # Put the test directory in PATH so that any mock FVPs are found first |
| 24 | newenv = {"PATH": str(testdir) + ":" + os.environ["PATH"]} |
| 25 | cli = [runfvp,] + list(args) |
| 26 | print(f"Calling {cli}") |
| 27 | ret = subprocess.run(cli, env=newenv, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) |
| 28 | if should_succeed: |
| 29 | self.assertEqual(ret.returncode, 0, f"runfvp exit {ret.returncode}, output: {ret.stdout}") |
| 30 | return ret.stdout |
| 31 | else: |
| 32 | self.assertNotEqual(ret.returncode, 0, f"runfvp exit {ret.returncode}, output: {ret.stdout}") |
| 33 | return ret.stdout |
| 34 | |
| 35 | def test_help(self): |
| 36 | output = self.run_fvp("--help") |
| 37 | self.assertIn("Run images in a FVP", output) |
| 38 | |
| 39 | def test_bad_options(self): |
| 40 | self.run_fvp("--this-is-an-invalid-option", should_succeed=False) |
| 41 | |
| 42 | def test_run_auto_tests(self): |
| 43 | newenv = {"PATH": str(testdir) + ":" + os.environ["PATH"]} |
| 44 | |
| 45 | cases = list(testdir.glob("auto-*.json")) |
| 46 | if not cases: |
| 47 | self.fail("No tests found") |
| 48 | for case in cases: |
| 49 | with self.subTest(case=case.stem): |
| 50 | self.run_fvp(case) |
| 51 | |
| 52 | def test_fvp_options(self): |
| 53 | # test-parameter sets one argument, add another manually |
| 54 | self.run_fvp(testdir / "test-parameter.json", "--", "--parameter", "board.dog=woof") |
| 55 | |
| 56 | class ConfFileTests(OESelftestTestCase): |
| 57 | def test_no_exe(self): |
| 58 | from fvp import conffile |
| 59 | with tempfile.NamedTemporaryFile('w') as tf: |
| 60 | tf.write('{}') |
| 61 | tf.flush() |
| 62 | |
| 63 | with self.assertRaises(ValueError): |
| 64 | conffile.load(tf.name) |
| 65 | |
| 66 | def test_minimal(self): |
| 67 | from fvp import conffile |
| 68 | with tempfile.NamedTemporaryFile('w') as tf: |
| 69 | tf.write('{"exe": "FVP_Binary"}') |
| 70 | tf.flush() |
| 71 | |
| 72 | conf = conffile.load(tf.name) |
| 73 | self.assertTrue('fvp-bindir' in conf) |
| 74 | self.assertTrue('fvp-bindir' in conf) |
| 75 | self.assertTrue("exe" in conf) |
| 76 | self.assertTrue("parameters" in conf) |
| 77 | self.assertTrue("data" in conf) |
| 78 | self.assertTrue("applications" in conf) |
| 79 | self.assertTrue("terminals" in conf) |
| 80 | self.assertTrue("args" in conf) |
| 81 | self.assertTrue("consoles" in conf) |
| 82 | |
| 83 | |
| 84 | class RunnerTests(OESelftestTestCase): |
| 85 | def create_mock(self): |
| 86 | return unittest.mock.patch("asyncio.create_subprocess_exec") |
| 87 | |
| 88 | def test_start(self): |
| 89 | from fvp import runner |
| 90 | with self.create_mock() as m: |
| 91 | fvp = runner.FVPRunner(self.logger) |
| 92 | asyncio.run(fvp.start({ |
| 93 | "fvp-bindir": "/usr/bin", |
| 94 | "exe": "FVP_Binary", |
| 95 | "parameters": {'foo': 'bar'}, |
| 96 | "data": ['data1'], |
| 97 | "applications": {'a1': 'file'}, |
| 98 | "terminals": {}, |
| 99 | "args": ['--extra-arg'], |
| 100 | })) |
| 101 | |
| 102 | m.assert_called_once_with('/usr/bin/FVP_Binary', |
| 103 | '--parameter', 'foo=bar', |
| 104 | '--data', 'data1', |
| 105 | '--application', 'a1=file', |
| 106 | '--extra-arg', |
| 107 | stdin=unittest.mock.ANY, |
| 108 | stdout=unittest.mock.ANY, |
| 109 | stderr=unittest.mock.ANY) |