sq: add some missing tests
Change-Id: I0509ceb638f9c7509e272b2b4bacce99b65287f9
This commit is contained in:
parent
754d2f521a
commit
96ad562422
|
@ -27,11 +27,12 @@ import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
import textwrap
|
import textwrap
|
||||||
from time import localtime, mktime, strftime, strptime
|
from time import time, localtime, mktime, strftime, strptime
|
||||||
from requests.exceptions import RequestException
|
from requests.exceptions import RequestException
|
||||||
from urllib3.exceptions import HTTPError
|
from urllib3.exceptions import HTTPError
|
||||||
|
|
||||||
import swiftclient
|
import swiftclient
|
||||||
|
from swiftclient.client import _ObjectBody
|
||||||
from swiftclient.service import SwiftError
|
from swiftclient.service import SwiftError
|
||||||
import swiftclient.shell
|
import swiftclient.shell
|
||||||
import swiftclient.utils
|
import swiftclient.utils
|
||||||
|
@ -135,6 +136,11 @@ class TestShell(unittest.TestCase):
|
||||||
super(TestShell, self).setUp()
|
super(TestShell, self).setUp()
|
||||||
tmpfile = tempfile.NamedTemporaryFile(delete=False)
|
tmpfile = tempfile.NamedTemporaryFile(delete=False)
|
||||||
self.tmpfile = tmpfile.name
|
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):
|
def tearDown(self):
|
||||||
try:
|
try:
|
||||||
|
@ -337,6 +343,44 @@ class TestShell(unittest.TestCase):
|
||||||
query_string='part-number=20')],
|
query_string='part-number=20')],
|
||||||
connection.return_value.head_object.mock_calls)
|
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')
|
@mock.patch('swiftclient.service.Connection')
|
||||||
def test_stat_object(self, connection):
|
def test_stat_object(self, connection):
|
||||||
return_headers = {
|
return_headers = {
|
||||||
|
@ -739,22 +783,54 @@ class TestShell(unittest.TestCase):
|
||||||
"--part-number option only allowed for "
|
"--part-number option only allowed for "
|
||||||
"object downloads")
|
"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.head_object.return_value = {}
|
||||||
connection.return_value.get_object.return_value = {}, ''
|
connection.return_value.get_object.return_value = {}, ''
|
||||||
connection.return_value.attempts = 0
|
connection.return_value.attempts = 0
|
||||||
with CaptureOutput():
|
with CaptureOutput():
|
||||||
swiftclient.shell.main(argv)
|
swiftclient.shell.main(argv)
|
||||||
self.assertEqual([mock.call(' --part-number', '7',
|
self.assertEqual([
|
||||||
headers={}, response_dict={},
|
mock.call('container', 'object', headers={}, response_dict={},
|
||||||
resp_chunk_size=65536),
|
resp_chunk_size=65536, query_string='part-number=7')
|
||||||
mock.call(' --part-number', 'container',
|
], connection.return_value.get_object.mock_calls)
|
||||||
headers={}, response_dict={},
|
|
||||||
resp_chunk_size=65536),
|
@mock.patch('swiftclient.service.Connection')
|
||||||
mock.call(' --part-number', 'object',
|
def test_download_version_id_and_part_number(self, connection_cls):
|
||||||
headers={}, response_dict={},
|
now = time()
|
||||||
resp_chunk_size=65536)],
|
t_start, t_auth, t_headers, t_finish = [now + (i * 0.01)
|
||||||
connection.return_value.get_object.mock_calls)
|
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.makedirs')
|
||||||
@mock.patch('swiftclient.service.Connection')
|
@mock.patch('swiftclient.service.Connection')
|
||||||
|
|
Loading…
Reference in New Issue