summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qplc.py
blob: 28310c67a0b3de4fe83eed0974fd3a9fabd7a9d9 (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
import shutil
import unittest
import os.path
import os
import sh
import stat
import time
import ping3
from netifaces import AF_INET
import netifaces as ni

from test.helpers.md5 import md5_file
from test.helpers.plc import dcpPLC
from test.helpers.iseelogger import logObj

class Qplc(unittest.TestCase):
    params = None
    __resultlist = None  # resultlist is a python list of python dictionaries
    __QPLCName = None
    __plc = None
    __board_uuid = None

    def __init__(self, testname, testfunc, varlist):
        self.params = varlist
        super(Qplc, self).__init__(testfunc)
        self._testMethodDoc = testname
        self.__xmlObj = varlist["xml"]
        self.__psdbObj = varlist["db"]
        self.__board_uuid = varlist["boarduuid"]

        self.__QPLCName = varlist.get('name', 'qplc')
        self.__firmware = varlist.get('firmware', self.__xmlObj.getKeyVal(self.__QPLCName, "firmware", ""))
        self.__firmware_md5 = varlist.get('firmware_md5', self.__xmlObj.getKeyVal(self.__QPLCName, "firmware_md5", ""))
        self.__factoryTool = varlist.get('factory_tool', self.__xmlObj.getKeyVal(self.__QPLCName, "factory_tool", ""))
        self.__gen_ip = varlist.get('gen_ip', self.__xmlObj.getKeyVal(self.__QPLCName, "gen_ip", "0"))
        self.__gen_mac = varlist.get('gen_mac', self.__xmlObj.getKeyVal(self.__QPLCName, "gen_mac", "0"))
        self.__mtd_device = varlist.get('mtd_device', self.__xmlObj.getKeyVal(self.__QPLCName, "mtd_device", "/dev/mtd0"))
        self.__firmware_Path = varlist.get('firmwarepath', self.__xmlObj.getKeyVal(self.__QPLCName, "firmwarepath", "/root/hwtest-files/firmware"))
        self.__skipflash = varlist.get('skipflash', self.__xmlObj.getKeyVal(self.__QPLCName, "skipflash", "0"))
        self.__plc = dcpPLC(self.__factoryTool, self.__mtd_device)
        self.__factory_password = varlist.get('factory_password', self.__xmlObj.getKeyVal(self.__QPLCName, "factory_password", "0"))
        self.__firmware_password = varlist.get('firmware_password', self.__xmlObj.getKeyVal(self.__QPLCName, "firmware_password", "0"))
        self.__plc_reset_wait = varlist.get('plc_reset_wait', self.__xmlObj.getKeyVal(self.__QPLCName, "plc_reset_wait", "60"))
        self.__resultlist = []


    def checkfiles(self):
        if not os.path.isfile(self.__factoryTool):
            return False, 'Factory tool Not found {}/{}'.format(self.__factoryTool)
        if not os.path.isfile('{}/{}'.format(self.__firmware_Path, self.__firmware)):
            return False, 'Firmware Not found {}/{}'.format(self.__firmware_Path, self.__firmware)
        if not os.path.isfile('{}/{}'.format(self.__firmware_Path, self.__firmware_md5)):
            return False, 'Firmware md5 Not found {}/{}'.format(self.__firmware_Path, self.__firmware_md5)
        try:
            mode = os.stat(self.__mtd_device).st_mode;
            if not stat.S_ISCHR(mode):
                return False, 'No device {} found'.format(self.__mtd_device)
        except:
            return False, 'No device {} found'.format(self.__mtd_device)
        return True, ''

    def __flashMemory(self):
        r, v = self.__plc.SaveFirmware('{}/{}'.format(self.__firmware_Path, self.__firmware))
        if not r:
            self.fail(v)

    def execute(self):
        # Check files
        v, m = self.checkfiles()
        if not v:
            self.fail(m)
        # do flash & verify
        if self.__skipflash == '0':
            self.__flashMemory()
        # do Plc boot
        self.__plc.setBootMode()
        # Wait plc goes UP
        if self.__gen_mac:
            res, v = self.__psdbObj.get_plc_macaddr(self.__board_uuid)
            if res == None and v == 'MAC limit by uuid':
                self.fail('MAC limit by uuid')
            elif res == None:
                self.fail('BUG: ?????')
            logObj.getlogger().info("MAC {}".format(res[0]))
            plcMAC = res[0][0]
            # wait for PLC to be turned on
            attemptcounter = 0
            plcfound = False
            while attemptcounter < 5 and not plcfound:
                if self.__plc.discover():
                    plcfound = True
                else:
                    attemptcounter += 1
                    time.sleep(5)
            # mandatory wait before configuring the PLC chip
            time.sleep(2)
            self.__plc.set_plc2('SYSTEM.PRODUCTION.SECTOR0_UNLOCK_PASSWORD',
                               self.__factory_password,
                               'SYSTEM.PRODUCTION.MAC_ADDR',
                               '{}'.format(plcMAC),
                               self.__firmware_password)
            # plc reset to save changes
            self.__plc.setPLCReset()
            time.sleep(int(self.__plc_reset_wait))
            # calculate the IP of the GPLC0000 board to ping
            ip_eth01 = ni.ifaddresses('eth0:1')[AF_INET][0]['addr']  # plc_test_ip = 10.10.1.113
            ip_eth01_splited = ip_eth01.split('.')
            ip_eth01_splited[3] = str(int(ip_eth01_splited[3]) + 100)
            plc_test_ip = ".".join(ip_eth01_splited)  # plc_test_ip = 10.10.1.213
            # ping against the GPLC0000 board
            ping3.EXCEPTIONS = True
            try:
                ping3.ping(plc_test_ip, timeout=0.1)
            except:
                self.fail('PLC ping timeout')

    def getresults(self):
        return self.__resultlist

    def gettextresult(self):
        return ""