Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix platform check for Debian 12 #880

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions charmhelpers/osplatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ def get_platform():
will be returned (which is the name of the module).
This string is used to decide which platform module should be imported.
"""
# linux_distribution is deprecated and will be removed in Python 3.7
# Warnings *not* disabled, as we certainly need to fix this.
if hasattr(platform, 'linux_distribution'):
tuple_platform = platform.linux_distribution()
current_platform = tuple_platform[0]
else:
current_platform = _get_platform_from_fs()
current_platform = _get_current_platform()

if "Ubuntu" in current_platform:
return "ubuntu"
elif "CentOS" in current_platform:
return "centos"
elif "debian" in current_platform:
elif "debian" in current_platform or "Debian" in current_platform:
# Stock Python does not detect Ubuntu and instead returns debian.
# Or at least it does in some build environments like Travis CI
return "ubuntu"
Expand All @@ -36,6 +30,24 @@ def get_platform():
.format(current_platform))


def _get_current_platform():
"""Return the current platform information for the OS.

Attempts to lookup linux distribution information from the platform
module for releases of python < 3.7. For newer versions of python,
the platform is determined from the /etc/os-release file.
"""
# linux_distribution is deprecated and will be removed in Python 3.7
# Warnings *not* disabled, as we certainly need to fix this.
if hasattr(platform, 'linux_distribution'):
tuple_platform = platform.linux_distribution()
current_platform = tuple_platform[0]
else:
current_platform = _get_platform_from_fs()

return current_platform


def _get_platform_from_fs():
"""Get Platform from /etc/os-release."""
with open(os.path.join(os.sep, 'etc', 'os-release')) as fin:
Expand Down
74 changes: 74 additions & 0 deletions tests/test_osplatform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#
# Copyright (C) 2024 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
try:
import unittest.mock as mock
except ImportError:
import mock

from charmhelpers import osplatform


class TestPlatform(unittest.TestCase):

@mock.patch.object(osplatform, "_get_current_platform")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: there is a BaseTestCase class in tests/utils.py that allows you to do these patches in-line as opposed to using the decorators.

The decorator approach quickly becomes hard to manage with all the positional arguments etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't disagree with the nit, but I guess for expediency, let's merge this. I think there's a larger body of work in re-working (or adding separate methods) .patch(...) and .patch_object to also/instead return the mock so that static code analysers in vscode and Neovim+LSP don't complain about the magic members that are added to the object as part of the patching functionality.

def test_get_platform_ubuntu(self, _platform):
_platform.return_value = "Ubuntu"
self.assertEqual("ubuntu", osplatform.get_platform())

@mock.patch.object(osplatform, "_get_current_platform")
def test_get_platform_centos(self, _platform):
_platform.return_value = "CentOS"
self.assertEqual("centos", osplatform.get_platform())

@mock.patch.object(osplatform, "_get_current_platform")
def test_get_platform_debian(self, _platform):
_platform.return_value = "debian gnu/linux"
self.assertEqual("ubuntu", osplatform.get_platform())

_platform.return_value = "Debian GNU/Linux"
self.assertEqual("ubuntu", osplatform.get_platform())

@mock.patch.object(osplatform, "_get_current_platform")
def test_get_platform_elementary(self, _platform):
_platform.return_value = "elementary linux"
self.assertEqual("ubuntu", osplatform.get_platform())

@mock.patch.object(osplatform, "_get_current_platform")
def test_get_platform_pop_os(self, _platform):
_platform.return_value = "Pop!_OS"
self.assertEqual("ubuntu", osplatform.get_platform())

@mock.patch.object(osplatform, "_get_current_platform")
def test_get_platform_unknown(self, _platform):
_platform.return_value = "crazy custom flavor"
self.assertRaises(RuntimeError, osplatform.get_platform)

@mock.patch.object(osplatform, "_get_platform_from_fs")
@mock.patch.object(osplatform, "platform")
def test_get_current_platform_module(self, _platform, _platform_from_fs):
_platform.linux_distribution.return_value = ("Ubuntu", "test")
self.assertEqual("Ubuntu", osplatform._get_current_platform())
_platform_from_fs.assert_not_called()

@mock.patch.object(osplatform, "_get_platform_from_fs")
@mock.patch.object(osplatform, "platform")
def test_get_current_platform_fs(self, _platform, _platform_from_fs):
# make sure hasattr says False
del _platform.linux_distribution
_platform_from_fs.return_value = "foobar"
self.assertEqual("foobar", osplatform._get_current_platform())
Loading