Revert "Compare the snaps versions to avoid suggesting a downgrade"

This reverts commit 2f9c7399ca.
This commit is contained in:
Dan Bungert 2023-02-22 14:34:51 -07:00
parent 9d5779891e
commit 0766922a3b
3 changed files with 5 additions and 161 deletions

View File

@ -1,84 +0,0 @@
# Copyright 2021 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
""" Module defining helpers to work with snap versions. """
from dataclasses import dataclass
from typing import Optional
import re
class SnapVersionParsingError(Exception):
""" Exception raised when a version string does not match the expected
format
"""
def __init__(self, message: str = "", version: str = ""):
self.message = message
self.version = version
super().__init__()
@dataclass
class SnapVersion:
""" Represent the version of a snap in either of the following forms:
* {major}.{minor}.{patch}
* {major}.{minor}.{patch}+git{buildId}.{commitId}
"""
major: int
minor: int
patch: int
git_build_id: Optional[int] = None
git_commit_id: Optional[str] = None
@classmethod
def from_string(cls, s: str) -> "SnapVersion":
""" Construct a SnapVersion object from a string representation """
try:
major, minor, patch = s.split(".", maxsplit=2)
git_build_id = None
git_commit_id = None
# Check if what we assume is the patch number does not contain a
# +git... information
match = re.fullmatch(r"(\d+)\+git(\d+)\.([0-9a-f]+)", patch)
if match:
patch, git_build_id, git_commit_id = match.groups()
return cls(int(major), int(minor), int(patch),
None if git_build_id is None else int(git_build_id),
git_commit_id)
except (ValueError, TypeError):
raise SnapVersionParsingError(version=s)
def __gt__(self, other: "SnapVersion"):
""" Tells whether a SnapVersion object is greater than the other """
if self.major > other.major:
return True
elif self.major < other.major:
return False
if self.minor > other.minor:
return True
elif self.minor < other.minor:
return False
if self.patch > other.patch:
return True
elif self.patch < other.patch:
return False
build_id_self = \
float("-inf") if self.git_build_id is None else self.git_build_id
build_id_other = \
float("-inf") if other.git_build_id is None else other.git_build_id
return build_id_self > build_id_other

View File

@ -1,49 +0,0 @@
import unittest
from subiquity.common.snap import SnapVersion, SnapVersionParsingError
class TestSnapVersion(unittest.TestCase):
def test_snap_version_from_string(self):
obj = SnapVersion.from_string("19.04.2")
self.assertEqual(obj.major, 19)
self.assertEqual(obj.minor, 4)
self.assertEqual(obj.patch, 2)
self.assertIsNone(obj.git_build_id)
self.assertIsNone(obj.git_commit_id)
obj = SnapVersion.from_string("21.10.3+git136.cde71504")
self.assertEqual(obj.major, 21)
self.assertEqual(obj.minor, 10)
self.assertEqual(obj.patch, 3)
self.assertEqual(obj.git_build_id, 136)
self.assertEqual(obj.git_commit_id, "cde71504")
def test_snap_version_from_string_invalid(self):
# Test with no patch number
with self.assertRaises(SnapVersionParsingError):
SnapVersion.from_string("19.02")
# Test unsupported version with RC (we support only +git)
with self.assertRaises(SnapVersionParsingError):
SnapVersion.from_string("19.02.2-rc1")
def test_snap_version_compare(self):
self.assertGreater(SnapVersion(20, 4, 2), SnapVersion(19, 4, 2))
self.assertGreater(SnapVersion(19, 5, 2), SnapVersion(19, 4, 1))
self.assertLess(SnapVersion(19, 4, 2), SnapVersion(20, 3, 2))
# Consider the version is greater if one has a build ID
self.assertGreater(SnapVersion.from_string("21.10.3+git136.cde71504"),
SnapVersion.from_string("21.10.3"))
# Consider the version is greater if the build ID is greater
self.assertGreater(SnapVersion.from_string("21.10.3+git136.cde71504"),
SnapVersion.from_string("21.10.3+git135.deadbeef"))
# Make sure we ignore the build ID when the patch version is different
self.assertLess(SnapVersion.from_string("21.10.2+git255.deadbeef"),
SnapVersion.from_string("21.10.3+git135.deadbeef"))
snap_version = SnapVersion.from_string("21.10.02")
self.assertFalse(snap_version < snap_version)
self.assertFalse(snap_version > snap_version)

View File

@ -34,10 +34,6 @@ from subiquity.common.types import (
RefreshCheckState,
RefreshStatus,
)
from subiquity.common.snap import (
SnapVersion,
SnapVersionParsingError,
)
from subiquity.server.controller import (
SubiquityController,
)
@ -217,30 +213,11 @@ class RefreshController(SubiquityController):
if snap.name != self.snap_name:
continue
self.status.new_snap_version = snap.version
# In certain circumstances, the version of the snap that is
# reported by snapd is older than the one currently running. In
# this scenario, we do not want to suggest an update that would
# result in a downgrade.
snapd_version_is_newer = True
try:
current_version = SnapVersion.from_string(
self.status.current_snap_version
)
snapd_version = SnapVersion.from_string(
self.status.new_snap_version
)
except SnapVersionParsingError as e:
log.warning("failed to parse snap version: %s", e.version)
else:
if not snapd_version > current_version:
snapd_version_is_newer = False
if snapd_version_is_newer:
context.description = (
"new version of snap available: %r"
% self.status.new_snap_version)
self.status.availability = RefreshCheckState.AVAILABLE
return
context.description = (
"new version of snap available: %r"
% self.status.new_snap_version)
self.status.availability = RefreshCheckState.AVAILABLE
return
else:
context.description = "no new version of snap available"
self.status.availability = RefreshCheckState.UNAVAILABLE