Merge ~chad.smith/cloud-init:bug/1780481-fix-get-linux-distro into cloud-init:master

Proposed by Chad Smith
Status: Merged
Approved by: Chad Smith
Approved revision: 46345394b6d8ec96f13ade59c9fffb2885349ade
Merge reported by: Server Team CI bot
Merged at revision: not available
Proposed branch: ~chad.smith/cloud-init:bug/1780481-fix-get-linux-distro
Merge into: cloud-init:master
Diff against target: 151 lines (+89/-12)
3 files modified
cloudinit/tests/test_util.py (+68/-1)
cloudinit/util.py (+18/-10)
tests/unittests/test_datasource/test_azure_helper.py (+3/-1)
Reviewer Review Type Date Requested Status
Server Team CI bot continuous-integration Approve
Scott Moser Approve
Review via email: mp+349081@code.launchpad.net

Commit message

ubuntu,centos,debian: get_linux_distro to align with platform.dist

A recent commit added get_linux_distro to replace the deprecated python
platform.dist module behavior before it is dropped from python. It added
behavior that was compliant on OpenSuSE and SLES, by returning
(<distro_name>, <distro_version>, <cpu-arch>).

Fix get_linux_distro to behave more like the specific distribution's
platform.dist on ubuntu, centos and debian, which will return the
distribution release codename as the third element instead of <cpu-arch>.

SLES and OpenSUSE will retain their current behavior.

Examples follow:
('sles', '15', 'x86_64')
('opensuse', '42.3', 'x86_64')
('debian', '9', 'stretch')
('ubuntu', '16.04', 'xenial')
('centos', '7', 'Core')

LP: #1780481

To post a comment you must log in.
Revision history for this message
Robert Schweikert (rjschwei) wrote :

LGTM +1

Revision history for this message
Server Team CI bot (server-team-bot) wrote :

FAILED: Continuous integration, rev:2099b4ce71ce2deba68e38b1443746ea0b125b9c
https://jenkins.ubuntu.com/server/job/cloud-init-ci/141/
Executed test runs:
    SUCCESS: Checkout
    FAILED: Unit & Style Tests

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/141/rebuild

review: Needs Fixing (continuous-integration)
a2f229b... by Chad Smith

need to mock /etc/os-release for azure util.is_FreeBSD checks

Revision history for this message
Server Team CI bot (server-team-bot) wrote :

FAILED: Continuous integration, rev:a2f229bcd228816ff3f2047ae3f3b4fb93fc86ba
https://jenkins.ubuntu.com/server/job/cloud-init-ci/142/
Executed test runs:
    SUCCESS: Checkout
    FAILED: Unit & Style Tests

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/142/rebuild

review: Needs Fixing (continuous-integration)
b1ea4ac... by Chad Smith

not bytes mr smith

Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:b1ea4acedb2a107f62fdcde639640ce2b8b007bc
https://jenkins.ubuntu.com/server/job/cloud-init-ci/143/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/143/rebuild

review: Approve (continuous-integration)
9ae57dd... by Chad Smith

shlex instead

Revision history for this message
Scott Moser (smoser) wrote :

2 comments
a.) it seems wrong to have sles and opensuse reatin their old behavior
b.) can get_linux_distro use load_shell_content ?

Revision history for this message
Server Team CI bot (server-team-bot) wrote :

FAILED: Continuous integration, rev:9ae57dd91778fd655f62e64e9d2e6adfa44c77dc
https://jenkins.ubuntu.com/server/job/cloud-init-ci/144/
Executed test runs:
    SUCCESS: Checkout
    FAILED: Unit & Style Tests

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/144/rebuild

review: Needs Fixing (continuous-integration)
a871140... by Chad Smith

use load_shell_content per smoser's review

Revision history for this message
Server Team CI bot (server-team-bot) wrote :

FAILED: Continuous integration, rev:a8711404dc56287eefdffd5e6a88ebc4de4cb250
https://jenkins.ubuntu.com/server/job/cloud-init-ci/145/
Executed test runs:
    SUCCESS: Checkout
    FAILED: Unit & Style Tests

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/145/rebuild

review: Needs Fixing (continuous-integration)
Revision history for this message
Scott Moser (smoser) wrote :

looks good to me with a fix of c-i

review: Approve
4634539... by Chad Smith

