summaryrefslogtreecommitdiff
path: root/test-cli/test_main.py
blob: 80dfbe2fde875fc50ff65c8e41c1368800f32c5d (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from test.helpers.int_registers import get_die_id
from test.helpers.int_registers import get_internal_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.tests.qethernet import Qethernet
from test.tests.qram import Qram
from test.tests.qusb import Qusb
from test.tests.qeeprom import Qeeprom
from test.tests.qserial import Qserial
from test.tests.qusbdual import Qusbdual
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.tests.qaudio import Qaudio
from test.tests.qdmesg import Qdmesg
from test.helpers.globalVariables import globalVar
import socket
from test.helpers.iseelogger import ISEE_Logger
import logging
from test.tasks.flasheeprom import flash_eeprom
from test.tasks.flashmemory import flash_memory
from test.helpers.qrreader import QRReader
from test.helpers.cmdline import LinuxKernel
from test.helpers.amper import Amper
from test.enums.StationStates import StationStates
import time

# global variables
psdbObj = TestSrv_Database()
xmlObj = None
loggerObj = None
test_abspath = None
tests_manually_executed = []


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


def execute_station_error(text):
    print("Error: {}".format(text))
    loggerObj.getlogger().info("Station error: #{}#".format(text))
    while True:
        currentstate = psdbObj.read_station_state(globalVar.station)
        if currentstate == StationStates.STATION_REBOOT.name:
            os.system("reboot")
            exit(1)
        time.sleep(1)


def check_first_state():
    currentstate = psdbObj.read_station_state(globalVar.station)
    if currentstate == StationStates.STATION_ERROR.name:
        while True:
            currentstate = psdbObj.read_station_state(globalVar.station)
            if currentstate == StationStates.STATION_REBOOT.name:
                os.system("reboot")
                exit(1)
            time.sleep(1)
    elif currentstate == StationStates.STATION_REBOOT.name:
        os.system("reboot")
        exit(1)
    else:
        starttime = time.time()
        while currentstate != StationStates.WAIT_TEST_START.name:
            if time.time() - starttime >= 30.0:
                # Error
                execute_station_error("Timeout while waiting for WAIT_TEST_START state")
            else:
                time.sleep(5)
                currentstate = psdbObj.read_station_state(globalVar.station)
                if currentstate == StationStates.STATION_ERROR.name:
                    while True:
                        currentstate = psdbObj.read_station_state(globalVar.station)
                        if currentstate == StationStates.STATION_REBOOT.name:
                            os.system("reboot")
                            exit(1)
                        time.sleep(1)
                elif currentstate == StationStates.STATION_REBOOT.name:
                    os.system("reboot")
                    exit(1)


def set_next_state(newstate):
    statewritten = psdbObj.change_station_state(globalVar.station, newstate)
    if statewritten == StationStates.STATION_REBOOT.name:
        os.system("reboot")
        exit(1)
    elif statewritten == StationStates.STATION_ERROR.name:
        while True:
            currentstate = psdbObj.read_station_state(globalVar.station)
            if currentstate == StationStates.STATION_REBOOT.name:
                os.system("reboot")
                exit(1)
            time.sleep(1)


def reboot_if_autotest():
    # reset board if AUTOTEST is enabled
    autotest = psdbObj.get_setup_variable("AUTOTEST_" + globalVar.station)
    if int(autotest) == 1:
        os.system("reboot")
        exit(0)


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


def add_test(suite, testdefname, paramlist):
    if testdefname == "RAM":
        suite.addTest(Qram(testdefname, "execute", paramlist))
        return 0
    elif testdefname == "SERIALDUAL":
        suite.addTest(Qduplex(testdefname, "execute", paramlist))
        return 0
    elif testdefname == "EEPROM":
        suite.addTest(Qeeprom(testdefname, "execute", paramlist))
        return 0
    elif testdefname == "SERIAL":
        suite.addTest(Qserial(testdefname, "execute", paramlist))
        return 0
    elif testdefname == "RTC":
        suite.addTest(Qrtc(testdefname, "execute", paramlist))
        return 0
    elif testdefname == "CONSUMPTION":
        suite.addTest(Qamper(testdefname, "execute", paramlist))
        return 0
    elif testdefname == "ETHERNET":
        suite.addTest(Qethernet(testdefname, "execute", paramlist))
        return 0
    elif testdefname == "NAND":
        suite.addTest(Qnand(testdefname, "execute", paramlist))
        return 0
    elif testdefname == "WIFI":
        suite.addTest(Qwifi(testdefname, "execute", paramlist))
        return 0
    elif testdefname == "USB":
        suite.addTest(Qusb(testdefname, "execute", paramlist))
        return 0
    elif testdefname == "AUDIO":
        suite.addTest(Qaudio(testdefname, "execute", paramlist))
        return 0
    elif testdefname == "USBLOOP":
        suite.addTest(Qusbdual(testdefname, "execute", paramlist))
        return 0
    elif testdefname == "DMESG":
        tests_manually_executed.append(Qdmesg(testdefname, "execute", paramlist))
        return 0
    else:
        return 1


def create_testsuite():
    # create an object TestSuite
    suite1 = unittest.TestSuite()
    # 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
        paramlist["xml"] = xmlObj
        paramlist["db"] = psdbObj
        # add test to TestSuite
        add_test(suite1, testdefname, paramlist)

    return suite1


def create_board():
    cmd = LinuxKernel()
    model_id = cmd.getkvar("bmodel", "none")
    variant = cmd.getkvar("bvariant", "none")

    if model_id == "none" or variant == "none":
        # Error
        execute_station_error("Cannot get model and variant information")

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

    # get station
    if "Station" not in globalVar.station:
        # Error
        execute_station_error("Wrong station name")

    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_internal_mac(globalVar.g_mid))
    print(globalVar.g_uuid)


