summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cloudinit/tests/test_util.py69
-rw-r--r--cloudinit/util.py28
-rw-r--r--tests/unittests/test_datasource/test_azure_helper.py4
3 files changed, 89 insertions, 12 deletions
diff --git a/cloudinit/tests/test_util.py b/cloudinit/tests/test_util.py
index 17853fc7..6a31e505 100644
--- a/cloudinit/tests/test_util.py
+++ b/cloudinit/tests/test_util.py
@@ -26,8 +26,51 @@ OS_RELEASE_SLES = dedent("""\
CPE_NAME="cpe:/o:suse:sles:12:sp3"\n
""")
+OS_RELEASE_OPENSUSE = dedent("""\
+NAME="openSUSE Leap"
+VERSION="42.3"
+ID=opensuse
+ID_LIKE="suse"
+VERSION_ID="42.3"
+PRETTY_NAME="openSUSE Leap 42.3"
+ANSI_COLOR="0;32"
+CPE_NAME="cpe:/o:opensuse:leap:42.3"
+BUG_REPORT_URL="https://bugs.opensuse.org"
+HOME_URL="https://www.opensuse.org/"
+""")
+
+OS_RELEASE_CENTOS = dedent("""\
+ NAME="CentOS Linux"
+ VERSION="7 (Core)"
+ ID="centos"
+ ID_LIKE="rhel fedora"
+ VERSION_ID="7"
+ PRETTY_NAME="CentOS Linux 7 (Core)"
+ ANSI_COLOR="0;31"
+ CPE_NAME="cpe:/o:centos:centos:7"
+ HOME_URL="https://www.centos.org/"
+ BUG_REPORT_URL="https://bugs.centos.org/"
+
+ CENTOS_MANTISBT_PROJECT="CentOS-7"
+ CENTOS_MANTISBT_PROJECT_VERSION="7"
+ REDHAT_SUPPORT_PRODUCT="centos"
+ REDHAT_SUPPORT_PRODUCT_VERSION="7"
+""")
+
+OS_RELEASE_DEBIAN = dedent("""\
+ PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
+ NAME="Debian GNU/Linux"
+ VERSION_ID="9"
+ VERSION="9 (stretch)"
+ ID=debian
+ HOME_URL="https://www.debian.org/"
+ SUPPORT_URL="https://www.debian.org/support"
+ BUG_REPORT_URL="https://bugs.debian.org/"
+""")
+
OS_RELEASE_UBUNTU = dedent("""\
NAME="Ubuntu"\n
+ # comment test
VERSION="16.04.3 LTS (Xenial Xerus)"\n
ID=ubuntu\n
ID_LIKE=debian\n
@@ -310,7 +353,31 @@ class TestGetLinuxDistro(CiTestCase):
m_os_release.return_value = OS_RELEASE_UBUNTU
m_path_exists.side_effect = TestGetLinuxDistro.os_release_exists
dist = util.get_linux_distro()
- self.assertEqual(('ubuntu', '16.04', platform.machine()), dist)
+ self.assertEqual(('ubuntu', '16.04', 'xenial'), dist)
+
+ @mock.patch('cloudinit.util.load_file')
+ def test_get_linux_centos(self, m_os_release, m_path_exists):
+ """Verify we get the correct name and release name on CentOS."""
+ m_os_release.return_value = OS_RELEASE_CENTOS
+ m_path_exists.side_effect = TestGetLinuxDistro.os_release_exists
+ dist = util.get_linux_distro()
+ self.assertEqual(('centos', '7', 'Core'), dist)
+
+ @mock.patch('cloudinit.util.load_file')
+ def test_get_linux_debian(self, m_os_release, m_path_exists):
+ """Verify we get the correct name and release name on Debian."""
+ m_os_release.return_value = OS_RELEASE_DEBIAN
+ m_path_exists.side_effect = TestGetLinuxDistro.os_release_exists
+ dist = util.get_linux_distro()
+ self.assertEqual(('debian', '9', 'stretch'), dist)
+
+ @mock.patch('cloudinit.util.load_file')
+ def test_get_linux_opensuse(self, m_os_release, m_path_exists):
+ """Verify we get the correct name and machine arch on OpenSUSE."""
+ m_os_release.return_value = OS_RELEASE_OPENSUSE
+ m_path_exists.side_effect = TestGetLinuxDistro.os_release_exists
+ dist = util.get_linux_distro()
+ self.assertEqual(('opensuse', '42.3', platform.machine()), dist)
@mock.patch('platform.dist')
def test_get_linux_distro_no_data(self, m_platform_dist, m_path_exists):
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 6da95113..d0b0e90a 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -579,16 +579,24 @@ def get_cfg_option_int(yobj, key, default=0):
def get_linux_distro():
distro_name = ''
distro_version = ''
+ flavor = ''
if os.path.exists('/etc/os-release'):
- os_release = load_file('/etc/os-release')
- for line in os_release.splitlines():
- if line.strip().startswith('ID='):
- distro_name = line.split('=')[-1]
- distro_name = distro_name.replace('"', '')
- if line.strip().startswith('VERSION_ID='):
- # Lets hope for the best that distros stay consistent ;)
- distro_version = line.split('=')[-1]
- distro_version = distro_version.replace('"', '')
+ os_release = load_shell_content(load_file('/etc/os-release'))
+ distro_name = os_release.get('ID', '')
+ distro_version = os_release.get('VERSION_ID', '')
+ if 'sles' in distro_name or 'suse' in distro_name:
+ # RELEASE_BLOCKER: We will drop this sles ivergent behavior in
+ # before 18.4 so that get_linux_distro returns a named tuple
+ # which will include both version codename and architecture
+ # on all distributions.
+ flavor = platform.machine()
+ else:
+ flavor = os_release.get('VERSION_CODENAME', '')
+ if not flavor:
+ match = re.match(r'[^ ]+ \((?P<codename>[^)]+)\)',
+ os_release.get('VERSION'))
+ if match:
+ flavor = match.groupdict()['codename']
else:
dist = ('', '', '')
try:
@@ -606,7 +614,7 @@ def get_linux_distro():
'expansion may have unexpected results')
return dist
- return (distro_name, distro_version, platform.machine())
+ return (distro_name, distro_version, flavor)
def system_info():
diff --git a/tests/unittests/test_datasource/test_azure_helper.py b/tests/unittests/test_datasource/test_azure_helper.py
index af9d3e1a..26b2b93d 100644
--- a/tests/unittests/test_datasource/test_azure_helper.py
+++ b/tests/unittests/test_datasource/test_azure_helper.py
@@ -85,7 +85,9 @@ class TestFindEndpoint(CiTestCase):
self.dhcp_options.return_value = {"eth0": {"unknown_245": "5:4:3:2"}}
self.assertEqual('5.4.3.2', wa_shim.find_endpoint(None))
- def test_latest_lease_used(self):
+ @mock.patch('cloudinit.sources.helpers.azure.util.is_FreeBSD')
+ def test_latest_lease_used(self, m_is_freebsd):
+ m_is_freebsd.return_value = False # To avoid hitting load_file
encoded_addresses = ['5:4:3:2', '4:3:2:1']
file_content = '\n'.join([self._build_lease_content(encoded_address)
for encoded_address in encoded_addresses])