proper indentation of debian /etc/os-release content. mock is_FreeBSD to avoid having to whitebox mock load_file calls

Revision history for this message
Server Team CI bot (server-team-bot) wrote :

PASSED: Continuous integration, rev:46345394b6d8ec96f13ade59c9fffb2885349ade
https://jenkins.ubuntu.com/server/job/cloud-init-ci/146/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/146/rebuild

review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/cloudinit/tests/test_util.py b/cloudinit/tests/test_util.py
index 17853fc..6a31e50 100644
--- a/cloudinit/tests/test_util.py
+++ b/cloudinit/tests/test_util.py
@@ -26,8 +26,51 @@ OS_RELEASE_SLES = dedent("""\
26 CPE_NAME="cpe:/o:suse:sles:12:sp3"\n26 CPE_NAME="cpe:/o:suse:sles:12:sp3"\n
27""")27""")
2828
29OS_RELEASE_OPENSUSE = dedent("""\
30NAME="openSUSE Leap"
31VERSION="42.3"
32ID=opensuse
33ID_LIKE="suse"
34VERSION_ID="42.3"
35PRETTY_NAME="openSUSE Leap 42.3"
36ANSI_COLOR="0;32"
37CPE_NAME="cpe:/o:opensuse:leap:42.3"
38BUG_REPORT_URL="https://bugs.opensuse.org"
39HOME_URL="https://www.opensuse.org/"
40""")
41
42OS_RELEASE_CENTOS = dedent("""\
43 NAME="CentOS Linux"
44 VERSION="7 (Core)"
45 ID="centos"
46 ID_LIKE="rhel fedora"
47 VERSION_ID="7"
48 PRETTY_NAME="CentOS Linux 7 (Core)"
49 ANSI_COLOR="0;31"
50 CPE_NAME="cpe:/o:centos:centos:7"
51 HOME_URL="https://www.centos.org/"
52 BUG_REPORT_URL="https://bugs.centos.org/"
53
54 CENTOS_MANTISBT_PROJECT="CentOS-7"
55 CENTOS_MANTISBT_PROJECT_VERSION="7"
56 REDHAT_SUPPORT_PRODUCT="centos"
57 REDHAT_SUPPORT_PRODUCT_VERSION="7"
58""")
59
60OS_RELEASE_DEBIAN = dedent("""\
61 PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
62 NAME="Debian GNU/Linux"
63 VERSION_ID="9"
64 VERSION="9 (stretch)"
65 ID=debian
66 HOME_URL="https://www.debian.org/"
67 SUPPORT_URL="https://www.debian.org/support"
68 BUG_REPORT_URL="https://bugs.debian.org/"
69""")
70
29OS_RELEASE_UBUNTU = dedent("""\71OS_RELEASE_UBUNTU = dedent("""\
30 NAME="Ubuntu"\n72 NAME="Ubuntu"\n
73 # comment test
31 VERSION="16.04.3 LTS (Xenial Xerus)"\n74 VERSION="16.04.3 LTS (Xenial Xerus)"\n
32 ID=ubuntu\n75 ID=ubuntu\n
33 ID_LIKE=debian\n76 ID_LIKE=debian\n
@@ -310,7 +353,31 @@ class TestGetLinuxDistro(CiTestCase):
310 m_os_release.return_value = OS_RELEASE_UBUNTU353 m_os_release.return_value = OS_RELEASE_UBUNTU
311 m_path_exists.side_effect = TestGetLinuxDistro.os_release_exists354 m_path_exists.side_effect = TestGetLinuxDistro.os_release_exists
312 dist = util.get_linux_distro()355 dist = util.get_linux_distro()
313 self.assertEqual(('ubuntu', '16.04', platform.machine()), dist)356 self.assertEqual(('ubuntu', '16.04', 'xenial'), dist)
357
358 @mock.patch('cloudinit.util.load_file')
359 def test_get_linux_centos(self, m_os_release, m_path_exists):
360 """Verify we get the correct name and release name on CentOS."""
361 m_os_release.return_value = OS_RELEASE_CENTOS
362 m_path_exists.side_effect = TestGetLinuxDistro.os_release_exists
363 dist = util.get_linux_distro()
364 self.assertEqual(('centos', '7', 'Core'), dist)
365
366 @mock.patch('cloudinit.util.load_file')
367 def test_get_linux_debian(self, m_os_release, m_path_exists):
368 """Verify we get the correct name and release name on Debian."""
369 m_os_release.return_value = OS_RELEASE_DEBIAN
370 m_path_exists.side_effect = TestGetLinuxDistro.os_release_exists
371 dist = util.get_linux_distro()
372 self.assertEqual(('debian', '9', 'stretch'), dist)
373
374 @mock.patch('cloudinit.util.load_file')
375 def test_get_linux_opensuse(self, m_os_release, m_path_exists):
376 """Verify we get the correct name and machine arch on OpenSUSE."""
377 m_os_release.return_value = OS_RELEASE_OPENSUSE
378 m_path_exists.side_effect = TestGetLinuxDistro.os_release_exists
379 dist = util.get_linux_distro()
380 self.assertEqual(('opensuse', '42.3', platform.machine()), dist)
314381
315 @mock.patch('platform.dist')382 @mock.patch('platform.dist')
316 def test_get_linux_distro_no_data(self, m_platform_dist, m_path_exists):383 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 6da9511..d0b0e90 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -579,16 +579,24 @@ def get_cfg_option_int(yobj, key, default=0):
579def get_linux_distro():579def get_linux_distro():
580 distro_name = ''580 distro_name = ''
581 distro_version = ''581 distro_version = ''
582 flavor = ''
582 if os.path.exists('/etc/os-release'):583 if os.path.exists('/etc/os-release'):
583 os_release = load_file('/etc/os-release')584 os_release = load_shell_content(load_file('/etc/os-release'))
584 for line in os_release.splitlines():585 distro_name = os_release.get('ID', '')
585 if line.strip().startswith('ID='):586 distro_version = os_release.get('VERSION_ID', '')
586 distro_name = line.split('=')[-1]587 if 'sles' in distro_name or 'suse' in distro_name:
587 distro_name = distro_name.replace('"', '')588 # RELEASE_BLOCKER: We will drop this sles ivergent behavior in
588 if line.strip().startswith('VERSION_ID='):589 # before 18.4 so that get_linux_distro returns a named tuple
589 # Lets hope for the best that distros stay consistent ;)590 # which will include both version codename and architecture
590 distro_version = line.split('=')[-1]591 # on all distributions.
591 distro_version = distro_version.replace('"', '')592 flavor = platform.machine()
593 else:
594 flavor = os_release.get('VERSION_CODENAME', '')
595 if not flavor:
596 match = re.match(r'[^ ]+ \((?P<codename>[^)]+)\)',
597 os_release.get('VERSION'))
598 if match:
599 flavor = match.groupdict()['codename']
592 else:600 else:
593 dist = ('', '', '')601 dist = ('', '', '')
594 try:602 try:
@@ -606,7 +614,7 @@ def get_linux_distro():
606 'expansion may have unexpected results')614 'expansion may have unexpected results')
607 return dist615 return dist
608616
609 return (distro_name, distro_version, platform.machine())617 return (distro_name, distro_version, flavor)
610618
611619
612def system_info():620def system_info():
diff --git a/tests/unittests/test_datasource/test_azure_helper.py b/tests/unittests/test_datasource/test_azure_helper.py
index af9d3e1..26b2b93 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):
85 self.dhcp_options.return_value = {"eth0": {"unknown_245": "5:4:3:2"}}85 self.dhcp_options.return_value = {"eth0": {"unknown_245": "5:4:3:2"}}
86 self.assertEqual('5.4.3.2', wa_shim.find_endpoint(None))86 self.assertEqual('5.4.3.2', wa_shim.find_endpoint(None))
8787
88 def test_latest_lease_used(self):88 @mock.patch('cloudinit.sources.helpers.azure.util.is_FreeBSD')
89 def test_latest_lease_used(self, m_is_freebsd):
90 m_is_freebsd.return_value = False # To avoid hitting load_file
89 encoded_addresses = ['5:4:3:2', '4:3:2:1']91 encoded_addresses = ['5:4:3:2', '4:3:2:1']
90 file_content = '\n'.join([self._build_lease_content(encoded_address)92 file_content = '\n'.join([self._build_lease_content(encoded_address)
91 for encoded_address in encoded_addresses])93 for encoded_address in encoded_addresses])

Subscribers

People subscribed via source and target branches