4
4
from pathlib import Path
5
5
6
6
7
- def read_yaml (test_info ):
7
+ def _get_default_backend (test_info : dict ) -> str :
8
+ return test_info .get ("backend" ) or test_info .get ("test_backend" , "xfs" )
9
+
10
+
11
+ def _get_default_server (test_info : dict ) -> str :
12
+ return (
13
+ test_info .get ("server" )
14
+ or test_info .get ("public_interfaces" , ["localhost" ])[0 ]
15
+ )
16
+
17
+
18
+ def _get_default_users (test_info : dict ) -> typing .Dict :
19
+ default_users = test_info .get ("users" )
20
+ if default_users is None :
21
+ default_users = {}
22
+ users = test_info .get ("test_users" )
23
+ if users is not None :
24
+ for user in users :
25
+ default_users [user ["username" ]] = user ["password" ]
26
+ return default_users
27
+
28
+
29
+ def read_yaml (test_info_file ):
8
30
"""Returns a dict containing the contents of the yaml file.
9
31
10
32
Parameters:
11
- test_info : filename of yaml file.
33
+ test_info_file : filename of yaml file.
12
34
13
35
Returns:
14
36
dict: The parsed test information yml as a dictionary.
15
37
"""
16
- with open (test_info ) as f :
38
+ with open (test_info_file ) as f :
17
39
test_info = yaml .load (f , Loader = yaml .FullLoader )
40
+
41
+ shares = test_info .get ("shares" , {})
42
+
43
+ # Copy exported_sharenames to shares
44
+ # Todo - remove once sit-environment is updated
45
+ for sharename in test_info .get ("exported_sharenames" , []):
46
+ assert sharename not in shares , "Duplicate share name present"
47
+ shares [sharename ] = {}
48
+
49
+ # Add missing fields with defaults
50
+ # Todo : Remove old field names once sit-environment is updated
51
+ default_backend = _get_default_backend (test_info )
52
+ default_server = _get_default_server (test_info )
53
+ default_users = _get_default_users (test_info )
54
+
55
+ for sharename in shares :
56
+ if shares [sharename ] is None :
57
+ shares [sharename ] = {"name" : sharename }
58
+ share = shares [sharename ]
59
+ share .setdefault ("name" , share )
60
+ share .setdefault ("backend" , {})
61
+ share .setdefault ("server" , default_server )
62
+ share .setdefault ("users" , default_users )
63
+ share ["backend" ].setdefault ("name" , default_backend )
64
+
65
+ test_info ["shares" ] = shares
66
+
18
67
return test_info
19
68
20
69
@@ -48,11 +97,14 @@ def get_mount_parameters(test_info: dict, share: str) -> typing.Dict[str, str]:
48
97
test_info: Dict containing the parsed yaml file.
49
98
share: The share for which to get the mount_params
50
99
"""
100
+ s = get_share (test_info , share )
101
+ server = s ["server" ]
102
+ users = list (s ["users" ].keys ())
51
103
return gen_mount_params (
52
- test_info [ "public_interfaces" ][ 0 ] ,
104
+ server ,
53
105
share ,
54
- test_info [ "test_users" ][ 0 ][ "username" ],
55
- test_info [ "test_users " ][0 ][ "password" ],
106
+ users [ 0 ],
107
+ s [ "users " ][users [ 0 ] ],
56
108
)
57
109
58
110
@@ -76,6 +128,46 @@ def generate_random_bytes(size: int) -> bytes:
76
128
return rba [:size ]
77
129
78
130
131
+ def get_shares (test_info : dict ) -> dict :
132
+ """
133
+ Get list of shares
134
+
135
+ Parameters:
136
+ test_info: Dict containing the parsed yaml file.
137
+ Returns:
138
+ list of dict of shares
139
+ """
140
+ return test_info ["shares" ]
141
+
142
+
143
+ def get_share (test_info : dict , sharename : str ) -> dict :
144
+ """
145
+ Get share dict for a given sharename
146
+
147
+ Parameters:
148
+ test_info: Dict containing the parsed yaml file.
149
+ sharename: name of the share
150
+ Returns:
151
+ dict of the share
152
+ """
153
+ shares = get_shares (test_info )
154
+ assert sharename in shares .keys (), "Share not found"
155
+ return shares [sharename ]
156
+
157
+
158
+ def is_premounted_share (share : dict ) -> bool :
159
+ """
160
+ Check if the share is a premounted share
161
+
162
+ Parameters:
163
+ share: dict of the share
164
+ Returns:
165
+ bool
166
+ """
167
+ mntdir = share .get ("path" )
168
+ return mntdir is not None
169
+
170
+
79
171
def get_premounted_shares (test_info : dict ) -> typing .List [Path ]:
80
172
"""
81
173
Get list of premounted shares
@@ -85,8 +177,8 @@ def get_premounted_shares(test_info: dict) -> typing.List[Path]:
85
177
Returns:
86
178
list of paths with shares
87
179
"""
88
- premounted_shares = test_info . get ( "premounted_shares" , [] )
89
- return [Path (mnt ) for mnt in premounted_shares ]
180
+ share_values = get_shares ( test_info ). values ( )
181
+ return [Path (s [ "path" ] ) for s in share_values if is_premounted_share ( s ) ]
90
182
91
183
92
184
def get_exported_shares (test_info : dict ) -> typing .List [str ]:
@@ -97,4 +189,8 @@ def get_exported_shares(test_info: dict) -> typing.List[str]:
97
189
Returns:
98
190
list of exported shares
99
191
"""
100
- return test_info .get ("exported_sharenames" , [])
192
+ arr = []
193
+ for share in get_shares (test_info ).values ():
194
+ if not is_premounted_share (share ):
195
+ arr .append (share ["name" ])
196
+ return arr
0 commit comments