imaged 2026.7.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- imaged/__init__.py +51 -0
- imaged/_dialects.py +166 -0
- imaged/_errors.py +112 -0
- imaged/_subprocess.py +370 -0
- imaged/tests/__init__.py +0 -0
- imaged/tests/test_engines.py +376 -0
- imaged/tests/test_engines_for_real.py +163 -0
- imaged-2026.7.2.dist-info/METADATA +86 -0
- imaged-2026.7.2.dist-info/RECORD +11 -0
- imaged-2026.7.2.dist-info/WHEEL +4 -0
- imaged-2026.7.2.dist-info/licenses/COPYING +19 -0
imaged/__init__.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""
|
|
2
|
+
One interface to docker, podman and Apple's container.
|
|
3
|
+
|
|
4
|
+
Start a container, speak to its standard streams, and get told what went
|
|
5
|
+
wrong in terms you can actually branch on -- with the same code and the
|
|
6
|
+
same answers whichever engine is installed.
|
|
7
|
+
|
|
8
|
+
Each engine is driven via its command line interface, which is the only
|
|
9
|
+
mechanism all three have in common, and which conveniently leaves the
|
|
10
|
+
engine to demultiplex container stdio onto real pipes for us.
|
|
11
|
+
|
|
12
|
+
`anyio` is the only async dependency, so this runs unmodified on both
|
|
13
|
+
asyncio and trio.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from imaged._dialects import (
|
|
17
|
+
CONTAINER,
|
|
18
|
+
DOCKER,
|
|
19
|
+
KNOWN,
|
|
20
|
+
PODMAN,
|
|
21
|
+
Dialect,
|
|
22
|
+
)
|
|
23
|
+
from imaged._errors import (
|
|
24
|
+
EngineError,
|
|
25
|
+
EngineFailed,
|
|
26
|
+
EngineNotRunning,
|
|
27
|
+
NoSuchContainer,
|
|
28
|
+
NoSuchEngine,
|
|
29
|
+
NoSuchImage,
|
|
30
|
+
SessionClosed,
|
|
31
|
+
Unsupported,
|
|
32
|
+
)
|
|
33
|
+
from imaged._subprocess import Engine, Session
|
|
34
|
+
|
|
35
|
+
__all__ = [
|
|
36
|
+
"CONTAINER",
|
|
37
|
+
"DOCKER",
|
|
38
|
+
"KNOWN",
|
|
39
|
+
"PODMAN",
|
|
40
|
+
"Dialect",
|
|
41
|
+
"Engine",
|
|
42
|
+
"EngineError",
|
|
43
|
+
"EngineFailed",
|
|
44
|
+
"EngineNotRunning",
|
|
45
|
+
"NoSuchContainer",
|
|
46
|
+
"NoSuchEngine",
|
|
47
|
+
"NoSuchImage",
|
|
48
|
+
"Session",
|
|
49
|
+
"SessionClosed",
|
|
50
|
+
"Unsupported",
|
|
51
|
+
]
|
imaged/_dialects.py
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The (few) ways the container engines we support differ from each other.
|
|
3
|
+
|
|
4
|
+
Pleasantly, they agree on nearly all of the spelling we need --
|
|
5
|
+
`create --interactive`, `start --attach --interactive`, `build --tag`
|
|
6
|
+
and `rm --force` are common to all three -- so what's left below is
|
|
7
|
+
quite small, and mostly concerns how each reports failure.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from attrs import frozen
|
|
13
|
+
|
|
14
|
+
from imaged._errors import (
|
|
15
|
+
EngineFailed,
|
|
16
|
+
EngineNotRunning,
|
|
17
|
+
NoSuchContainer,
|
|
18
|
+
NoSuchImage,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@frozen
|
|
23
|
+
class Dialect:
|
|
24
|
+
"""
|
|
25
|
+
One container engine, and how its CLI differs from the others.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
name: str
|
|
29
|
+
|
|
30
|
+
#: How the engine spells fetching an image.
|
|
31
|
+
#: Apple's container hides it under its image subcommand.
|
|
32
|
+
pull: tuple[str, ...] = ("pull",)
|
|
33
|
+
|
|
34
|
+
#: Flags silencing whatever progress output a pull would otherwise
|
|
35
|
+
#: emit.
|
|
36
|
+
#: This matters more than it may look, as a caller cannot tell
|
|
37
|
+
#: something the engine wrote to standard error apart from something
|
|
38
|
+
#: the container wrote there.
|
|
39
|
+
quiet_pull: tuple[str, ...] = ("--quiet",)
|
|
40
|
+
|
|
41
|
+
#: How the engine spells showing a *container's* configuration.
|
|
42
|
+
#: docker and podman also have a bare `inspect` which will happily
|
|
43
|
+
#: look at images and networks too, and which says "no such object"
|
|
44
|
+
#: rather than naming what it couldn't find.
|
|
45
|
+
inspect: tuple[str, ...] = ("container", "inspect")
|
|
46
|
+
|
|
47
|
+
#: Flags which disable networking entirely for a container.
|
|
48
|
+
no_network: tuple[str, ...] = ("--network", "none")
|
|
49
|
+
|
|
50
|
+
#: How the engine spells deleting an image.
|
|
51
|
+
remove_image: tuple[str, ...] = ("rmi", "--force")
|
|
52
|
+
|
|
53
|
+
#: Whether the engine can reattach to a running container's stdio.
|
|
54
|
+
#: Apple's container has no attach subcommand, and its exec starts a
|
|
55
|
+
#: new process rather than reattaching to the existing one.
|
|
56
|
+
attaches: bool = True
|
|
57
|
+
|
|
58
|
+
#: Substrings indicating the engine's daemon or service is down.
|
|
59
|
+
not_running: tuple[str, ...] = ()
|
|
60
|
+
|
|
61
|
+
#: Substrings indicating an image does not exist.
|
|
62
|
+
no_such_image: tuple[str, ...] = ()
|
|
63
|
+
|
|
64
|
+
#: Substrings indicating a container does not exist.
|
|
65
|
+
no_such_container: tuple[str, ...] = ()
|
|
66
|
+
|
|
67
|
+
#: The argv prefix to invoke, where it isn't simply the engine's
|
|
68
|
+
#: name -- an absolute path, or something like
|
|
69
|
+
#: `('docker', '--context', 'somewhere')`.
|
|
70
|
+
executable: tuple[str, ...] | None = None
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def command(self) -> tuple[str, ...]:
|
|
74
|
+
"""
|
|
75
|
+
The argv prefix which invokes this engine.
|
|
76
|
+
"""
|
|
77
|
+
return self.executable or (self.name,)
|
|
78
|
+
|
|
79
|
+
def classify(
|
|
80
|
+
self,
|
|
81
|
+
argv: tuple[str, ...],
|
|
82
|
+
returncode: int,
|
|
83
|
+
stderr: str,
|
|
84
|
+
subject: str,
|
|
85
|
+
) -> Exception:
|
|
86
|
+
"""
|
|
87
|
+
Work out what an engine was trying to tell us when it failed.
|
|
88
|
+
|
|
89
|
+
`subject` is whatever the command was operating on -- an image
|
|
90
|
+
or a container -- as the engines are inconsistent about whether
|
|
91
|
+
they mention it themselves.
|
|
92
|
+
"""
|
|
93
|
+
message = stderr.casefold()
|
|
94
|
+
|
|
95
|
+
# Check this first, as an engine which isn't running will happily
|
|
96
|
+
# claim an image doesn't exist rather than admitting as much.
|
|
97
|
+
if any(each in message for each in self.not_running):
|
|
98
|
+
return EngineNotRunning(engine=self.name)
|
|
99
|
+
if any(each in message for each in self.no_such_image):
|
|
100
|
+
return NoSuchImage(image=subject)
|
|
101
|
+
if any(each in message for each in self.no_such_container):
|
|
102
|
+
return NoSuchContainer(id=subject)
|
|
103
|
+
return EngineFailed(
|
|
104
|
+
argv=argv,
|
|
105
|
+
returncode=returncode,
|
|
106
|
+
stderr=stderr,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
DOCKER = Dialect(
|
|
111
|
+
name="docker",
|
|
112
|
+
not_running=(
|
|
113
|
+
"cannot connect to the docker daemon",
|
|
114
|
+
"is the docker daemon running",
|
|
115
|
+
),
|
|
116
|
+
no_such_image=(
|
|
117
|
+
"manifest unknown",
|
|
118
|
+
"pull access denied",
|
|
119
|
+
"repository does not exist",
|
|
120
|
+
# Registries (including ghcr.io) decline to distinguish "no such
|
|
121
|
+
# image" from "you may not look at this image", so we treat the
|
|
122
|
+
# two alike.
|
|
123
|
+
": denied",
|
|
124
|
+
"403 (forbidden)",
|
|
125
|
+
),
|
|
126
|
+
no_such_container=("no such container",),
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
PODMAN = Dialect(
|
|
130
|
+
name="podman",
|
|
131
|
+
not_running=(
|
|
132
|
+
"cannot connect to podman",
|
|
133
|
+
"unable to connect to podman socket",
|
|
134
|
+
),
|
|
135
|
+
no_such_image=(
|
|
136
|
+
"manifest unknown",
|
|
137
|
+
"unable to find a name and tag match",
|
|
138
|
+
": denied",
|
|
139
|
+
"403 (forbidden)",
|
|
140
|
+
),
|
|
141
|
+
no_such_container=(
|
|
142
|
+
"no such container",
|
|
143
|
+
"no container with name or id",
|
|
144
|
+
),
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
CONTAINER = Dialect(
|
|
148
|
+
name="container",
|
|
149
|
+
pull=("image", "pull"),
|
|
150
|
+
quiet_pull=("--disable-progress-updates",),
|
|
151
|
+
inspect=("inspect",),
|
|
152
|
+
remove_image=("image", "rm"),
|
|
153
|
+
attaches=False,
|
|
154
|
+
not_running=(
|
|
155
|
+
"xpc connection error",
|
|
156
|
+
"container system service has been started",
|
|
157
|
+
),
|
|
158
|
+
# FIXME: Unverified. Apple's container refuses to say anything at all
|
|
159
|
+
# until its service is running, so these need filling in from
|
|
160
|
+
# a machine where it is.
|
|
161
|
+
no_such_image=(),
|
|
162
|
+
no_such_container=(),
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
#: Every engine we know how to drive, in the order we look for them.
|
|
166
|
+
KNOWN = (DOCKER, PODMAN, CONTAINER)
|
imaged/_errors.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Errors which can happen whilst speaking to a container engine.
|
|
3
|
+
|
|
4
|
+
Every engine reports the same failure in its own way, and none of them do
|
|
5
|
+
so machine readably -- not even over their HTTP APIs, which is why the
|
|
6
|
+
code this layer replaces was reduced to matching on substrings of prose.
|
|
7
|
+
Turning that mess into something a caller can branch on is most of the
|
|
8
|
+
reason this layer exists at all.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from attrs import frozen
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class EngineError(Exception):
|
|
17
|
+
"""
|
|
18
|
+
Something went wrong whilst speaking to a container engine.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@frozen
|
|
23
|
+
class NoSuchEngine(EngineError):
|
|
24
|
+
"""
|
|
25
|
+
None of the container engines we know how to drive are installed.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
tried: tuple[str, ...]
|
|
29
|
+
|
|
30
|
+
def __str__(self):
|
|
31
|
+
return f"No container engine found (tried {', '.join(self.tried)})."
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@frozen
|
|
35
|
+
class EngineNotRunning(EngineError):
|
|
36
|
+
"""
|
|
37
|
+
The engine is installed, but whatever it speaks to isn't running.
|
|
38
|
+
|
|
39
|
+
For docker and podman that's a daemon (and on macOS, a VM housing it).
|
|
40
|
+
For Apple's container it's a launchd-managed service.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
engine: str
|
|
44
|
+
|
|
45
|
+
def __str__(self):
|
|
46
|
+
return f"The {self.engine} engine isn't running."
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@frozen
|
|
50
|
+
class NoSuchImage(EngineError):
|
|
51
|
+
"""
|
|
52
|
+
The image doesn't exist, locally nor in the registry it came from.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
image: str
|
|
56
|
+
|
|
57
|
+
def __str__(self):
|
|
58
|
+
return f"No image named {self.image!r}."
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@frozen
|
|
62
|
+
class NoSuchContainer(EngineError):
|
|
63
|
+
"""
|
|
64
|
+
No container with the given ID exists.
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
id: str
|
|
68
|
+
|
|
69
|
+
def __str__(self):
|
|
70
|
+
return f"No container with ID {self.id!r}."
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@frozen
|
|
74
|
+
class Unsupported(EngineError):
|
|
75
|
+
"""
|
|
76
|
+
The engine cannot do what was asked of it.
|
|
77
|
+
|
|
78
|
+
This is a capability gap rather than a failure -- Apple's container,
|
|
79
|
+
for instance, has no way to reattach to a running container's stdio.
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
engine: str
|
|
83
|
+
operation: str
|
|
84
|
+
|
|
85
|
+
def __str__(self):
|
|
86
|
+
return f"{self.engine} cannot {self.operation}."
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@frozen
|
|
90
|
+
class SessionClosed(EngineError):
|
|
91
|
+
"""
|
|
92
|
+
The container's standard streams are closed, but we tried to use them.
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
stderr: bytes = b""
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@frozen
|
|
99
|
+
class EngineFailed(EngineError):
|
|
100
|
+
"""
|
|
101
|
+
The engine failed in some way we do not specifically recognize.
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
argv: tuple[str, ...]
|
|
105
|
+
returncode: int
|
|
106
|
+
stderr: str
|
|
107
|
+
|
|
108
|
+
def __str__(self):
|
|
109
|
+
return (
|
|
110
|
+
f"`{' '.join(self.argv)}` exited with {self.returncode}:\n"
|
|
111
|
+
f"{self.stderr}"
|
|
112
|
+
)
|
imaged/_subprocess.py
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Driving a container engine by running its command line interface.
|
|
3
|
+
|
|
4
|
+
This is the only mechanism all the engines have in common.
|
|
5
|
+
Apple's container has no HTTP API at all, and speaking to it over XPC
|
|
6
|
+
instead would cover it alone whilst leaving docker and podman needing a
|
|
7
|
+
second mechanism anyhow.
|
|
8
|
+
|
|
9
|
+
Doing so also means the engine demultiplexes container stdio onto real
|
|
10
|
+
pipes for us, rather than leaving us to unpick its framing by hand.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from contextlib import asynccontextmanager, contextmanager, suppress
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
from shutil import which
|
|
18
|
+
from tempfile import TemporaryDirectory
|
|
19
|
+
from typing import TYPE_CHECKING, cast
|
|
20
|
+
import sys
|
|
21
|
+
|
|
22
|
+
from anyio.streams.buffered import BufferedByteReceiveStream
|
|
23
|
+
from attrs import field, frozen
|
|
24
|
+
import anyio
|
|
25
|
+
|
|
26
|
+
from imaged._dialects import KNOWN
|
|
27
|
+
from imaged._errors import (
|
|
28
|
+
NoSuchContainer,
|
|
29
|
+
NoSuchEngine,
|
|
30
|
+
NoSuchImage,
|
|
31
|
+
SessionClosed,
|
|
32
|
+
Unsupported,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
if TYPE_CHECKING:
|
|
36
|
+
from collections.abc import AsyncGenerator, Generator
|
|
37
|
+
|
|
38
|
+
from anyio.abc import ByteReceiveStream, Process
|
|
39
|
+
|
|
40
|
+
from imaged._dialects import Dialect
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
#: The longest line we will buffer from a container before giving up.
|
|
44
|
+
#: Responses carrying large schemas are entirely normal, so this is less
|
|
45
|
+
#: a protocol limit than a way to stay bounded if a harness never emits
|
|
46
|
+
#: a newline at all.
|
|
47
|
+
_MAX_LINE = 32 * 1024 * 1024
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@contextmanager
|
|
51
|
+
def _prepared(context: Path) -> Generator[Path]:
|
|
52
|
+
"""
|
|
53
|
+
A build context in a state an engine can be handed.
|
|
54
|
+
|
|
55
|
+
On most platforms that's simply the directory we were given.
|
|
56
|
+
Windows needs a normalized copy of it, as git's autocrlf will have
|
|
57
|
+
rewritten LF to CRLF on checkout -- which breaks shebang lines
|
|
58
|
+
inside a Linux container -- and its filesystem has no executable bit
|
|
59
|
+
for the engine to preserve.
|
|
60
|
+
"""
|
|
61
|
+
# A context which isn't there is handed over as-is, so that the
|
|
62
|
+
# engine says so rather than us quietly building from an empty
|
|
63
|
+
# directory we just made.
|
|
64
|
+
if sys.platform != "win32" or not context.is_dir():
|
|
65
|
+
yield context
|
|
66
|
+
return
|
|
67
|
+
|
|
68
|
+
with TemporaryDirectory() as temporary:
|
|
69
|
+
prepared = Path(temporary)
|
|
70
|
+
for path in sorted(context.rglob("*")):
|
|
71
|
+
if path.is_dir():
|
|
72
|
+
continue
|
|
73
|
+
data = path.read_bytes().replace(b"\r\n", b"\n")
|
|
74
|
+
copy = prepared / path.relative_to(context)
|
|
75
|
+
copy.parent.mkdir(parents=True, exist_ok=True)
|
|
76
|
+
copy.write_bytes(data)
|
|
77
|
+
copy.chmod(0o755 if data.startswith(b"#!") else 0o644)
|
|
78
|
+
yield prepared
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@frozen
|
|
82
|
+
class Session:
|
|
83
|
+
"""
|
|
84
|
+
A live bidirectional connection to a container's standard streams.
|
|
85
|
+
|
|
86
|
+
Standard error is collected into a file rather than a pipe.
|
|
87
|
+
A container which says a great deal there therefore cannot deadlock
|
|
88
|
+
by filling a pipe nobody is draining, and reading it needs no
|
|
89
|
+
background task -- which in turn keeps this free of any cancel scope
|
|
90
|
+
spanning a yield, and so portable to trio.
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
_process: Process = field(alias="process", repr=False)
|
|
94
|
+
_stdout: BufferedByteReceiveStream = field(alias="stdout", repr=False)
|
|
95
|
+
_stderr: Path = field(alias="stderr", repr=False)
|
|
96
|
+
|
|
97
|
+
@property
|
|
98
|
+
def alive(self) -> bool:
|
|
99
|
+
"""
|
|
100
|
+
Is the container still running?
|
|
101
|
+
"""
|
|
102
|
+
return self._process.returncode is None
|
|
103
|
+
|
|
104
|
+
def stderr(self) -> bytes:
|
|
105
|
+
"""
|
|
106
|
+
Whatever the container has written to standard error so far.
|
|
107
|
+
"""
|
|
108
|
+
try:
|
|
109
|
+
return self._stderr.read_bytes()
|
|
110
|
+
except FileNotFoundError: # the session is long over
|
|
111
|
+
return b""
|
|
112
|
+
|
|
113
|
+
async def send(self, line: str) -> None:
|
|
114
|
+
"""
|
|
115
|
+
Write a single line to the container's standard input.
|
|
116
|
+
"""
|
|
117
|
+
stdin = self._process.stdin
|
|
118
|
+
if stdin is None:
|
|
119
|
+
raise SessionClosed(stderr=self.stderr())
|
|
120
|
+
try:
|
|
121
|
+
await stdin.send(f"{line}\n".encode())
|
|
122
|
+
except (anyio.BrokenResourceError, anyio.ClosedResourceError):
|
|
123
|
+
raise SessionClosed(stderr=self.stderr()) from None
|
|
124
|
+
|
|
125
|
+
async def receive(self) -> str:
|
|
126
|
+
"""
|
|
127
|
+
Read a single line from the container's standard output.
|
|
128
|
+
|
|
129
|
+
Raises `SessionClosed` if the container has gone away.
|
|
130
|
+
Callers wanting to wait only so long should wrap this in an
|
|
131
|
+
`anyio.fail_after` scope, which is safe to abandon and retry --
|
|
132
|
+
anything already read stays buffered for the next attempt.
|
|
133
|
+
"""
|
|
134
|
+
try:
|
|
135
|
+
line = await self._stdout.receive_until(b"\n", _MAX_LINE)
|
|
136
|
+
except (
|
|
137
|
+
anyio.BrokenResourceError,
|
|
138
|
+
anyio.ClosedResourceError,
|
|
139
|
+
anyio.EndOfStream,
|
|
140
|
+
anyio.IncompleteRead,
|
|
141
|
+
):
|
|
142
|
+
raise SessionClosed(stderr=self.stderr()) from None
|
|
143
|
+
except anyio.DelimiterNotFound:
|
|
144
|
+
# A harness which has said this much without a newline is
|
|
145
|
+
# never going to send one.
|
|
146
|
+
raise SessionClosed(stderr=self.stderr()) from None
|
|
147
|
+
# Tolerate CRLF, as line oriented protocols generally do.
|
|
148
|
+
return line.decode().removesuffix("\r")
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
@frozen
|
|
152
|
+
class Engine:
|
|
153
|
+
"""
|
|
154
|
+
A container engine, driven via its command line interface.
|
|
155
|
+
"""
|
|
156
|
+
|
|
157
|
+
_dialect: Dialect = field(alias="dialect")
|
|
158
|
+
|
|
159
|
+
@classmethod
|
|
160
|
+
def detect(cls, *dialects: Dialect) -> Engine:
|
|
161
|
+
"""
|
|
162
|
+
Use the first engine we know about which is actually installed.
|
|
163
|
+
"""
|
|
164
|
+
candidates = dialects or KNOWN
|
|
165
|
+
for dialect in candidates:
|
|
166
|
+
if which(dialect.command[0]) is not None:
|
|
167
|
+
return cls(dialect=dialect)
|
|
168
|
+
raise NoSuchEngine(tried=tuple(each.name for each in candidates))
|
|
169
|
+
|
|
170
|
+
@classmethod
|
|
171
|
+
def named(cls, name: str) -> Engine:
|
|
172
|
+
"""
|
|
173
|
+
Use one specific engine, by name.
|
|
174
|
+
"""
|
|
175
|
+
for dialect in KNOWN:
|
|
176
|
+
if dialect.name == name:
|
|
177
|
+
return cls(dialect=dialect)
|
|
178
|
+
raise NoSuchEngine(tried=(name,))
|
|
179
|
+
|
|
180
|
+
@property
|
|
181
|
+
def name(self) -> str:
|
|
182
|
+
"""
|
|
183
|
+
Which engine this is.
|
|
184
|
+
"""
|
|
185
|
+
return self._dialect.name
|
|
186
|
+
|
|
187
|
+
@property
|
|
188
|
+
def attaches(self) -> bool:
|
|
189
|
+
"""
|
|
190
|
+
Can this engine reattach to a running container's stdio?
|
|
191
|
+
"""
|
|
192
|
+
return self._dialect.attaches
|
|
193
|
+
|
|
194
|
+
async def pull(self, image: str) -> None:
|
|
195
|
+
"""
|
|
196
|
+
Fetch an image from wherever it lives.
|
|
197
|
+
"""
|
|
198
|
+
await self._engine(
|
|
199
|
+
*self._dialect.pull,
|
|
200
|
+
*self._dialect.quiet_pull,
|
|
201
|
+
image,
|
|
202
|
+
subject=image,
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
async def create(
|
|
206
|
+
self,
|
|
207
|
+
image: str,
|
|
208
|
+
*command: str,
|
|
209
|
+
network: bool = True,
|
|
210
|
+
) -> str:
|
|
211
|
+
"""
|
|
212
|
+
Create (but do not start) a container, returning its ID.
|
|
213
|
+
|
|
214
|
+
Any `command` given replaces whatever the image would otherwise
|
|
215
|
+
run.
|
|
216
|
+
|
|
217
|
+
Networking is whatever the engine does by default, unless it's
|
|
218
|
+
switched off here, in which case the container gets none at all.
|
|
219
|
+
How an engine provides networking is its own business, and they
|
|
220
|
+
do differ; having none is the part they agree on.
|
|
221
|
+
"""
|
|
222
|
+
args = ["create", "--interactive"]
|
|
223
|
+
if not network:
|
|
224
|
+
args.extend(self._dialect.no_network)
|
|
225
|
+
args.append(image)
|
|
226
|
+
args.extend(command)
|
|
227
|
+
return (await self._engine(*args, subject=image)).strip()
|
|
228
|
+
|
|
229
|
+
async def create_pulling_if_needed(
|
|
230
|
+
self,
|
|
231
|
+
image: str,
|
|
232
|
+
*command: str,
|
|
233
|
+
network: bool = True,
|
|
234
|
+
) -> str:
|
|
235
|
+
"""
|
|
236
|
+
Create a container, fetching its image first if we lack it.
|
|
237
|
+
|
|
238
|
+
Engines differ on whether creating pulls implicitly, so we ask
|
|
239
|
+
for it explicitly rather than relying on either behavior.
|
|
240
|
+
"""
|
|
241
|
+
try:
|
|
242
|
+
return await self.create(image, *command, network=network)
|
|
243
|
+
except NoSuchImage:
|
|
244
|
+
await self.pull(image)
|
|
245
|
+
return await self.create(image, *command, network=network)
|
|
246
|
+
|
|
247
|
+
async def remove(self, id: str) -> None:
|
|
248
|
+
"""
|
|
249
|
+
Delete a container, running or not.
|
|
250
|
+
"""
|
|
251
|
+
await self._engine("rm", "--force", id, subject=id)
|
|
252
|
+
|
|
253
|
+
async def start_detached(self, id: str) -> None:
|
|
254
|
+
"""
|
|
255
|
+
Start a container without speaking to it.
|
|
256
|
+
"""
|
|
257
|
+
await self._engine("start", id, subject=id)
|
|
258
|
+
|
|
259
|
+
async def build(self, tag: str, context: Path) -> None:
|
|
260
|
+
"""
|
|
261
|
+
Build an image from a directory holding its build context.
|
|
262
|
+
|
|
263
|
+
A directory is the one form every engine accepts -- Apple's
|
|
264
|
+
container takes nothing else, and whilst docker will read a tar
|
|
265
|
+
archive from standard input, podman reserves that for reading a
|
|
266
|
+
Containerfile.
|
|
267
|
+
"""
|
|
268
|
+
with _prepared(context) as prepared:
|
|
269
|
+
await self._engine(
|
|
270
|
+
"build",
|
|
271
|
+
"--tag",
|
|
272
|
+
tag,
|
|
273
|
+
str(prepared),
|
|
274
|
+
subject=tag,
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
async def remove_image(self, tag: str) -> None:
|
|
278
|
+
"""
|
|
279
|
+
Delete an image.
|
|
280
|
+
"""
|
|
281
|
+
await self._engine(*self._dialect.remove_image, tag, subject=tag)
|
|
282
|
+
|
|
283
|
+
async def exists(self, id: str) -> bool:
|
|
284
|
+
"""
|
|
285
|
+
Is there a container with the given ID?
|
|
286
|
+
"""
|
|
287
|
+
try:
|
|
288
|
+
await self._engine(*self._dialect.inspect, id, subject=id)
|
|
289
|
+
except NoSuchContainer:
|
|
290
|
+
return False
|
|
291
|
+
return True
|
|
292
|
+
|
|
293
|
+
@asynccontextmanager
|
|
294
|
+
async def start(self, id: str) -> AsyncGenerator[Session]:
|
|
295
|
+
"""
|
|
296
|
+
Start a created container and speak to its standard streams.
|
|
297
|
+
"""
|
|
298
|
+
args = ("start", "--attach", "--interactive", id)
|
|
299
|
+
async with self._session(*args) as session:
|
|
300
|
+
yield session
|
|
301
|
+
|
|
302
|
+
@asynccontextmanager
|
|
303
|
+
async def attach(self, id: str) -> AsyncGenerator[Session]:
|
|
304
|
+
"""
|
|
305
|
+
Speak to the standard streams of an already running container.
|
|
306
|
+
"""
|
|
307
|
+
if not self._dialect.attaches:
|
|
308
|
+
raise Unsupported(
|
|
309
|
+
engine=self._dialect.name,
|
|
310
|
+
operation="attach to a running container",
|
|
311
|
+
)
|
|
312
|
+
# Confirm it exists up front, as otherwise a bad ID shows up only
|
|
313
|
+
# as a session which closes immediately for no stated reason.
|
|
314
|
+
await self._engine(*self._dialect.inspect, id, subject=id)
|
|
315
|
+
async with self._session("attach", id) as session:
|
|
316
|
+
yield session
|
|
317
|
+
|
|
318
|
+
async def _engine(self, *args: str, subject: str) -> str:
|
|
319
|
+
"""
|
|
320
|
+
Run an engine command to completion, returning its stdout.
|
|
321
|
+
"""
|
|
322
|
+
argv = (*self._dialect.command, *args)
|
|
323
|
+
try:
|
|
324
|
+
completed = await anyio.run_process(argv, check=False)
|
|
325
|
+
except FileNotFoundError:
|
|
326
|
+
raise NoSuchEngine(tried=(self._dialect.name,)) from None
|
|
327
|
+
|
|
328
|
+
if completed.returncode:
|
|
329
|
+
raise self._dialect.classify(
|
|
330
|
+
argv=argv,
|
|
331
|
+
returncode=completed.returncode,
|
|
332
|
+
stderr=completed.stderr.decode(),
|
|
333
|
+
subject=subject,
|
|
334
|
+
)
|
|
335
|
+
return completed.stdout.decode()
|
|
336
|
+
|
|
337
|
+
@asynccontextmanager
|
|
338
|
+
async def _session(self, *args: str) -> AsyncGenerator[Session]:
|
|
339
|
+
"""
|
|
340
|
+
Speak to whatever container the given command connects us to.
|
|
341
|
+
"""
|
|
342
|
+
argv = (*self._dialect.command, *args)
|
|
343
|
+
with TemporaryDirectory() as directory:
|
|
344
|
+
# We hand the container a file to write standard error to,
|
|
345
|
+
# and read it back via a second, independent handle.
|
|
346
|
+
# Sharing the one handle would mean sharing a file offset
|
|
347
|
+
# with the container, so our seeking in order to read could
|
|
348
|
+
# land its next write somewhere it doesn't belong.
|
|
349
|
+
path = Path(directory) / "stderr"
|
|
350
|
+
with path.open("wb") as stderr:
|
|
351
|
+
try:
|
|
352
|
+
process = await anyio.open_process(argv, stderr=stderr)
|
|
353
|
+
except FileNotFoundError:
|
|
354
|
+
raise NoSuchEngine(
|
|
355
|
+
tried=(self._dialect.name,),
|
|
356
|
+
) from None
|
|
357
|
+
|
|
358
|
+
try:
|
|
359
|
+
yield Session(
|
|
360
|
+
process=process,
|
|
361
|
+
stdout=BufferedByteReceiveStream(
|
|
362
|
+
cast("ByteReceiveStream", process.stdout),
|
|
363
|
+
),
|
|
364
|
+
stderr=path,
|
|
365
|
+
)
|
|
366
|
+
finally:
|
|
367
|
+
if process.returncode is None:
|
|
368
|
+
with suppress(ProcessLookupError): # may beat us
|
|
369
|
+
process.kill()
|
|
370
|
+
await process.wait()
|
imaged/tests/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Tests for the container engine layer.
|
|
3
|
+
|
|
4
|
+
These deliberately drive a stand-in engine rather than a real one, as
|
|
5
|
+
what's worth testing here is the plumbing -- argv construction, stdio
|
|
6
|
+
round trips, how sessions end and how failures are classified -- none of
|
|
7
|
+
which needs a container to exercise.
|
|
8
|
+
Whether each real engine actually speaks the dialect described in
|
|
9
|
+
`imaged._dialects` is what the against-a-real-engine tests are for.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import sys
|
|
15
|
+
|
|
16
|
+
import pytest
|
|
17
|
+
|
|
18
|
+
from imaged import (
|
|
19
|
+
CONTAINER,
|
|
20
|
+
DOCKER,
|
|
21
|
+
PODMAN,
|
|
22
|
+
Dialect,
|
|
23
|
+
Engine,
|
|
24
|
+
EngineFailed,
|
|
25
|
+
EngineNotRunning,
|
|
26
|
+
NoSuchContainer,
|
|
27
|
+
NoSuchEngine,
|
|
28
|
+
NoSuchImage,
|
|
29
|
+
SessionClosed,
|
|
30
|
+
Unsupported,
|
|
31
|
+
_subprocess,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
pytestmark = pytest.mark.anyio
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
#: An "engine" which knows just enough to be driven like a real one.
|
|
38
|
+
#: Starting a container gets you an echo server standing in for a
|
|
39
|
+
#: harness, which dies on request so that closure is testable.
|
|
40
|
+
FAKE_ENGINE = """\
|
|
41
|
+
import pathlib, sys
|
|
42
|
+
|
|
43
|
+
# A real container is Linux, so don't let Windows translate ours.
|
|
44
|
+
sys.stdout.reconfigure(newline=chr(10))
|
|
45
|
+
|
|
46
|
+
args = sys.argv[1:]
|
|
47
|
+
pathlib.Path(__file__).with_name("argv").open("a").write(f"{args}\\n")
|
|
48
|
+
|
|
49
|
+
match args:
|
|
50
|
+
case ["create", *rest]:
|
|
51
|
+
if "nonexistent" in rest[-1]:
|
|
52
|
+
print("Error: manifest unknown", file=sys.stderr)
|
|
53
|
+
sys.exit(1)
|
|
54
|
+
print("fake-container-id")
|
|
55
|
+
case ["start", *rest]:
|
|
56
|
+
while True:
|
|
57
|
+
line = sys.stdin.readline()
|
|
58
|
+
if not line:
|
|
59
|
+
break
|
|
60
|
+
if line.strip() == "die":
|
|
61
|
+
sys.exit(0)
|
|
62
|
+
if line.strip() == "complain":
|
|
63
|
+
print("something went wrong", file=sys.stderr, flush=True)
|
|
64
|
+
continue
|
|
65
|
+
if line.strip() == "crlf":
|
|
66
|
+
sys.stdout.write("fine" + chr(13) + chr(10))
|
|
67
|
+
sys.stdout.flush()
|
|
68
|
+
continue
|
|
69
|
+
if line.strip() == "flood":
|
|
70
|
+
sys.stdout.write("x" * 1000) # ... and never a newline
|
|
71
|
+
sys.stdout.flush()
|
|
72
|
+
continue
|
|
73
|
+
print(line.strip()[::-1], flush=True)
|
|
74
|
+
case ["container", "inspect", *rest]:
|
|
75
|
+
if "nonexistent" in rest[-1]:
|
|
76
|
+
print("Error: no such container", file=sys.stderr)
|
|
77
|
+
sys.exit(1)
|
|
78
|
+
case ["build", *rest]:
|
|
79
|
+
context = pathlib.Path(rest[-1])
|
|
80
|
+
if not context.is_dir():
|
|
81
|
+
print(f"not a context directory: {rest[-1]}", file=sys.stderr)
|
|
82
|
+
sys.exit(1)
|
|
83
|
+
names = sorted(each.name for each in context.iterdir())
|
|
84
|
+
pathlib.Path(__file__).with_name("contents").write_text(str(names))
|
|
85
|
+
case ["pull", *rest] | ["rm", *rest]:
|
|
86
|
+
pass
|
|
87
|
+
case _:
|
|
88
|
+
print(f"unknown invocation: {args}", file=sys.stderr)
|
|
89
|
+
sys.exit(2)
|
|
90
|
+
"""
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@pytest.fixture(params=["asyncio", "trio"])
|
|
94
|
+
def anyio_backend(request):
|
|
95
|
+
"""
|
|
96
|
+
Everything here should hold on either event loop.
|
|
97
|
+
"""
|
|
98
|
+
return request.param
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@pytest.fixture
|
|
102
|
+
def engine(tmp_path):
|
|
103
|
+
script = tmp_path / "fake-engine.py"
|
|
104
|
+
script.write_text(FAKE_ENGINE)
|
|
105
|
+
return Engine(
|
|
106
|
+
dialect=Dialect(
|
|
107
|
+
name="fake",
|
|
108
|
+
executable=(sys.executable, str(script)),
|
|
109
|
+
no_such_image=("manifest unknown",),
|
|
110
|
+
no_such_container=("no such container",),
|
|
111
|
+
),
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@pytest.fixture
|
|
116
|
+
def argv(tmp_path):
|
|
117
|
+
def _argv():
|
|
118
|
+
path = tmp_path / "argv"
|
|
119
|
+
return path.read_text().splitlines() if path.exists() else []
|
|
120
|
+
|
|
121
|
+
return _argv
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class TestSessions:
|
|
125
|
+
async def test_round_trip(self, engine):
|
|
126
|
+
id = await engine.create("some-image")
|
|
127
|
+
async with engine.start(id) as session:
|
|
128
|
+
await session.send("hello")
|
|
129
|
+
assert await session.receive() == "olleh"
|
|
130
|
+
await session.send("goodbye")
|
|
131
|
+
assert await session.receive() == "eybdoog"
|
|
132
|
+
|
|
133
|
+
async def test_receiving_from_a_dead_container(self, engine):
|
|
134
|
+
id = await engine.create("some-image")
|
|
135
|
+
async with engine.start(id) as session:
|
|
136
|
+
await session.send("die")
|
|
137
|
+
with pytest.raises(SessionClosed):
|
|
138
|
+
await session.receive()
|
|
139
|
+
|
|
140
|
+
async def test_stderr_is_collected_without_ending_the_session(
|
|
141
|
+
self,
|
|
142
|
+
engine,
|
|
143
|
+
):
|
|
144
|
+
id = await engine.create("some-image")
|
|
145
|
+
async with engine.start(id) as session:
|
|
146
|
+
await session.send("complain")
|
|
147
|
+
await session.send("hello")
|
|
148
|
+
assert await session.receive() == "olleh"
|
|
149
|
+
assert b"something went wrong" in session.stderr()
|
|
150
|
+
|
|
151
|
+
async def test_stderr_survives_closure(self, engine):
|
|
152
|
+
id = await engine.create("some-image")
|
|
153
|
+
async with engine.start(id) as session:
|
|
154
|
+
await session.send("complain")
|
|
155
|
+
await session.send("die")
|
|
156
|
+
with pytest.raises(SessionClosed):
|
|
157
|
+
await session.receive()
|
|
158
|
+
assert b"something went wrong" in session.stderr()
|
|
159
|
+
|
|
160
|
+
async def test_the_container_is_gone_afterwards(self, engine):
|
|
161
|
+
id = await engine.create("some-image")
|
|
162
|
+
async with engine.start(id) as session:
|
|
163
|
+
assert session.alive
|
|
164
|
+
assert not session.alive
|
|
165
|
+
|
|
166
|
+
async def test_a_container_speaking_crlf(self, engine):
|
|
167
|
+
id = await engine.create("some-image")
|
|
168
|
+
async with engine.start(id) as session:
|
|
169
|
+
await session.send("crlf")
|
|
170
|
+
assert await session.receive() == "fine"
|
|
171
|
+
|
|
172
|
+
async def test_a_container_which_never_sends_a_newline(
|
|
173
|
+
self,
|
|
174
|
+
engine,
|
|
175
|
+
monkeypatch,
|
|
176
|
+
):
|
|
177
|
+
monkeypatch.setattr(_subprocess, "_MAX_LINE", 100)
|
|
178
|
+
id = await engine.create("some-image")
|
|
179
|
+
async with engine.start(id) as session:
|
|
180
|
+
await session.send("flood")
|
|
181
|
+
with pytest.raises(SessionClosed):
|
|
182
|
+
await session.receive()
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
class TestCreating:
|
|
186
|
+
async def test_networking_is_the_engine_default(self, engine, argv):
|
|
187
|
+
"""
|
|
188
|
+
We say nothing at all, rather than second-guessing the engine.
|
|
189
|
+
"""
|
|
190
|
+
await engine.create("some-image")
|
|
191
|
+
assert "--network" not in argv()[0]
|
|
192
|
+
|
|
193
|
+
async def test_networking_can_be_switched_off(self, engine, argv):
|
|
194
|
+
await engine.create("some-image", network=False)
|
|
195
|
+
assert "'--network', 'none'" in argv()[0]
|
|
196
|
+
|
|
197
|
+
async def test_stdin_is_always_open(self, engine, argv):
|
|
198
|
+
await engine.create("some-image")
|
|
199
|
+
assert "--interactive" in argv()[0]
|
|
200
|
+
|
|
201
|
+
async def test_nonexistent_image(self, engine):
|
|
202
|
+
with pytest.raises(NoSuchImage) as excinfo:
|
|
203
|
+
await engine.create("nonexistent-image")
|
|
204
|
+
assert excinfo.value.image == "nonexistent-image"
|
|
205
|
+
|
|
206
|
+
async def test_pulls_only_when_needed(self, engine, argv):
|
|
207
|
+
await engine.create_pulling_if_needed("some-image")
|
|
208
|
+
assert not any(each.startswith("['pull'") for each in argv())
|
|
209
|
+
|
|
210
|
+
async def test_pulls_when_missing(self, engine, argv):
|
|
211
|
+
# The stand-in never starts existing, so this ends up back where
|
|
212
|
+
# it started -- but only after having tried a pull.
|
|
213
|
+
with pytest.raises(NoSuchImage):
|
|
214
|
+
await engine.create_pulling_if_needed("nonexistent-image")
|
|
215
|
+
assert any(each.startswith("['pull'") for each in argv())
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
class TestBuilding:
|
|
219
|
+
"""
|
|
220
|
+
A directory is the one form of build context every engine takes.
|
|
221
|
+
"""
|
|
222
|
+
|
|
223
|
+
async def test_from_a_directory(self, engine, tmp_path):
|
|
224
|
+
"""
|
|
225
|
+
Whatever reaches the engine holds the context we were given.
|
|
226
|
+
|
|
227
|
+
It isn't always the very directory we passed -- on Windows it's
|
|
228
|
+
a normalized copy -- so the contents are what's worth asserting.
|
|
229
|
+
"""
|
|
230
|
+
context = tmp_path / "ctx"
|
|
231
|
+
context.mkdir()
|
|
232
|
+
context.joinpath("Dockerfile").write_text("FROM scratch\n")
|
|
233
|
+
await engine.build(tag="some-image", context=context)
|
|
234
|
+
assert tmp_path.joinpath("contents").read_text() == "['Dockerfile']"
|
|
235
|
+
|
|
236
|
+
async def test_a_context_which_isnt_there(self, engine, tmp_path):
|
|
237
|
+
with pytest.raises(EngineFailed):
|
|
238
|
+
await engine.build(tag="x", context=tmp_path / "nope")
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
class TestAttaching:
|
|
242
|
+
async def test_nonexistent_container(self, engine):
|
|
243
|
+
with pytest.raises(NoSuchContainer):
|
|
244
|
+
async with engine.attach("nonexistent-container"):
|
|
245
|
+
pass
|
|
246
|
+
|
|
247
|
+
async def test_unsupported(self):
|
|
248
|
+
engine = Engine(dialect=CONTAINER)
|
|
249
|
+
with pytest.raises(Unsupported) as excinfo:
|
|
250
|
+
async with engine.attach("whatever"):
|
|
251
|
+
pass
|
|
252
|
+
assert excinfo.value.engine == "container"
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
class TestDetection:
|
|
256
|
+
def test_no_engine_at_all(self):
|
|
257
|
+
nowhere = Dialect(name="definitely-not-installed-anywhere")
|
|
258
|
+
with pytest.raises(NoSuchEngine):
|
|
259
|
+
Engine.detect(nowhere)
|
|
260
|
+
|
|
261
|
+
def test_by_name(self):
|
|
262
|
+
assert Engine.named("podman").name == "podman"
|
|
263
|
+
|
|
264
|
+
def test_by_unknown_name(self):
|
|
265
|
+
with pytest.raises(NoSuchEngine):
|
|
266
|
+
Engine.named("kubernetes-lol")
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
class TestClassification:
|
|
270
|
+
"""
|
|
271
|
+
Each engine says the same things differently, and none of them
|
|
272
|
+
usefully.
|
|
273
|
+
"""
|
|
274
|
+
|
|
275
|
+
@pytest.mark.parametrize(
|
|
276
|
+
"dialect, stderr",
|
|
277
|
+
[
|
|
278
|
+
(DOCKER, "Cannot connect to the Docker daemon at unix://x.sock"),
|
|
279
|
+
(DOCKER, "Is the docker daemon running?"),
|
|
280
|
+
(PODMAN, "Cannot connect to Podman. Please verify your conn"),
|
|
281
|
+
(PODMAN, "Error: unable to connect to Podman socket"),
|
|
282
|
+
(CONTAINER, 'interrupted: "XPC connection error: Connection'),
|
|
283
|
+
(
|
|
284
|
+
CONTAINER,
|
|
285
|
+
(
|
|
286
|
+
"Ensure container system service has been started "
|
|
287
|
+
"with `container system start`."
|
|
288
|
+
),
|
|
289
|
+
),
|
|
290
|
+
],
|
|
291
|
+
)
|
|
292
|
+
def test_not_running(self, dialect, stderr):
|
|
293
|
+
error = dialect.classify(
|
|
294
|
+
argv=("x",),
|
|
295
|
+
returncode=1,
|
|
296
|
+
stderr=stderr,
|
|
297
|
+
subject="whatever",
|
|
298
|
+
)
|
|
299
|
+
assert isinstance(error, EngineNotRunning)
|
|
300
|
+
|
|
301
|
+
@pytest.mark.parametrize(
|
|
302
|
+
"dialect, stderr",
|
|
303
|
+
[
|
|
304
|
+
(
|
|
305
|
+
DOCKER,
|
|
306
|
+
(
|
|
307
|
+
"Error response from daemon: manifest for foo:latest "
|
|
308
|
+
"not found: manifest unknown"
|
|
309
|
+
),
|
|
310
|
+
),
|
|
311
|
+
(DOCKER, "Error response from daemon: pull access denied for x"),
|
|
312
|
+
(DOCKER, 'Head "https://ghcr.io/v2/x/manifests/latest": denied'),
|
|
313
|
+
(PODMAN, "Error: unable to find a name and tag match for x"),
|
|
314
|
+
(
|
|
315
|
+
PODMAN,
|
|
316
|
+
(
|
|
317
|
+
"Error: initializing source docker://ghcr.io/x:nope: "
|
|
318
|
+
"Requesting bearer token: invalid status code from "
|
|
319
|
+
"registry 403 (Forbidden)"
|
|
320
|
+
),
|
|
321
|
+
),
|
|
322
|
+
(PODMAN, 'Head "https://ghcr.io/v2/x/manifests/latest": denied'),
|
|
323
|
+
],
|
|
324
|
+
)
|
|
325
|
+
def test_no_such_image(self, dialect, stderr):
|
|
326
|
+
error = dialect.classify(
|
|
327
|
+
argv=("x",),
|
|
328
|
+
returncode=1,
|
|
329
|
+
stderr=stderr,
|
|
330
|
+
subject="some-image",
|
|
331
|
+
)
|
|
332
|
+
assert isinstance(error, NoSuchImage)
|
|
333
|
+
assert error.image == "some-image"
|
|
334
|
+
|
|
335
|
+
@pytest.mark.parametrize(
|
|
336
|
+
"dialect, stderr",
|
|
337
|
+
[
|
|
338
|
+
(DOCKER, "Error response from daemon: No such container: abcd"),
|
|
339
|
+
(PODMAN, 'Error: no container with name or ID "abcd" found'),
|
|
340
|
+
],
|
|
341
|
+
)
|
|
342
|
+
def test_no_such_container(self, dialect, stderr):
|
|
343
|
+
error = dialect.classify(
|
|
344
|
+
argv=("x",),
|
|
345
|
+
returncode=1,
|
|
346
|
+
stderr=stderr,
|
|
347
|
+
subject="abcd",
|
|
348
|
+
)
|
|
349
|
+
assert isinstance(error, NoSuchContainer)
|
|
350
|
+
assert error.id == "abcd"
|
|
351
|
+
|
|
352
|
+
def test_a_downed_engine_beats_a_missing_image(self):
|
|
353
|
+
"""
|
|
354
|
+
An engine which isn't running will cheerfully tell you an image
|
|
355
|
+
doesn't exist, which is true but unhelpful.
|
|
356
|
+
"""
|
|
357
|
+
error = DOCKER.classify(
|
|
358
|
+
argv=("x",),
|
|
359
|
+
returncode=1,
|
|
360
|
+
stderr=(
|
|
361
|
+
"Cannot connect to the Docker daemon. "
|
|
362
|
+
"manifest unknown" # both, and the daemon is the story
|
|
363
|
+
),
|
|
364
|
+
subject="some-image",
|
|
365
|
+
)
|
|
366
|
+
assert isinstance(error, EngineNotRunning)
|
|
367
|
+
|
|
368
|
+
def test_anything_else(self):
|
|
369
|
+
error = DOCKER.classify(
|
|
370
|
+
argv=("docker", "create", "x"),
|
|
371
|
+
returncode=125,
|
|
372
|
+
stderr="something nobody has ever seen before",
|
|
373
|
+
subject="x",
|
|
374
|
+
)
|
|
375
|
+
assert isinstance(error, EngineFailed)
|
|
376
|
+
assert "something nobody has ever seen before" in str(error)
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The same behavior, asserted against an actual container engine.
|
|
3
|
+
|
|
4
|
+
`imaged` exists so that three engines answer identically, which is a
|
|
5
|
+
claim only an actual engine can settle.
|
|
6
|
+
So these run one set of assertions against whichever engine
|
|
7
|
+
`IMAGED_ENGINE` names, and CI runs them once per engine.
|
|
8
|
+
|
|
9
|
+
They are the only tests here needing a container runtime, and skip
|
|
10
|
+
themselves when there isn't one.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from os import environ
|
|
16
|
+
|
|
17
|
+
import pytest
|
|
18
|
+
|
|
19
|
+
from imaged import (
|
|
20
|
+
Engine,
|
|
21
|
+
NoSuchEngine,
|
|
22
|
+
NoSuchImage,
|
|
23
|
+
SessionClosed,
|
|
24
|
+
Unsupported,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
pytestmark = [pytest.mark.anyio, pytest.mark.real]
|
|
28
|
+
|
|
29
|
+
#: Something tiny, ubiquitous, and able to hold a conversation.
|
|
30
|
+
IMAGE = "docker.io/library/alpine:3.22"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@pytest.fixture(params=["asyncio", "trio"])
|
|
34
|
+
def anyio_backend(request):
|
|
35
|
+
return request.param
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@pytest.fixture(scope="module")
|
|
39
|
+
def engine():
|
|
40
|
+
named = environ.get("IMAGED_ENGINE")
|
|
41
|
+
try:
|
|
42
|
+
return Engine.named(named) if named else Engine.detect()
|
|
43
|
+
except NoSuchEngine as error: # pragma: no cover
|
|
44
|
+
pytest.skip(str(error))
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@pytest.fixture
|
|
48
|
+
async def container(engine):
|
|
49
|
+
"""
|
|
50
|
+
Containers running the given command, cleaned up afterwards.
|
|
51
|
+
"""
|
|
52
|
+
created: list[str] = []
|
|
53
|
+
|
|
54
|
+
async def _container(*command: str) -> str:
|
|
55
|
+
id = await engine.create_pulling_if_needed(IMAGE, *command)
|
|
56
|
+
created.append(id)
|
|
57
|
+
return id
|
|
58
|
+
|
|
59
|
+
yield _container
|
|
60
|
+
|
|
61
|
+
for id in created:
|
|
62
|
+
await engine.remove(id)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
async def test_round_trip(engine, container):
|
|
66
|
+
id = await container("cat")
|
|
67
|
+
async with engine.start(id) as session:
|
|
68
|
+
await session.send("hello")
|
|
69
|
+
assert await session.receive() == "hello"
|
|
70
|
+
await session.send("goodbye")
|
|
71
|
+
assert await session.receive() == "goodbye"
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
async def test_a_container_which_exits(engine, container):
|
|
75
|
+
id = await container("true")
|
|
76
|
+
async with engine.start(id) as session:
|
|
77
|
+
with pytest.raises(SessionClosed):
|
|
78
|
+
await session.receive()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
async def test_stderr_is_kept_separate(engine, container):
|
|
82
|
+
id = await container("sh", "-c", "echo trouble >&2; exec cat")
|
|
83
|
+
async with engine.start(id) as session:
|
|
84
|
+
await session.send("hello")
|
|
85
|
+
assert await session.receive() == "hello"
|
|
86
|
+
assert b"trouble" in session.stderr()
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
async def test_stderr_survives_the_container(engine, container):
|
|
90
|
+
id = await container("sh", "-c", "echo very wrong >&2; exit 1")
|
|
91
|
+
async with engine.start(id) as session:
|
|
92
|
+
with pytest.raises(SessionClosed):
|
|
93
|
+
await session.receive()
|
|
94
|
+
assert b"very wrong" in session.stderr()
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
async def test_networking_can_be_switched_off(engine):
|
|
98
|
+
"""
|
|
99
|
+
Engines differ in how they *give* a container networking -- rootless
|
|
100
|
+
podman is not bridged the way docker is -- so what's worth asserting
|
|
101
|
+
is the part they agree on, which is having none at all.
|
|
102
|
+
"""
|
|
103
|
+
id = await engine.create_pulling_if_needed(
|
|
104
|
+
IMAGE,
|
|
105
|
+
"sh",
|
|
106
|
+
"-c",
|
|
107
|
+
"ls /sys/class/net",
|
|
108
|
+
network=False,
|
|
109
|
+
)
|
|
110
|
+
try:
|
|
111
|
+
async with engine.start(id) as session:
|
|
112
|
+
assert await session.receive() == "lo"
|
|
113
|
+
finally:
|
|
114
|
+
await engine.remove(id)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
async def test_nonexistent_image(engine):
|
|
118
|
+
with pytest.raises(NoSuchImage):
|
|
119
|
+
await engine.create_pulling_if_needed(
|
|
120
|
+
"ghcr.io/julian/imaged-definitely-not-a-real-image:nope",
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
async def test_exists(engine, container):
|
|
125
|
+
id = await container("cat")
|
|
126
|
+
assert await engine.exists(id)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
async def test_doesnt_exist(engine):
|
|
130
|
+
assert not await engine.exists("imaged-definitely-not-a-container")
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
async def test_attaching_to_a_running_container(engine, container):
|
|
134
|
+
id = await container("cat")
|
|
135
|
+
if not engine.attaches:
|
|
136
|
+
with pytest.raises(Unsupported):
|
|
137
|
+
async with engine.attach(id):
|
|
138
|
+
pass
|
|
139
|
+
return
|
|
140
|
+
|
|
141
|
+
await engine.start_detached(id)
|
|
142
|
+
async with engine.attach(id) as session:
|
|
143
|
+
await session.send("hello")
|
|
144
|
+
assert await session.receive() == "hello"
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
async def test_building(engine, tmp_path):
|
|
148
|
+
context = tmp_path / "context"
|
|
149
|
+
context.mkdir()
|
|
150
|
+
context.joinpath("Dockerfile").write_text(
|
|
151
|
+
f"FROM {IMAGE}\nRUN echo built > /built\n",
|
|
152
|
+
)
|
|
153
|
+
tag = "imaged-test-build:latest"
|
|
154
|
+
await engine.build(tag=tag, context=context)
|
|
155
|
+
try:
|
|
156
|
+
id = await engine.create(tag, "cat", "/built")
|
|
157
|
+
try:
|
|
158
|
+
async with engine.start(id) as session:
|
|
159
|
+
assert await session.receive() == "built"
|
|
160
|
+
finally:
|
|
161
|
+
await engine.remove(id)
|
|
162
|
+
finally:
|
|
163
|
+
await engine.remove_image(tag)
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: imaged
|
|
3
|
+
Version: 2026.7.2
|
|
4
|
+
Summary: One interface to docker, podman and Apple's container.
|
|
5
|
+
Project-URL: Documentation, https://imaged.readthedocs.io/
|
|
6
|
+
Project-URL: Homepage, https://github.com/Julian/imaged
|
|
7
|
+
Project-URL: Issues, https://github.com/Julian/imaged/issues/
|
|
8
|
+
Project-URL: Funding, https://github.com/sponsors/Julian
|
|
9
|
+
Project-URL: Source, https://github.com/Julian/imaged
|
|
10
|
+
Author-email: Julian Berman <Julian+imaged@GrayVines.com>
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: COPYING
|
|
13
|
+
Keywords: anyio,containers,docker,oci,podman
|
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
21
|
+
Requires-Python: >=3.13
|
|
22
|
+
Requires-Dist: anyio>=4
|
|
23
|
+
Requires-Dist: attrs>=22.2.0
|
|
24
|
+
Description-Content-Type: text/x-rst
|
|
25
|
+
|
|
26
|
+
==========
|
|
27
|
+
``imaged``
|
|
28
|
+
==========
|
|
29
|
+
|
|
30
|
+
|PyPI| |Pythons| |CI| |ReadTheDocs|
|
|
31
|
+
|
|
32
|
+
.. |PyPI| image:: https://img.shields.io/pypi/v/imaged.svg
|
|
33
|
+
:alt: PyPI version
|
|
34
|
+
:target: https://pypi.org/project/imaged/
|
|
35
|
+
|
|
36
|
+
.. |Pythons| image:: https://img.shields.io/pypi/pyversions/imaged.svg
|
|
37
|
+
:alt: Supported Python versions
|
|
38
|
+
:target: https://pypi.org/project/imaged/
|
|
39
|
+
|
|
40
|
+
.. |CI| image:: https://github.com/Julian/imaged/workflows/CI/badge.svg
|
|
41
|
+
:alt: Build status
|
|
42
|
+
:target: https://github.com/Julian/imaged/actions?query=workflow%3ACI
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
.. |ReadTheDocs| image:: https://readthedocs.org/projects/imaged/badge/?version=stable&style=flat
|
|
46
|
+
:alt: ReadTheDocs status
|
|
47
|
+
:target: https://imaged.readthedocs.io/en/stable/
|
|
48
|
+
|
|
49
|
+
``imaged`` gives you one interface to `docker <https://www.docker.com/>`_,
|
|
50
|
+
`podman <https://podman.io/>`_ and `Apple's container
|
|
51
|
+
<https://github.com/apple/container>`_.
|
|
52
|
+
Start a container, speak to its standard streams, and be told what went wrong
|
|
53
|
+
in terms you can branch on.
|
|
54
|
+
|
|
55
|
+
.. code-block:: python
|
|
56
|
+
|
|
57
|
+
from imaged import Engine
|
|
58
|
+
|
|
59
|
+
engine = Engine.detect()
|
|
60
|
+
|
|
61
|
+
id = await engine.create_pulling_if_needed("alpine", "cat")
|
|
62
|
+
async with engine.start(id) as session:
|
|
63
|
+
await session.send("hello")
|
|
64
|
+
assert await session.receive() == "hello"
|
|
65
|
+
|
|
66
|
+
Why
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
Every engine reports the same failure differently, and none of them do so
|
|
70
|
+
machine readably -- not even over their HTTP APIs.
|
|
71
|
+
Working out that an image simply doesn't exist otherwise means matching on
|
|
72
|
+
substrings of prose, separately for each engine.
|
|
73
|
+
``imaged`` does that once, and raises ``NoSuchImage``.
|
|
74
|
+
|
|
75
|
+
Each engine is driven via its command line interface, which is the only
|
|
76
|
+
mechanism all three have in common, as Apple's container has no HTTP API at
|
|
77
|
+
all.
|
|
78
|
+
Doing so also leaves the engine to demultiplex container stdio onto real
|
|
79
|
+
pipes, rather than leaving you to unpick its framing.
|
|
80
|
+
|
|
81
|
+
Containers get no networking unless you ask for it, on the theory that
|
|
82
|
+
something you are running should have to say so before it can phone home.
|
|
83
|
+
|
|
84
|
+
`anyio <https://anyio.readthedocs.io/>`_ is the only async dependency, so
|
|
85
|
+
this runs unmodified on both asyncio and trio.
|
|
86
|
+
Its own test suite runs on both, against all three engines.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
imaged/__init__.py,sha256=OjnLR0dEoFxw6QDk1vP5wZvAwYybxKLIXhvdlEsRi-Y,1153
|
|
2
|
+
imaged/_dialects.py,sha256=Vtzt8a6QeKNEdaaXyWk639nzSlf19gtBuA_sJoAz5rg,5218
|
|
3
|
+
imaged/_errors.py,sha256=L7gIDBsQ2ahyZRUI86cA_xKTpRPRFuaqBG2OnSmdeWU,2475
|
|
4
|
+
imaged/_subprocess.py,sha256=lehg2haNYM0WZGHbXA-y5vtnfBFHggvFE1TxVfG0rTY,12547
|
|
5
|
+
imaged/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
imaged/tests/test_engines.py,sha256=D0HaUbrC7cQRyXllPFSB-NnxzkwQw_eocfaw7ywkBUU,12554
|
|
7
|
+
imaged/tests/test_engines_for_real.py,sha256=_PNvRK5-xUMxQPjnLe7NS0DWmnCaiU59BLP42NtgqyY,4622
|
|
8
|
+
imaged-2026.7.2.dist-info/METADATA,sha256=L92dw7eIjAyj3gE08jNgp5NaX874YWufhSns-oYZCyE,3194
|
|
9
|
+
imaged-2026.7.2.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
|
|
10
|
+
imaged-2026.7.2.dist-info/licenses/COPYING,sha256=duktQE6d1JF-H9wu6teJU9N75pt5RjnoMCAsT6pavPo,1057
|
|
11
|
+
imaged-2026.7.2.dist-info/RECORD,,
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2026 Julian Berman
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|