Refactor python tests to run against python3.9

* Introduce sub-package compat to meet packaging and import requirements
* Update documenation for running tests
* Add requirements.txt to improve IDE support

Signed-off-by: Jhon Honce <jhonce@redhat.com>
This commit is contained in:
Jhon Honce
2021-03-01 10:55:20 -07:00
parent 73044b2817
commit 7927fe01f1
11 changed files with 77 additions and 33 deletions

View File

@ -64,7 +64,9 @@ class TestApi(unittest.TestCase):
super().setUpClass()
TestApi.podman = Podman()
TestApi.service = TestApi.podman.open("system", "service", "tcp:localhost:8080", "--time=0")
TestApi.service = TestApi.podman.open(
"system", "service", "tcp:localhost:8080", "--time=0"
)
# give the service some time to be ready...
time.sleep(2)
@ -241,7 +243,9 @@ class TestApi(unittest.TestCase):
def test_post_create_compat(self):
"""Create network and connect container during create"""
net = requests.post(PODMAN_URL + "/v1.40/networks/create", json={"Name": "TestNetwork"})
net = requests.post(
PODMAN_URL + "/v1.40/networks/create", json={"Name": "TestNetwork"}
)
self.assertEqual(net.status_code, 201, net.text)
create = requests.post(
@ -450,11 +454,15 @@ class TestApi(unittest.TestCase):
self.assertIn(k, o)
def test_network_compat(self):
name = "Network_" + "".join(random.choice(string.ascii_letters) for i in range(10))
name = "Network_" + "".join(
random.choice(string.ascii_letters) for i in range(10)
)
# Cannot test for 0 existing networks because default "podman" network always exists
create = requests.post(PODMAN_URL + "/v1.40/networks/create", json={"Name": name})
create = requests.post(
PODMAN_URL + "/v1.40/networks/create", json={"Name": name}
)
self.assertEqual(create.status_code, 201, create.content)
obj = json.loads(create.content)
self.assertIn(type(obj), (dict,))
@ -484,8 +492,12 @@ class TestApi(unittest.TestCase):
self.assertEqual(inspect.status_code, 404, inspect.content)
# network prune
prune_name = "Network_" + "".join(random.choice(string.ascii_letters) for i in range(10))
prune_create = requests.post(PODMAN_URL + "/v1.40/networks/create", json={"Name": prune_name})
prune_name = "Network_" + "".join(
random.choice(string.ascii_letters) for i in range(10)
)
prune_create = requests.post(
PODMAN_URL + "/v1.40/networks/create", json={"Name": prune_name}
)
self.assertEqual(create.status_code, 201, prune_create.content)
prune = requests.post(PODMAN_URL + "/v1.40/networks/prune")
@ -493,9 +505,10 @@ class TestApi(unittest.TestCase):
obj = json.loads(prune.content)
self.assertTrue(prune_name in obj["NetworksDeleted"])
def test_volumes_compat(self):
name = "Volume_" + "".join(random.choice(string.ascii_letters) for i in range(10))
name = "Volume_" + "".join(
random.choice(string.ascii_letters) for i in range(10)
)
ls = requests.get(PODMAN_URL + "/v1.40/volumes")
self.assertEqual(ls.status_code, 200, ls.content)
@ -511,7 +524,9 @@ class TestApi(unittest.TestCase):
for k in required_keys:
self.assertIn(k, obj)
create = requests.post(PODMAN_URL + "/v1.40/volumes/create", json={"Name": name})
create = requests.post(
PODMAN_URL + "/v1.40/volumes/create", json={"Name": name}
)
self.assertEqual(create.status_code, 201, create.content)
# See https://docs.docker.com/engine/api/v1.40/#operation/VolumeCreate
@ -688,15 +703,21 @@ class TestApi(unittest.TestCase):
"""Verify issue #8865"""
pod_name = list()
pod_name.append("Pod_" + "".join(random.choice(string.ascii_letters) for i in range(10)))
pod_name.append("Pod_" + "".join(random.choice(string.ascii_letters) for i in range(10)))
pod_name.append(
"Pod_" + "".join(random.choice(string.ascii_letters) for i in range(10))
)
pod_name.append(
"Pod_" + "".join(random.choice(string.ascii_letters) for i in range(10))
)
r = requests.post(
_url("/pods/create"),
json={
"name": pod_name[0],
"no_infra": False,
"portmappings": [{"host_ip": "127.0.0.1", "host_port": 8889, "container_port": 89}],
"portmappings": [
{"host_ip": "127.0.0.1", "host_port": 8889, "container_port": 89}
],
},
)
self.assertEqual(r.status_code, 201, r.text)
@ -715,7 +736,9 @@ class TestApi(unittest.TestCase):
json={
"name": pod_name[1],
"no_infra": False,
"portmappings": [{"host_ip": "127.0.0.1", "host_port": 8889, "container_port": 89}],
"portmappings": [
{"host_ip": "127.0.0.1", "host_port": 8889, "container_port": 89}
],
},
)
self.assertEqual(r.status_code, 201, r.text)

