summaryrefslogtreecommitdiff
path: root/contrib/python/test
diff options
context:
space:
mode:
authorJhon Honce <jhonce@redhat.com>2018-05-14 18:01:08 -0700
committerAtomic Bot <atomic-devel@projectatomic.io>2018-05-16 14:01:10 +0000
commit1aaf8df5be32d755a3f72f9259c66c70fbf850d8 (patch)
tree6e573fc7ae988e36d6d94973cec238aee72feaad /contrib/python/test
parentc7bc7580a6a9faf6a3159d6c17ff1dfb3710e318 (diff)
downloadpodman-1aaf8df5be32d755a3f72f9259c66c70fbf850d8.tar.gz
podman-1aaf8df5be32d755a3f72f9259c66c70fbf850d8.tar.bz2
podman-1aaf8df5be32d755a3f72f9259c66c70fbf850d8.zip
Refactor libpod python varlink bindings
- More pythonic - Leverage context managers to help with socket leaks - Add system unittest's - Add image unittest's - Add container unittest's - Add models for system, containers and images, and their collections - Add helper functions for datetime parsing/formatting - GetInfo() implemented - Add support for setuptools - Update documentation - Support for Python 3.4-3.6 Signed-off-by: Jhon Honce <jhonce@redhat.com> Closes: #748 Approved by: baude
Diffstat (limited to 'contrib/python/test')
-rw-r--r--contrib/python/test/__init__.py0
-rw-r--r--contrib/python/test/podman_testcase.py106
-rw-r--r--contrib/python/test/test_containers.py186
-rw-r--r--contrib/python/test/test_images.py151
-rw-r--r--contrib/python/test/test_libs.py46
-rwxr-xr-xcontrib/python/test/test_runner.sh116
-rw-r--r--contrib/python/test/test_system.py49
7 files changed, 654 insertions, 0 deletions
diff --git a/contrib/python/test/__init__.py b/contrib/python/test/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/contrib/python/test/__init__.py
diff --git a/contrib/python/test/podman_testcase.py b/contrib/python/test/podman_testcase.py
new file mode 100644
index 000000000..fc99f26ce
--- /dev/null
+++ b/contrib/python/test/podman_testcase.py
@@ -0,0 +1,106 @@
+import contextlib
+import functools
+import itertools
+import os
+import subprocess
+import time
+import unittest
+
+from varlink import VarlinkError
+
+MethodNotImplemented = 'org.varlink.service.MethodNotImplemented'
+
+
+class PodmanTestCase(unittest.TestCase):
+ """Hide the sausage making of initializing storage."""
+
+ @classmethod
+ def setUpClass(cls):
+ if hasattr(PodmanTestCase, 'alpine_process'):
+ PodmanTestCase.tearDownClass()
+
+ def run_cmd(*args):
+ cmd = list(itertools.chain(*args))
+ try:
+ pid = subprocess.Popen(
+ cmd,
+ close_fds=True,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ out, err = pid.communicate()
+ except OSError as e:
+ print('{}: {}({})'.format(cmd, e.strerror, e.returncode))
+ except ValueError as e:
+ print('{}: {}'.format(cmd, e.message))
+ raise
+ else:
+ return out.strip()
+
+ tmpdir = os.environ.get('TMPDIR', '/tmp')
+ podman_args = [
+ '--storage-driver=vfs',
+ '--root={}/crio'.format(tmpdir),
+ '--runroot={}/crio-run'.format(tmpdir),
+ '--cni-config-dir={}/cni/net.d'.format(tmpdir),
+ ]
+
+ run_podman = functools.partial(run_cmd, ['podman'], podman_args)
+
+ id = run_podman(['pull', 'alpine'])
+ setattr(PodmanTestCase, 'alpine_id', id)
+
+ run_podman(['pull', 'busybox'])
+ run_podman(['images'])
+
+ run_cmd(['rm', '-f', '{}/alpine_gold.tar'.format(tmpdir)])
+ run_podman([
+ 'save', '--output', '{}/alpine_gold.tar'.format(tmpdir), 'alpine'
+ ])
+
+ PodmanTestCase.alpine_log = open(
+ os.path.join('/tmp/', 'alpine.log'), 'w')
+
+ cmd = ['podman']
+ cmd.extend(podman_args)
+ cmd.extend(['run', '-d', 'alpine', 'sleep', '500'])
+ PodmanTestCase.alpine_process = subprocess.Popen(
+ cmd,
+ stdout=PodmanTestCase.alpine_log,
+ stderr=subprocess.STDOUT,
+ )
+
+ PodmanTestCase.busybox_log = open(
+ os.path.join('/tmp/', 'busybox.log'), 'w')
+
+ cmd = ['podman']
+ cmd.extend(podman_args)
+ cmd.extend(['create', 'busybox'])
+ PodmanTestCase.busybox_process = subprocess.Popen(
+ cmd,
+ stdout=PodmanTestCase.busybox_log,
+ stderr=subprocess.STDOUT,
+ )
+ # give podman time to start ctnr
+ time.sleep(2)
+
+ # Close our handle of file
+ PodmanTestCase.alpine_log.close()
+ PodmanTestCase.busybox_log.close()
+
+ @classmethod
+ def tearDownClass(cls):
+ try:
+ PodmanTestCase.alpine_process.kill()
+ assert 0 == PodmanTestCase.alpine_process.wait(500)
+ delattr(PodmanTestCase, 'alpine_process')
+
+ PodmanTestCase.busybox_process.kill()
+ assert 0 == PodmanTestCase.busybox_process.wait(500)
+ except Exception as e:
+ print('Exception: {}'.format(e))
+ raise
+
+ @contextlib.contextmanager
+ def assertRaisesNotImplemented(self):
+ with self.assertRaisesRegex(VarlinkError, MethodNotImplemented):
+ yield
diff --git a/contrib/python/test/test_containers.py b/contrib/python/test/test_containers.py
new file mode 100644
index 000000000..9f6123e05
--- /dev/null
+++ b/contrib/python/test/test_containers.py
@@ -0,0 +1,186 @@
+import os
+import time
+import unittest
+from test.podman_testcase import PodmanTestCase
+
+import podman
+from podman import datetime_parse
+
+
+class TestContainers(PodmanTestCase):
+ @classmethod
+ def setUpClass(cls):
+ super().setUpClass()
+
+ @classmethod
+ def tearDownClass(cls):
+ super().tearDownClass()
+
+ def setUp(self):
+ self.tmpdir = os.environ['TMPDIR']
+ self.host = os.environ['PODMAN_HOST']
+
+ self.pclient = podman.Client(self.host)
+ self.ctns = self.loadCache()
+ # TODO: Change to start() when Implemented
+ self.alpine_ctnr.restart()
+
+ def tearDown(self):
+ pass
+
+ def loadCache(self):
+ with podman.Client(self.host) as pclient:
+ self.ctns = list(pclient.containers.list())
+
+ self.alpine_ctnr = next(
+ iter([c for c in self.ctns if 'alpine' in c['image']] or []), None)
+ return self.ctns
+
+ def test_list(self):
+ actual = self.loadCache()
+ self.assertGreaterEqual(len(actual), 2)
+ self.assertIsNotNone(self.alpine_ctnr)
+ self.assertIn('alpine', self.alpine_ctnr.image)
+
+ def test_delete_stopped(self):
+ before = self.loadCache()
+ self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.stop())
+ actual = self.pclient.containers.delete_stopped()
+ self.assertIn(self.alpine_ctnr.id, actual)
+ after = self.loadCache()
+
+ self.assertLess(len(after), len(before))
+ TestContainers.setUpClass()
+ self.loadCache()
+
+ def test_create(self):
+ with self.assertRaisesNotImplemented():
+ self.pclient.containers.create()
+
+ def test_get(self):
+ actual = self.pclient.containers.get(self.alpine_ctnr.id)
+ for k in ['id', 'status', 'ports']:
+ self.assertEqual(actual[k], self.alpine_ctnr[k])
+
+ with self.assertRaises(podman.ContainerNotFound):
+ self.pclient.containers.get("bozo")
+
+ def test_attach(self):
+ with self.assertRaisesNotImplemented():
+ self.alpine_ctnr.attach()
+
+ def test_processes(self):
+ actual = list(self.alpine_ctnr.processes())
+ self.assertGreaterEqual(len(actual), 2)
+
+ def test_start_stop_wait(self):
+ self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.stop())
+ self.alpine_ctnr.refresh()
+ self.assertFalse(self.alpine_ctnr['running'])
+
+ self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.restart())
+ self.alpine_ctnr.refresh()
+ self.assertTrue(self.alpine_ctnr.running)
+
+ self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.stop())
+ self.alpine_ctnr.refresh()
+ self.assertFalse(self.alpine_ctnr['containerrunning'])
+
+ actual = self.alpine_ctnr.wait()
+ self.assertEqual(0, actual)
+
+ def test_changes(self):
+ actual = self.alpine_ctnr.changes()
+
+ self.assertListEqual(
+ sorted(['changed', 'added', 'deleted']), sorted(
+ list(actual.keys())))
+
+ # TODO: brittle, depends on knowing history of ctnr
+ self.assertGreaterEqual(len(actual['changed']), 2)
+ self.assertGreaterEqual(len(actual['added']), 3)
+ self.assertEqual(len(actual['deleted']), 0)
+
+ def test_kill(self):
+ self.assertTrue(self.alpine_ctnr.running)
+ self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.kill(9))
+ time.sleep(2)
+
+ self.alpine_ctnr.refresh()
+ self.assertFalse(self.alpine_ctnr.running)
+
+ def test_inspect(self):
+ actual = self.alpine_ctnr.inspect()
+ self.assertEqual(actual.id, self.alpine_ctnr.id)
+ self.assertEqual(
+ datetime_parse(actual.created),
+ datetime_parse(self.alpine_ctnr.createdat))
+
+ def test_export(self):
+ target = os.path.join(self.tmpdir, 'alpine_export_ctnr.tar')
+
+ actual = self.alpine_ctnr.export(target)
+ self.assertEqual(actual, target)
+ self.assertTrue(os.path.isfile(target))
+ self.assertGreater(os.path.getsize(target), 0)
+
+ def test_remove(self):
+ before = self.loadCache()
+
+ with self.assertRaises(podman.ErrorOccurred):
+ self.alpine_ctnr.remove()
+
+ self.assertEqual(
+ self.alpine_ctnr.id, self.alpine_ctnr.remove(force=True))
+ after = self.loadCache()
+
+ self.assertLess(len(after), len(before))
+ TestContainers.setUpClass()
+ self.loadCache()
+
+ def test_restart(self):
+ self.assertTrue(self.alpine_ctnr.running)
+ before = self.alpine_ctnr.runningfor
+
+ self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.restart())
+
+ self.alpine_ctnr.refresh()
+ after = self.alpine_ctnr.runningfor
+ self.assertTrue(self.alpine_ctnr.running)
+
+ # TODO: restore check when restart zeros counter
+ # self.assertLess(after, before)
+
+ def test_rename(self):
+ with self.assertRaisesNotImplemented():
+ self.alpine_ctnr.rename('new_alpine')
+
+ def test_resize_tty(self):
+ with self.assertRaisesNotImplemented():
+ self.alpine_ctnr.resize_tty(132, 43)
+
+ def test_pause_unpause(self):
+ self.assertTrue(self.alpine_ctnr.running)
+
+ self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.pause())
+ self.alpine_ctnr.refresh()
+ self.assertFalse(self.alpine_ctnr.running)
+
+ self.assertEqual(self.alpine_ctnr.id, self.alpine_ctnr.unpause())
+ self.alpine_ctnr.refresh()
+ self.assertTrue(self.alpine_ctnr.running)
+
+ def test_stats(self):
+ self.alpine_ctnr.restart()
+ actual = self.alpine_ctnr.stats()
+ self.assertEqual(self.alpine_ctnr.id, actual.id)
+ self.assertEqual(self.alpine_ctnr.names, actual.name)
+
+ def test_logs(self):
+ self.alpine_ctnr.restart()
+ actual = list(self.alpine_ctnr.logs())
+ self.assertIsNotNone(actual)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/contrib/python/test/test_images.py b/contrib/python/test/test_images.py
new file mode 100644
index 000000000..7195c06d5
--- /dev/null
+++ b/contrib/python/test/test_images.py
@@ -0,0 +1,151 @@
+import itertools
+import os
+import unittest
+from test.podman_testcase import PodmanTestCase
+
+import podman
+
+
+class TestImages(PodmanTestCase):
+ @classmethod
+ def setUpClass(cls):
+ super().setUpClass()
+
+ @classmethod
+ def tearDownClass(cls):
+ super().tearDownClass()
+
+ def setUp(self):
+ self.tmpdir = os.environ['TMPDIR']
+ self.host = os.environ['PODMAN_HOST']
+
+ self.pclient = podman.Client(self.host)
+ self.images = self.loadCache()
+
+ def tearDown(self):
+ pass
+
+ def loadCache(self):
+ with podman.Client(self.host) as pclient:
+ self.images = list(pclient.images.list())
+
+ self.alpine_image = next(
+ iter([
+ i for i in self.images
+ if 'docker.io/library/alpine:latest' in i['repoTags']
+ ] or []), None)
+ return self.images
+
+ def test_list(self):
+ actual = self.loadCache()
+ self.assertGreaterEqual(len(actual), 2)
+ self.assertIsNotNone(self.alpine_image)
+
+ def test_build(self):
+ with self.assertRaisesNotImplemented():
+ self.pclient.images.build()
+
+ def test_create(self):
+ with self.assertRaisesNotImplemented():
+ self.pclient.images.create()
+
+ def test_create_from(self):
+ with self.assertRaisesNotImplemented():
+ self.pclient.images.create_from()
+
+ def test_export(self):
+ path = os.path.join(self.tmpdir, 'alpine_export.tar')
+ target = 'oci-archive:{}:latest'.format(path)
+
+ actual = self.alpine_image.export(target, False)
+ self.assertTrue(actual)
+ self.assertTrue(os.path.isfile(path))
+
+ def test_history(self):
+ count = 0
+ for record in self.alpine_image.history():
+ count += 1
+ self.assertEqual(record.id, self.alpine_image.id)
+ self.assertGreater(count, 0)
+
+ def test_inspect(self):
+ actual = self.alpine_image.inspect()
+ self.assertEqual(actual.id, self.alpine_image.id)
+
+ def test_push(self):
+ path = '{}/alpine_push'.format(self.tmpdir)
+ target = 'dir:{}'.format(path)
+ self.alpine_image.push(target)
+
+ self.assertTrue(os.path.isfile(os.path.join(path, 'manifest.json')))
+ self.assertTrue(os.path.isfile(os.path.join(path, 'version')))
+
+ def test_tag(self):
+ self.assertEqual(self.alpine_image.id,
+ self.alpine_image.tag('alpine:fubar'))
+ self.loadCache()
+ self.assertIn('alpine:fubar', self.alpine_image.repoTags)
+
+ def test_remove(self):
+ before = self.loadCache()
+
+ # assertRaises doesn't follow the import name :(
+ with self.assertRaises(podman.ErrorOccurred):
+ 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)
+ self.assertEqual(self.alpine_image.id, actual)
+ after = self.loadCache()
+
+ self.assertLess(len(after), len(before))
+ TestImages.setUpClass()
+ self.loadCache()
+
+ def test_import_delete_unused(self):
+ before = self.loadCache()
+ # create unused image, so we have something to delete
+ source = os.path.join(self.tmpdir, 'alpine_gold.tar')
+ new_img = self.pclient.images.import_image(source, 'alpine2:latest',
+ 'unittest.test_import')
+ after = self.loadCache()
+
+ self.assertEqual(len(before) + 1, len(after))
+ self.assertIsNotNone(
+ next(iter([i for i in after if new_img in i['id']] or []), None))
+
+ actual = self.pclient.images.delete_unused()
+ self.assertIn(new_img, actual)
+
+ after = self.loadCache()
+ self.assertEqual(len(before), len(after))
+
+ TestImages.setUpClass()
+ self.loadCache()
+
+ def test_pull(self):
+ before = self.loadCache()
+ actual = self.pclient.images.pull('prom/busybox:latest')
+ after = self.loadCache()
+
+ self.assertEqual(len(before) + 1, len(after))
+ self.assertIsNotNone(
+ next(iter([i for i in after if actual in i['id']] or []), None))
+
+ def test_search(self):
+ actual = self.pclient.images.search('alpine', 25)
+ names, lengths = itertools.tee(actual)
+
+ for img in names:
+ self.assertIn('alpine', img['name'])
+ self.assertTrue(0 < len(list(lengths)) <= 25)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/contrib/python/test/test_libs.py b/contrib/python/test/test_libs.py
new file mode 100644
index 000000000..e2160fc30
--- /dev/null
+++ b/contrib/python/test/test_libs.py
@@ -0,0 +1,46 @@
+import datetime
+import unittest
+
+import podman
+
+
+class TestLibs(unittest.TestCase):
+ def setUp(self):
+ pass
+
+ def tearDown(self):
+ pass
+
+ def test_parse(self):
+ expected = datetime.datetime.strptime(
+ '2018-05-08T14:12:53.797795-0700', '%Y-%m-%dT%H:%M:%S.%f%z')
+ for v in [
+ '2018-05-08T14:12:53.797795191-07:00',
+ '2018-05-08T14:12:53.797795-07:00',
+ '2018-05-08T14:12:53.797795-0700',
+ '2018-05-08 14:12:53.797795191 -0700 MST'
+ ]:
+ actual = podman.datetime_parse(v)
+ self.assertEqual(actual, expected)
+
+ podman.datetime_parse(datetime.datetime.now().isoformat())
+
+ def test_parse_fail(self):
+ # chronologist humor: '1752-09-05T12:00:00.000000-0000' also not
+ # handled correctly by python for my locale.
+ for v in [
+ '1752-9-5',
+ '1752-09-05',
+ ]:
+ with self.assertRaises(ValueError):
+ podman.datetime_parse(v)
+
+ def test_format(self):
+ expected = '2018-05-08T18:24:52.753227-07:00'
+ dt = podman.datetime_parse(expected)
+ actual = podman.datetime_format(dt)
+ self.assertEqual(actual, expected)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/contrib/python/test/test_runner.sh b/contrib/python/test/test_runner.sh
new file mode 100755
index 000000000..3e6dee110
--- /dev/null
+++ b/contrib/python/test/test_runner.sh
@@ -0,0 +1,116 @@
+#!/bin/bash
+
+# podman needs to play some games with resources
+if [[ $(id -u) != 0 ]]; then
+ echo >&2 $0 must be run as root.
+ exit 2
+fi
+
+while getopts "vh" arg; do
+ case $arg in
+ v ) VERBOSE='-v' ;;
+ h ) echo >2 $0 [-v] [-h] [test.TestCase|test.TestCase.step] ; exit 2 ;;
+ esac
+done
+shift $((OPTIND-1))
+
+# Create temporary directory for storage
+export TMPDIR=`mktemp -d /tmp/podman.XXXXXXXXXX`
+
+function umount {
+ # xargs -r always ran once, so write any mount points to file first
+ mount |awk "/$1/"' { print $3 }' >${TMPDIR}/mounts
+ if [[ -s ${TMPDIR}/mounts ]]; then
+ xargs <${TMPDIR}/mounts -t umount
+ fi
+}
+
+function cleanup {
+ umount '^(shm|nsfs)'
+ umount '\/run\/netns'
+ rm -fr ${TMPDIR}
+}
+trap cleanup EXIT
+
+# setup path to find new binaries _NOT_ system binaries
+if [[ ! -x ../../bin/podman ]]; then
+ echo 1>&2 Cannot find podman binary from libpod root directory, Or, run \"make binaries\"
+ exit 1
+fi
+export PATH=../../bin:$PATH
+
+function showlog {
+ [ -s "$1" ] && (echo $1 =====; cat "$1")
+}
+
+# Need a location to store the podman socket
+mkdir -p ${TMPDIR}/{podman,crio,crio-run,cni/net.d}
+
+# Cannot be done in python unittest fixtures. EnvVar not picked up.
+export REGISTRIES_CONFIG_PATH=${TMPDIR}/registry.conf
+cat >$REGISTRIES_CONFIG_PATH <<-EOT
+ [registries.search]
+ registries = ['docker.io']
+ [registries.insecure]
+ registries = []
+ [registries.block]
+ registries = []
+EOT
+
+export CNI_CONFIG_PATH=${TMPDIR}/cni/net.d
+cat >$CNI_CONFIG_PATH/87-podman-bridge.conflist <<-EOT
+{
+ "cniVersion": "0.3.0",
+ "name": "podman",
+ "plugins": [{
+ "type": "bridge",
+ "bridge": "cni0",
+ "isGateway": true,
+ "ipMasq": true,
+ "ipam": {
+ "type": "host-local",
+ "subnet": "10.88.0.0/16",
+ "routes": [{
+ "dst": "0.0.0.0/0"
+ }]
+ }
+ },
+ {
+ "type": "portmap",
+ "capabilities": {
+ "portMappings": true
+ }
+ }
+ ]
+}
+EOT
+
+export PODMAN_HOST="unix:${TMPDIR}/podman/io.projectatomic.podman"
+PODMAN_ARGS="--storage-driver=vfs\
+ --root=${TMPDIR}/crio\
+ --runroot=${TMPDIR}/crio-run\
+ --cni-config-dir=$CNI_CONFIG_PATH\
+ "
+PODMAN="podman $PODMAN_ARGS"
+
+# document what we're about to do...
+$PODMAN --version
+
+set -x
+# Run podman in background without systemd for test purposes
+$PODMAN varlink ${PODMAN_HOST} >/tmp/test_runner.output 2>&1 &
+
+if [[ -z $1 ]]; then
+ export PYTHONPATH=.
+ python3 -m unittest discover -s . $VERBOSE
+else
+ export PYTHONPATH=.:./test
+ python3 -m unittest $1 $VERBOSE
+fi
+
+set +x
+pkill podman
+pkill -9 conmon
+
+showlog /tmp/alpine.log
+showlog /tmp/busybox.log
diff --git a/contrib/python/test/test_system.py b/contrib/python/test/test_system.py
new file mode 100644
index 000000000..c0d30acd7
--- /dev/null
+++ b/contrib/python/test/test_system.py
@@ -0,0 +1,49 @@
+import os
+import unittest
+
+import varlink
+
+import podman
+
+
+class TestSystem(unittest.TestCase):
+ def setUp(self):
+ self.host = os.environ['PODMAN_HOST']
+
+ def tearDown(self):
+ pass
+
+ def test_bad_address(self):
+ with self.assertRaisesRegex(varlink.client.ConnectionError,
+ "Invalid address 'bad address'"):
+ podman.Client('bad address')
+
+ def test_ping(self):
+ with podman.Client(self.host) as pclient:
+ self.assertTrue(pclient.system.ping())
+
+ def test_versions(self):
+ with podman.Client(self.host) as pclient:
+ # Values change with each build so we cannot test too much
+ self.assertListEqual(
+ sorted([
+ 'built', 'client_version', 'git_commit', 'go_version',
+ 'os_arch', 'version'
+ ]), sorted(list(pclient.system.versions._fields)))
+ pclient.system.versions
+ self.assertIsNot(podman.__version__, '0.0.0')
+
+ def test_info(self):
+ with podman.Client(self.host) as pclient:
+ actual = pclient.system.info()
+ # Values change too much to do exhaustive testing
+ self.assertIsNotNone(actual.podman['go_version'])
+ self.assertListEqual(
+ sorted([
+ 'host', 'insecure_registries', 'podman', 'registries',
+ 'store'
+ ]), sorted(list(actual._fields)))
+
+
+if __name__ == '__main__':
+ unittest.main()