mirror of
https://github.com/containers/podman.git
synced 2025-06-17 23:20:59 +08:00
Implement python podman create and start
- Added alias 'container()' to image model for CreateContainer() - Fixed return in containers_create.go to wrap error in varlink exception - Added a wait time to container.kill(), number of seconds to wait for the container to change state - Refactored cached_property() to use system libraries - Refactored tests to speed up performance Signed-off-by: Jhon Honce <jhonce@redhat.com> Closes: #821 Approved by: rhatdan
This commit is contained in:
@ -1,8 +1,8 @@
|
|||||||
"""Support files for podman API implementation."""
|
"""Support files for podman API implementation."""
|
||||||
import datetime
|
import datetime
|
||||||
import threading
|
import functools
|
||||||
from dateutil.parser import parse as dateutil_parse
|
|
||||||
|
|
||||||
|
from dateutil.parser import parse as dateutil_parse
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'cached_property',
|
'cached_property',
|
||||||
@ -11,37 +11,28 @@ __all__ = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class cached_property(object):
|
def cached_property(fn):
|
||||||
"""cached_property() - computed once per instance, cached as attribute.
|
"""Cache return to save future expense."""
|
||||||
|
return property(functools.lru_cache(maxsize=8)(fn))
|
||||||
|
|
||||||
Maybe this will make a future version of python.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, func):
|
# Cannot subclass collections.UserDict, breaks varlink
|
||||||
"""Construct context manager."""
|
class Config(dict):
|
||||||
self.func = func
|
"""Silently ignore None values, only take key once."""
|
||||||
self.__doc__ = func.__doc__
|
|
||||||
self.lock = threading.RLock()
|
|
||||||
|
|
||||||
def __get__(self, instance, cls=None):
|
def __init__(self, **kwargs):
|
||||||
"""Retrieve previous value, or call func()."""
|
"""Construct dictionary."""
|
||||||
if instance is None:
|
super(Config, self).__init__(kwargs)
|
||||||
return self
|
|
||||||
|
|
||||||
attrname = self.func.__name__
|
def __setitem__(self, key, value):
|
||||||
try:
|
"""Store unique, not None values."""
|
||||||
cache = instance.__dict__
|
if value is None:
|
||||||
except AttributeError: # objects with __slots__ have no __dict__
|
return
|
||||||
msg = ("No '__dict__' attribute on {}"
|
|
||||||
" instance to cache {} property.").format(
|
|
||||||
repr(type(instance).__name__), repr(attrname))
|
|
||||||
raise TypeError(msg) from None
|
|
||||||
|
|
||||||
with self.lock:
|
if super().__contains__(key):
|
||||||
# check if another thread filled cache while we awaited lock
|
return
|
||||||
if attrname not in cache:
|
|
||||||
cache[attrname] = self.func(instance)
|
super().__setitem__(key, value)
|
||||||
return cache[attrname]
|
|
||||||
|
|
||||||
|
|
||||||
def datetime_parse(string):
|
def datetime_parse(string):
|
||||||
|
@ -4,6 +4,7 @@ import functools
|
|||||||
import getpass
|
import getpass
|
||||||
import json
|
import json
|
||||||
import signal
|
import signal
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
class Container(collections.UserDict):
|
class Container(collections.UserDict):
|
||||||
@ -16,28 +17,34 @@ class Container(collections.UserDict):
|
|||||||
self._client = client
|
self._client = client
|
||||||
self._id = id
|
self._id = id
|
||||||
|
|
||||||
self._refresh(data)
|
with client() as podman:
|
||||||
|
self._refresh(podman)
|
||||||
|
|
||||||
assert self._id == self.data['id'],\
|
assert self._id == self.data['id'],\
|
||||||
'Requested container id({}) does not match store id({})'.format(
|
'Requested container id({}) does not match store id({})'.format(
|
||||||
self._id, self.id
|
self._id, self.id
|
||||||
)
|
)
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
"""Get items from parent dict + apply aliases."""
|
"""Get items from parent dict."""
|
||||||
if key == 'running':
|
|
||||||
key = 'containerrunning'
|
|
||||||
return super().__getitem__(key)
|
return super().__getitem__(key)
|
||||||
|
|
||||||
def _refresh(self, data):
|
def _refresh(self, podman):
|
||||||
super().update(data)
|
ctnr = podman.GetContainer(self._id)
|
||||||
for k, v in data.items():
|
super().update(ctnr['container'])
|
||||||
|
|
||||||
|
for k, v in self.data.items():
|
||||||
setattr(self, k, v)
|
setattr(self, k, v)
|
||||||
setattr(self, 'running', data['containerrunning'])
|
if 'containerrunning' in self.data:
|
||||||
|
setattr(self, 'running', self.data['containerrunning'])
|
||||||
|
self.data['running'] = self.data['containerrunning']
|
||||||
|
|
||||||
|
return self
|
||||||
|
|
||||||
def refresh(self):
|
def refresh(self):
|
||||||
"""Refresh status fields for this container."""
|
"""Refresh status fields for this container."""
|
||||||
ctnr = Containers(self._client).get(self.id)
|
with self._client() as podman:
|
||||||
self._refresh(ctnr)
|
return self._refresh(podman)
|
||||||
|
|
||||||
def attach(self, detach_key=None, no_stdin=False, sig_proxy=True):
|
def attach(self, detach_key=None, no_stdin=False, sig_proxy=True):
|
||||||
"""Attach to running container."""
|
"""Attach to running container."""
|
||||||
@ -49,8 +56,7 @@ class Container(collections.UserDict):
|
|||||||
"""Show processes running in container."""
|
"""Show processes running in container."""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
results = podman.ListContainerProcesses(self.id)
|
results = podman.ListContainerProcesses(self.id)
|
||||||
for p in results['container']:
|
yield from results['container']
|
||||||
yield p
|
|
||||||
|
|
||||||
def changes(self):
|
def changes(self):
|
||||||
"""Retrieve container changes."""
|
"""Retrieve container changes."""
|
||||||
@ -58,14 +64,24 @@ class Container(collections.UserDict):
|
|||||||
results = podman.ListContainerChanges(self.id)
|
results = podman.ListContainerChanges(self.id)
|
||||||
return results['container']
|
return results['container']
|
||||||
|
|
||||||
def kill(self, signal=signal.SIGTERM):
|
def kill(self, signal=signal.SIGTERM, wait=25):
|
||||||
"""Send signal to container, return id if successful.
|
"""Send signal to container.
|
||||||
|
|
||||||
default signal is signal.SIGTERM.
|
default signal is signal.SIGTERM.
|
||||||
|
wait n of seconds, 0 waits forever.
|
||||||
"""
|
"""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
results = podman.KillContainer(self.id, signal)
|
podman.KillContainer(self.id, signal)
|
||||||
return results['container']
|
timeout = time.time() + wait
|
||||||
|
while True:
|
||||||
|
self._refresh(podman)
|
||||||
|
if self.status != 'running':
|
||||||
|
return self
|
||||||
|
|
||||||
|
if wait and timeout < time.time():
|
||||||
|
raise TimeoutError()
|
||||||
|
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
def _lower_hook(self):
|
def _lower_hook(self):
|
||||||
"""Convert all keys to lowercase."""
|
"""Convert all keys to lowercase."""
|
||||||
@ -80,10 +96,8 @@ class Container(collections.UserDict):
|
|||||||
"""Retrieve details about containers."""
|
"""Retrieve details about containers."""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
results = podman.InspectContainer(self.id)
|
results = podman.InspectContainer(self.id)
|
||||||
obj = json.loads(
|
obj = json.loads(results['container'], object_hook=self._lower_hook())
|
||||||
results['container'], object_hook=self._lower_hook())
|
return collections.namedtuple('ContainerInspect', obj.keys())(**obj)
|
||||||
return collections.namedtuple('ContainerInspect',
|
|
||||||
obj.keys())(**obj)
|
|
||||||
|
|
||||||
def export(self, target):
|
def export(self, target):
|
||||||
"""Export container from store to tarball.
|
"""Export container from store to tarball.
|
||||||
@ -134,16 +148,16 @@ class Container(collections.UserDict):
|
|||||||
return results['image']
|
return results['image']
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""Start container, return id on success."""
|
"""Start container, return container on success."""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
results = podman.StartContainer(self.id)
|
podman.StartContainer(self.id)
|
||||||
return results['container']
|
return self._refresh(podman)
|
||||||
|
|
||||||
def stop(self, timeout=25):
|
def stop(self, timeout=25):
|
||||||
"""Stop container, return id on success."""
|
"""Stop container, return id on success."""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
results = podman.StopContainer(self.id, timeout)
|
podman.StopContainer(self.id, timeout)
|
||||||
return results['container']
|
return self._refresh(podman)
|
||||||
|
|
||||||
def remove(self, force=False):
|
def remove(self, force=False):
|
||||||
"""Remove container, return id on success.
|
"""Remove container, return id on success.
|
||||||
@ -157,8 +171,8 @@ class Container(collections.UserDict):
|
|||||||
def restart(self, timeout=25):
|
def restart(self, timeout=25):
|
||||||
"""Restart container with timeout, return id on success."""
|
"""Restart container with timeout, return id on success."""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
results = podman.RestartContainer(self.id, timeout)
|
podman.RestartContainer(self.id, timeout)
|
||||||
return results['container']
|
return self._refresh(podman)
|
||||||
|
|
||||||
def rename(self, target):
|
def rename(self, target):
|
||||||
"""Rename container, return id on success."""
|
"""Rename container, return id on success."""
|
||||||
@ -177,21 +191,20 @@ class Container(collections.UserDict):
|
|||||||
def pause(self):
|
def pause(self):
|
||||||
"""Pause container, return id on success."""
|
"""Pause container, return id on success."""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
results = podman.PauseContainer(self.id)
|
podman.PauseContainer(self.id)
|
||||||
return results['container']
|
return self._refresh(podman)
|
||||||
|
|
||||||
def unpause(self):
|
def unpause(self):
|
||||||
"""Unpause container, return id on success."""
|
"""Unpause container, return id on success."""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
results = podman.UnpauseContainer(self.id)
|
podman.UnpauseContainer(self.id)
|
||||||
return results['container']
|
return self._refresh(podman)
|
||||||
|
|
||||||
def update_container(self, *args, **kwargs):
|
def update_container(self, *args, **kwargs):
|
||||||
"""TODO: Update container..., return id on success."""
|
"""TODO: Update container..., return id on success."""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
results = podman.UpdateContainer()
|
podman.UpdateContainer()
|
||||||
self.refresh()
|
return self._refresh(podman)
|
||||||
return results['container']
|
|
||||||
|
|
||||||
def wait(self):
|
def wait(self):
|
||||||
"""Wait for container to finish, return 'returncode'."""
|
"""Wait for container to finish, return 'returncode'."""
|
||||||
@ -210,8 +223,7 @@ class Container(collections.UserDict):
|
|||||||
"""Retrieve container logs."""
|
"""Retrieve container logs."""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
results = podman.GetContainerLogs(self.id)
|
results = podman.GetContainerLogs(self.id)
|
||||||
for line in results:
|
yield from results
|
||||||
yield line
|
|
||||||
|
|
||||||
|
|
||||||
class Containers(object):
|
class Containers(object):
|
||||||
@ -234,15 +246,6 @@ class Containers(object):
|
|||||||
results = podman.DeleteStoppedContainers()
|
results = podman.DeleteStoppedContainers()
|
||||||
return results['containers']
|
return results['containers']
|
||||||
|
|
||||||
def create(self, *args, **kwargs):
|
|
||||||
"""Create container layer over the specified image.
|
|
||||||
|
|
||||||
See podman-create.1.md for kwargs details.
|
|
||||||
"""
|
|
||||||
with self._client() as podman:
|
|
||||||
results = podman.CreateContainer()
|
|
||||||
return results['id']
|
|
||||||
|
|
||||||
def get(self, id):
|
def get(self, id):
|
||||||
"""Retrieve container details from store."""
|
"""Retrieve container details from store."""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
|
@ -3,6 +3,9 @@ import collections
|
|||||||
import functools
|
import functools
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from . import Config
|
||||||
|
from .containers import Container
|
||||||
|
|
||||||
|
|
||||||
class Image(collections.UserDict):
|
class Image(collections.UserDict):
|
||||||
"""Model for an Image."""
|
"""Model for an Image."""
|
||||||
@ -25,8 +28,42 @@ class Image(collections.UserDict):
|
|||||||
"""Get items from parent dict."""
|
"""Get items from parent dict."""
|
||||||
return super().__getitem__(key)
|
return super().__getitem__(key)
|
||||||
|
|
||||||
|
def _split_token(self, values=None, sep='='):
|
||||||
|
mapped = {}
|
||||||
|
if values:
|
||||||
|
for var in values:
|
||||||
|
k, v = var.split(sep, 1)
|
||||||
|
mapped[k] = v
|
||||||
|
return mapped
|
||||||
|
|
||||||
|
def create(self, *args, **kwargs):
|
||||||
|
"""Create container from image.
|
||||||
|
|
||||||
|
Pulls defaults from image.inspect()
|
||||||
|
"""
|
||||||
|
# Inialize config from parameters
|
||||||
|
with self._client() as podman:
|
||||||
|
details = self.inspect()
|
||||||
|
|
||||||
|
# TODO: remove network settings once defaults implemented on service side
|
||||||
|
config = Config(image_id=self.id, **kwargs)
|
||||||
|
config['command'] = details.containerconfig['cmd']
|
||||||
|
config['env'] = self._split_token(details.containerconfig['env'])
|
||||||
|
config['image'] = details.repotags[0]
|
||||||
|
config['labels'] = self._split_token(details.labels)
|
||||||
|
config['net_mode'] = 'bridge'
|
||||||
|
config['network'] = 'bridge'
|
||||||
|
config['work_dir'] = '/tmp'
|
||||||
|
|
||||||
|
with self._client() as podman:
|
||||||
|
id = podman.CreateContainer(config)['container']
|
||||||
|
cntr = podman.GetContainer(id)
|
||||||
|
return Container(self._client, id, cntr['container'])
|
||||||
|
|
||||||
|
container = create
|
||||||
|
|
||||||
def export(self, dest, compressed=False):
|
def export(self, dest, compressed=False):
|
||||||
"""Write image to dest, return True on success."""
|
"""Write image to dest, return id on success."""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
results = podman.ExportImage(self.id, dest, compressed)
|
results = podman.ExportImage(self.id, dest, compressed)
|
||||||
return results['image']
|
return results['image']
|
||||||
@ -89,12 +126,6 @@ class Images(object):
|
|||||||
for img in results['images']:
|
for img in results['images']:
|
||||||
yield Image(self._client, img['id'], img)
|
yield Image(self._client, img['id'], img)
|
||||||
|
|
||||||
def create(self, *args, **kwargs):
|
|
||||||
"""Create image from configuration."""
|
|
||||||
with self._client() as podman:
|
|
||||||
results = podman.CreateImage()
|
|
||||||
return results['image']
|
|
||||||
|
|
||||||
def build(self, *args, **kwargs):
|
def build(self, *args, **kwargs):
|
||||||
"""Build container from image.
|
"""Build container from image.
|
||||||
|
|
||||||
@ -125,9 +156,9 @@ class Images(object):
|
|||||||
def search(self, id, limit=25):
|
def search(self, id, limit=25):
|
||||||
"""Search registries for id."""
|
"""Search registries for id."""
|
||||||
with self._client() as podman:
|
with self._client() as podman:
|
||||||
results = podman.SearchImage(id)
|
results = podman.SearchImage(id, limit)
|
||||||
for img in results['images']:
|
for img in results['images']:
|
||||||
yield img
|
yield collections.namedtuple('ImageSearch', img.keys())(**img)
|
||||||
|
|
||||||
def get(self, id):
|
def get(self, id):
|
||||||
"""Get Image from id."""
|
"""Get Image from id."""
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
import signal
|
||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
from test.podman_testcase import PodmanTestCase
|
from test.podman_testcase import PodmanTestCase
|
||||||
@ -21,42 +22,39 @@ class TestContainers(PodmanTestCase):
|
|||||||
self.host = os.environ['PODMAN_HOST']
|
self.host = os.environ['PODMAN_HOST']
|
||||||
|
|
||||||
self.pclient = podman.Client(self.host)
|
self.pclient = podman.Client(self.host)
|
||||||
self.ctns = self.loadCache()
|
self.loadCache()
|
||||||
# TODO: Change to start() when Implemented
|
|
||||||
self.alpine_ctnr.restart()
|
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def loadCache(self):
|
def loadCache(self):
|
||||||
with podman.Client(self.host) as pclient:
|
self.containers = list(self.pclient.containers.list())
|
||||||
self.ctns = list(pclient.containers.list())
|
|
||||||
|
|
||||||
self.alpine_ctnr = next(
|
self.alpine_ctnr = next(
|
||||||
iter([c for c in self.ctns if 'alpine' in c['image']] or []), None)
|
iter([c for c in self.containers if 'alpine' in c['image']] or []),
|
||||||
return self.ctns
|
None)
|
||||||
|
|
||||||
|
if self.alpine_ctnr and self.alpine_ctnr.status != 'running':
|
||||||
|
self.alpine_ctnr.start()
|
||||||
|
|
||||||
def test_list(self):
|
def test_list(self):
|
||||||
actual = self.loadCache()
|
self.assertGreaterEqual(len(self.containers), 2)
|
||||||
self.assertGreaterEqual(len(actual), 2)
|
|
||||||
self.assertIsNotNone(self.alpine_ctnr)
|
self.assertIsNotNone(self.alpine_ctnr)
|
||||||
self.assertIn('alpine', self.alpine_ctnr.image)
|
self.assertIn('alpine', self.alpine_ctnr.image)
|
||||||
|
|
||||||
def test_delete_stopped(self):
|
def test_delete_stopped(self):
|
||||||
before = self.loadCache()
|
before = len(self.containers)
|
||||||
self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.stop())
|
|
||||||
|
self.alpine_ctnr.stop()
|
||||||
|
target = self.alpine_ctnr.id
|
||||||
actual = self.pclient.containers.delete_stopped()
|
actual = self.pclient.containers.delete_stopped()
|
||||||
self.assertIn(self.alpine_ctnr.id, actual)
|
self.assertIn(target, actual)
|
||||||
after = self.loadCache()
|
|
||||||
|
|
||||||
self.assertLess(len(after), len(before))
|
|
||||||
TestContainers.setUpClass()
|
|
||||||
self.loadCache()
|
self.loadCache()
|
||||||
|
after = len(self.containers)
|
||||||
|
|
||||||
@unittest.skip('TODO: Reinstate with create PR')
|
self.assertLess(after, before)
|
||||||
def test_create(self):
|
TestContainers.setUpClass()
|
||||||
with self.assertRaisesNotImplemented():
|
|
||||||
self.pclient.containers.create()
|
|
||||||
|
|
||||||
def test_get(self):
|
def test_get(self):
|
||||||
actual = self.pclient.containers.get(self.alpine_ctnr.id)
|
actual = self.pclient.containers.get(self.alpine_ctnr.id)
|
||||||
@ -75,19 +73,16 @@ class TestContainers(PodmanTestCase):
|
|||||||
self.assertGreaterEqual(len(actual), 2)
|
self.assertGreaterEqual(len(actual), 2)
|
||||||
|
|
||||||
def test_start_stop_wait(self):
|
def test_start_stop_wait(self):
|
||||||
self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.stop())
|
ctnr = self.alpine_ctnr.stop()
|
||||||
self.alpine_ctnr.refresh()
|
self.assertFalse(ctnr['running'])
|
||||||
self.assertFalse(self.alpine_ctnr['running'])
|
|
||||||
|
|
||||||
self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.restart())
|
ctnr.start()
|
||||||
self.alpine_ctnr.refresh()
|
self.assertTrue(ctnr.running)
|
||||||
self.assertTrue(self.alpine_ctnr.running)
|
|
||||||
|
|
||||||
self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.stop())
|
ctnr.stop()
|
||||||
self.alpine_ctnr.refresh()
|
self.assertFalse(ctnr['containerrunning'])
|
||||||
self.assertFalse(self.alpine_ctnr['containerrunning'])
|
|
||||||
|
|
||||||
actual = self.alpine_ctnr.wait()
|
actual = ctnr.wait()
|
||||||
self.assertGreaterEqual(actual, 0)
|
self.assertGreaterEqual(actual, 0)
|
||||||
|
|
||||||
def test_changes(self):
|
def test_changes(self):
|
||||||
@ -104,11 +99,8 @@ class TestContainers(PodmanTestCase):
|
|||||||
|
|
||||||
def test_kill(self):
|
def test_kill(self):
|
||||||
self.assertTrue(self.alpine_ctnr.running)
|
self.assertTrue(self.alpine_ctnr.running)
|
||||||
self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.kill(9))
|
ctnr = self.alpine_ctnr.kill(signal.SIGKILL)
|
||||||
time.sleep(2)
|
self.assertFalse(ctnr.running)
|
||||||
|
|
||||||
self.alpine_ctnr.refresh()
|
|
||||||
self.assertFalse(self.alpine_ctnr.running)
|
|
||||||
|
|
||||||
def test_inspect(self):
|
def test_inspect(self):
|
||||||
actual = self.alpine_ctnr.inspect()
|
actual = self.alpine_ctnr.inspect()
|
||||||
@ -129,21 +121,21 @@ class TestContainers(PodmanTestCase):
|
|||||||
def test_commit(self):
|
def test_commit(self):
|
||||||
# TODO: Test for STOPSIGNAL when supported by OCI
|
# TODO: Test for STOPSIGNAL when supported by OCI
|
||||||
# TODO: Test for message when supported by OCI
|
# TODO: Test for message when supported by OCI
|
||||||
# TODO: Test for EXPOSE when issue#795 fixed
|
details = self.pclient.images.get(
|
||||||
# 'EXPOSE=8888/tcp, 8888/udp'
|
self.alpine_ctnr.inspect().image).inspect()
|
||||||
|
changes = ['ENV=' + i for i in details.containerconfig['env']]
|
||||||
|
changes.append('CMD=/usr/bin/zsh')
|
||||||
|
changes.append('ENTRYPOINT=/bin/sh date')
|
||||||
|
changes.append('ENV=TEST=test_containers.TestContainers.test_commit')
|
||||||
|
changes.append('EXPOSE=80')
|
||||||
|
changes.append('EXPOSE=8888')
|
||||||
|
changes.append('LABEL=unittest=test_commit')
|
||||||
|
changes.append('USER=bozo:circus')
|
||||||
|
changes.append('VOLUME=/data')
|
||||||
|
changes.append('WORKDIR=/data/application')
|
||||||
|
|
||||||
id = self.alpine_ctnr.commit(
|
id = self.alpine_ctnr.commit(
|
||||||
'alpine3',
|
'alpine3', author='Bozo the clown', changes=changes, pause=True)
|
||||||
author='Bozo the clown',
|
|
||||||
changes=[
|
|
||||||
'CMD=/usr/bin/zsh',
|
|
||||||
'ENTRYPOINT=/bin/sh date',
|
|
||||||
'ENV=TEST=test_containers.TestContainers.test_commit',
|
|
||||||
'LABEL=unittest=test_commit',
|
|
||||||
'USER=bozo:circus',
|
|
||||||
'VOLUME=/data',
|
|
||||||
'WORKDIR=/data/application',
|
|
||||||
],
|
|
||||||
pause=True)
|
|
||||||
img = self.pclient.images.get(id)
|
img = self.pclient.images.get(id)
|
||||||
self.assertIsNotNone(img)
|
self.assertIsNotNone(img)
|
||||||
|
|
||||||
@ -152,12 +144,14 @@ class TestContainers(PodmanTestCase):
|
|||||||
self.assertListEqual(['/usr/bin/zsh'], details.containerconfig['cmd'])
|
self.assertListEqual(['/usr/bin/zsh'], details.containerconfig['cmd'])
|
||||||
self.assertListEqual(['/bin/sh date'],
|
self.assertListEqual(['/bin/sh date'],
|
||||||
details.containerconfig['entrypoint'])
|
details.containerconfig['entrypoint'])
|
||||||
self.assertListEqual(
|
self.assertIn('TEST=test_containers.TestContainers.test_commit',
|
||||||
['TEST=test_containers.TestContainers.test_commit'],
|
|
||||||
details.containerconfig['env'])
|
details.containerconfig['env'])
|
||||||
# self.assertDictEqual({
|
self.assertTrue(
|
||||||
# '8888/tcp': {}
|
[e for e in details.containerconfig['env'] if 'PATH=' in e])
|
||||||
# }, details.containerconfig['exposedports'])
|
self.assertDictEqual({
|
||||||
|
'80': {},
|
||||||
|
'8888': {},
|
||||||
|
}, details.containerconfig['exposedports'])
|
||||||
self.assertDictEqual({'unittest': 'test_commit'}, details.labels)
|
self.assertDictEqual({'unittest': 'test_commit'}, details.labels)
|
||||||
self.assertEqual('bozo:circus', details.containerconfig['user'])
|
self.assertEqual('bozo:circus', details.containerconfig['user'])
|
||||||
self.assertEqual({'/data': {}}, details.containerconfig['volumes'])
|
self.assertEqual({'/data': {}}, details.containerconfig['volumes'])
|
||||||
@ -165,28 +159,27 @@ class TestContainers(PodmanTestCase):
|
|||||||
details.containerconfig['workingdir'])
|
details.containerconfig['workingdir'])
|
||||||
|
|
||||||
def test_remove(self):
|
def test_remove(self):
|
||||||
before = self.loadCache()
|
before = len(self.containers)
|
||||||
|
|
||||||
with self.assertRaises(podman.ErrorOccurred):
|
with self.assertRaises(podman.ErrorOccurred):
|
||||||
self.alpine_ctnr.remove()
|
self.alpine_ctnr.remove()
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.alpine_ctnr.id, self.alpine_ctnr.remove(force=True))
|
self.alpine_ctnr.id, self.alpine_ctnr.remove(force=True))
|
||||||
after = self.loadCache()
|
|
||||||
|
|
||||||
self.assertLess(len(after), len(before))
|
|
||||||
TestContainers.setUpClass()
|
|
||||||
self.loadCache()
|
self.loadCache()
|
||||||
|
after = len(self.containers)
|
||||||
|
|
||||||
|
self.assertLess(after, before)
|
||||||
|
TestContainers.setUpClass()
|
||||||
|
|
||||||
def test_restart(self):
|
def test_restart(self):
|
||||||
self.assertTrue(self.alpine_ctnr.running)
|
self.assertTrue(self.alpine_ctnr.running)
|
||||||
before = self.alpine_ctnr.runningfor
|
before = self.alpine_ctnr.runningfor
|
||||||
|
|
||||||
self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.restart())
|
ctnr = self.alpine_ctnr.restart()
|
||||||
|
self.assertTrue(ctnr.running)
|
||||||
|
|
||||||
self.alpine_ctnr.refresh()
|
|
||||||
after = self.alpine_ctnr.runningfor
|
after = self.alpine_ctnr.runningfor
|
||||||
self.assertTrue(self.alpine_ctnr.running)
|
|
||||||
|
|
||||||
# TODO: restore check when restart zeros counter
|
# TODO: restore check when restart zeros counter
|
||||||
# self.assertLess(after, before)
|
# self.assertLess(after, before)
|
||||||
@ -202,22 +195,22 @@ class TestContainers(PodmanTestCase):
|
|||||||
def test_pause_unpause(self):
|
def test_pause_unpause(self):
|
||||||
self.assertTrue(self.alpine_ctnr.running)
|
self.assertTrue(self.alpine_ctnr.running)
|
||||||
|
|
||||||
self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.pause())
|
ctnr = self.alpine_ctnr.pause()
|
||||||
self.alpine_ctnr.refresh()
|
self.assertEqual(ctnr.status, 'paused')
|
||||||
self.assertFalse(self.alpine_ctnr.running)
|
|
||||||
|
|
||||||
self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.unpause())
|
ctnr = self.alpine_ctnr.unpause()
|
||||||
self.alpine_ctnr.refresh()
|
self.assertTrue(ctnr.running)
|
||||||
self.assertTrue(self.alpine_ctnr.running)
|
self.assertTrue(ctnr.status, 'running')
|
||||||
|
|
||||||
def test_stats(self):
|
def test_stats(self):
|
||||||
self.alpine_ctnr.restart()
|
self.assertTrue(self.alpine_ctnr.running)
|
||||||
|
|
||||||
actual = self.alpine_ctnr.stats()
|
actual = self.alpine_ctnr.stats()
|
||||||
self.assertEqual(self.alpine_ctnr.id, actual.id)
|
self.assertEqual(self.alpine_ctnr.id, actual.id)
|
||||||
self.assertEqual(self.alpine_ctnr.names, actual.name)
|
self.assertEqual(self.alpine_ctnr.names, actual.name)
|
||||||
|
|
||||||
def test_logs(self):
|
def test_logs(self):
|
||||||
self.alpine_ctnr.restart()
|
self.assertTrue(self.alpine_ctnr.running)
|
||||||
actual = list(self.alpine_ctnr.logs())
|
actual = list(self.alpine_ctnr.logs())
|
||||||
self.assertIsNotNone(actual)
|
self.assertIsNotNone(actual)
|
||||||
|
|
||||||
|
@ -46,8 +46,11 @@ class TestImages(PodmanTestCase):
|
|||||||
self.pclient.images.build()
|
self.pclient.images.build()
|
||||||
|
|
||||||
def test_create(self):
|
def test_create(self):
|
||||||
with self.assertRaisesNotImplemented():
|
actual = self.alpine_image.container()
|
||||||
self.pclient.images.create()
|
self.assertIsNotNone(actual)
|
||||||
|
self.assertEqual(actual.status, 'configured')
|
||||||
|
cntr = actual.start()
|
||||||
|
self.assertIn(cntr.status, ['running', 'exited'])
|
||||||
|
|
||||||
def test_export(self):
|
def test_export(self):
|
||||||
path = os.path.join(self.tmpdir, 'alpine_export.tar')
|
path = os.path.join(self.tmpdir, 'alpine_export.tar')
|
||||||
@ -58,9 +61,7 @@ class TestImages(PodmanTestCase):
|
|||||||
self.assertTrue(os.path.isfile(path))
|
self.assertTrue(os.path.isfile(path))
|
||||||
|
|
||||||
def test_history(self):
|
def test_history(self):
|
||||||
count = 0
|
for count, record in enumerate(self.alpine_image.history()):
|
||||||
for record in self.alpine_image.history():
|
|
||||||
count += 1
|
|
||||||
self.assertEqual(record.id, self.alpine_image.id)
|
self.assertEqual(record.id, self.alpine_image.id)
|
||||||
self.assertGreater(count, 0)
|
self.assertGreater(count, 0)
|
||||||
|
|
||||||
@ -89,13 +90,6 @@ class TestImages(PodmanTestCase):
|
|||||||
with self.assertRaises(podman.ErrorOccurred):
|
with self.assertRaises(podman.ErrorOccurred):
|
||||||
self.alpine_image.remove()
|
self.alpine_image.remove()
|
||||||
|
|
||||||
# TODO: remove this block once force=True works
|
|
||||||
with podman.Client(self.host) as pclient:
|
|
||||||
for ctnr in pclient.containers.list():
|
|
||||||
if 'alpine' in ctnr.image:
|
|
||||||
ctnr.stop()
|
|
||||||
ctnr.remove()
|
|
||||||
|
|
||||||
actual = self.alpine_image.remove(force=True)
|
actual = self.alpine_image.remove(force=True)
|
||||||
self.assertEqual(self.alpine_image.id, actual)
|
self.assertEqual(self.alpine_image.id, actual)
|
||||||
after = self.loadCache()
|
after = self.loadCache()
|
||||||
@ -136,11 +130,11 @@ class TestImages(PodmanTestCase):
|
|||||||
|
|
||||||
def test_search(self):
|
def test_search(self):
|
||||||
actual = self.pclient.images.search('alpine', 25)
|
actual = self.pclient.images.search('alpine', 25)
|
||||||
names, lengths = itertools.tee(actual)
|
names, length = itertools.tee(actual)
|
||||||
|
|
||||||
for img in names:
|
for img in names:
|
||||||
self.assertIn('alpine', img['name'])
|
self.assertIn('alpine', img.name)
|
||||||
self.assertTrue(0 < len(list(lengths)) <= 25)
|
self.assertTrue(0 < len(list(length)) <= 25)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -40,7 +40,7 @@ fi
|
|||||||
export PATH=../../bin:$PATH
|
export PATH=../../bin:$PATH
|
||||||
|
|
||||||
function showlog {
|
function showlog {
|
||||||
[ -s "$1" ] && (echo $1 =====; cat "$1")
|
[ -s "$1" ] && (echo $1 =====; cat "$1"; echo)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Need a location to store the podman socket
|
# Need a location to store the podman socket
|
||||||
@ -109,8 +109,9 @@ else
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
set +x
|
set +x
|
||||||
pkill podman
|
pkill -9 podman
|
||||||
pkill -9 conmon
|
pkill -9 conmon
|
||||||
|
|
||||||
|
showlog /tmp/test_runner.output
|
||||||
showlog /tmp/alpine.log
|
showlog /tmp/alpine.log
|
||||||
showlog /tmp/busybox.log
|
showlog /tmp/busybox.log
|
||||||
|
@ -41,7 +41,7 @@ func (i *LibpodAPI) CreateContainer(call ioprojectatomicpodman.VarlinkCall, conf
|
|||||||
|
|
||||||
newImage, err := runtime.ImageRuntime().New(ctx, config.Image, rtc.SignaturePolicyPath, "", os.Stderr, nil, image.SigningOptions{}, false, false)
|
newImage, err := runtime.ImageRuntime().New(ctx, config.Image, rtc.SignaturePolicyPath, "", os.Stderr, nil, image.SigningOptions{}, false, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return call.ReplyErrorOccurred(err.Error())
|
||||||
}
|
}
|
||||||
data, err := newImage.Inspect(ctx)
|
data, err := newImage.Inspect(ctx)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user