Skip to content

Commit

Permalink
images_test: import units
Browse files Browse the repository at this point in the history
Import and use units constants in the test to improve
readability.

Signed-off-by: Albert Esteve <[email protected]>
  • Loading branch information
aesteve-rh committed Jul 29, 2022
1 parent ccb022c commit 8ff7393
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions test/handlers/images_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from ovirt_imageio._internal import config
from ovirt_imageio._internal import server
from ovirt_imageio._internal.units import KiB, MiB

from .. import testutil
from .. import http
Expand Down Expand Up @@ -236,7 +237,7 @@ def test_upload_invalid_range(tmpdir, srv, client, content_range):


def test_upload_close_connection(tmpdir, srv, client):
image_size = 4096
image_size = 4 * KiB
image = testutil.create_tempfile(tmpdir, "image", b"a" * image_size)
ticket = testutil.create_ticket(
url="file://" + str(image), size=image_size)
Expand Down Expand Up @@ -296,24 +297,24 @@ def test_download(tmpdir, srv, client, rng, start, end):

def test_download_image_size_gt_ticket_size(tmpdir, srv, client):
image = testutil.create_tempfile(tmpdir, "image", size=8192)
ticket = testutil.create_ticket(url="file://" + str(image), size=4096)
ticket = testutil.create_ticket(url="file://" + str(image), size=4 * KiB)
srv.auth.add(ticket)
res = client.get("/images/" + ticket["uuid"])
assert res.status == 200
assert len(res.read()) == 4096
assert len(res.read()) == 4 * KiB


def test_download_ticket_size_gt_image_size(tmpdir, srv, client):
image = testutil.create_tempfile(tmpdir, "image", size=4096)
image = testutil.create_tempfile(tmpdir, "image", size=4 * KiB)
ticket = testutil.create_ticket(url="file://" + str(image), size=8192)
srv.auth.add(ticket)
res = client.get("/images/" + ticket["uuid"])
assert res.status == 200
assert len(res.read()) == 4096
assert len(res.read()) == 4 * KiB


