Skip to content

Commit

Permalink
Refactor python tests to run against python3.9
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
jwhonce committed Mar 1, 2021
1 parent 73044b2 commit 7927fe0
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 33 deletions.
49 changes: 36 additions & 13 deletions test/apiv2/rest_api/test_rest_v2_0_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def setUpClass(cls):
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)

Expand Down Expand Up @@ -241,7 +243,9 @@ def test_post_create_compat_connect(self):

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(
Expand Down Expand Up @@ -450,11 +454,15 @@ def test_history_compat(self):
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,))
Expand Down Expand Up @@ -484,18 +492,23 @@ def test_network_compat(self):
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")
self.assertEqual(prune.status_code, 200, prune.content)
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)
Expand All @@ -511,7 +524,9 @@ def test_volumes_compat(self):
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
Expand Down Expand Up @@ -688,15 +703,21 @@ def test_pod_start_conflict(self):
"""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)
Expand All @@ -715,7 +736,9 @@ def test_pod_start_conflict(self):
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)
Expand Down
Empty file added test/python/__init__.py
Empty file.
14 changes: 10 additions & 4 deletions test/python/docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from docker import DockerClient

from test.python.docker import constant
from .compat import constant


class Podman(object):
Expand Down Expand Up @@ -39,7 +39,9 @@ def __init__(self):
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(
{
Expand All @@ -51,10 +53,14 @@ def __init__(self):
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(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Empty file.
Original file line number Diff line number Diff line change
@@ -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

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

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):
Expand Down Expand Up @@ -87,9 +88,11 @@ def test_start_container(self):
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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

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):
Expand Down Expand Up @@ -78,7 +79,9 @@ def test_list_images(self):
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"""
Expand All @@ -91,7 +94,7 @@ def test_search_bogus_image(self):
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"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

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):
Expand Down
6 changes: 6 additions & 0 deletions test/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7927fe0

Please sign in to comment.