diff options
author | Gonéri Le Bouder <goneri@lebouder.net> | 2020-04-24 15:42:36 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-24 13:42:36 -0600 |
commit | 72f6eb0339f7fcf7b8b02be2e86e5f7477cf731c (patch) | |
tree | 753d4a0791b0f044f789c57f025dc5d6e2558030 /tests/unittests | |
parent | e4499963d663b52ca4b66423c0445909d710cfbd (diff) | |
download | vyos-cloud-init-72f6eb0339f7fcf7b8b02be2e86e5f7477cf731c.tar.gz vyos-cloud-init-72f6eb0339f7fcf7b8b02be2e86e5f7477cf731c.zip |
BSD: find_devs_with_ refactoring (#298)
Refactoring of the `find_devs_with_*bsd()` methods:
- centralize everything in `util.py`
- add test coverage
Diffstat (limited to 'tests/unittests')
-rw-r--r-- | tests/unittests/test_datasource/test_nocloud.py | 17 | ||||
-rw-r--r-- | tests/unittests/test_util.py | 74 |
2 files changed, 90 insertions, 1 deletions
diff --git a/tests/unittests/test_datasource/test_nocloud.py b/tests/unittests/test_datasource/test_nocloud.py index 18bea0b9..2e6b53ff 100644 --- a/tests/unittests/test_datasource/test_nocloud.py +++ b/tests/unittests/test_datasource/test_nocloud.py @@ -288,8 +288,23 @@ class TestNoCloudDataSource(CiTestCase): self.mocks.enter_context( mock.patch.object(util, 'is_FreeBSD', return_value=True)) + def _mfind_devs_with_freebsd( + criteria=None, oformat='device', + tag=None, no_cache=False, path=None): + if not criteria: + return ["/dev/msdosfs/foo", "/dev/iso9660/foo"] + if criteria.startswith("LABEL="): + return ["/dev/msdosfs/foo", "/dev/iso9660/foo"] + elif criteria == "TYPE=vfat": + return ["/dev/msdosfs/foo"] + elif criteria == "TYPE=iso9660": + return ["/dev/iso9660/foo"] + return [] + self.mocks.enter_context( - mock.patch.object(os.path, 'exists', return_value=True)) + mock.patch.object( + util, 'find_devs_with_freebsd', + side_effect=_mfind_devs_with_freebsd)) dsrc = dsNoCloud(sys_cfg=sys_cfg, distro=None, paths=self.paths) ret = dsrc._get_devices('foo') diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py index 2900997b..e45bedc1 100644 --- a/tests/unittests/test_util.py +++ b/tests/unittests/test_util.py @@ -1190,4 +1190,78 @@ def test_find_devs_with_openbsd_with_criteria(m_subp): assert devlist == ['/dev/cd0a'] +@mock.patch('glob.glob') +def test_find_devs_with_freebsd(m_glob): + def fake_glob(pattern): + msdos = ["/dev/msdosfs/EFISYS"] + iso9660 = ["/dev/iso9660/config-2"] + if pattern == "/dev/msdosfs/*": + return msdos + elif pattern == "/dev/iso9660/*": + return iso9660 + raise Exception + m_glob.side_effect = fake_glob + + devlist = util.find_devs_with_freebsd() + assert set(devlist) == set([ + '/dev/iso9660/config-2', '/dev/msdosfs/EFISYS']) + devlist = util.find_devs_with_freebsd(criteria="TYPE=iso9660") + assert devlist == ['/dev/iso9660/config-2'] + devlist = util.find_devs_with_freebsd(criteria="TYPE=vfat") + assert devlist == ['/dev/msdosfs/EFISYS'] + + +@mock.patch("cloudinit.util.subp") +def test_find_devs_with_netbsd(m_subp): + side_effect_values = [ + ("ld0 dk0 dk1 cd0", ""), + ( + ( + "mscdlabel: CDIOREADTOCHEADER: " + "Inappropriate ioctl for device\n" + "track (ctl=4) at sector 0\n" + "disklabel not written\n" + ), + "", + ), + ( + ( + "mscdlabel: CDIOREADTOCHEADER: " + "Inappropriate ioctl for device\n" + "track (ctl=4) at sector 0\n" + "disklabel not written\n" + ), + "", + ), + ( + ( + "mscdlabel: CDIOREADTOCHEADER: " + "Inappropriate ioctl for device\n" + "track (ctl=4) at sector 0\n" + "disklabel not written\n" + ), + "", + ), + ( + ( + "track (ctl=4) at sector 0\n" + 'ISO filesystem, label "config-2", ' + "creation time: 2020/03/31 17:29\n" + "adding as 'a'\n" + ), + "", + ), + ] + m_subp.side_effect = side_effect_values + devlist = util.find_devs_with_netbsd() + assert set(devlist) == set( + ["/dev/ld0", "/dev/dk0", "/dev/dk1", "/dev/cd0"] + ) + m_subp.side_effect = side_effect_values + devlist = util.find_devs_with_netbsd(criteria="TYPE=iso9660") + assert devlist == ["/dev/cd0"] + m_subp.side_effect = side_effect_values + devlist = util.find_devs_with_netbsd(criteria="TYPE=vfat") + assert devlist == ["/dev/ld0", "/dev/dk0", "/dev/dk1"] + # vi: ts=4 expandtab |