From f32d39230d0ee949bdb996be3aea2c8f3dd35dd3 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Mon, 11 Jan 2021 16:28:18 -0800 Subject: [PATCH] Move some helper functions to base class The ImageImportLocking test class has some useful helper functions that are general enough to be used elsewhere. This moves those to the SynchronousAPIBase test class. Change-Id: Idbc6ab16ecac2eb9c83c58432f98414d68e16a52 --- glance/tests/functional/__init__.py | 65 +++++++++++++++++++ .../v2/test_images_import_locking.py | 65 ------------------- 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/glance/tests/functional/__init__.py b/glance/tests/functional/__init__.py index 914d35157b..042f9ca7ee 100644 --- a/glance/tests/functional/__init__.py +++ b/glance/tests/functional/__init__.py @@ -34,6 +34,7 @@ import socket import subprocess import sys import tempfile +from testtools import content as ttc import textwrap import time from unittest import mock @@ -1683,3 +1684,67 @@ class SynchronousAPIBase(test_utils.BaseTestCase): :returns: A webob.Response object """ return self.api_request('DELETE', url, heaers=headers) + + def _import_copy(self, image_id, stores): + """Do an import of image_id to the given stores.""" + body = {'method': {'name': 'copy-image'}, + 'stores': stores, + 'all_stores': False} + + return self.api_post( + '/v2/images/%s/import' % image_id, + json=body) + + def _import_direct(self, image_id, stores): + """Do an import of image_id to the given stores.""" + body = {'method': {'name': 'glance-direct'}, + 'stores': stores, + 'all_stores': False} + + return self.api_post( + '/v2/images/%s/import' % image_id, + json=body) + + def _create_and_stage(self, data_iter=None): + resp = self.api_post('/v2/images', + json={'name': 'foo', + 'container_format': 'bare', + 'disk_format': 'raw'}) + image = jsonutils.loads(resp.text) + + if data_iter: + resp = self.api_put( + '/v2/images/%s/stage' % image['id'], + headers={'Content-Type': 'application/octet-stream'}, + body_file=data_iter) + else: + resp = self.api_put( + '/v2/images/%s/stage' % image['id'], + headers={'Content-Type': 'application/octet-stream'}, + data=b'IMAGEDATA') + self.assertEqual(204, resp.status_code) + + return image['id'] + + def _create_and_import(self, stores=[], data_iter=None): + """Create an image, stage data, and import into the given stores. + + :returns: image_id + """ + image_id = self._create_and_stage(data_iter=data_iter) + + resp = self._import_direct(image_id, stores) + self.assertEqual(202, resp.status_code) + + # Make sure it goes active + for i in range(0, 10): + image = self.api_get('/v2/images/%s' % image_id).json + if not image.get('os_glance_import_task'): + break + self.addDetail('Create-Import task id', + ttc.text_content(image['os_glance_import_task'])) + time.sleep(1) + + self.assertEqual('active', image['status']) + + return image_id diff --git a/glance/tests/functional/v2/test_images_import_locking.py b/glance/tests/functional/v2/test_images_import_locking.py index 11848022d0..108bebce09 100644 --- a/glance/tests/functional/v2/test_images_import_locking.py +++ b/glance/tests/functional/v2/test_images_import_locking.py @@ -20,7 +20,6 @@ from unittest import mock import uuid from oslo_log import log as logging -from oslo_serialization import jsonutils from oslo_utils import fixture as time_fixture from oslo_utils import units @@ -32,70 +31,6 @@ LOG = logging.getLogger(__name__) class TestImageImportLocking(functional.SynchronousAPIBase): - def _import_copy(self, image_id, stores): - """Do an import of image_id to the given stores.""" - body = {'method': {'name': 'copy-image'}, - 'stores': stores, - 'all_stores': False} - - return self.api_post( - '/v2/images/%s/import' % image_id, - json=body) - - def _import_direct(self, image_id, stores): - """Do an import of image_id to the given stores.""" - body = {'method': {'name': 'glance-direct'}, - 'stores': stores, - 'all_stores': False} - - return self.api_post( - '/v2/images/%s/import' % image_id, - json=body) - - def _create_and_stage(self, data_iter=None): - resp = self.api_post('/v2/images', - json={'name': 'foo', - 'container_format': 'bare', - 'disk_format': 'raw'}) - image = jsonutils.loads(resp.text) - - if data_iter: - resp = self.api_put( - '/v2/images/%s/stage' % image['id'], - headers={'Content-Type': 'application/octet-stream'}, - body_file=data_iter) - else: - resp = self.api_put( - '/v2/images/%s/stage' % image['id'], - headers={'Content-Type': 'application/octet-stream'}, - data=b'IMAGEDATA') - self.assertEqual(204, resp.status_code) - - return image['id'] - - def _create_and_import(self, stores=[], data_iter=None): - """Create an image, stage data, and import into the given stores. - - :returns: image_id - """ - image_id = self._create_and_stage(data_iter=data_iter) - - resp = self._import_direct(image_id, stores) - self.assertEqual(202, resp.status_code) - - # Make sure it goes active - for i in range(0, 10): - image = self.api_get('/v2/images/%s' % image_id).json - if not image.get('os_glance_import_task'): - break - self.addDetail('Create-Import task id', - ttc.text_content(image['os_glance_import_task'])) - time.sleep(1) - - self.assertEqual('active', image['status']) - - return image_id - def _get_image_import_task(self, image_id, task_id=None): if task_id is None: image = self.api_get('/v2/images/%s' % image_id).json