diff --git a/tests/test_pe_helper.py b/tests/test_pe_helper.py new file mode 100644 index 0000000..4d6f746 --- /dev/null +++ b/tests/test_pe_helper.py @@ -0,0 +1,80 @@ + + +import pytest +from simplezfs.pe_helper import ExternalPEHelper, PEHelperBase, SudoPEHelper + + +class TestPEHelperBase: + + def test_repr(self): + pehb = PEHelperBase() + assert 'PEHelperBase' in str(pehb) + + def test_not_implemented(self): + pehb = PEHelperBase() + + with pytest.raises(NotImplementedError): + pehb.zfs_mount('test') + with pytest.raises(NotImplementedError): + pehb.zfs_set_mountpoint('test', '/test') + + +class TestExternalPEHelper: + + def test_repr(self): + eph = ExternalPEHelper('/bin/true') + assert 'ExternalPEHelper' in str(eph) + assert '/bin/true' in str(eph) + + # ######################################################################## + def test_get_executable(self): + eph = ExternalPEHelper('/bin/true') + assert eph.executable == '/bin/true' + + def test_set_executable(self): + eph = ExternalPEHelper('/bin/false') + assert eph.executable == '/bin/false' + + eph.executable = '/bin/true' + assert eph.executable == '/bin/true' + + def test_set_executable_not_found_ctor(self): + with pytest.raises(FileNotFoundError) as e: + ExternalPEHelper('/file/does/not/exist') + assert 'No such file or directory' in str(e.value) + + def test_set_executable_not_found_property(self): + eph = ExternalPEHelper('/bin/true') + with pytest.raises(FileNotFoundError) as e: + eph.executable = '/file/does/not/exist' + assert 'No such file or directory' in str(e.value) + + def test_set_executable_directory_ctor(self): + with pytest.raises(FileNotFoundError) as e: + ExternalPEHelper('/') + assert 'must be a file' in str(e.value) + + def test_set_executable_directory_property(self): + eph = ExternalPEHelper('/bin/true') + with pytest.raises(FileNotFoundError) as e: + eph.executable = '/' + assert 'must be a file' in str(e.value) + + def test_set_executable_nonexecutable_ctor(self): + with pytest.raises(FileNotFoundError) as e: + ExternalPEHelper('/etc/hosts') + assert 'must be executable' in str(e.value) + + def test_set_executable_nonexecutable_property(self): + eph = ExternalPEHelper('/bin/true') + with pytest.raises(FileNotFoundError) as e: + eph.executable = '/etc/hosts' + assert 'must be executable' in str(e.value) + + +class TestSudoPEHelper: + + def test_repr(self): + sph = SudoPEHelper() + assert 'SudoPEHelper' in str(sph) + assert 'sudo' in str(sph) diff --git a/tests/test_zfs.py b/tests/test_zfs.py index b4abccf..710bd76 100644 --- a/tests/test_zfs.py +++ b/tests/test_zfs.py @@ -7,6 +7,7 @@ from unittest.mock import patch import pytest # type: ignore from simplezfs.exceptions import ValidationError +from simplezfs.pe_helper import PEHelperBase from simplezfs.types import Dataset, DatasetType from simplezfs.zfs import ZFS, get_zfs from simplezfs.zfs_cli import ZFSCli @@ -29,6 +30,19 @@ class TestZFS: ########################################################################## + def test_get_pe_helper_default(self): + zfs = ZFS() + assert zfs.pe_helper is None + + def test_get_pe_helper_set_valid(self): + zfs = ZFS() + assert zfs.pe_helper is None + zfs.pe_helper = PEHelperBase() + assert zfs.pe_helper is not None + assert isinstance(zfs.pe_helper, PEHelperBase) + + ########################################################################## + def test_get_property_notimplemented(self): zfs = ZFS() with pytest.raises(NotImplementedError):