From 9a9f090c8669d5cfe61e5388e4c2d9eacbc2d964 Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Sat, 9 Nov 2024 14:20:22 +0000 Subject: [PATCH] Fix Pod property type annotations to allow for missing fields in inspect output (#648) --- .../components/pod/cli_wrapper.py | 26 +++++++++---------- python_on_whales/components/pod/models.py | 9 ++++++- tests/python_on_whales/components/test_pod.py | 16 ++++++++++++ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/python_on_whales/components/pod/cli_wrapper.py b/python_on_whales/components/pod/cli_wrapper.py index 7891600d..a87b80cb 100644 --- a/python_on_whales/components/pod/cli_wrapper.py +++ b/python_on_whales/components/pod/cli_wrapper.py @@ -96,19 +96,19 @@ def created(self) -> datetime: return self._get_inspect_result().created @property - def create_command(self) -> List[str]: + def create_command(self) -> Optional[List[str]]: return self._get_inspect_result().create_command @property - def exit_policy(self) -> str: + def exit_policy(self) -> Optional[str]: return self._get_inspect_result().exit_policy @property - def state(self) -> str: + def state(self) -> Optional[str]: return self._get_inspect_result().state @property - def hostname(self) -> str: + def hostname(self) -> Optional[str]: return self._get_inspect_result().hostname @property @@ -116,39 +116,39 @@ def labels(self) -> Mapping[str, str]: return self._get_inspect_result().labels @property - def create_cgroup(self) -> bool: + def create_cgroup(self) -> Optional[bool]: return self._get_inspect_result().create_cgroup @property - def cgroup_parent(self) -> str: + def cgroup_parent(self) -> Optional[str]: return self._get_inspect_result().cgroup_parent @property - def cgroup_path(self) -> str: + def cgroup_path(self) -> Optional[str]: return self._get_inspect_result().cgroup_path @property - def create_infra(self) -> bool: + def create_infra(self) -> Optional[bool]: return self._get_inspect_result().create_infra @property - def infra_container_id(self) -> str: + def infra_container_id(self) -> Optional[str]: return self._get_inspect_result().infra_container_id @property - def infra_config(self) -> PodInfraConfig: + def infra_config(self) -> Optional[PodInfraConfig]: return self._get_inspect_result().infra_config @property - def shared_namespaces(self) -> List[str]: + def shared_namespaces(self) -> Optional[List[str]]: return self._get_inspect_result().shared_namespaces @property - def num_containers(self) -> int: + def num_containers(self) -> Optional[int]: return self._get_inspect_result().num_containers @property - def containers(self) -> List[PodContainer]: + def containers(self) -> Optional[List[PodContainer]]: return self._get_inspect_result().containers def __repr__(self): diff --git a/python_on_whales/components/pod/models.py b/python_on_whales/components/pod/models.py index 80931e6f..7e14411b 100644 --- a/python_on_whales/components/pod/models.py +++ b/python_on_whales/components/pod/models.py @@ -1,6 +1,10 @@ from datetime import datetime from typing import List, Mapping, Optional +import pydantic +from pydantic import AliasChoices +from typing_extensions import Annotated + from python_on_whales.components.container.models import PortBinding from python_on_whales.utils import DockerCamelModel @@ -32,7 +36,10 @@ class PodContainer(DockerCamelModel): class PodInspectResult(DockerCamelModel): id: Optional[str] = None name: Optional[str] = None - created: Optional[datetime] = None + created: Annotated[ + Optional[datetime], + pydantic.Field(validation_alias=AliasChoices("Created", "CreatedAt")), + ] = None create_command: Optional[List[str]] = None exit_policy: Optional[str] = None state: Optional[str] = None diff --git a/tests/python_on_whales/components/test_pod.py b/tests/python_on_whales/components/test_pod.py index d8a2805f..e48666e6 100644 --- a/tests/python_on_whales/components/test_pod.py +++ b/tests/python_on_whales/components/test_pod.py @@ -1,3 +1,4 @@ +import datetime as dt import json import re from pathlib import Path @@ -23,6 +24,11 @@ def test_create_simple(podman_client: DockerClient): assert pod.exists() assert pod.name == pod_name assert pod.state.lower() == "created" + assert ( + dt.datetime.now().astimezone() - dt.timedelta(minutes=1) + < pod.created + < dt.datetime.now().astimezone() + ) assert not pod.exists() @@ -31,6 +37,11 @@ def test_start_simple(podman_client: DockerClient): with podman_client.pod.create(pod_name) as pod: pod.start() assert pod.state.lower() == "running" + assert ( + dt.datetime.now().astimezone() - dt.timedelta(minutes=1) + < pod.created + < dt.datetime.now().astimezone() + ) assert not pod.exists() @@ -41,6 +52,11 @@ def test_stop_simple(podman_client: DockerClient): assert pod.state.lower() == "running" pod.stop() assert pod.state.lower() == "exited" + assert ( + dt.datetime.now().astimezone() - dt.timedelta(minutes=1) + < pod.created + < dt.datetime.now().astimezone() + ) assert not pod.exists()