21
21
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
22
22
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23
23
# POSSIBILITY OF SUCH DAMAGE.
24
+
24
25
import os
26
+ import re
27
+ import subprocess
25
28
26
29
import pytest
27
30
28
- from iocage_lib .ioc_common import checkoutput
31
+ import iocage_lib .ioc_common
32
+ from .data_classes import ZFS , Jail , ResourceSelector , Row
29
33
30
34
31
35
def pytest_addoption (parser ):
32
- parser .addoption ("--zpool" , action = "store" , default = None ,
33
- help = "Specify a zpool to use." )
34
- parser .addoption ("--release" , action = "store" , default = "11.2-RELEASE" ,
35
- help = "Specify a RELEASE to use." )
36
- parser .addoption ("--server" , action = "store" , default = "ftp.freebsd.org" ,
37
- help = "FTP server to login to." )
38
- parser .addoption ("--user" , action = "store" , default = "anonymous" ,
39
- help = "The user to use for fetching." )
40
- parser .addoption ("--password" , action = "store" , default = "anonymous@" ,
41
- help = "The password to use for fetching." )
42
- parser .addoption ("--http" , action = "store_true" ,
43
- help = "Have --server define a HTTP server instead." )
44
- parser .addoption ("--noupdate" , action = "store_true" ,
45
- help = "Decide whether or not to update the fetch to the"
46
- " latest patch level." )
47
- parser .addoption ("--auth" , action = "store" , default = None ,
48
- help = "Authentication method for HTTP fetching. Valid"
49
- " values: basic, digest" )
50
- parser .addoption ("--file" , action = "store_true" ,
51
- help = "Use a local file directory for root-dir instead of "
52
- "FTP or HTTP." )
53
36
parser .addoption (
54
- "--root-dir" , action = "store" ,
55
- help = "Root directory containing all the RELEASEs for fetching." )
37
+ '--zpool' , action = 'store' , default = None ,
38
+ help = 'Specify a zpool to use.'
39
+ )
40
+ parser .addoption (
41
+ '--release' , action = 'store' , default = '11.2-RELEASE' ,
42
+ help = 'Specify a RELEASE to use.'
43
+ )
44
+ parser .addoption (
45
+ '--server' , action = 'store' , default = 'download.freebsd.org' ,
46
+ help = 'FTP server to login to.'
47
+ )
48
+ parser .addoption (
49
+ '--user' , action = 'store' , default = 'anonymous' ,
50
+ help = 'The user to use for fetching.'
51
+ )
52
+ parser .addoption (
53
+ '--password' , action = 'store' , default = 'anonymous@' ,
54
+ help = 'The password to use for fetching.'
55
+ )
56
+ parser .addoption (
57
+ '--http' , action = 'store_true' ,
58
+ help = 'Have --server define a HTTP server instead.'
59
+ )
60
+ parser .addoption (
61
+ '--noupdate' , action = 'store_true' ,
62
+ help = 'Decide whether or not to update the fetch to the latest '
63
+ 'patch level.'
64
+ )
65
+ parser .addoption (
66
+ '--auth' , action = 'store' , default = None ,
67
+ help = 'Authentication method for HTTP fetching. Valid'
68
+ ' values: basic, digest'
69
+ )
70
+ parser .addoption (
71
+ '--file' , action = 'store_true' ,
72
+ help = 'Use a local file directory for root-dir instead of '
73
+ 'FTP or HTTP.'
74
+ )
75
+ parser .addoption (
76
+ '--root-dir' , action = 'store' ,
77
+ help = 'Root directory containing all the RELEASEs for fetching.'
78
+ )
79
+ parser .addoption (
80
+ '--jail_ip' , action = 'store' , default = None ,
81
+ help = 'Static IP to use creating jails'
82
+ )
83
+ parser .addoption (
84
+ '--dhcp' , action = 'store_true' , default = False ,
85
+ help = 'Use DHCP for creating jails'
86
+ )
56
87
57
88
58
89
def pytest_runtest_setup (item ):
59
90
if 'require_root' in item .keywords and not os .getuid () == 0 :
60
- pytest .skip ("Need to be root to run" )
91
+ pytest .skip ('Need to be root to run' )
92
+
93
+ if 'require_zpool' in item .keywords and not item .config .getvalue ('zpool' ):
94
+ pytest .skip ('Need --zpool option to run' )
95
+
96
+ if 'require_dhcp' in item .keywords and not item .config .getvalue ('dhcp' ):
97
+ pytest .skip ('Need --dhcp option to run' )
98
+
99
+ if (
100
+ 'require_jail_ip' in item .keywords
101
+ and not item .config .getvalue ('jail_ip' )
102
+ ):
103
+ pytest .skip ('Need --jail_ip option to run' )
104
+
105
+ if (
106
+ 'require_networking' in item .keywords
107
+ and all (
108
+ not v for v in (
109
+ item .config .getvalue ('--dhcp' ),
110
+ item .config .getvalue ('--jail_ip' )
111
+ )
112
+ )
113
+ ):
114
+ pytest .skip ('Need either --dhcp or --jail_ip option to run' )
61
115
62
- if 'require_zpool' in item .keywords and not item .config .getvalue ("zpool" ):
63
- pytest .skip ("Need --zpool option to run" )
116
+ if (
117
+ 'require_networking' in item .keywords and all (
118
+ v for v in (
119
+ item .config .getvalue ('--dhcp' ),
120
+ item .config .getvalue ('--jail_ip' )
121
+ )
122
+ )
123
+ ):
124
+ pytest .skip ('Need either --dhcp or --jail_ip option to run, not both' )
64
125
65
126
66
127
@pytest .fixture
67
128
def zpool (request ):
68
129
"""Specify a zpool to use."""
69
- return request .config .getoption (" --zpool" )
130
+ return request .config .getoption (' --zpool' )
70
131
71
132
72
133
@pytest .fixture
73
- def release (request ):
134
+ def jail_ip (request ):
135
+ """Specify a jail ip to use."""
136
+ return request .config .getoption ('--jail_ip' )
137
+
138
+
139
+ @pytest .fixture
140
+ def dhcp (request ):
141
+ """Specify if dhcp is to be used."""
142
+ return request .config .getoption ('--dhcp' )
143
+
144
+
145
+ @pytest .fixture
146
+ def release (request , hardened ):
74
147
"""Specify a RELEASE to use."""
75
- return request .config .getoption ("--release" )
148
+ release = request .config .getoption ('--release' )
149
+ if hardened :
150
+ release = release .replace ('-RELEASE' , '-STABLE' )
151
+ release = re .sub (r'\W\w.' , '-' , release )
152
+ return release
76
153
77
154
78
155
@pytest .fixture
79
156
def server (request ):
80
157
"""FTP server to login to."""
81
- return request .config .getoption (" --server" )
158
+ return request .config .getoption (' --server' )
82
159
83
160
84
161
@pytest .fixture
85
162
def user (request ):
86
163
"""The user to use for fetching."""
87
- return request .config .getoption (" --user" )
164
+ return request .config .getoption (' --user' )
88
165
89
166
90
167
@pytest .fixture
91
168
def password (request ):
92
169
"""The password to use for fetching."""
93
- return request .config .getoption (" --password" )
170
+ return request .config .getoption (' --password' )
94
171
95
172
96
173
@pytest .fixture
97
174
def root_dir (request ):
98
175
"""Root directory containing all the RELEASEs for fetching."""
99
- return request .config .getoption (" --root-dir" )
176
+ return request .config .getoption (' --root-dir' )
100
177
101
178
102
179
@pytest .fixture
103
180
def http (request ):
104
181
"""Have --server define a HTTP server instead."""
105
- return request .config .getoption (" --http" )
182
+ return request .config .getoption (' --http' )
106
183
107
184
108
185
@pytest .fixture
109
186
def hardened (request ):
110
187
"""Have fetch expect the default HardeneBSD layout instead."""
111
- freebsd_version = checkoutput (["freebsd-version" ])
188
+ # TODO: This isn't probably being used anywhere except for
189
+ # in release fixture, let's move it there and remove this
112
190
113
- if "HBSD" in freebsd_version :
191
+ freebsd_version = iocage_lib .ioc_common .checkoutput (['freebsd-version' ])
192
+
193
+ if 'HBSD' in freebsd_version :
114
194
_hardened = True
115
195
else :
116
196
_hardened = False
@@ -121,16 +201,108 @@ def hardened(request):
121
201
@pytest .fixture
122
202
def _file (request ):
123
203
"""Use a local file directory for root-dir instead of FTP or HTTP."""
124
- return request .config .getoption (" --file" )
204
+ return request .config .getoption (' --file' )
125
205
126
206
127
207
@pytest .fixture
128
208
def auth (request ):
129
209
"""Authentication method for HTTP fetching. Valid values: basic, digest"""
130
- return request .config .getoption (" --auth" )
210
+ return request .config .getoption (' --auth' )
131
211
132
212
133
213
@pytest .fixture
134
214
def noupdate (request ):
135
215
""" Decide whether or not to update the fetch to the latest patch level."""
136
- return request .config .getoption ("--noupdate" )
216
+ return request .config .getoption ('--noupdate' )
217
+
218
+
219
+ @pytest .fixture
220
+ def invoke_cli ():
221
+ def invoke (cmd , reason = None , assert_returncode = True ):
222
+ cmd .insert (0 , 'iocage' )
223
+ cmd = [str (c ) for c in cmd ]
224
+
225
+ result = subprocess .run (cmd , stdout = subprocess .PIPE )
226
+ reason = f'{ reason } : { result .stderr } ' if reason else result .stderr
227
+
228
+ if assert_returncode :
229
+ assert result .returncode == 0 , reason
230
+
231
+ result .output = result .stdout .decode ('utf-8' )
232
+
233
+ return result
234
+
235
+ return invoke
236
+
237
+
238
+ @pytest .fixture
239
+ def write_file ():
240
+ def write_to_file (location , data ):
241
+ with iocage_lib .ioc_common .open_atomic (location , 'w' ) as f :
242
+ f .write (data )
243
+
244
+ return write_to_file
245
+
246
+
247
+ @pytest .fixture
248
+ def remove_file ():
249
+ def remove (path ):
250
+ if os .path .exists (path ):
251
+ os .remove (path )
252
+
253
+ return remove
254
+
255
+
256
+ @pytest .fixture
257
+ def zfs ():
258
+ return ZFS ()
259
+
260
+
261
+ @pytest .fixture
262
+ def jail ():
263
+ return Jail
264
+
265
+
266
+ @pytest .fixture
267
+ def resource_selector ():
268
+ return ResourceSelector ()
269
+
270
+
271
+ @pytest .fixture
272
+ def skip_test ():
273
+ def skip (condition , reason = '' ):
274
+ # if condition evaluates to True, let's skip the test
275
+ if condition :
276
+ pytest .skip (reason )
277
+
278
+ return skip
279
+
280
+
281
+ @pytest .fixture
282
+ def freebsd_download_server ():
283
+ return f'http://download.freebsd.org/ftp/releases/{ os .uname ()[4 ]} '
284
+
285
+
286
+ @pytest .fixture
287
+ def parse_rows_output ():
288
+ def _output_list (data , type ):
289
+ rows = []
290
+ for index , line in enumerate (data .split ('\n ' )):
291
+ if all (
292
+ s not in line for s in ('----' , '====' )
293
+ ) and line and index != 1 :
294
+ rows .append (Row (line , type ))
295
+ return rows
296
+
297
+ return _output_list
298
+
299
+
300
+ @pytest .fixture
301
+ def jails_as_rows ():
302
+ def _default_jails (resources , ** kwargs ):
303
+ return [
304
+ resource .convert_to_row (** kwargs )
305
+ for resource in resources
306
+ ]
307
+
308
+ return _default_jails
0 commit comments