Add tests for get/put_archive with encoded data

* Add decorate to skip test case based on selected api
  version.
* If the selected api version is 1.24 or lower, run the test
  case to get/put_archive without encoding
* If the selected api version is 1.25 or higher, run the test
  case to get/put_archive with encoded data.

Related-Bug: #1789777
Change-Id: I3cfc8f0bf5c94d7ed63c111773cebd0aa2811787
This commit is contained in:
Hongbin Lu 2018-08-28 03:41:37 +00:00
parent 517dad8552
commit 37a5de6930
2 changed files with 82 additions and 0 deletions

View File

@ -642,6 +642,7 @@ class TestContainer(base.BaseZunTest):
self._ensure_network_attached(model, network)
@decorators.idempotent_id('037d800c-2262-4e15-90cd-95292b5ef958')
@utils.requires_microversion(min_microversion, '1.1', '1.24')
def test_put_and_get_archive_from_container(self):
_, model = self._run_container()
self.assertEqual(1, len(model.addresses))
@ -678,6 +679,45 @@ class TestContainer(base.BaseZunTest):
self.assertEqual(body['stat']['name'], tarinfo.name)
self.assertEqual(body['stat']['size'], tarinfo.size)
@decorators.idempotent_id('4ea3a2a5-cf89-48e7-bdd2-0bafc70ca7cb')
@utils.requires_microversion(min_microversion, '1.25')
def test_put_and_get_archive_from_container_encoded(self):
_, model = self._run_container()
self.assertEqual(1, len(model.addresses))
# Create a simple tarstream
file_content = 'Random text'
tarstream = BytesIO()
with tarfile.open(fileobj=tarstream, mode='w') as tar:
encoded_file_content = file_content.encode()
tarinfo = tarfile.TarInfo(name='test.txt')
tarinfo.size = len(encoded_file_content)
tarinfo.mtime = time.time()
tar.addfile(tarinfo, BytesIO(encoded_file_content))
# We're at the end of the tarstream, go back to the beginning
tarstream.seek(0)
req_body = json.dump_as_bytes({
'data': utils.encode_file_data(tarstream.getvalue())})
resp, _ = self.container_client.put_archive(
model.uuid, params={'path': '/tmp'}, body=req_body)
self.assertEqual(200, resp.status)
resp, body = self.container_client.get_archive(
model.uuid, params={'path': '/tmp/test.txt'})
self.assertEqual(200, resp.status)
# Get content
body = json.loads(body)
tardata = BytesIO(utils.decode_file_data(body['data']))
with tarfile.open(fileobj=tardata, mode='r') as tar:
untar_content = tar.extractfile('test.txt').read()
self.assertEqual(file_content, untar_content.decode())
self.assertEqual(body['stat']['name'], tarinfo.name)
self.assertEqual(body['stat']['size'], tarinfo.size)
def _ensure_network_detached(self, container, network):
def is_network_detached():
_, model = self.container_client.get_container(container.uuid)

View File

@ -10,8 +10,17 @@
# License for the specific language governing permissions and limitations
# under the License.
import base64
import functools
import time
import six
from tempest import config
from tempest.lib.common import api_version_utils
CONF = config.CONF
def wait_for_condition(condition, interval=2, timeout=60):
start_time = time.time()
@ -23,3 +32,36 @@ def wait_for_condition(condition, interval=2, timeout=60):
time.sleep(interval)
raise Exception(("Timed out after %s seconds. Started on %s and ended "
"on %s") % (timeout, start_time, end_time))
def requires_microversion(cls_min_version, min_version, max_version='latest',
**kwargs):
"""A decorator to skip tests if a microversion is not matched
@param extension
@param service
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*func_args, **func_kwargs):
selected_version = api_version_utils.select_request_microversion(
cls_min_version,
CONF.container_service.min_microversion)
api_version_utils.check_skip_with_microversion(
min_version,
max_version,
selected_version,
selected_version)
return func(*func_args, **func_kwargs)
return wrapper
return decorator
def encode_file_data(data):
if six.PY3 and isinstance(data, str):
data = data.encode('utf-8')
return base64.b64encode(data).decode('utf-8')
def decode_file_data(data):
return base64.b64decode(data)