Skip to content

Commit be33344

Browse files
lpefferkornBrandon Schneider
authored and
Brandon Schneider
committed
Improve testing (#54)
* Updating .gitignore with .coverage,build/ * Adding code coverage, more integration of functional tests with pytest access
1 parent deccf6a commit be33344

19 files changed

+145
-24
lines changed

.coveragerc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[run]
2+
branch = True
3+
omit = */tests/*
4+
[report]
5+
show_missing = True

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
.tox/
88
.cache/
99
.idea/
10+
.coverage
1011
venv/
12+
build/
1113
venv-*/

iocage/tests/0002_ioc_check_test.py

-7
This file was deleted.

iocage/tests/README.md

+57-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,62 @@
1-
# Running tests:
2-
================
3-
**These tests are written for `pytest`**
1+
# Code testing
2+
All the tests are written using the `pytest` unit testing framework. Code coverage is provided by `pytest-cov`
43

5-
- Make sure you're in the iocage directory
6-
- Supply --zpool="POOL" to pytest
4+
Before running tests, test dependencies can be installed by running:
5+
```
6+
$ pip install pytest-cov pytest-pep8
7+
```
8+
9+
## Unit tests
10+
11+
Located in the ``tests/unit_tests`` directory, they can be started as a normal user with the following command:
12+
13+
```
14+
$ pytest
15+
```
16+
17+
## Functional tests
18+
19+
Located in the ``tests/functional_tests``, they need a root acces and the name of a ZFS pool
720

8-
#####Example:
21+
**/!\ The contents of the specified ZFS pool will be destroyed**
22+
23+
To start the functional tests, run pytest with root privileges and the name of a zpool:
924
```
10-
git clone https://github.com/iocage/iocage.git
11-
cd iocage/iocage
12-
sudo pytest --zpool="TEST" --server="custom_server"
25+
$ sudo pytest --zpool=mypool
1326
```
1427

15-
**To list all supported fixtures to pytest:**
16-
`pytest --fixtures`
28+
Other parameters are available, to see them run:
29+
```
30+
$ pytest --fixtures
31+
```
32+
Extract:
33+
```
34+
zpool
35+
Specify a zpool to use.
36+
release
37+
Specify a RELEASE to use.
38+
server
39+
FTP server to login to.
40+
user
41+
The user to use for fetching.
42+
password
43+
The password to use for fetching.
44+
root_dir
45+
Root directory containing all the RELEASEs for fetching.
46+
http
47+
Have --server define a HTTP server instead.
48+
hardened
49+
Have fetch expect the default HardeneBSD layout instead.
50+
_file
51+
Use a local file directory for root-dir instead of FTP or HTTP.
52+
auth
53+
Authentication method for HTTP fetching. Valid values: basic, digest
54+
```
55+
56+
57+
#Example
58+
```
59+
$ git clone https://github.com/iocage/iocage.git
60+
$ cd iocage/iocage
61+
$ sudo pytest --zpool="TEST" --server="custom_server"
62+
```

iocage/tests/__init__.py

Whitespace-only changes.

iocage/tests/conftest.py

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import os
23

34

45
def pytest_addoption(parser):
@@ -31,6 +32,14 @@ def pytest_addoption(parser):
3132
help="Root directory containing all the RELEASEs for fetching.")
3233

3334

35+
def pytest_runtest_setup(item):
36+
if 'require_root' in item.keywords and not os.getuid() == 0:
37+
pytest.skip("Need to be root to run")
38+
39+
if 'require_zpool' in item.keywords and not item.config.getvalue("zpool"):
40+
pytest.skip("Need --zpool option to run")
41+
42+
3443
@pytest.fixture
3544
def zpool(request):
3645
"""Specify a zpool to use."""

iocage/tests/0001_ioc_activate_test.py iocage/tests/functional_tests/0001_ioc_activate_test.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import os
1+
import pytest
22
import subprocess
33

4+
require_root = pytest.mark.require_root
5+
require_zpool = pytest.mark.require_zpool
46

