Improve tmpfile cleanup in functests
This change replaces when possible homemade temporary file management by tempfile.NamedTemporaryFile[1][2] and defines only when needed a cleanup for a temporary file[2]. [1] functional/tests/compute/v2/test_keypair.py [2] functional/tests/object/v1/test_object.py Change-Id: I728ab96381ca9f3fd1f899dd50e5ceb5e97b9397
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import os
|
import tempfile
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from functional.common import test
|
from functional.common import test
|
||||||
@@ -44,19 +44,18 @@ class KeypairTests(test.TestCase):
|
|||||||
cls.assertOutput('', raw_output)
|
cls.assertOutput('', raw_output)
|
||||||
|
|
||||||
def test_keypair_create(self):
|
def test_keypair_create(self):
|
||||||
TMP_FILE = uuid.uuid4().hex
|
with tempfile.NamedTemporaryFile() as f:
|
||||||
self.addCleanup(os.remove, TMP_FILE)
|
|
||||||
with open(TMP_FILE, 'w') as f:
|
|
||||||
f.write(PUBLIC_KEY)
|
f.write(PUBLIC_KEY)
|
||||||
|
f.flush()
|
||||||
|
|
||||||
raw_output = self.openstack(
|
raw_output = self.openstack(
|
||||||
'keypair create --public-key ' + TMP_FILE + ' tmpkey',
|
'keypair create --public-key %s tmpkey' % f.name,
|
||||||
)
|
)
|
||||||
self.addCleanup(
|
self.addCleanup(
|
||||||
self.openstack,
|
self.openstack,
|
||||||
'keypair delete tmpkey',
|
'keypair delete tmpkey',
|
||||||
)
|
)
|
||||||
self.assertIn('tmpkey', raw_output)
|
self.assertIn('tmpkey', raw_output)
|
||||||
|
|
||||||
def test_keypair_list(self):
|
def test_keypair_list(self):
|
||||||
opts = self.get_list_opts(self.HEADERS)
|
opts = self.get_list_opts(self.HEADERS)
|
||||||
|
@@ -11,6 +11,7 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import tempfile
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from functional.common import test
|
from functional.common import test
|
||||||
@@ -24,17 +25,14 @@ class ObjectTests(test.TestCase):
|
|||||||
"""Functional tests for Object commands. """
|
"""Functional tests for Object commands. """
|
||||||
|
|
||||||
CONTAINER_NAME = uuid.uuid4().hex
|
CONTAINER_NAME = uuid.uuid4().hex
|
||||||
OBJECT_NAME = uuid.uuid4().hex
|
|
||||||
TMP_FILE = 'tmp.txt'
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
super(ObjectTests, self).setUp()
|
|
||||||
self.addCleanup(os.remove, self.OBJECT_NAME)
|
|
||||||
self.addCleanup(os.remove, self.TMP_FILE)
|
|
||||||
with open(self.OBJECT_NAME, 'w') as f:
|
|
||||||
f.write('test content')
|
|
||||||
|
|
||||||
def test_object(self):
|
def test_object(self):
|
||||||
|
with tempfile.NamedTemporaryFile() as f:
|
||||||
|
f.write('test content')
|
||||||
|
f.flush()
|
||||||
|
self._test_object(f.name)
|
||||||
|
|
||||||
|
def _test_object(self, object_file):
|
||||||
raw_output = self.openstack('container create ' + self.CONTAINER_NAME)
|
raw_output = self.openstack('container create ' + self.CONTAINER_NAME)
|
||||||
items = self.parse_listing(raw_output)
|
items = self.parse_listing(raw_output)
|
||||||
self.assert_show_fields(items, CONTAINER_FIELDS)
|
self.assert_show_fields(items, CONTAINER_FIELDS)
|
||||||
@@ -50,7 +48,7 @@ class ObjectTests(test.TestCase):
|
|||||||
# TODO(stevemar): Assert returned fields
|
# TODO(stevemar): Assert returned fields
|
||||||
|
|
||||||
raw_output = self.openstack('object create ' + self.CONTAINER_NAME
|
raw_output = self.openstack('object create ' + self.CONTAINER_NAME
|
||||||
+ ' ' + self.OBJECT_NAME)
|
+ ' ' + object_file)
|
||||||
items = self.parse_listing(raw_output)
|
items = self.parse_listing(raw_output)
|
||||||
self.assert_show_fields(items, OBJECT_FIELDS)
|
self.assert_show_fields(items, OBJECT_FIELDS)
|
||||||
|
|
||||||
@@ -59,23 +57,25 @@ class ObjectTests(test.TestCase):
|
|||||||
self.assert_table_structure(items, BASIC_LIST_HEADERS)
|
self.assert_table_structure(items, BASIC_LIST_HEADERS)
|
||||||
|
|
||||||
self.openstack('object save ' + self.CONTAINER_NAME
|
self.openstack('object save ' + self.CONTAINER_NAME
|
||||||
+ ' ' + self.OBJECT_NAME)
|
+ ' ' + object_file)
|
||||||
# TODO(stevemar): Assert returned fields
|
# TODO(stevemar): Assert returned fields
|
||||||
|
|
||||||
|
tmp_file = 'tmp.txt'
|
||||||
|
self.addCleanup(os.remove, tmp_file)
|
||||||
self.openstack('object save ' + self.CONTAINER_NAME
|
self.openstack('object save ' + self.CONTAINER_NAME
|
||||||
+ ' ' + self.OBJECT_NAME + ' --file ' + self.TMP_FILE)
|
+ ' ' + object_file + ' --file ' + tmp_file)
|
||||||
# TODO(stevemar): Assert returned fields
|
# TODO(stevemar): Assert returned fields
|
||||||
|
|
||||||
self.openstack('object show ' + self.CONTAINER_NAME
|
self.openstack('object show ' + self.CONTAINER_NAME
|
||||||
+ ' ' + self.OBJECT_NAME)
|
+ ' ' + object_file)
|
||||||
# TODO(stevemar): Assert returned fields
|
# TODO(stevemar): Assert returned fields
|
||||||
|
|
||||||
raw_output = self.openstack('object delete ' + self.CONTAINER_NAME
|
raw_output = self.openstack('object delete ' + self.CONTAINER_NAME
|
||||||
+ ' ' + self.OBJECT_NAME)
|
+ ' ' + object_file)
|
||||||
self.assertEqual(0, len(raw_output))
|
self.assertEqual(0, len(raw_output))
|
||||||
|
|
||||||
self.openstack('object create ' + self.CONTAINER_NAME
|
self.openstack('object create ' + self.CONTAINER_NAME
|
||||||
+ ' ' + self.OBJECT_NAME)
|
+ ' ' + object_file)
|
||||||
raw_output = self.openstack('container delete -r ' +
|
raw_output = self.openstack('container delete -r ' +
|
||||||
self.CONTAINER_NAME)
|
self.CONTAINER_NAME)
|
||||||
self.assertEqual(0, len(raw_output))
|
self.assertEqual(0, len(raw_output))
|
||||||
|
Reference in New Issue
Block a user