mirror of
https://github.com/containers/podman.git
synced 2025-07-29 19:33:13 +08:00
Fix python dockerpy tests
* Refactor packaging so unittest discovery works * Refactor tests to use python3-docker.rpm that ships with Fedora32 * Flush image cache between tests suites * Update documentation to reflect changes Outstanding issue: * client.get_image() does not fail if image does not exist Signed-off-by: Jhon Honce <jhonce@redhat.com>
This commit is contained in:
40
test/python/dockerpy/README.md
Normal file
40
test/python/dockerpy/README.md
Normal file
@ -0,0 +1,40 @@
|
||||
# Dockerpy regression test
|
||||
|
||||
Python test suite to validate Podman endpoints using dockerpy library
|
||||
|
||||
## Running Tests
|
||||
|
||||
To run the tests locally in your sandbox (Fedora 32):
|
||||
|
||||
```shell script
|
||||
# dnf install python3-docker
|
||||
```
|
||||
|
||||
### Run the entire test suite
|
||||
|
||||
```shell
|
||||
# cd test/python/dockerpy
|
||||
# PYTHONPATH=/usr/bin/python python -m unittest discover .
|
||||
```
|
||||
|
||||
Passing the -v option to your test script will instruct unittest.main() to enable a higher level of verbosity, and produce detailed output:
|
||||
|
||||
```shell
|
||||
# cd test/python/dockerpy
|
||||
# PYTHONPATH=/usr/bin/python python -m unittest -v discover .
|
||||
```
|
||||
|
||||
### Run a specific test class
|
||||
|
||||
```shell
|
||||
# cd test/python/dockerpy
|
||||
# PYTHONPATH=/usr/bin/python python -m unittest -v tests.test_images
|
||||
```
|
||||
|
||||
### Run a specific test within the test class
|
||||
|
||||
```shell
|
||||
# cd test/python/dockerpy
|
||||
# PYTHONPATH=/usr/bin/python python -m unittest tests.test_images.TestImages.test_import_image
|
||||
|
||||
```
|
0
test/python/dockerpy/tests/__init__.py
Normal file
0
test/python/dockerpy/tests/__init__.py
Normal file
105
test/python/dockerpy/tests/common.py
Normal file
105
test/python/dockerpy/tests/common.py
Normal file
@ -0,0 +1,105 @@
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
from docker import APIClient
|
||||
|
||||
from . import constant
|
||||
|
||||
alpineDict = {
|
||||
"name": "docker.io/library/alpine:latest",
|
||||
"shortName": "alpine",
|
||||
"tarballName": "alpine.tar"
|
||||
}
|
||||
|
||||
|
||||
def get_client():
|
||||
client = APIClient(base_url="http://localhost:8080", timeout=15)
|
||||
return client
|
||||
|
||||
|
||||
client = get_client()
|
||||
|
||||
|
||||
def podman():
|
||||
binary = os.getenv("PODMAN_BINARY")
|
||||
if binary is None:
|
||||
binary = "../../../bin/podman"
|
||||
return binary
|
||||
|
||||
|
||||
def restore_image_from_cache(TestClass):
|
||||
alpineImage = os.path.join(constant.ImageCacheDir,
|
||||
alpineDict["tarballName"])
|
||||
if not os.path.exists(alpineImage):
|
||||
os.makedirs(constant.ImageCacheDir, exist_ok=True)
|
||||
client.pull(constant.ALPINE)
|
||||
image = client.get_image(constant.ALPINE)
|
||||
tarball = open(alpineImage, mode="wb")
|
||||
for frame in image:
|
||||
tarball.write(frame)
|
||||
tarball.close()
|
||||
else:
|
||||
subprocess.run(
|
||||
[podman(), "load", "-i", alpineImage],
|
||||
shell=False,
|
||||
stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
check=True,
|
||||
)
|
||||
|
||||
|
||||
def flush_image_cache(TestCase):
|
||||
for f in pathlib.Path(constant.ImageCacheDir).glob("*"):
|
||||
f.unlink(f)
|
||||
|
||||
|
||||
def run_top_container():
|
||||
c = client.create_container(image=constant.ALPINE,
|
||||
command='/bin/sleep 5',
|
||||
name=constant.TOP)
|
||||
client.start(container=c.get("Id"))
|
||||
return c.get("Id")
|
||||
|
||||
|
||||
def enable_sock(TestClass):
|
||||
TestClass.podman = subprocess.Popen(
|
||||
[
|
||||
podman(), "system", "service", "tcp:localhost:8080",
|
||||
"--log-level=debug", "--time=0"
|
||||
],
|
||||
shell=False,
|
||||
stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
time.sleep(2)
|
||||
|
||||
|
||||
def terminate_connection(TestClass):
|
||||
TestClass.podman.terminate()
|
||||
stdout, stderr = TestClass.podman.communicate(timeout=0.5)
|
||||
if stdout:
|
||||
print("\nService Stdout:\n" + stdout.decode('utf-8'))
|
||||
if stderr:
|
||||
print("\nService Stderr:\n" + stderr.decode('utf-8'))
|
||||
|
||||
if TestClass.podman.returncode > 0:
|
||||
sys.stderr.write("podman exited with error code {}\n".format(
|
||||
TestClass.podman.returncode))
|
||||
sys.exit(2)
|
||||
|
||||
|
||||
def remove_all_containers():
|
||||
containers = client.containers(quiet=True)
|
||||
for c in containers:
|
||||
client.remove_container(container=c.get("Id"), force=True)
|
||||
|
||||
|
||||
def remove_all_images():
|
||||
allImages = client.images()
|
||||
for image in allImages:
|
||||
client.remove_image(image, force=True)
|
13
test/python/dockerpy/tests/constant.py
Normal file
13
test/python/dockerpy/tests/constant.py
Normal file
@ -0,0 +1,13 @@
|
||||
BB = "docker.io/library/busybox:latest"
|
||||
NGINX = "docker.io/library/nginx:latest"
|
||||
ALPINE = "docker.io/library/alpine:latest"
|
||||
ALPINE_SHORTNAME = "alpine"
|
||||
ALPINELISTTAG = "docker.io/library/alpine:3.10.2"
|
||||
ALPINELISTDIGEST = "docker.io/library/alpine@sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb"
|
||||
ALPINEAMD64DIGEST = "docker.io/library/alpine@sha256:acd3ca9941a85e8ed16515bfc5328e4e2f8c128caa72959a58a127b7801ee01f"
|
||||
ALPINEAMD64ID = "961769676411f082461f9ef46626dd7a2d1e2b2a38e6a44364bcbecf51e66dd4"
|
||||
ALPINEARM64DIGEST = "docker.io/library/alpine@sha256:db7f3dcef3d586f7dd123f107c93d7911515a5991c4b9e51fa2a43e46335a43e"
|
||||
ALPINEARM64ID = "915beeae46751fc564998c79e73a1026542e945ca4f73dc841d09ccc6c2c0672"
|
||||
infra = "k8s.gcr.io/pause:3.2"
|
||||
TOP = "top"
|
||||
ImageCacheDir = "/tmp/podman/imagecachedir"
|
@ -1,18 +1,15 @@
|
||||
|
||||
import unittest
|
||||
import docker
|
||||
import requests
|
||||
import os
|
||||
from docker import Client
|
||||
from . import constant
|
||||
from . import common
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import requests
|
||||
|
||||
from . import common, constant
|
||||
|
||||
client = common.get_client()
|
||||
|
||||
class TestContainers(unittest.TestCase):
|
||||
|
||||
podman = None
|
||||
class TestContainers(unittest.TestCase):
|
||||
topContainerId = ""
|
||||
|
||||
def setUp(self):
|
||||
@ -33,6 +30,7 @@ class TestContainers(unittest.TestCase):
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
common.terminate_connection(cls)
|
||||
common.flush_image_cache(cls)
|
||||
return super().tearDownClass()
|
||||
|
||||
def test_inspect_container(self):
|
||||
@ -52,7 +50,6 @@ class TestContainers(unittest.TestCase):
|
||||
container = client.create_container(image="alpine", detach=True)
|
||||
self.assertEqual(len(container), 2)
|
||||
|
||||
|
||||
def test_start_container(self):
|
||||
# Start bogus container
|
||||
with self.assertRaises(requests.HTTPError) as error:
|
||||
@ -82,7 +79,10 @@ class TestContainers(unittest.TestCase):
|
||||
# Stop a running container and validate the state
|
||||
client.stop(TestContainers.topContainerId)
|
||||
container = client.inspect_container(constant.TOP)
|
||||
self.assertIn(container["State"]["Status"],"stopped exited",)
|
||||
self.assertIn(
|
||||
container["State"]["Status"],
|
||||
"stopped exited",
|
||||
)
|
||||
|
||||
def test_restart_container(self):
|
||||
# Restart bogus container
|
||||
@ -151,7 +151,6 @@ class TestContainers(unittest.TestCase):
|
||||
client.pause(TestContainers.topContainerId)
|
||||
self.assertEqual(error.exception.response.status_code, 500)
|
||||
|
||||
|
||||
def test_unpause_container(self):
|
||||
# Unpause bogus container
|
||||
with self.assertRaises(requests.HTTPError) as error:
|
@ -1,17 +1,18 @@
|
||||
|
||||
import os
|
||||
import stat
|
||||
import unittest
|
||||
from os import remove
|
||||
from stat import ST_SIZE
|
||||
|
||||
import docker
|
||||
import requests
|
||||
import os
|
||||
from docker import Client
|
||||
from . import constant
|
||||
from . import common
|
||||
|
||||
from . import common, constant
|
||||
|
||||
client = common.get_client()
|
||||
|
||||
class TestImages(unittest.TestCase):
|
||||
|
||||
podman = None
|
||||
class TestImages(unittest.TestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
common.restore_image_from_cache(self)
|
||||
@ -25,13 +26,12 @@ class TestImages(unittest.TestCase):
|
||||
super().setUpClass()
|
||||
common.enable_sock(cls)
|
||||
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
common.terminate_connection(cls)
|
||||
common.flush_image_cache(cls)
|
||||
return super().tearDownClass()
|
||||
|
||||
|
||||
# Inspect Image
|
||||
|
||||
def test_inspect_image(self):
|
||||
@ -44,12 +44,11 @@ class TestImages(unittest.TestCase):
|
||||
# Tag Image
|
||||
|
||||
# Validates if invalid image name is given a bad response is encountered.
|
||||
|
||||
def test_tag_invalid_image(self):
|
||||
with self.assertRaises(requests.HTTPError):
|
||||
client.tag("dummy", "demo")
|
||||
|
||||
|
||||
|
||||
# Validates if the image is tagged successfully.
|
||||
def test_tag_valid_image(self):
|
||||
client.tag(constant.ALPINE, "demo", constant.ALPINE_SHORTNAME)
|
||||
@ -68,6 +67,7 @@ class TestImages(unittest.TestCase):
|
||||
|
||||
# List Image
|
||||
# List All Images
|
||||
|
||||
def test_list_images(self):
|
||||
allImages = client.images()
|
||||
self.assertEqual(len(allImages), 1)
|
||||
@ -76,13 +76,13 @@ class TestImages(unittest.TestCase):
|
||||
allImages = client.images()
|
||||
self.assertEqual(len(allImages), 2)
|
||||
|
||||
|
||||
# List images with filter
|
||||
filters = {'reference': 'alpine'}
|
||||
allImages = client.images(filters=filters)
|
||||
self.assertEqual(len(allImages), 1)
|
||||
|
||||
# Search Image
|
||||
|
||||
def test_search_image(self):
|
||||
response = client.search("alpine")
|
||||
for i in response:
|
||||
@ -94,6 +94,7 @@ class TestImages(unittest.TestCase):
|
||||
# Image Exist (No docker-py support yet)
|
||||
|
||||
# Remove Image
|
||||
|
||||
def test_remove_image(self):
|
||||
# Check for error with wrong image name
|
||||
with self.assertRaises(requests.HTTPError):
|
||||
@ -106,10 +107,12 @@ class TestImages(unittest.TestCase):
|
||||
self.assertEqual(len(allImages), 0)
|
||||
|
||||
# Image History
|
||||
|
||||
def test_image_history(self):
|
||||
# Check for error with wrong image name
|
||||
with self.assertRaises(requests.HTTPError):
|
||||
client.remove_image("dummy")
|
||||
client.history("dummy")
|
||||
|
||||
imageHistory = client.history(constant.ALPINE)
|
||||
alpine_image = client.inspect_image(constant.ALPINE)
|
||||
for h in imageHistory:
|
||||
@ -119,28 +122,37 @@ class TestImages(unittest.TestCase):
|
||||
|
||||
# Prune Image (No docker-py support yet)
|
||||
|
||||
def test_get_image_dummy(self):
|
||||
# FIXME: seems to be an error in the library
|
||||
self.skipTest("Documentation and library do not match")
|
||||
# Check for error with wrong image name
|
||||
with self.assertRaises(docker.errors.ImageNotFound):
|
||||
client.get_image("dummy")
|
||||
|
||||
# Export Image
|
||||
|
||||
def test_export_image(self):
|
||||
client.pull(constant.BB)
|
||||
file = os.path.join(constant.ImageCacheDir , "busybox.tar")
|
||||
if not os.path.exists(constant.ImageCacheDir):
|
||||
os.makedirs(constant.ImageCacheDir)
|
||||
# Check for error with wrong image name
|
||||
with self.assertRaises(requests.HTTPError):
|
||||
client.get_image("dummy")
|
||||
response = client.get_image(constant.BB)
|
||||
image_tar = open(file,mode="wb")
|
||||
image_tar.write(response.data)
|
||||
image_tar.close()
|
||||
os.stat(file)
|
||||
|
||||
image = client.get_image(constant.BB)
|
||||
|
||||
file = os.path.join(constant.ImageCacheDir, "busybox.tar")
|
||||
tarball = open(file, mode="wb")
|
||||
for frame in image:
|
||||
tarball.write(frame)
|
||||
tarball.close()
|
||||
sz = os.path.getsize(file)
|
||||
self.assertGreater(sz, 0)
|
||||
|
||||
|
||||
# Import|Load Image
|
||||
|
||||
def test_import_image(self):
|
||||
allImages = client.images()
|
||||
self.assertEqual(len(allImages), 1)
|
||||
file = os.path.join(constant.ImageCacheDir , "busybox.tar")
|
||||
file = os.path.join(constant.ImageCacheDir, "alpine.tar")
|
||||
client.import_image_from_file(filename=file)
|
||||
allImages = client.images()
|
||||
self.assertEqual(len(allImages), 2)
|
@ -1,11 +1,10 @@
|
||||
import unittest
|
||||
import docker
|
||||
from docker import Client
|
||||
from . import constant
|
||||
from . import common
|
||||
|
||||
from . import common, constant
|
||||
|
||||
client = common.get_client()
|
||||
|
||||
|
||||
class TestInfo_Version(unittest.TestCase):
|
||||
|
||||
podman = None
|
||||
@ -31,7 +30,6 @@ class TestInfo_Version(unittest.TestCase):
|
||||
common.terminate_connection(cls)
|
||||
return super().tearDownClass()
|
||||
|
||||
|
||||
def test_Info(self):
|
||||
self.assertIsNotNone(client.info())
|
||||
|
@ -1,30 +0,0 @@
|
||||
# Dockerpy regression test
|
||||
|
||||
Python test suite to validate Podman endpoints using dockerpy library
|
||||
|
||||
Running tests
|
||||
=============
|
||||
To run the tests locally in your sandbox:
|
||||
|
||||
#### Run the entire test
|
||||
|
||||
```
|
||||
sudo PYTHONPATH=/usr/bin/python python -m dockerpy.images
|
||||
```
|
||||
|
||||
Passing the -v option to your test script will instruct unittest.main() to enable a higher level of verbosity, and produce detailed output:
|
||||
|
||||
```
|
||||
sudo PYTHONPATH=/usr/bin/python python -m unittest -v dockerpy.images
|
||||
```
|
||||
#### Run a specific test class
|
||||
|
||||
```
|
||||
sudo PYTHONPATH=/usr/bin/python python -m unittest -v dockerpy.images.TestImages
|
||||
```
|
||||
|
||||
#### Run a specific test within the test class
|
||||
|
||||
```
|
||||
sudo PYTHONPATH=/usr/bin/python python -m unittest -v dockerpy.images.TestImages.test_list_images
|
||||
```
|
@ -1,85 +0,0 @@
|
||||
import docker
|
||||
import subprocess
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from docker import Client
|
||||
from . import constant
|
||||
|
||||
alpineDict = {
|
||||
"name": "docker.io/library/alpine:latest",
|
||||
"shortName": "alpine",
|
||||
"tarballName": "alpine.tar"}
|
||||
|
||||
def get_client():
|
||||
client = docker.Client(base_url="http://localhost:8080",timeout=15)
|
||||
return client
|
||||
|
||||
client = get_client()
|
||||
|
||||
def podman():
|
||||
binary = os.getenv("PODMAN_BINARY")
|
||||
if binary is None:
|
||||
binary = "bin/podman"
|
||||
return binary
|
||||
|
||||
def restore_image_from_cache(TestClass):
|
||||
alpineImage = os.path.join(constant.ImageCacheDir , alpineDict["tarballName"])
|
||||
if not os.path.exists(alpineImage):
|
||||
os.makedirs(constant.ImageCacheDir)
|
||||
client.pull(constant.ALPINE)
|
||||
response = client.get_image(constant.ALPINE)
|
||||
image_tar = open(alpineImage,mode="wb")
|
||||
image_tar.write(response.data)
|
||||
image_tar.close()
|
||||
else :
|
||||
TestClass.podman = subprocess.run(
|
||||
[
|
||||
podman(), "load", "-i", alpineImage
|
||||
],
|
||||
shell=False,
|
||||
stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
def run_top_container():
|
||||
c = client.create_container(image=constant.ALPINE,command='/bin/sleep 5',name=constant.TOP)
|
||||
client.start(container=c.get("Id"))
|
||||
return c.get("Id")
|
||||
|
||||
def enable_sock(TestClass):
|
||||
TestClass.podman = subprocess.Popen(
|
||||
[
|
||||
podman(), "system", "service", "tcp:localhost:8080",
|
||||
"--log-level=debug", "--time=0"
|
||||
],
|
||||
shell=False,
|
||||
stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
time.sleep(2)
|
||||
|
||||
def terminate_connection(TestClass):
|
||||
TestClass.podman.terminate()
|
||||
stdout, stderr = TestClass.podman.communicate(timeout=0.5)
|
||||
if stdout:
|
||||
print("\nService Stdout:\n" + stdout.decode('utf-8'))
|
||||
if stderr:
|
||||
print("\nService Stderr:\n" + stderr.decode('utf-8'))
|
||||
|
||||
if TestClass.podman.returncode > 0:
|
||||
sys.stderr.write("podman exited with error code {}\n".format(
|
||||
TestClass.podman.returncode))
|
||||
sys.exit(2)
|
||||
|
||||
def remove_all_containers():
|
||||
containers = client.containers(quiet=True)
|
||||
for c in containers:
|
||||
client.remove_container(container=c.get("Id"), force=True)
|
||||
|
||||
def remove_all_images():
|
||||
allImages = client.images()
|
||||
for image in allImages:
|
||||
client.remove_image(image,force=True)
|
@ -1,13 +0,0 @@
|
||||
BB = "docker.io/library/busybox:latest"
|
||||
NGINX = "docker.io/library/nginx:latest"
|
||||
ALPINE = "docker.io/library/alpine:latest"
|
||||
ALPINE_SHORTNAME = "alpine"
|
||||
ALPINELISTTAG = "docker.io/library/alpine:3.10.2"
|
||||
ALPINELISTDIGEST = "docker.io/library/alpine@sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb"
|
||||
ALPINEAMD64DIGEST = "docker.io/library/alpine@sha256:acd3ca9941a85e8ed16515bfc5328e4e2f8c128caa72959a58a127b7801ee01f"
|
||||
ALPINEAMD64ID = "961769676411f082461f9ef46626dd7a2d1e2b2a38e6a44364bcbecf51e66dd4"
|
||||
ALPINEARM64DIGEST = "docker.io/library/alpine@sha256:db7f3dcef3d586f7dd123f107c93d7911515a5991c4b9e51fa2a43e46335a43e"
|
||||
ALPINEARM64ID = "915beeae46751fc564998c79e73a1026542e945ca4f73dc841d09ccc6c2c0672"
|
||||
infra = "k8s.gcr.io/pause:3.2"
|
||||
TOP = "top"
|
||||
ImageCacheDir = "/tmp/podman/imagecachedir"
|
Reference in New Issue
Block a user