~aleteoryx/muditaos

ref: 71a82520296605c6ce9be4473b38f58b9c6ed445 muditaos/test/pytest/conftest.py -rw-r--r-- 3.1 KiB
71a82520 — SP2FET [EGD-4580] Add BT settings middleware 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

import time

import pytest

import sys
import os.path

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))

from harness import log
from harness.harness import Harness
from harness.interface.error import TestError, Error

simulator_port = 'simulator'


def pytest_addoption(parser):
    parser.addoption("--port", type=str, action="store", required=False)
    parser.addoption("--timeout", type=int, action="store", default=15)
    parser.addoption("--phone_number", type=int, action="store")
    parser.addoption("--call_duration", type=int, action="store", default=30)
    parser.addoption("--sms_text", type=str, action="store", default='')


@pytest.fixture(scope='session')
def phone_number(request):
    phone_number = request.config.option.phone_number
    assert phone_number
    return phone_number


@pytest.fixture(scope='session')
def call_duration(request):
    call_duration = request.config.option.call_duration
    assert call_duration
    return call_duration


@pytest.fixture(scope='session')
def sms_text(request):
    sms_text = request.config.option.sms_text
    assert sms_text != ''
    return sms_text


@pytest.fixture(scope='session')
def harness(request):
    port_name = request.config.option.port

    RETRY_EVERY = 0.7 # second
    retries = request.config.option.timeout / RETRY_EVERY

    if port_name is None:
        log.warning("no port provided! trying automatic detection")
        try:
            harness = Harness.from_detect()
        except TestError as e:
            if e.get_error_code() == Error.PORT_NOT_FOUND:
                pytest.exit("couldn't find any viable port. exiting")
            else:
                raise(e)
    else:
        assert '/dev' in port_name or simulator_port in port_name

        if simulator_port in port_name:
            while retries > 0:
                try:
                    file = open("/tmp/purephone_pts_name", "r")
                    break
                except FileNotFoundError as err:
                    time.sleep(RETRY_EVERY)
                    retries -= 1
                    log.info("waiting for simulator port...")
            else:
                raise TestError(Error.PORT_FILE_NOT_FOUND)

            port_name = file.readline()
            if port_name.isascii():
                log.debug("found {} entry!".format(port_name))
            else:
                pytest.exit("not a valid sim pts entry!")

        harness = Harness(port_name)
    return harness

@pytest.fixture(scope='session')
def phone_unlocked(harness):
    harness.unlock_phone()
    assert harness.is_phone_unlocked

def pytest_configure(config):
    config.addinivalue_line("markers",
                            "service_desktop_test: mark test if it's related to service-desktop API")
    config.addinivalue_line("markers",
                            "rt1051: mark test if it's target only (eg. calls, messages)")
    config.addinivalue_line("markers",
                            "usb_cdc_echo: mark test if it's intended for usb-cdc echo mode")