0
test/python/__init__.py Normal file
View File

View File

@ -8,7 +8,7 @@ import tempfile
from docker import DockerClient
from test.python.docker import constant
from .compat import constant
class Podman(object):
@ -39,7 +39,9 @@ class Podman(object):
self.cmd.append("--root=" + os.path.join(self.anchor_directory, "crio"))
self.cmd.append("--runroot=" + os.path.join(self.anchor_directory, "crio-run"))
os.environ["REGISTRIES_CONFIG_PATH"] = os.path.join(self.anchor_directory, "registry.conf")
os.environ["REGISTRIES_CONFIG_PATH"] = os.path.join(
self.anchor_directory, "registry.conf"
)
p = configparser.ConfigParser()
p.read_dict(
{
@ -51,10 +53,14 @@ class Podman(object):
with open(os.environ["REGISTRIES_CONFIG_PATH"], "w") as w:
p.write(w)
os.environ["CNI_CONFIG_PATH"] = os.path.join(self.anchor_directory, "cni", "net.d")
os.environ["CNI_CONFIG_PATH"] = os.path.join(
self.anchor_directory, "cni", "net.d"
)
os.makedirs(os.environ["CNI_CONFIG_PATH"], exist_ok=True)
self.cmd.append("--cni-config-dir=" + os.environ["CNI_CONFIG_PATH"])
cni_cfg = os.path.join(os.environ["CNI_CONFIG_PATH"], "87-podman-bridge.conflist")
cni_cfg = os.path.join(
os.environ["CNI_CONFIG_PATH"], "87-podman-bridge.conflist"
)
# json decoded and encoded to ensure legal json
buf = json.loads(
"""

View File

@ -13,26 +13,26 @@ To run the tests locally in your sandbox (Fedora 32,33):
### Run the entire test suite
All commands are run from the root of the repository.
```shell
# python3 -m unittest discover test/python/docker
# python3 -m unittest discover -s test/python/docker
```
Passing the -v option to your test script will instruct unittest.main() to enable a higher level of verbosity, and produce detailed output:
```shell
# python3 -m unittest -v discover test/python/docker
# python3 -m unittest -v discover -s test/python/docker
```
### Run a specific test class
```shell
# cd test/python/docker
# python3 -m unittest -v tests.test_images
# python3 -m unittest -v test.python.docker.compat.test_images.TestImages
```
### Run a specific test within the test class
```shell
# cd test/python/docker
# python3 -m unittest tests.test_images.TestImages.test_import_image
# python3 -m unittest test.python.docker.compat.test_images.TestImages.test_tag_valid_image
```

View File

View File

@ -1,10 +1,12 @@
from docker import DockerClient
from test.python.docker import constant
from test.python.docker.compat import constant
def run_top_container(client: DockerClient):
c = client.containers.create(constant.ALPINE, command="top", detach=True, tty=True, name="top")
c = client.containers.create(
constant.ALPINE, command="top", detach=True, tty=True, name="top"
)
c.start()
return c.id

View File

@ -5,7 +5,8 @@ import unittest
from docker import DockerClient, errors
from test.python.docker import Podman, common, constant
from test.python.docker import Podman
from test.python.docker.compat import common, constant
class TestContainers(unittest.TestCase):
@ -87,9 +88,11 @@ class TestContainers(unittest.TestCase):
self.assertEqual(len(containers), 2)
def test_start_container_with_random_port_bind(self):
container = self.client.containers.create(image=constant.ALPINE,
name="containerWithRandomBind",
ports={'1234/tcp': None})
container = self.client.containers.create(
image=constant.ALPINE,
name="containerWithRandomBind",
ports={"1234/tcp": None},
)
containers = self.client.containers.list(all=True)
self.assertTrue(container in containers)

View File

@ -7,7 +7,8 @@ import unittest
from docker import DockerClient, errors
from test.python.docker import Podman, common, constant
from test.python.docker import Podman
from test.python.docker.compat import common, constant
class TestImages(unittest.TestCase):
@ -78,7 +79,9 @@ class TestImages(unittest.TestCase):
self.assertEqual(len(self.client.images.list()), 2)
# List images with filter
self.assertEqual(len(self.client.images.list(filters={"reference": "alpine"})), 1)
self.assertEqual(
len(self.client.images.list(filters={"reference": "alpine"})), 1
)
def test_search_image(self):
"""Search for image"""
@ -91,7 +94,7 @@ class TestImages(unittest.TestCase):
r = self.client.images.search("bogus/bogus")
except:
return
self.assertTrue(len(r)==0)
self.assertTrue(len(r) == 0)
def test_remove_image(self):
"""Remove image"""

View File

@ -5,7 +5,8 @@ import unittest
from docker import DockerClient
from test.python.docker import Podman, common, constant
from test.python.docker import Podman, constant
from test.python.docker.compat import common
class TestSystem(unittest.TestCase):

View File

@ -0,0 +1,6 @@
docker~=4.4.3
requests~=2.20.0
setuptools~=50.3.2
python-dateutil~=2.8.1
PyYAML~=5.4.1