sq: add some missing tests

Change-Id: I0509ceb638f9c7509e272b2b4bacce99b65287f9
This commit is contained in:
Clay Gerrard 2024-04-17 17:55:46 -05:00
parent 356f8ca57d
commit 4934ac1d8a
1 changed files with 88 additions and 12 deletions

View File

@ -27,11 +27,12 @@ import tempfile
import unittest
from unittest import mock
import textwrap
from time import localtime, mktime, strftime, strptime
from time import time, localtime, mktime, strftime, strptime
from requests.exceptions import RequestException
from urllib3.exceptions import HTTPError
import swiftclient
from swiftclient.client import _ObjectBody
from swiftclient.service import SwiftError
import swiftclient.shell
import swiftclient.utils
@ -135,6 +136,11 @@ class TestShell(unittest.TestCase):
super(TestShell, self).setUp()
tmpfile = tempfile.NamedTemporaryFile(delete=False)
self.tmpfile = tmpfile.name
self.tmpdir = tempfile.TemporaryDirectory()
self.tmpdir.__enter__()
self.addCleanup(lambda: self.tmpdir.__exit__(None, None, None))
# do not pollute the test running dir with stub downloads
os.chdir(self.tmpdir.name)
def tearDown(self):
try:
@ -337,6 +343,44 @@ class TestShell(unittest.TestCase):
query_string='part-number=20')],
connection.return_value.head_object.mock_calls)
@mock.patch('swiftclient.service.Connection')
def test_stat_version_id_and_part_number(self, connection_cls):
mock_conn = connection_cls.return_value
mock_conn.url = 'http://127.0.0.1/v1/AUTH_account'
mock_conn.head_object.return_value = CaseInsensitiveDict({
'X-Static-Large-Object': 'True',
'Content-Length': '1000000',
"ETag": '"cac0f63d4c7cfc6a4abdcb06b382e465"',
'X-Manifest-Etag': 'b4f746e58ea5ecfa2bc6383982d6570b',
'Content-Range': 'bytes 1000000-1999999/10000000',
'X-Parts-Count': '10',
'X-Object-Version-Id': '1713378563.05537',
})
argv = ["", "stat", "container", "object",
"--part-number", "2", "--version-id", "1713378563.05537"]
with CaptureOutput() as ctx:
swiftclient.shell.main(argv)
self.assertEqual([
mock.call('container', 'object', headers={},
query_string='version-id=1713378563.05537&part-number=2')
], mock_conn.head_object.call_args_list)
expected_lines = """
Account: AUTH_account
Container: container
Object: object
Content Length: 1000000
ETag: "cac0f63d4c7cfc6a4abdcb06b382e465"
X-Static-Large-Object: True
X-Object-Version-Id: 1713378563.05537
X-Manifest-Etag: b4f746e58ea5ecfa2bc6383982d6570b
Content-Range: bytes 1000000-1999999/10000000
X-Parts-Count: 10
"""
for line in expected_lines.splitlines():
expected = line.strip()
self.assertIn(expected, ctx.out, '\n' + ctx.out)
@mock.patch('swiftclient.service.Connection')
def test_stat_object(self, connection):
return_headers = {
@ -739,22 +783,54 @@ class TestShell(unittest.TestCase):
"--part-number option only allowed for "
"object downloads")
argv = ["", "download", " --part-number", "7", "container", "object"]
argv = ["", "download", "--part-number", "7", "container", "object"]
connection.return_value.head_object.return_value = {}
connection.return_value.get_object.return_value = {}, ''
connection.return_value.attempts = 0
with CaptureOutput():
swiftclient.shell.main(argv)
self.assertEqual([mock.call(' --part-number', '7',
headers={}, response_dict={},
resp_chunk_size=65536),
mock.call(' --part-number', 'container',
headers={}, response_dict={},
resp_chunk_size=65536),
mock.call(' --part-number', 'object',
headers={}, response_dict={},
resp_chunk_size=65536)],
connection.return_value.get_object.mock_calls)
self.assertEqual([
mock.call('container', 'object', headers={}, response_dict={},
resp_chunk_size=65536, query_string='part-number=7')
], connection.return_value.get_object.mock_calls)
@mock.patch('swiftclient.service.Connection')
def test_download_version_id_and_part_number(self, connection_cls):
now = time()
t_start, t_auth, t_headers, t_finish = [now + (i * 0.01)
for i in range(4)]
mock_conn = connection_cls.return_value
mock_conn.attempts = 0
mock_conn.url = 'http://127.0.0.1/v1/AUTH_account'
mock_conn.auth_end_time = t_auth
part_headers = {
'X-Static-Large-Object': 'True',
'Content-Length': '100000',
"ETag": '"cac0f63d4c7cfc6a4abdcb06b382e465"',
'X-Manifest-Etag': 'b4f746e58ea5ecfa2bc6383982d6570b',
'Content-Range': 'bytes 100000-199999/1000000',
'X-Parts-Count': '10',
'X-Object-Version-Id': '1713378563.05537',
}
part_body = _ObjectBody(io.BytesIO(b'a' * 100000),
chunk_size=65536, conn_to_close=None)
mock_conn.get_object.return_value = (
part_headers, part_body)
argv = ["", "download", "container", "obj",
"--part-number", "2", "--version-id", "1713378563.05537"]
with CaptureOutput() as ctx, \
mock.patch('swiftclient.service.time', side_effect=[
t_start, t_headers, t_finish]):
swiftclient.shell.main(argv)
self.assertEqual([
mock.call('container', 'obj', headers={}, response_dict={},
resp_chunk_size=65536,
query_string='version-id=1713378563.05537&part-number=2')
], mock_conn.get_object.mock_calls)
self.assertEqual(
'obj [auth 0.010s, headers 0.020s, total 0.030s, 5.000 MB/s]\n',
ctx.out)
@mock.patch('swiftclient.service.makedirs')
@mock.patch('swiftclient.service.Connection')