Skip to content

Commit

Permalink
Fix Pod property type annotations to allow for missing fields in insp…
Browse files Browse the repository at this point in the history
…ect output (#648)
  • Loading branch information
LewisGaul authored Nov 9, 2024
1 parent b590c97 commit 9a9f090
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
26 changes: 13 additions & 13 deletions python_on_whales/components/pod/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,59 +96,59 @@ 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
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):
Expand Down
9 changes: 8 additions & 1 deletion python_on_whales/components/pod/models.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/python_on_whales/components/test_pod.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime as dt
import json
import re
from pathlib import Path
Expand All @@ -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()


Expand All @@ -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()


Expand All @@ -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()


Expand Down

0 comments on commit 9a9f090

Please sign in to comment.