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