Skip to content

Commit

Permalink
Merge pull request #86 from ryanlovett/format-arg
Browse files Browse the repository at this point in the history
Use different format arg when using `docker`.
  • Loading branch information
manics authored Mar 2, 2024
2 parents 8d31527 + bdc6f85 commit abad9ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
8 changes: 5 additions & 3 deletions repo2podman/podman.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,12 @@ class PodmanContainer(Container):
def __init__(self, cid, podman_executable="podman"):
self.id = cid
self._podman_executable = podman_executable
self.format_arg = "{{json .}}"
self.reload()

def reload(self):
lines = exec_podman(
["inspect", "--type", "container", "--format", "json", self.id],
["inspect", "--type", "container", "--format", self.format_arg, self.id],
capture="stdout",
exe=self._podman_executable,
)
Expand Down Expand Up @@ -374,6 +375,7 @@ class PodmanEngine(ContainerEngine):
def __init__(self, *, parent):
super().__init__(parent=parent)

self.format_arg = "{{json .}}"
lines = exec_podman(["info"], capture="stdout", exe=self.podman_executable)
log_debug(lines)

Expand Down Expand Up @@ -483,7 +485,7 @@ def remove_local(tags):
yield tag[10:]

lines = exec_podman(
["image", "list", "--format", "json"],
["image", "list", "--format", self.format_arg],
capture="stdout",
exe=self.podman_executable,
)
Expand All @@ -503,7 +505,7 @@ def remove_local(tags):

def inspect_image(self, image):
lines = exec_podman(
["inspect", "--type", "image", "--format", "json", image],
["inspect", "--type", "image", "--format", self.format_arg, image],
capture="stdout",
exe=self.podman_executable,
)
Expand Down
15 changes: 9 additions & 6 deletions tests/unit/test_podman.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def test_run():
c.remove()
with pytest.raises(PodmanCommandError) as exc:
c.reload()
assert "".join(exc.value.output).strip() == "[]"
# Podman 3 returns "[]" instead of ""
assert "".join(exc.value.output).strip() in ("[]", "")


def test_run_autoremove():
Expand All @@ -77,7 +78,8 @@ def test_run_autoremove():
sleep(3)
with pytest.raises(PodmanCommandError) as exc:
c.reload()
assert "".join(exc.value.output).strip() == "[]"
# Podman 3 returns "[]" instead of ""
assert "".join(exc.value.output).strip() in ("[]", "")


def test_run_detach_wait():
Expand All @@ -93,7 +95,8 @@ def test_run_detach_wait():
c.remove()
with pytest.raises(PodmanCommandError) as exc:
c.reload()
assert "".join(exc.value.output).strip() == "[]"
# Podman 3 returns "[]" instead of ""
assert "".join(exc.value.output).strip() in ("[]", "")


def test_run_detach_nostream():
Expand Down Expand Up @@ -146,8 +149,8 @@ def test_custom_executable(tmp_path):
exe = tmp_path.joinpath("custom_exe.sh")
with exe.open("w") as f:
f.write("#!/bin/sh\n")
f.write(f"echo $@ >> {log}\n")
f.write("exec podman $@\n")
f.write(f'echo "$@" >> {log}\n')
f.write('exec podman "$@"\n')
exe.chmod(0o755)

client = PodmanEngine(parent=None)
Expand All @@ -168,7 +171,7 @@ def test_custom_executable(tmp_path):
lines = f.read().splitlines()
assert lines == [
f"run --detach --log-level=debug {BUSYBOX} id -un",
f"inspect --type container --format json {cid}",
f"inspect --type container --format {{{{json .}}}} {cid}",
f"logs {cid}",
f"rm {cid}",
]

0 comments on commit abad9ca

Please sign in to comment.