7+
@require_root
8+
@require_zpool
59
def test_activate(zpool):
6-
if os.geteuid() != 0:
7-
raise RuntimeError(
8-
"You need to have root privileges to run any tests.")
910
try:
1011
subprocess.check_call(["zfs", "set", "org.freebsd.ioc:active=yes",
1112
zpool], stdout=subprocess.PIPE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pytest
2+
from iocage.lib.ioc_check import IOCCheck
3+
4+
require_root = pytest.mark.require_root
5+
require_zpool = pytest.mark.require_zpool
6+
7+
8+
@require_root
9+
@require_zpool
10+
def test_check():
11+
IOCCheck()
12+
13+
assert True == True

iocage/tests/0003_ioc_fetch_test.py iocage/tests/functional_tests/0003_ioc_fetch_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import pytest
12
from iocage.lib.ioc_fetch import IOCFetch
23

4+
require_root = pytest.mark.require_root
5+
require_zpool = pytest.mark.require_zpool
36

7+
8+
@require_root
9+
@require_zpool
410
def test_fetch(release, server, user, password, auth, root_dir, http, _file,
511
hardened):
612
IOCFetch(release, server, user, password, auth, root_dir, http=http,

iocage/tests/0004_ioc_create_test.py iocage/tests/functional_tests/0004_ioc_create_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import pytest
12
from iocage.lib.ioc_create import IOCCreate
23

4+
require_root = pytest.mark.require_root
5+
require_zpool = pytest.mark.require_zpool
36

7+
8+
@require_root
9+
@require_zpool
410
def test_create(release, hardened):
511
prop = ("tag=test",)
612
prop_short = ("tag=test_short",)

iocage/tests/0005_ioc_start_test.py iocage/tests/functional_tests/0005_ioc_start_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import pytest
12
from iocage.lib.ioc_json import IOCJson
23
from iocage.lib.ioc_list import IOCList
34
from iocage.lib.ioc_start import IOCStart
45

6+
require_root = pytest.mark.require_root
7+
require_zpool = pytest.mark.require_zpool
58

9+
10+
@require_root
11+
@require_zpool
612
def test_start():
713
jails, paths = IOCList("uuid").list_datasets()
814

iocage/tests/0006_ioc_stop_test.py iocage/tests/functional_tests/0006_ioc_stop_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import pytest
12
from iocage.lib.ioc_json import IOCJson
23
from iocage.lib.ioc_list import IOCList
34
from iocage.lib.ioc_stop import IOCStop
45

6+
require_root = pytest.mark.require_root
7+
require_zpool = pytest.mark.require_zpool
58

9+
10+
@require_root
11+
@require_zpool
612
def test_stop():
713
jails, paths = IOCList("uuid").list_datasets()
814

iocage/tests/0007_ioc_set_test.py iocage/tests/functional_tests/0007_ioc_set_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import pytest
12
from iocage.lib.ioc_json import IOCJson
23
from iocage.lib.ioc_list import IOCList
34

5+
require_root = pytest.mark.require_root
6+
require_zpool = pytest.mark.require_zpool
47

8+
9+
@require_root
10+
@require_zpool
511
def test_set():
612
_, paths = IOCList("uuid").list_datasets()
713

iocage/tests/0008_ioc_get_test.py iocage/tests/functional_tests/0008_ioc_get_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import pytest
12
from iocage.lib.ioc_json import IOCJson
23
from iocage.lib.ioc_list import IOCList
34

5+
require_root = pytest.mark.require_root
6+
require_zpool = pytest.mark.require_zpool
47

8+
9+
@require_root
10+
@require_zpool
511
def test_get():
612
_, paths = IOCList("uuid").list_datasets()
713

iocage/tests/0009_ioc_destroy_test.py iocage/tests/functional_tests/0009_ioc_destroy_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import pytest
12
from iocage.lib.ioc_destroy import IOCDestroy
23
from iocage.lib.ioc_list import IOCList
34

5+
require_root = pytest.mark.require_root
6+
require_zpool = pytest.mark.require_zpool
47

8+
9+
@require_root
10+
@require_zpool
511
def test_destroy():
612
jails, paths = IOCList("uuid").list_datasets()
713

iocage/tests/9999_ioc_clean_test.py iocage/tests/functional_tests/9999_ioc_clean_test.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import os
2-
2+
import pytest
33
from iocage.lib.ioc_clean import IOCClean
44

5+
require_root = pytest.mark.require_root
6+
require_zpool = pytest.mark.require_zpool
7+
58

9+
@require_root
10+
@require_zpool
611
def test_clean():
712
# Unless we change directory (not sure why) this will crash pytest.
813
os.chdir("/")

setup.cfg

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[tool:pytest]
2+
addopts = -v -x -rs --ignore=setup.py --pep8 --cov-report term-missing --cov=iocage iocage iocage/tests
3+
pep8maxlinelength = 80
4+
pep8ignore = * ALL

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
'iocage = iocage.main:main'
3131
]
3232
},
33-
data_files=_data
33+
data_files=_data,
34+
tests_require=['pytest', 'pytest-cov', 'pytest-pep8']
3435
)

0 commit comments

Comments
 (0)