示例#1
0
def test_session_04(tmp_path, mocker):
    """test Session.location"""
    Status.PATH = str(tmp_path)
    fake_server = mocker.Mock(spec=Sapphire)
    fake_server.get_port.return_value = 1
    fake_adapter = mocker.Mock(spec=Adapter)
    fake_adapter.TEST_DURATION = 1
    fake_iomgr = mocker.Mock(spec=IOManager)
    fake_iomgr.harness = mocker.Mock(spec=TestFile)
    fake_iomgr.landing_page.return_value = "x"
    fake_target = mocker.Mock(spec=Target)
    fake_target.rl_reset = 1

    fake_target.forced_close = False
    session = Session(fake_adapter, False, [], fake_iomgr, fake_adapter,
                      fake_target)
    session.server = fake_server
    assert session.location == "http://127.0.0.1:1/x?timeout=1000&close_after=1&forced_close=0"

    fake_target.forced_close = True
    session = Session(fake_adapter, False, [], fake_iomgr, fake_adapter,
                      fake_target)
    session.server = fake_server
    assert session.location == "http://127.0.0.1:1/x?timeout=1000&close_after=1"

    fake_iomgr.harness = None
    session = Session(fake_adapter, False, [], fake_iomgr, fake_adapter,
                      fake_target)
    session.server = fake_server
    assert session.location == "http://127.0.0.1:1/x"
示例#2
0
def test_session_03(mocker, tmp_path):
    """test Session.launch_target()"""
    Status.PATH = str(tmp_path)
    fake_server = mocker.Mock(spec=Sapphire)
    fake_server.get_port.return_value = 1
    fake_adapter = mocker.Mock(spec=Adapter)
    fake_adapter.TEST_DURATION = 10
    fake_iomgr = mocker.Mock(spec=IOManager)
    fake_iomgr.harness = None
    fake_iomgr.tests = []
    fake_iomgr.working_path = str(tmp_path)
    fake_iomgr.landing_page.return_value = "x"

    class FakeTarget(object):
        def __init__(self, launch_raise=None):
            self._closed = True
            self._raise = launch_raise
            self.forced_close = False
            self.prefs = None
            self.rl_reset = 10

        def cleanup(self):
            pass

        def close(self):
            pass

        @property
        def closed(self):
            return self._closed

        def detect_failure(self, ignored, was_timeout):
            pass

        def launch(self, _):
            if self._raise is not None:
                raise self._raise("Test")
            self._closed = False

        def monitor(self):
            pass

        def save_logs(self, result_logs, meta=True):
            pass

    fake_target = FakeTarget()
    session = Session(fake_adapter, False, [], fake_iomgr, None, fake_target)
    session.server = fake_server
    session.launch_target()
    assert not fake_target.closed

    fake_target = FakeTarget(launch_raise=TargetLaunchError)
    fake_reporter = mocker.Mock(spec=Reporter)
    session = Session(fake_adapter, False, [], fake_iomgr, fake_reporter,
                      fake_target)
    session.server = fake_server
    with pytest.raises(TargetLaunchError):
        session.launch_target()
    assert fake_target.closed
    assert session.status.results == 1

    fake_target = FakeTarget(launch_raise=TargetLaunchTimeout)
    fake_reporter = mocker.Mock(spec=Reporter)
    session = Session(fake_adapter, False, [], fake_iomgr, fake_reporter,
                      fake_target)
    session.server = fake_server
    with pytest.raises(TargetLaunchTimeout):
        session.launch_target()
    assert fake_target.closed