def get_taskvars_list(uuid):
    varlist = {}
    for row in psdbObj.get_task_variables_list(uuid):
        varname, varvalue = row
        varlist[varname] = varvalue
    return varlist


def main():
    # initialize the board
    create_board()

    # create a process
    globalVar.testid_ctl = psdbObj.open_test(globalVar.g_uuid)
    # Change state to "TESTS_RUNNING"
    set_next_state(StationStates.TESTS_RUNNING.name)

    # generate suits
    suite1 = create_testsuite()
    # Execute tests (round 1)
    runner1 = SimpleTestRunner(psdbObj)
    testresult = runner1.run(suite1)

    # Execute manually tests
    for test in tests_manually_executed:
        test.execute()

    # execute aditional tasks, only if the test was successful
    if testresult.wasSuccessful():
        # Change state to "TESTS_OK"
        set_next_state(StationStates.TESTS_OK.name)
        # Change state to "EXTRATASKS_RUNNING"
        set_next_state(StationStates.EXTRATASKS_RUNNING.name)
        # create task control
        globalVar.taskid_ctl = psdbObj.open_task(globalVar.g_uuid)
        # get extra variables
        varlist = get_taskvars_list(globalVar.g_uuid)

        alltasksok = False

        # flash eeprom
        resulteeprom = 0
        if "eeprompath" in varlist and len(varlist["eeprompath"]) > 0:
            mac0 = psdbObj.get_board_macaddr(globalVar.g_uuid)
            resulteeprom, eepromdata = flash_eeprom(varlist["eeprompath"], globalVar.g_uuid, mac0)
            psdbObj.create_task_result(globalVar.taskid_ctl, "FLASHEEPROM",
                                       "TASK_OK" if resulteeprom == 0 else "TASK_FAIL", eepromdata)

        # flash non-volatile memory
        resultmemory = 0
        if "flashimagepath" in varlist and len(varlist["flashimagepath"]) > 0:
            resultmemory = flash_memory(varlist["flashimagepath"])
            psdbObj.create_task_result(globalVar.taskid_ctl, "FLASHMEMORY",
                                       "TASK_OK" if resultmemory == 0 else "TASK_FAIL",
                                       varlist["flashimagepath"])

        # update status with the result
        if resulteeprom == 0 and resultmemory == 0:
            # finish tasks
            psdbObj.update_taskctl_status(globalVar.taskid_ctl, "TASK_BOARD_OK")

            # Change state to "WAITING_FOR_SCANNER"
            set_next_state(StationStates.WAITING_FOR_SCANNER.name)

            # get barcode using the scanner, only if autotest is disabled
            autotest = psdbObj.get_setup_variable("AUTOTEST_" + globalVar.station)
            if int(autotest) == 1:
                psdbObj.set_factorycode(globalVar.g_uuid, "1234567890")  # fake factory code
            else:
                qrreceived = False
                while not qrreceived:
                    qr = QRReader()
                    if qr.openQR():
                        # waits 5s to receive a valid code
                        if qr.readQRasync(5):
                            qrreceived = True
                            factorycode = qr.getQRNumber()
                            psdbObj.set_factorycode(globalVar.g_uuid, factorycode)
                        qr.closeQR()

            # Change state to "FINISHED"
            set_next_state(StationStates.FINISHED.name)
        else:
            psdbObj.update_taskctl_status(globalVar.taskid_ctl, "TASK_BOARD_FAIL")
            loggerObj.getlogger().info("Station error: #Unable to complete extra tasks#")
    else:
        # Change state to "TESTS_FAILED"
        set_next_state(StationStates.TESTS_FAILED.name)

    # reset board if AUTOTEST is enabled
    reboot_if_autotest()


if __name__ == "__main__":
    # Clear the shell screen
    clear()
    test_abspath = os.path.dirname(os.path.abspath(__file__))

    # create logger
    loggerObj = ISEE_Logger(logging.INFO)

    # Try to parse the setup.xml file
    try:
        xmlObj = XMLSetup(os.path.join(test_abspath, "setup.xml"))
    except:
        # Error
        execute_station_error("Cannot parse setup.xml file")

    # Try to connect to the DB, according to setup.xml configuration
    if not psdbObj.open(xmlObj):
        # Error
        execute_station_error("Cannot open DB connection")

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

    # Check if current state is "WAIT_TEST_START". if not, waits here
    check_first_state()

    # Change state to "TESTS_CHECKING_ENV"
    set_next_state(StationStates.TESTS_CHECKING_ENV.name)

    # Wait before beginning
    time.sleep(2)

    # Look for a barcode scanner
    qr = QRReader()
    if qr.openQR():
        qr.closeQR()
    else:
        # Error
        execute_station_error("Cannot find a barcode scanner")

    # Look for an amperimeter
    amp = Amper()
    if not amp.open():
        # Error
        execute_station_error("Cannot open an amperimeter COM port")
    if not amp.hello():
        if not amp.hello():
            # Error
            execute_station_error("Cannot open find an amperimeter")

    # Execute main
    main()

    exit(0)