From b056c5b234d1de3952f9ae4a9af40a009adf87f1 Mon Sep 17 00:00:00 2001 From: Massimiliano Date: Tue, 23 Apr 2024 15:34:09 +0200 Subject: [PATCH] Fall back from distutils -> packaging.version (bugfix) (#1203) * Remove dependency on distutils -> packaging.version * Test the changes Minor: black formatting * Fixed return value * NOQA flake8 builtins * two spaces whatever * No fall back, only new thing --- providers/base/bin/wifi_nmcli_backup.py | 9 ++--- providers/base/bin/wifi_nmcli_test.py | 10 ++--- .../base/tests/test_wifi_nmcli_backup.py | 38 +++++++++++++++++++ providers/base/tests/test_wifi_nmcli_test.py | 38 +++++++++++++++++++ 4 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 providers/base/tests/test_wifi_nmcli_backup.py create mode 100644 providers/base/tests/test_wifi_nmcli_test.py diff --git a/providers/base/bin/wifi_nmcli_backup.py b/providers/base/bin/wifi_nmcli_backup.py index a585ac37c..e2c2dcf02 100755 --- a/providers/base/bin/wifi_nmcli_backup.py +++ b/providers/base/bin/wifi_nmcli_backup.py @@ -12,7 +12,8 @@ import subprocess as sp import sys -from distutils.version import LooseVersion +from packaging import version as version_parser + NM_CON_DIR = "/etc/NetworkManager/system-connections" SAVE_DIR = os.path.join( @@ -25,12 +26,10 @@ def legacy_nmcli(): cmd = "nmcli -v" output = sp.check_output(cmd, shell=True) - version = LooseVersion(output.strip().split()[-1].decode()) + version = version_parser.parse(output.strip().split()[-1].decode()) # check if using an earlier nmcli version with different api # nmcli in cosmic is 1.12.4, bionic is 1.10 - if version < LooseVersion("1.12.0"): - return True - return False + return version < version_parser.parse("1.12.0") # Creation of keyfile names can be found in: diff --git a/providers/base/bin/wifi_nmcli_test.py b/providers/base/bin/wifi_nmcli_test.py index 3adb6f3a1..0f735c5a9 100755 --- a/providers/base/bin/wifi_nmcli_test.py +++ b/providers/base/bin/wifi_nmcli_test.py @@ -17,21 +17,21 @@ import sys import time -from distutils.version import LooseVersion +from packaging import version as version_parser + from gateway_ping_test import ping + print = functools.partial(print, flush=True) def legacy_nmcli(): cmd = "nmcli -v" output = sp.check_output(cmd, shell=True) - version = LooseVersion(output.strip().split()[-1].decode()) + version = version_parser.parse(output.strip().split()[-1].decode()) # check if using the 16.04 nmcli because of this bug # https://bugs.launchpad.net/plano/+bug/1896806 - if version < LooseVersion("1.9.9"): - return True - return False + return version < version_parser.parse("1.9.9") def print_head(txt): diff --git a/providers/base/tests/test_wifi_nmcli_backup.py b/providers/base/tests/test_wifi_nmcli_backup.py new file mode 100644 index 000000000..9cb65cfd9 --- /dev/null +++ b/providers/base/tests/test_wifi_nmcli_backup.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# Copyright 2024 Canonical Ltd. +# Written by: +# Massimiliano Girardi +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +import unittest +from unittest.mock import patch + +from wifi_nmcli_backup import legacy_nmcli + + +class WifiNmcliBackupTests(unittest.TestCase): + @patch("wifi_nmcli_backup.sp") + def test_legacy_nmcli_true(self, subprocess_mock): + subprocess_mock.check_output.return_value = ( + b"nmcli tool, version 1.11.3-5" + ) + self.assertTrue(legacy_nmcli()) + + @patch("wifi_nmcli_backup.sp") + def test_legacy_nmcli_false(self, subprocess_mock): + subprocess_mock.check_output.return_value = ( + b"nmcli tool, version 1.46.0-2" + ) + self.assertFalse(legacy_nmcli()) diff --git a/providers/base/tests/test_wifi_nmcli_test.py b/providers/base/tests/test_wifi_nmcli_test.py new file mode 100644 index 000000000..e0b52a392 --- /dev/null +++ b/providers/base/tests/test_wifi_nmcli_test.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +# Copyright 2024 Canonical Ltd. +# Written by: +# Massimiliano Girardi +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +import unittest +from unittest.mock import patch + +from wifi_nmcli_test import legacy_nmcli + + +class WifiNmcliBackupTests(unittest.TestCase): + @patch("wifi_nmcli_test.sp") + def test_legacy_nmcli_true(self, subprocess_mock): + subprocess_mock.check_output.return_value = ( + b"nmcli tool, version 1.9.8-5" + ) + self.assertTrue(legacy_nmcli()) + + @patch("wifi_nmcli_test.sp") + def test_legacy_nmcli_false(self, subprocess_mock): + subprocess_mock.check_output.return_value = ( + b"nmcli tool, version 1.46.0-2" + ) + self.assertFalse(legacy_nmcli())