Skip to content

Commit

Permalink
fix: Remove system field in OSPlatform
Browse files Browse the repository at this point in the history
- Remove system field in OSPlatform
- Add support_all_series field in ToolVersionInfo
  • Loading branch information
jneo8 committed Nov 1, 2023
1 parent 0a113b0 commit ca11310
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 126 deletions.
115 changes: 20 additions & 95 deletions src/checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import hashlib
import logging
import typing as t
from dataclasses import dataclass
from dataclasses import dataclass, field
from pathlib import Path

from os_platform import Architecture, UbuntuSeries, get_os_platform
Expand All @@ -34,74 +34,50 @@ class ToolVersionInfo:
"""Tool version information for checksum comparison."""

version: str
supported_series: t.List[UbuntuSeries]
supported_architectures: t.List[Architecture]
sha256_checksum: str
link: t.Optional[str] = None
desc: str = ""
support_all_series: bool = False
supported_series: t.List[UbuntuSeries] = field(default_factory=lambda: [])


STORCLI_VERSION_INFOS: t.List[ToolVersionInfo] = [
ToolVersionInfo(
version="007.2705.0000.0000",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/1232743397",
desc="MR 7.27",
sha256_checksum="45ff0d3c7fc8b77f64de7de7b3698307971546a6be00982934a19ee44f5d91bb",
),
ToolVersionInfo(
version="007.2612.0000.0000",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/1232743291",
desc="MR 7.26",
sha256_checksum="5ab2c1b608934626817828ced85e4aeaee7dc97fbd6e3f4fed00b13a95a06e14",
),
ToolVersionInfo(
version="007.2508.0000.0000",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/1232743203",
desc="MR 7.25",
sha256_checksum="17c3f5292de6491f1388c9305ba65836730614b6defe17039b427fced2f75e0b",
),
ToolVersionInfo(
version="007.2408.0000.0000",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/1232743081",
desc="MR 7.24",
sha256_checksum="8ecf2d46e253e243c5d169adcd84f2701e52e3815913694f074e80af5a98cbab",
),
ToolVersionInfo(
version="007.2310.0000.0000",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/Unified_storcli_all_os_7.2309.0000.0000.zip",
desc="MR 7.23",
Expand Down Expand Up @@ -151,64 +127,39 @@ class ToolVersionInfo:
SAS2IRCU_VERSION_INFOS: t.List[ToolVersionInfo] = [
ToolVersionInfo(
version="20.00.00.00",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/12351735",
desc="P20, linux_x86",
sha256_checksum="37467826d0b22aad47287efe70bb34e47f475d70e9b1b64cbd63f57607701e73",
),
ToolVersionInfo(
version="19.00.00.00",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/12351734",
desc="P19, linux_x86",
sha256_checksum="4baaec21865973c0a6da617e37850cc27512715e6ab22df18b1f67d068e5095c",
),
ToolVersionInfo(
version="18.00.00.00",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/12351733",
desc="P18, linux_x86",
sha256_checksum="b6ed72275066e80ebe9813cd15f1d019eba9daddbd9dfd8ad426da78801f15d8",
),
ToolVersionInfo(
version="17.00.00.00",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/12351732",
desc="P17, linux_x86",
sha256_checksum="07e9236b99bbe4a3ae6148f8668e1ce0331d83c2fcb0c4841d000454c6200c1f",
),
ToolVersionInfo(
version="16.00.00.00",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/12351731",
desc="P16, linux_x86",
Expand All @@ -219,64 +170,39 @@ class ToolVersionInfo:
SAS3IRCU_VERSION_INFOS: t.List[ToolVersionInfo] = [
ToolVersionInfo(
version="16.00.00.00",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/SAS3IRCU_P15.zip",
desc="P15, linux_x86",
sha256_checksum="f150eb37bb332668949a3eccf9636e0e03f874aecd17a39d586082c6be1386bd",
),
ToolVersionInfo(
version="15.00.00.00",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/SAS3IRCU_P14.zip",
desc="P14, linux_x86",
sha256_checksum="5825b90964d1950551e5ed5100ddf1141360b0acf8dd3c6ddb4fe5874d6bbabb",
),
ToolVersionInfo(
version="14.00.00.00",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/SAS3IRCU_P13.zip",
desc="P13, linux_x86",
sha256_checksum="1ce45e57efa0a2d8c5c3d61a0950ab7e950a317aff3155fc1831099e19274c32",
),
ToolVersionInfo(
version="13.00.00.00",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/SAS3IRCU_P12.zip",
desc="P12, linux_x86",
sha256_checksum="cb4010a3d2bc4f9b75859a0c599d889f9fb847e4dfc88abf74082a13b9490a59",
),
ToolVersionInfo(
version="12.00.00.00",
supported_series=[
UbuntuSeries.JAMMY,
UbuntuSeries.FOCAL,
UbuntuSeries.BIONIC,
UbuntuSeries.XENIAL,
],
support_all_series=True,
supported_architectures=[Architecture.X86_64],
link="https://docs.broadcom.com/docs/SAS3IRCU_P11.zip",
desc="P11, linux_x86",
Expand All @@ -295,9 +221,8 @@ def validate_checksum(support_version_infos: t.List[ToolVersionInfo], path: Path

supported_checksums = []
for info in support_version_infos:
if (
os_platform.series in info.supported_series
and os_platform.machine in info.supported_architectures
if os_platform.machine in info.supported_architectures and (
info.support_all_series or os_platform.series in info.supported_series
):
supported_checksums.append(info.sha256_checksum)

Expand Down
19 changes: 6 additions & 13 deletions src/os_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,22 @@ class Architecture(str, Enum):
class OSPlatform:
"""Description of an operating system platform."""

system: str
release: str
machine: str

@property
def series(self) -> t.Optional[UbuntuSeries]:
"""Return series base on system and release."""
if self.system == "ubuntu":
for series in UbuntuSeries:
if series == self.release:
return series
for series in UbuntuSeries:
if series == self.release:
return series
return None


def get_os_platform() -> OSPlatform:
"""Determine a system/release combo for an OS using /etc/os-release if available."""
system = platform.system()
release = platform.release()
machine = platform.machine()
info = distro.info()
release = info.get("version", "")

if system == "Linux":
info = distro.info()
system = info.get("id", system)
release = info.get("version", release)

return OSPlatform(system=system, release=release, machine=machine)
return OSPlatform(release=release, machine=machine)
29 changes: 26 additions & 3 deletions tests/unit/test_checksum.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from unittest import mock

from checksum import PERCCLI_VERSION_INFOS, validate_checksum
from checksum import PERCCLI_VERSION_INFOS, STORCLI_VERSION_INFOS, validate_checksum
from os_platform import OSPlatform


class TestCheckFileSum:
@mock.patch(
"checksum.get_os_platform",
return_value=OSPlatform(
system="ubuntu",
release="20.04",
machine="x86_64",
),
Expand All @@ -26,6 +25,31 @@ def test_validate_checksum(self, mock_sha256, mock_get_os_platform, tmp_path):
ok = validate_checksum(PERCCLI_VERSION_INFOS, target)
assert ok

@mock.patch(
"checksum.get_os_platform",
return_value=OSPlatform(
release="14.04", # old version which should be cover by field support_all_series
machine="x86_64",
),
)
@mock.patch("checksum.hashlib.sha256")
def test_validate_checksum_support_all_series(
self,
mock_sha256,
mock_get_os_platform,
tmp_path,
):
mock_sha256.return_value = mock.Mock()
mock_sha256.return_value.hexdigest.return_value = (
"45ff0d3c7fc8b77f64de7de7b3698307971546a6be00982934a19ee44f5d91bb"
)

target = tmp_path / "storcli"
target.write_text("fake file")

ok = validate_checksum(STORCLI_VERSION_INFOS, target)
assert ok

def test_validate_checksum_fail(self, tmp_path):
target = tmp_path / "perccli"
target.write_text("fake file")
Expand All @@ -36,7 +60,6 @@ def test_validate_checksum_fail(self, tmp_path):
@mock.patch(
"checksum.get_os_platform",
return_value=OSPlatform(
system="ubuntu",
release="20.04",
machine="fake machine architecture",
),
Expand Down
26 changes: 11 additions & 15 deletions tests/unit/test_os_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from os_platform import OSPlatform, UbuntuSeries, get_os_platform


@pytest.mark.parametrize("system", ["Windows", "Darwin", "Java", ""])
@pytest.mark.parametrize("release", ["NT", "Sparkling", "Jaguar", "0.0", ""])
@pytest.mark.parametrize("release", ["22.04", "20.04"])
@pytest.mark.parametrize(
"machine",
[
Expand All @@ -16,26 +15,23 @@
"riscv64",
],
)
def test_get_os_platform_non_linux(system, release, machine):
def test_get_os_platform_non_linux(release, machine):
"""Get platform from a patched Windows machine."""
with patch("platform.system", return_value=system):
with patch("platform.release", return_value=release):
with patch("platform.machine", return_value=machine):
result = get_os_platform()
assert result == OSPlatform(system=system, release=release, machine=machine)
with patch("distro.info", return_value={"version": release}):
with patch("platform.machine", return_value=machine):
result = get_os_platform()
assert result == OSPlatform(release=release, machine=machine)


@pytest.mark.parametrize("system", ["ubuntu"])
@pytest.mark.parametrize(
"release,series",
[("22.04", UbuntuSeries.JAMMY), ("20.04", UbuntuSeries.FOCAL), ("NR", None)],
)
@pytest.mark.parametrize("machine", ["AMD64", "x86_86", "arm64", "riscv64"])
def test_os_platform_series(system, release, series, machine):
def test_os_platform_series(release, series, machine):
"""Get platform from a patched Windows machine."""
with patch("platform.system", return_value=system):
with patch("platform.release", return_value=release):
with patch("platform.machine", return_value=machine):
result = get_os_platform()
assert result == OSPlatform(system=system, release=release, machine=machine)
with patch("distro.info", return_value={"version": release}):
with patch("platform.machine", return_value=machine):
result = get_os_platform()
assert result == OSPlatform(release=release, machine=machine)
assert result.series == series

0 comments on commit ca11310

Please sign in to comment.