summaryrefslogtreecommitdiff
path: root/test-cli/test_main.py
blob: f8e3c1a26086017cb6b0d3cdefa6ab104a76a14c (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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from test.helpers.int_registers import get_die_id
from test.helpers.int_registers import get_mac
from subprocess import call
import os
import unittest
from test.helpers.testsrv_db import TestSrv_Database
from test.helpers.setup_xml import XMLSetup
from test.runners.simple import SimpleTestRunner
from test.helpers.syscmd import TestSysCommand
from test.tests.qethernet import Qethernet
from test.tests.qaudio import Qaudio
from test.tests.qram import Qram
from test.tests.qusb import Qusb
from test.tests.qi2c import Qi2c
from test.tests.qeeprom import Qeeprom
from test.tests.qserial import Qserial
from test.tests.qwifi import Qwifi
from test.tests.qrtc import Qrtc
from test.tests.qduplex_ser import Qduplex
from test.tests.qamper import Qamper
from test.tests.qnand import Qnand
from test.helpers.globalVariables import globalVar
import socket
from test.helpers.iseelogger import ISEE_Logger
import logging
import binascii

psdbObj = TestSrv_Database()
xmlObj = None
loggerObj = None


# define clear function
def clear():
    # check and make call for specific operating system
    _ = call('clear' if os.name == 'posix' else 'cls')


def create_paramslist(params):
    paramlist = {}
    for row in params:
        varname, varvalue = row
        paramlist[varname] = varvalue
    return paramlist


def add_test_task(suite, testdefname, paramlist):
    if testdefname == "AUDIO":
        suite.addTest(Qaudio(testdefname, "execute", paramlist))
    elif testdefname == "RAM":
        suite.addTest(Qram(testdefname, "execute", paramlist))
    elif testdefname == "SERIALDUAL":
        suite.addTest(Qduplex(testdefname, "execute", paramlist))
    elif testdefname == "EEPROM":
        suite.addTest(Qeeprom(testdefname, "execute", paramlist))
    elif testdefname == "SERIAL":
        suite.addTest(Qserial(testdefname, "execute", paramlist))
    elif testdefname == "RTC":
        suite.addTest(Qrtc(testdefname, "execute", paramlist))
    elif testdefname == "CONSUMPTION":
        suite.addTest(Qamper(testdefname, "execute", paramlist))
    elif testdefname == "DMESG":
        suite.addTest(TestSysCommand(testdefname, "execute", paramlist))
    elif testdefname == "ETHERNET":
        suite.addTest(Qethernet(testdefname, "execute", paramlist))
    elif testdefname == "NAND":
        suite.addTest(Qnand(testdefname, "execute", paramlist))
    elif testdefname == "I2C":
        suite.addTest(Qi2c(testdefname, "execute", paramlist))
    elif testdefname == "WIFI":
        suite.addTest(Qwifi(testdefname, "execute", paramlist))
    elif testdefname == "USB":
        suite.addTest(Qusb(testdefname, "execute", paramlist))
    else:
        raise Exception("Wrong testdefname")


def create_testsuite():
    # create an object TestSuite
    suite = unittest.TestSuite()
    # get id of the full test for this board
    globalVar.testid_ctl = psdbObj.open_test(globalVar.g_uuid)
    # get list of tests for this board
    tests = psdbObj.get_tests_list(globalVar.g_uuid)
    # loop in every test for this board
    for row in tests:
        testid, testdefname = row
        # get params for this test
        params = psdbObj.get_test_params_list(testid)
        paramlist = create_paramslist(params)
        # add the testid as parameter
        paramlist["testid"] = testid
        paramlist["boarduuid"] = globalVar.g_uuid
        paramlist["testidctl"] = globalVar.testid_ctl
        # add test to TestSuite
        add_test_task(suite, testdefname, paramlist)

    return suite


def create_board():
    model_id = xmlObj.gettagKey('board', 'model')
    variant = xmlObj.gettagKey('board', 'variant')

    # get model id
    globalVar.g_mid = model_id + "-" + variant

    # get station
    globalVar.station = socket.gethostname()

    processor_id = get_die_id(globalVar.g_mid)
    print(globalVar.g_mid)
    print(processor_id)
    globalVar.g_uuid = psdbObj.create_board(processor_id, model_id, variant, globalVar.station,
                                            get_mac(globalVar.g_mid))


def program_eeprom(eeprompath):
    # check if eeprompath is correct
    if os.path.isfile(eeprompath):
        # create u-boot data struct
        data = bytearray()
        data += (2029785358).to_bytes(4, 'big')  # magicid --> 0x78FC110E
        data += bytearray([0, 0, 0, 0])  # crc32 = 0
        data += bytearray(globalVar.g_uuid + "\0",
                          'ascii')  # uuid --> 'c0846c8a-5fa5-11ea-8576-f8b156ac62d7' and \0 at the end
        data += binascii.unhexlify((get_mac(globalVar.g_mid)).replace(':', ''))  # mac0 --> 'f8:b1:56:ac:62:d7'
        data += bytearray([0, 0, 0, 0, 0, 0])  # mac1 --> 0:0:0:0:0:0
        # calculate crc
        crc = (binascii.crc32(data, 0)).to_bytes(4, 'big')
        data[4:8] = crc
        # write into eeprom an validate
        f = open(eeprompath, "r+b")
        f.write(data)
        f.seek(0)
        data_rx = f.read(57)
        for i in range(57):
            if data_rx[i] != data[i]:
                print("Error while programming eeprom memory.")
                break
        print("Eeprom programmed succesfully.")


def flash_nvmemory(flashscript):
    if os.path.isfile(flashscript):
        print("a")


def create_boardvariables_list(uuid):
    varlist = {}
    for row in psdbObj.get_board_variables(globalVar.g_uuid):
        varname, varvalue = row
        varlist[varname] = varvalue
    return varlist


def main():
    # initialize the board
    create_board()
    # create and run tests according to the board type
    runner = SimpleTestRunner(psdbObj)
    runner.run(create_testsuite())
    # execute aditional tasks
    varlist = create_boardvariables_list(globalVar.g_uuid)
    program_eeprom(varlist["eeprompath"])
    flash_nvmemory(varlist["flashscript"])


if __name__ == "__main__":
    # Clear the shell screen
    clear()

    # create logger
    loggerObj = ISEE_Logger(logging.INFO)
    # logger = loggerObj.getlogger().info("Starting test script...")

    # Try to parse the setup.xml file
    try:
        xmlObj = XMLSetup("setup.xml")
    except:
        print("Error: Cannot parse setup.xml file")
        exit(1)

    # Try to connect to the DB, according to setup.xml configuration
    if psdbObj.open(xmlObj):
        main()
    else:
        print("Error: Cannot open DB connection")
        exit(2)