summaryrefslogtreecommitdiff
path: root/test-cli/test/helpers/syscmd.py
blob: a869bd713ae5dcf43ab0388c4029a4fffd721a5d (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
import unittest
import subprocess
from test.helpers.globalVariables import globalVar


class TestSysCommand(unittest.TestCase):
    __str_cmd = None
    __testname = None
    __outfilename = None
    __outdata = None
    __outtofile = False

    #varlist: str_cmd, outtofile
    def __init__(self, testname, testfunc, varlist):
        """ init """
        super(TestSysCommand, self).__init__(testfunc)
        if "str_cmd" in varlist:
            self.__str_cmd = varlist["str_cmd"]
        else:
            raise Exception('str_cmd param inside TestSysCommand have been be defined')
        self.__testname = testname
        if "outtofile" in varlist:
            self.__outtofile = varlist["outtofile"]
            if self.__outtofile is True:
                self.__outfilename = '/tmp/{}.txt'.format(testname)
        else:
            self.__outtofile = None
            self.__outfilename = None
        self._testMethodDoc = testname


    def getName(self):
        return self.__testname

    def execute(self):
        res = -1
        try:
            completed = subprocess.run(
                self.__str_cmd,
                check=True,
                shell=True,
                stdout=subprocess.PIPE,
            )
            self.assertTrue(completed.returncode is 0)
            if completed.returncode is 0:
                if self.__outtofile is True:
                    f = open(self.__outfilename, 'wb')
                    f.write(completed.stdout)
                    f.close()
                res = 0
            else:
                res = -3
            outdata = completed.stdout
            self.longMessage=str(outdata).replace("'","")
            self.assertTrue(True)
        except subprocess.CalledProcessError as err:
            self.assertTrue(False)
            res = -1
        except Exception as t:
            res = -2
        return res

    def remove_file(self):
        pass

class SysCommand(object):
    __str_cmd = None
    __cmdname = None
    __outdata = None
    __errdata = None

    def __init__(self, cmdname, str_cmd):
        """ init """
        self.__str_cmd = str_cmd
        self.__cmdname = cmdname

    def getName(self):
        return self.__testname

    def execute(self):
        res = -1
        try:
            self.__outdata = None
            self.__errdata = None
            completed = subprocess.run(
                self.__str_cmd,
                check=True,
                shell=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            )
            self.__outdata = completed.stdout
            if completed.returncode is 0:
                res = 0
                if completed.stderr.decode('ascii') != "":
                    res = -1
                    self.__errdata = completed.stderr
        except subprocess.CalledProcessError as err:
            res = -2
        except Exception as t:
            res = -3
        return res

    def getOutput(self):
        return self.__outdata

    def getOutErr(self):
        return self.__errdata

    def getOutputlines(self):
        return self.__outdata.splitlines()

    def save_file(self, fname):
        f = open(fname, 'wb')
        f.write(self.__outdata)
        f.close()