summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qserial.py
blob: faca50566724a7e643114365beb6c0607e9d4683 (plain)
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
import unittest
import uuid
import serial
import time


class Qserial(unittest.TestCase):
    params = None
    __port = None
    __baudrate = None
    __resultlist = None  # resultlist is a python list of python dictionaries

    # varlist: port, baudrate
    def __init__(self, testname, testfunc, varlist):
        self.params = varlist
        super(Qserial, self).__init__(testfunc)
        if "port" in varlist:
            self.__port = varlist["port"]
        else:
            raise Exception('port param inside Qserial must be defined')

        if "baudrate" in varlist:
            self.__baudrate = varlist["baudrate"]
        else:
            raise Exception('baudrate param inside Qserial must be defined')
        self._testMethodDoc = testname
        self.__resultlist = []

        # open serial connection
        self.__serial = serial.Serial(self.__port, self.__baudrate, timeout=1)

    def __del__(self):
        self.__serial.close()

    def execute(self):
        # generate a random uuid
        test_uuid = str(uuid.uuid4()).encode()
        # clean serial port
        self.__serial.flushInput()
        self.__serial.flushOutput()
        # send the uuid through serial port
        self.__serial.write(test_uuid)
        time.sleep(0.05)  # there might be a small delay
        if self.__serial.inWaiting() == 0:
            self.__resultlist.append(
                {
                    "desc": "Test result",
                    "data": "FAILED: port {} wait timeout exceded".format(self.__port),
                    "type": "string"
                }
            )
            self.fail("failed: port {} wait timeout exceded".format(self.__port))
        else:
            # check if what it was sent is equal to what has been received
            if self.__serial.readline() != test_uuid:
                self.__resultlist.append(
                    {
                        "desc": "Test result",
                        "data": "FAILED: port {} write/read mismatch".format(self.__port),
                        "type": "string"
                    }
                )
                self.fail("failed: port {} write/read mismatch".format(self.__port))

        # Test successful
        self.__resultlist.append(
            {
                "desc": "Test result",
                "data": "OK",
                "type": "string"
            }
        )

    def getresults(self):
        return self.__resultlist