def test_download_range_forbidden(tmpdir, srv, client):
image = testutil.create_tempfile(tmpdir, "image", size=4096)
image = testutil.create_tempfile(tmpdir, "image", size=4 * KiB)
ticket = testutil.create_ticket(url="file://" + str(image), size=8192)
srv.auth.add(ticket)
res = client.get("/images/" + ticket["uuid"],
Expand All @@ -324,7 +325,7 @@ def test_download_range_forbidden(tmpdir, srv, client):

def test_download_range_unavailable(tmpdir, srv, client):
image = testutil.create_tempfile(tmpdir, "image", size=8192)
ticket = testutil.create_ticket(url="file://" + str(image), size=4096)
ticket = testutil.create_ticket(url="file://" + str(image), size=4 * KiB)
srv.auth.add(ticket)
res = client.get("/images/" + ticket["uuid"],
headers={"Range": "bytes=0-4096"})
Expand All @@ -333,7 +334,7 @@ def test_download_range_unavailable(tmpdir, srv, client):


def test_download_no_range(tmpdir, srv, client):
size = 1024
size = KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(url="file://" + str(image), size=size)
srv.auth.add(ticket)
Expand All @@ -344,7 +345,7 @@ def test_download_no_range(tmpdir, srv, client):


def test_download_extends_ticket(tmpdir, srv, client, fake_time):
size = 1024
size = KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(url="file://" + str(image), size=size)
srv.auth.add(ticket)
Expand Down Expand Up @@ -381,7 +382,7 @@ def test_download_partial_not_satistieble(tmpdir, srv, client):
# actual image size reported by vdsm - one byte difference is enough to
# cause a failure.
# See https://bugzilla.redhat.com/1512315.
size = 1024
size = KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(url="file://" + str(image), size=size + 1)
srv.auth.add(ticket)
Expand All @@ -397,31 +398,31 @@ def test_download_partial_no_range(tmpdir, srv, client):
# is only an upper limit. Or maybe we should treat the ticket size as the
# expected size?
# This is another variant of https://bugzilla.redhat.com/1512315.
size = 1024
size = KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(url="file://" + str(image), size=size + 1)
srv.auth.add(ticket)
res = client.get("/images/" + ticket["uuid"])
assert res.status == http_client.OK
# Should return the available image data, not the ticket size. Reading
# this response will fail with IncompleteRead.
assert res.length == 1024
assert res.length == KiB


def test_download_partial_no_range_empty(tmpdir, srv, client):
# Image is empty, no range, should return an empty file - we return invalid
# http response that fail on the client side with BadStatusLine: ''.
# See https://bugzilla.redhat.com/1512312
image = testutil.create_tempfile(tmpdir, "image") # Empty image
ticket = testutil.create_ticket(url="file://" + str(image), size=1024)
ticket = testutil.create_ticket(url="file://" + str(image), size=KiB)
srv.auth.add(ticket)
res = client.get("/images/" + ticket["uuid"])
assert res.status == http_client.OK
assert res.length == 0


def test_download_no_range_end(tmpdir, srv, client):
size = 1024
size = KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(url="file://" + str(image), size=size)
srv.auth.add(ticket)
Expand All @@ -433,7 +434,7 @@ def test_download_no_range_end(tmpdir, srv, client):


def test_download_holes(tmpdir, srv, client):
size = 1024
size = KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(url="file://" + str(image), size=size)
srv.auth.add(ticket)
Expand All @@ -445,7 +446,7 @@ def test_download_holes(tmpdir, srv, client):


def test_download_filename_in_ticket(tmpdir, srv, client):
size = 1024
size = KiB
filename = "\u05d0.raw" # hebrew aleph
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(url="file://" + str(image), size=size,
Expand All @@ -472,7 +473,7 @@ def test_download_out_of_range(tmpdir, srv, client, rng, end):

def test_download_progress(tmpdir, srv, client, monkeypatch):
# We need to read at least one buffer to update the transfered value.
monkeypatch.setattr(srv.config.backend_file, "buffer_size", 1024**2)
monkeypatch.setattr(srv.config.backend_file, "buffer_size", MiB)

# And we need to request enough data so the server does not complete before
# the client read all the data.
Expand Down Expand Up @@ -511,7 +512,7 @@ def test_download_progress(tmpdir, srv, client, monkeypatch):


def test_download_close_connection(tmpdir, srv, client):
image_size = 4096
image_size = 4 * KiB
image = testutil.create_tempfile(tmpdir, "image", b"a" * image_size)
ticket = testutil.create_ticket(
url="file://" + str(image), size=image_size)
Expand Down Expand Up @@ -705,7 +706,7 @@ def test_options_all(srv, client):


def test_options_read_write(srv, client, tmpdir):
size = 128 * 1024
size = 128 * KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(
url="file://" + str(image), size=size, ops=["read", "write"])
Expand All @@ -721,7 +722,7 @@ def test_options_read_write(srv, client, tmpdir):


def test_options_read(srv, client, tmpdir):
size = 128 * 1024
size = 128 * KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(
url="file://" + str(image), size=size, ops=["read"])
Expand All @@ -737,7 +738,7 @@ def test_options_read(srv, client, tmpdir):


def test_options_write(srv, client, tmpdir):
size = 128 * 1024
size = 128 * KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(
url="file://" + str(image), size=size, ops=["write"])
Expand All @@ -754,7 +755,7 @@ def test_options_write(srv, client, tmpdir):


def test_options_extends_ticket(srv, client, tmpdir, fake_time):
size = 128 * 1024
size = 128 * KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(url="file://" + str(image), size=size)
srv.auth.add(ticket)
Expand Down Expand Up @@ -786,7 +787,7 @@ def test_options_for_nonexistent_ticket(srv, client):


def test_options_ticket_expired(srv, client, tmpdir, fake_time):
size = 128 * 1024
size = 128 * KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(
url="file://" + str(image), size=size, timeout=300)
Expand Down Expand Up @@ -828,9 +829,9 @@ def test_response_version_error(tmpdir, srv, client):
])
def test_keep_alive_connection_on_success(tmpdir, srv, client, method, body):
# After successful request the connection should remain open.
image = testutil.create_tempfile(tmpdir, "image", size=1024)
image = testutil.create_tempfile(tmpdir, "image", size=KiB)
ticket = testutil.create_ticket(url="file://" + str(image),
size=1024)
size=KiB)
srv.auth.add(ticket)
uri = "/images/%(uuid)s" % ticket
# Disabling auto_open so we can test if a connection was closed.
Expand Down Expand Up @@ -915,7 +916,7 @@ def test_cors_options_all(srv, client):


def test_cors_get_ok(tmpdir, srv, client):
size = 8192
size = 8 * KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(url="file://" + str(image), size=size)
srv.auth.add(ticket)
Expand Down Expand Up @@ -944,7 +945,7 @@ def test_cors_get_error(srv, client):


def test_cors_put_ok(tmpdir, srv, client):
size = 8192
size = 8 * KiB
image = testutil.create_tempfile(tmpdir, "image", size=size)
ticket = testutil.create_ticket(url="file://" + str(image), size=size)
srv.auth.add(ticket)
Expand Down

0 comments on commit 8ff7393

Please sign in to comment.