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:
Cedric Brandily 2016-04-06 00:37:16 +02:00
parent 139a45bb71
commit 3c82c0e62e
2 changed files with 26 additions and 27 deletions

View File

@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import tempfile
import uuid
from functional.common import test
@ -44,19 +44,18 @@ class KeypairTests(test.TestCase):
cls.assertOutput('', raw_output)
def test_keypair_create(self):
TMP_FILE = uuid.uuid4().hex
self.addCleanup(os.remove, TMP_FILE)
with open(TMP_FILE, 'w') as f:
with tempfile.NamedTemporaryFile() as f:
f.write(PUBLIC_KEY)
f.flush()
raw_output = self.openstack(
'keypair create --public-key ' + TMP_FILE + ' tmpkey',
)
self.addCleanup(
self.openstack,
'keypair delete tmpkey',
)
self.assertIn('tmpkey', raw_output)
raw_output = self.openstack(
'keypair create --public-key %s tmpkey' % f.name,
)
self.addCleanup(
self.openstack,
'keypair delete tmpkey',
)
self.assertIn('tmpkey', raw_output)
def test_keypair_list(self):
opts = self.get_list_opts(self.HEADERS)

View File

@ -11,6 +11,7 @@
# under the License.
import os
import tempfile
import uuid
from functional.common import test
@ -24,17 +25,14 @@ class ObjectTests(test.TestCase):
"""Functional tests for Object commands. """
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):
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)
items = self.parse_listing(raw_output)
self.assert_show_fields(items, CONTAINER_FIELDS)
@ -50,7 +48,7 @@ class ObjectTests(test.TestCase):
# TODO(stevemar): Assert returned fields
raw_output = self.openstack('object create ' + self.CONTAINER_NAME
+ ' ' + self.OBJECT_NAME)
+ ' ' + object_file)
items = self.parse_listing(raw_output)
self.assert_show_fields(items, OBJECT_FIELDS)
@ -59,23 +57,25 @@ class ObjectTests(test.TestCase):
self.assert_table_structure(items, BASIC_LIST_HEADERS)
self.openstack('object save ' + self.CONTAINER_NAME
+ ' ' + self.OBJECT_NAME)
+ ' ' + object_file)
# TODO(stevemar): Assert returned fields
tmp_file = 'tmp.txt'
self.addCleanup(os.remove, tmp_file)
self.openstack('object save ' + self.CONTAINER_NAME
+ ' ' + self.OBJECT_NAME + ' --file ' + self.TMP_FILE)
+ ' ' + object_file + ' --file ' + tmp_file)
# TODO(stevemar): Assert returned fields
self.openstack('object show ' + self.CONTAINER_NAME
+ ' ' + self.OBJECT_NAME)
+ ' ' + object_file)
# TODO(stevemar): Assert returned fields
raw_output = self.openstack('object delete ' + self.CONTAINER_NAME
+ ' ' + self.OBJECT_NAME)
+ ' ' + object_file)
self.assertEqual(0, len(raw_output))
self.openstack('object create ' + self.CONTAINER_NAME
+ ' ' + self.OBJECT_NAME)
+ ' ' + object_file)
raw_output = self.openstack('container delete -r ' +
self.CONTAINER_NAME)
self.assertEqual(0, len(raw_output))