Skip to content

Commit 65bad4a

Browse files
sonicajBrandon Schneider
authored and
Brandon Schneider
committed
Add More tests (iocage#740)
* Initial commit for adding more tests (iocage#736) This commit adds the basics for the functional tests coming in. Ticket: #59247 * Starting/Stopping Jails Tests This commit adds tests relating to starting and stopping jails * Setting/Getting Props Tests This commit adds tests relating to setting and getting props on jails * Exec/Clone/Debug Tests This commit adds tests relating to exec, clone and debug commands * Df/Export/Import Tests This commit adds tests relating to df, export and import commands * Console/Rename/Restart Tests This commit adds tests relating to console, rename and restart commands * Fstab/Snapshot Tests This commit adds tests relating to fstab and snapshot related commands * List Tests This commit adds tests relating to list command * Tests for activate and fetch This commit refines the existing tests for activate and fetch * Creating Jails tests This commit adds tests for creating jails * Clean/Destroy Tests This commit adds tests relating to destroy and clean commands * Not stopping at first failed test This commit fixes a bug where the tests stopped at the first failed test.
1 parent 6110ffe commit 65bad4a

28 files changed

+2641
-226
lines changed

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tool:pytest]
2-
addopts = -v -x -rs --ignore=setup.py --pep8 --cov-report term-missing --cov=iocage tests
2+
addopts = -vv -rs --ignore=setup.py --pep8 --cov-report term-missing --cov=iocage tests
33
pep8maxlinelength = 80
44
pep8ignore = * ALL
55
[aliases]

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

+212-40
Original file line numberDiff line numberDiff line change
@@ -21,96 +21,176 @@
2121
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2222
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2323
# POSSIBILITY OF SUCH DAMAGE.
24+
2425
import os
26+
import re
27+
import subprocess
2528

2629
import pytest
2730

28-
from iocage_lib.ioc_common import checkoutput
31+
import iocage_lib.ioc_common
32+
from .data_classes import ZFS, Jail, ResourceSelector, Row
2933

3034

3135
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.")
5336
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+
)
5687

5788

5889
def pytest_runtest_setup(item):
5990
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')
61115

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')
64125

65126

66127
@pytest.fixture
67128
def zpool(request):
68129
"""Specify a zpool to use."""
69-
return request.config.getoption("--zpool")
130+
return request.config.getoption('--zpool')
70131

71132

72133
@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):
74147
"""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
76153

77154

78155
@pytest.fixture
79156
def server(request):
80157
"""FTP server to login to."""
81-
return request.config.getoption("--server")
158+
return request.config.getoption('--server')
82159

83160

84161
@pytest.fixture
85162
def user(request):
86163
"""The user to use for fetching."""
87-
return request.config.getoption("--user")
164+
return request.config.getoption('--user')
88165

89166

90167
@pytest.fixture
91168
def password(request):
92169
"""The password to use for fetching."""
93-
return request.config.getoption("--password")
170+
return request.config.getoption('--password')
94171

95172

96173
@pytest.fixture
97174
def root_dir(request):
98175
"""Root directory containing all the RELEASEs for fetching."""
99-
return request.config.getoption("--root-dir")
176+
return request.config.getoption('--root-dir')
100177

101178

102179
@pytest.fixture
103180
def http(request):
104181
"""Have --server define a HTTP server instead."""
105-
return request.config.getoption("--http")
182+
return request.config.getoption('--http')
106183

107184

108185
@pytest.fixture
109186
def hardened(request):
110187
"""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
112190

113-
if "HBSD" in freebsd_version:
191+
freebsd_version = iocage_lib.ioc_common.checkoutput(['freebsd-version'])
192+
193+
if 'HBSD' in freebsd_version:
114194
_hardened = True
115195
else:
116196
_hardened = False
@@ -121,16 +201,108 @@ def hardened(request):
121201
@pytest.fixture
122202
def _file(request):
123203
"""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')
125205

126206

127207
@pytest.fixture
128208
def auth(request):
129209
"""Authentication method for HTTP fetching. Valid values: basic, digest"""
130-
return request.config.getoption("--auth")
210+
return request.config.getoption('--auth')
131211

132212

133213
@pytest.fixture
134214
def noupdate(request):
135215
""" 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

Comments
 (0)