summaryrefslogtreecommitdiffstats
path: root/Libraries/ClientUtils/ATFClientUtils.py
blob: e3a1bfe408c5524f681f37dda5f8124065f36469 (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
#!/usr/bin/env python

import re
import ATFUtils

def create_mountpoint(mountname, **arguments):
    """
    Description:
        Create a Mount Point On the Client

    Parameter:
        mountname: Name of the MountPoint
        **arguments: key=value pair for specifying server, host, user

    Example:
        mountname=mount1 
        server=server1
        host=host1
        user=user1

    Returns:
        Success: 0
        Failure: 1
    """

    command = "mkdir -p"
    mountpoint = ATFUtils.TestEnvObj.get_mountpoint(mountname)

    if mountpoint == '':
        ATFUtils.Logger.error("MountPoint: %s Not defined in GlobalParam File"
                % mountname)
        return 1
    else:
        command = command + " " + mountpoint
        
    arguments['user'] = 'root'
    status, stdin, stdout, stderr = ATFUtils.execute_command(command,
                                                            **arguments)

    if status == 1:
        return 1
    else:
        output = ATFUtils.parse_output(stdout, stderr)
        return ATFUtils.set_environ(ATF_MOUNTPOINT = mountname)

def umount(mountname, **arguments):
    """
    Description:
        Unmount a mount point

    Parameters:
        mountname: Name of the mountpoint to unmount
        **arguments: key=value pair for specifying server, host, user

    Example:
        mountname=mount1 
        server=server1
        host=host1
        user=user1

    Returns:
        Success: 0
        Failure: 1
    """

    command = "umount"

    mountpoint = ATFUtils.TestEnvObj.get_mountpoint(mountname)

    if mountpoint == '':
        ATFUtils.Logger.error("MountPoint: %s Not defined in GlobalParam File"
                % mountname)
        return 1
    else:
        command = command + " " + mountpoint

    arguments['user'] = 'root'
    status, stdin, stdout, stderr = ATFUtils.execute_command(command,
                                                            **arguments)

    if status == 1:
        return 1
    else:
        return ATFUtils.parse_output(stdout, stderr)
    
            
def mount(mountname, fstype, hostvolume, **arguments):
    """
    Description:
        Mount a filesystem

    Parameter:
        mountname: Absolute Path of the mount point
        fstype: FileSystem type
        hostvolume: HostVolumeName to mount on mountpoint
        **arguments: key=value pair for specifying host, user

    Returns:
        Success: 0
        Failure: 1
    """

    command = "mount"
    mounttype = " -t " + fstype
    command = command + mounttype
    hostkey, volumekey = re.split(":", hostvolume)

    host = ATFUtils.TestEnvObj.get_host(hostkey)
    if host == '':
        ATFUtils.Logger.error("Host %s Not defined in GlobalParam File" %
                hostkey)
        return 1

    volumename = ATFUtils.TestEnvObj.get_volume(volumekey)
    if volumename == '':
        ATFUtils.Logger.error("Volume %s Not defined in GlobalParam File" %
                volumekey)
        return 1
      
    mountpoint = ATFUtils.TestEnvObj.get_mountpoint(mountname)
    if mountpoint == '':
        ATFUtils.Logger.error("MountPoint: %s Not defined in GlobalParam File"
                % mountname)
        return 1
 
    hostvolume = host + ":" + volumename
    command = command + " " + hostvolume + " " + mountpoint
    arguments['user'] = 'root'
    status, stdin, stdout, stderr = ATFUtils.execute_command(command,
                                                            **arguments)

    if status == 1:
        return 1
    else:
        return ATFUtils.parse_output(stdout, stderr)


def touch(filename, mountname='', **arguments):
    """
    Description:
        Creates a File.

    Parameters:
        filename: Name of a file
        mountname : Name of the MountPoint
        **arguments: key=value pair for specifying host, user

    Returns:
        Success: 0
        Failure: 1
    """

    command = "touch"

    if mountname == '':
        mountname = ATFUtils.get_environ('ATF_MOUNTPOINT')
        if mountname == 1:
            return 1
        
    mountpoint = ATFUtils.TestEnvObj.get_mountpoint(mountname)
    if mountpoint == '':
        ATFUtils.Logger.error("MountPoint: %s Not defined in GlobalParam File"
                % mountname)
        return 1

    abspath = mountpoint + "/" + filename
    command = command + " " + abspath
    status, stdin, stdout, stderr = ATFUtils.execute_command(command, 
                                                            **arguments)

    if status == 1:
        return 1
    else:
        return ATFUtils.parse_output(stdout, stderr)
        
def mkdir(dirname, mountname='', **arguments):
    """
    Description:
        Create  a Directory under the mountpoint

    Parameters:
        dirname: directory path (Relative path from the mount point)
        mountname: Name of the Mount Point
        **arguments: key=value pair for specifying host, user

    Returns:
        Success: 0
        Failure: 1
    """

    command = "mkdir"

    if mountname == '':
        mountname = ATFUtils.get_environ('ATF_MOUNTPOINT')
        if mountname == 1:
            return 1
        
    mountpoint = ATFUtils.TestEnvObj.get_mountpoint(mountname)
    if mountpoint == '':
        ATFUtils.Logger.error("MountPoint: %s Not defined in GlobalParam File"
                % mountname)
        return 1
        
    abspath = mountpoint + "/" + dirname
    command = command + " " + abspath
    status, stdin, stdout, stderr = ATFUtils.execute_command(command, 
                                                            **arguments)

    if status == 1:
        return 1
    else:
        return ATFUtils.parse_output(stdout, stderr)