From cea678641300a45371834b1263994063dccfe8ee Mon Sep 17 00:00:00 2001 From: Willem Jan Withagen Date: Tue, 8 Nov 2016 15:24:08 +0100 Subject: [PATCH] ceph-detect-init: FreeBSD introduction of bsdrc ceph-detect-init/ceph_detect_init/freebsd/__init__.py New file ceph_detect_init/__init__.py: Only test FreeBSD after it is not Linux If we do it the other way around it will not work during testing on FreeBSD because it will always have platform.system() == FreeBSD So first test for Linux, and only then check to see if it is FreeBSD. ceph-detect-init/tests/test_all.py: add tests for FreeBSD ceph-detect-init/run-tox.sh: ReEnable to run test for FreeBSD Signed-off-by: Willem Jan Withagen Signed-off-by: Kefu Chai --- .../ceph_detect_init/__init__.py | 23 ++++++-- .../ceph_detect_init/freebsd/__init__.py | 11 ++++ src/ceph-detect-init/run-tox.sh | 5 -- src/ceph-detect-init/tests/test_all.py | 53 ++++++++++++++++--- 4 files changed, 74 insertions(+), 18 deletions(-) create mode 100644 src/ceph-detect-init/ceph_detect_init/freebsd/__init__.py diff --git a/src/ceph-detect-init/ceph_detect_init/__init__.py b/src/ceph-detect-init/ceph_detect_init/__init__.py index 561900d2f33f9..b749ea08b5224 100644 --- a/src/ceph-detect-init/ceph_detect_init/__init__.py +++ b/src/ceph-detect-init/ceph_detect_init/__init__.py @@ -21,6 +21,7 @@ from ceph_detect_init import fedora from ceph_detect_init import rhel from ceph_detect_init import suse from ceph_detect_init import gentoo +from ceph_detect_init import freebsd import logging import platform @@ -65,6 +66,7 @@ def _get_distro(distro, use_rhceph=False): 'gentoo': gentoo, 'funtoo': gentoo, 'exherbo': gentoo, + 'freebsd': freebsd, } if distro == 'redhat' and use_rhceph: @@ -90,11 +92,22 @@ def _normalized_distro_name(distro): def platform_information(): """detect platform information from remote host.""" - linux_distro = platform.linux_distribution( - supported_dists=platform._supported_dists + ('alpine',)) - logging.debug('platform_information: linux_distribution = ' + - str(linux_distro)) - distro, release, codename = linux_distro + if platform.system() == 'Linux': + linux_distro = platform.linux_distribution( + supported_dists=platform._supported_dists + ('alpine',)) + logging.debug('platform_information: linux_distribution = ' + + str(linux_distro)) + distro, release, codename = linux_distro + elif platform.system() == 'FreeBSD': + distro = 'freebsd' + release = platform.release() + codename = platform.version().split(' ')[3].split(':')[0] + logging.debug( + 'platform_information: release = {}, version = {}'.format( + platform.release(), platform.version())) + else: + raise exc.UnsupportedPlatform(platform.system(), '', '') + # this could be an empty string in Debian if not codename and 'debian' in distro.lower(): debian_codenames = { diff --git a/src/ceph-detect-init/ceph_detect_init/freebsd/__init__.py b/src/ceph-detect-init/ceph_detect_init/freebsd/__init__.py new file mode 100644 index 0000000000000..5244e1932801e --- /dev/null +++ b/src/ceph-detect-init/ceph_detect_init/freebsd/__init__.py @@ -0,0 +1,11 @@ +distro = None +release = None +codename = None + + +def choose_init(): + """Select a init system + + Returns the name of a init system (upstart, sysvinit ...). + """ + return 'bsdrc' diff --git a/src/ceph-detect-init/run-tox.sh b/src/ceph-detect-init/run-tox.sh index 0c135c5aedb30..9736dc400598b 100755 --- a/src/ceph-detect-init/run-tox.sh +++ b/src/ceph-detect-init/run-tox.sh @@ -17,11 +17,6 @@ # GNU Library Public License for more details. # -if [ x"`uname`"x = xFreeBSDx ]; then - echo FreeBSD init system has not been integrated. - exit 0 -fi - # run from the ceph-detect-init directory or from its parent : ${CEPH_DETECT_INIT_VIRTUALENV:=/tmp/ceph-detect-init-virtualenv} test -d ceph-detect-init && cd ceph-detect-init diff --git a/src/ceph-detect-init/tests/test_all.py b/src/ceph-detect-init/tests/test_all.py index d0d4e06c2eb19..d83f7b5fdfea2 100644 --- a/src/ceph-detect-init/tests/test_all.py +++ b/src/ceph-detect-init/tests/test_all.py @@ -32,6 +32,7 @@ from ceph_detect_init import main from ceph_detect_init import rhel from ceph_detect_init import suse from ceph_detect_init import gentoo +from ceph_detect_init import freebsd logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.DEBUG) @@ -42,6 +43,9 @@ class TestCephDetectInit(testtools.TestCase): def test_alpine(self): self.assertEqual('openrc', alpine.choose_init()) + def test_freebsd(self): + self.assertEqual('bsdrc', freebsd.choose_init()) + def test_centos(self): with mock.patch('ceph_detect_init.centos.release', '7.0'): @@ -145,17 +149,21 @@ class TestCephDetectInit(testtools.TestCase): self.assertEqual('unknown', gentoo.choose_init()) def test_get(self): - g = ceph_detect_init.get - with mock.patch('platform.linux_distribution', - lambda **kwargs: (('unknown', '', ''))): + with mock.patch.multiple( + 'platform', + system=lambda: 'Linux', + linux_distribution=lambda **kwargs: (('unknown', '', ''))): + g = ceph_detect_init.get self.assertRaises(exc.UnsupportedPlatform, g) try: g() except exc.UnsupportedPlatform as e: self.assertIn('Platform is not supported', str(e)) - with mock.patch('platform.linux_distribution', - lambda **kwargs: (('debian', '6.0', ''))): + with mock.patch.multiple( + 'platform', + system=lambda: 'Linux', + linux_distribution=lambda **kwargs: (('debian', '6.0', ''))): distro = ceph_detect_init.get() self.assertEqual(debian, distro) self.assertEqual('debian', distro.name) @@ -166,6 +174,24 @@ class TestCephDetectInit(testtools.TestCase): self.assertEqual('squeeze', distro.codename) self.assertEqual('sysvinit', distro.init) + with mock.patch.multiple('platform', + system=lambda: 'FreeBSD', + release=lambda: '12.0-CURRENT', + version=lambda: 'FreeBSD 12.0 #1 r306554M:'): + distro = ceph_detect_init.get() + self.assertEqual(freebsd, distro) + self.assertEqual('freebsd', distro.name) + self.assertEqual('freebsd', distro.normalized_name) + self.assertEqual('freebsd', distro.distro) + self.assertFalse(distro.is_el) + self.assertEqual('12.0-CURRENT', distro.release) + self.assertEqual('r306554M', distro.codename) + self.assertEqual('bsdrc', distro.init) + + with mock.patch('platform.system', + lambda: 'cephix'): + self.assertRaises(exc.UnsupportedPlatform, ceph_detect_init.get) + def test_get_distro(self): g = ceph_detect_init._get_distro self.assertEqual(None, g(None)) @@ -205,7 +231,8 @@ class TestCephDetectInit(testtools.TestCase): self.assertEqual('gentoo', n('Exherbo')) self.assertEqual('gentoo', n('exherbo')) - def test_platform_information(self): + @mock.patch('platform.system', lambda: 'Linux') + def test_platform_information_linux(self): with mock.patch('platform.linux_distribution', lambda **kwargs: (('debian', '6.0', ''))): self.assertEqual(('debian', '6.0', 'squeeze'), @@ -231,12 +258,22 @@ class TestCephDetectInit(testtools.TestCase): self.assertEqual(('debian', 'sid/jessie', 'sid'), ceph_detect_init.platform_information()) + @mock.patch('platform.system', lambda: 'FreeBSD') + def test_platform_information_freebsd(self): + with mock.patch.multiple('platform', + release=lambda: '12.0-CURRENT', + version=lambda: 'FreeBSD 12.0 #1 r306554M:'): + self.assertEqual(('freebsd', '12.0-CURRENT', 'r306554M'), + ceph_detect_init.platform_information()) + def test_run(self): argv = ['--use-rhceph', '--verbose'] self.assertEqual(0, main.run(argv)) - with mock.patch('platform.linux_distribution', - lambda **kwargs: (('unknown', '', ''))): + with mock.patch.multiple( + 'platform', + system=lambda: 'Linux', + linux_distribution=lambda **kwargs: (('unknown', '', ''))): self.assertRaises(exc.UnsupportedPlatform, main.run, argv) self.assertEqual(0, main.run(argv + ['--default=sysvinit'])) -- 2.39.5