summaryrefslogtreecommitdiffstats
path: root/libs/utils/clientutils.py
blob: 347e1596340b350c6c11eae2ebaf104c2762bbe0 (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
"""clientutils module contains functions required for performing
certain operations on client

Supported Wrappers :-
-----------
*) umount
*) umountall
*) mount
*) mountall
"""

import atfutils
import hostutils
from atfglobals import GlobalObj

def umount(mountkey):
    """unmounts a mountpoint

    Parameters:
        mountkey : name given to a mount as specified in testenv.cfg file.
        Ex:-"mount1"

    Returns:
        Success : 0
        Failure : 1`
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "umount"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()

    mount_obj = env.getMount(mountkey)
    if not mount_obj:
        logger.error("InValid Mount. '%s' not defined in TestEnvironment"
                     % mountkey)
        return 1

    clientkey = mount_obj.client
    client_connection = cm.getConnection(clientkey)
    if not client_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % clientkey)
        return 1

    command = ' '.join([base_command, mount_obj.dir])
    logger.debug('%s: Executing Command: %s' % (clientkey, command))
    output = client_connection.executecommand(command)
    return_status = atfutils.assert_success(output['exitstatus'])
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    if return_status:
        stdoutdata = str(output["stdoutdata"])
        if ((stdoutdata.rfind("not found")) or (stdoutdata.rfind("not mount"))):
            return_status = 0

        else:
            logger.error("Unable to umount %s" % mountkey)

    return return_status

def umountall():
    """unmounts all mount specified in testenv.cfg file.
        Ex:- mount1, mount2 etc

    Parameters:
        None

    Returns:
        Success : 0
        Failure : 1`
    """
    env = GlobalObj.getTestenvObj()
    failure_flag = False

    mounts_keys = env.getMountsKeys()
    for mountkey in mounts_keys:
        return_status = umount(mountkey)
        if return_status:
            failure_flag = True

    if failure_flag:
        return 1
    else:
        return 0

def mount(mountkey):
    """mounts a filesystem

    Parameters:
        mountkey : name given to a mount as specified in testenv.cfg file.
        Ex:-"mount1"

    Returns:
        Success : 0
        Failure : 1`
    """
    logger = GlobalObj.getLoggerObj()
    base_command = "mount"
    env = GlobalObj.getTestenvObj()
    cm = GlobalObj.getConnectionsManagerObj()
    command = [base_command]
    options = []

    mount_obj = env.getMount(mountkey)
    if not mount_obj:
        logger.error("InValid Mount. %s not defined in TestEnvironment"
                     % mountkey)
        return 1

    clientkey = mount_obj.client
    client_connection = cm.getConnection(clientkey)
    if not client_connection:
        logger.error("SSH connection to host '%s' has not been established"
                     % clientkey)
        return 1

    return_status = hostutils.mkdir(clientkey, mount_obj.dir)
    if return_status:
        return return_status

    mountdevice_obj = mount_obj.device
    device = mountdevice_obj.hostname + ":/" + mountdevice_obj.volumename
    options.extend(["-t", mount_obj.type])
    if mount_obj.logfile:
        options.extend(["-o", ("log-file="+mount_obj.logfile),
                        "log-level=INFO"])
    if mount_obj.options:
        options.extend([mount_obj.option])
    options.extend([device, mount_obj.dir])

    command.extend(options)
    command = ' '.join(command)

    logger.debug('%s: Executing Command: %s' % (clientkey, command))
    output = client_connection.executecommand(command)
    return_status = atfutils.assert_success(output['exitstatus'])
    atfutils.print_stdout(output['stdoutdata'])
    atfutils.print_stderr(output['stderrdata'])
    return return_status

def mountall():
    """mounts a filesystem for all mounts specified in testenv.cfg file.

    Parameters:
        None

    Returns:
        Success : 0
        Failure : 1`
    """
    env = GlobalObj.getTestenvObj()

    mounts_keys = env.getMountsKeys()
    for mountkey in mounts_keys:
        return_status = mount(mountkey)
        if return_status:
            return return_status

    return 0

def md5sum_of_mount(mountkey):
    """
    Parameter: mount (tye: string)
    Returns: output of arequal-checksum command execution(type:dict)
        Key : Value of the Output
        exitstatus: exit status of the arequal-checksum command on mount
        stdoutdata: stdout data of arequal-checksum command execution
        stderrdata: stderr data of arequal-checksum command execution
    """
    output = {}
    output["exitstatus"] = None
    output["stdoutdata"] = None
    output["stderrdata"] = None

    logger = GlobalObj.getLoggerObj()
    env = GlobalObj.getTestenvObj()
    mount_obj = env.getMount(mountkey)
    if not mount_obj:
        logger.error("InValid Mount. %s not defined in TestEnvironment"
                     % mountkey)
        output["exitstatus"] = 1
        return output

    clientkey = mount_obj.client
    path = mount_obj.dir
    output = hostutils.md5sum(clientkey, path)
    return output

def md5sum_of_mounts(mounts):
    """
    Description:
        Calculate md5sum of mounts using arequal-checksum

    Parameters: mounts (type: List)

    Returns: md5sums of all the mounts (type: dict)
        Keys: mounts
        Value: ouput (type:dict)
            exitstatus: exit status of the arequal-checksum command on mount
            stdoutdata: stdout data of arequal-checksum command execution
            stderrdata: stderr data of arequal-checksum command execution
    """
    md5sums = {}
    for mountkey in mounts:
        output = md5sum_of_mount(mountkey)
        md5sums[mountkey] = output

    return md5sums

def execute_on_mount(mountkey, command, commandInput=None):
    """
    """
    output = {}
    output["exitstatus"] = None
    output["stdoutdata"] = None
    output["stderrdata"] = None

    logger = GlobalObj.getLoggerObj()
    env = GlobalObj.getTestenvObj()
    mount_obj = env.getMount(mountkey)
    if not mount_obj:
        logger.error("InValid Mount. %s not defined in TestEnvironment"
                     % mountkey)
        output["exitstatus"] = 1
        return output


    clientkey = mount_obj.client
    mountdir = mount_obj.dir
    command = "cd " + mountdir + " ;" + command
    output = hostutils.execute_command(clientkey, command, commandInput)
    return output

__all__ = ['execute_on_mount',
           'md5sum_of_mounts',
           'umount',
           'umountall',
           'mount',
           'mountall']