|
| 1 | +import unittest |
| 2 | +import yaml |
| 3 | + |
| 4 | +from time import sleep |
| 5 | +from unittest.mock import patch, MagicMock, PropertyMock |
| 6 | + |
| 7 | +from fiotest.main import Coordinator |
| 8 | +from fiotest.runner import SpecRunner |
| 9 | +from fiotest.spec import TestSpec |
| 10 | + |
| 11 | +SIMPLE_TEST_SPEC = """ |
| 12 | +sequence: |
| 13 | + - tests: |
| 14 | + - name: test1 |
| 15 | + context: |
| 16 | + url: https://example.com/{LMP_FACTORY}/{CUSTOM_VERSION}/{LMP_MACHINE} |
| 17 | + command: |
| 18 | + - /bin/true |
| 19 | + on_host: true |
| 20 | + - name: test2 |
| 21 | + command: |
| 22 | + - /bin/true |
| 23 | +""" |
| 24 | + |
| 25 | +class TestMain(unittest.TestCase): |
| 26 | + def setUp(self): |
| 27 | + data = yaml.safe_load(SIMPLE_TEST_SPEC) |
| 28 | + self.testspec = TestSpec.parse_obj(data) |
| 29 | + |
| 30 | + @patch("fiotest.host.sudo_execute", return_value=0) |
| 31 | + def test_coordinator_check_for_updates_pre(self, mock_sudo_execute): |
| 32 | + self.coordinator = Coordinator(self.testspec) |
| 33 | + self.assertEqual(False, self.coordinator.callbacks_enabled) |
| 34 | + self.assertEqual(True, self.coordinator.timer.is_alive()) |
| 35 | + self.coordinator.on_check_for_updates_pre("foo") |
| 36 | + sleep(1) # it takes a moment for thread to complete |
| 37 | + self.assertEqual(True, self.coordinator.callbacks_enabled) |
| 38 | + self.assertEqual(False, self.coordinator.timer.is_alive()) |
| 39 | + |
| 40 | + @patch("fiotest.main.SpecRunner") |
| 41 | + @patch("fiotest.host.sudo_execute", return_value=0) |
| 42 | + def test_coordinator_on_install_post_ok(self, mock_sudo_execute, mock_specrunner): |
| 43 | + mock_runner = MagicMock() |
| 44 | + mock_specrunner.return_value = mock_runner |
| 45 | + type(mock_specrunner).reboot_state = PropertyMock(return_value="/foo/bar") |
| 46 | + |
| 47 | + self.coordinator = Coordinator(self.testspec) |
| 48 | + self.coordinator.on_check_for_updates_pre("foo") |
| 49 | + self.coordinator.on_install_post("foo", "OK") |
| 50 | + mock_specrunner.assert_called_once_with(self.testspec) |
| 51 | + mock_runner.start.assert_called_once() |
| 52 | + |
| 53 | + @patch("fiotest.main.SpecRunner") |
| 54 | + @patch("fiotest.host.sudo_execute", return_value=0) |
| 55 | + def test_coordinator_on_install_post_fail(self, mock_sudo_execute, mock_specrunner): |
| 56 | + mock_runner = MagicMock() |
| 57 | + mock_specrunner.return_value = mock_runner |
| 58 | + type(mock_specrunner).reboot_state = PropertyMock(return_value="/foo/bar") |
| 59 | + |
| 60 | + self.coordinator = Coordinator(self.testspec) |
| 61 | + self.coordinator.on_check_for_updates_pre("foo") |
| 62 | + self.coordinator.on_install_post("foo", "FAIL") |
| 63 | + mock_specrunner.assert_not_called() |
| 64 | + mock_runner.start.assert_not_called() |
| 65 | + |
| 66 | + @patch("fiotest.main.SpecRunner") |
| 67 | + @patch("fiotest.host.sudo_execute", return_value=0) |
| 68 | + def test_coordinator_on_install_pre_no_runner(self, mock_sudo_execute, mock_specrunner): |
| 69 | + mock_runner = MagicMock() |
| 70 | + mock_specrunner.return_value = mock_runner |
| 71 | + type(mock_specrunner).reboot_state = PropertyMock(return_value="/foo/bar") |
| 72 | + |
| 73 | + self.coordinator = Coordinator(self.testspec) |
| 74 | + self.coordinator.on_check_for_updates_pre("foo") |
| 75 | + self.coordinator.on_install_pre("foo") |
| 76 | + mock_runner.stop.assert_not_called() |
| 77 | + |
| 78 | + @patch("fiotest.main.SpecRunner") |
| 79 | + @patch("fiotest.host.sudo_execute", return_value=0) |
| 80 | + def test_coordinator_on_install_pre(self, mock_sudo_execute, mock_specrunner): |
| 81 | + mock_runner = MagicMock() |
| 82 | + mock_specrunner.return_value = mock_runner |
| 83 | + type(mock_specrunner).reboot_state = PropertyMock(return_value="/foo/bar") |
| 84 | + |
| 85 | + self.coordinator = Coordinator(self.testspec) |
| 86 | + self.coordinator.on_check_for_updates_pre("foo") |
| 87 | + self.coordinator.on_install_post("foo", "OK") |
| 88 | + mock_specrunner.assert_called_once_with(self.testspec) |
| 89 | + mock_runner.start.assert_called_once() |
| 90 | + self.coordinator.on_install_pre("foo") |
| 91 | + mock_runner.stop.assert_called() |
| 92 | + |
| 93 | + |
| 94 | +class TestRunner(unittest.TestCase): |
| 95 | + def setUp(self): |
| 96 | + data = yaml.safe_load(SIMPLE_TEST_SPEC) |
| 97 | + self.testspec = TestSpec.parse_obj(data) |
| 98 | + |
| 99 | + @patch("fiotest.api.API.test_url", return_value="https://example.com/{FOO}") |
| 100 | + @patch("fiotest.runner.API.file_variables", return_value={"LMP_FACTORY": "factory", "CUSTOM_VERSION": 123, "LMP_MACHINE": "foo"}) |
| 101 | + @patch("fiotest.api.DeviceGatewayClient") |
| 102 | + @patch("requests.get") |
| 103 | + @patch("subprocess.Popen") |
| 104 | + @patch("os.path.exists", return_value=True) |
| 105 | + def test_run(self, mock_path_exists, mock_popen, mock_requests_get, mock_gateway_client, mock_file_variables, mock_test_url): |
| 106 | + mock_response = MagicMock() |
| 107 | + mock_response.status_code = 200 |
| 108 | + mock_requests_get.return_value = mock_response |
| 109 | + specrunner = SpecRunner(self.testspec) |
| 110 | + specrunner.running = True # run synchronously for testing |
| 111 | + specrunner.run() |
| 112 | + mock_requests_get.assert_called_with("https://example.com/factory/123/foo") |
| 113 | + mock_popen.assert_called() |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + unittest.main() |
0 commit comments