Make sure all temp files get deleted in test_ringbuilder

This patch makes test_ringbuilder create a temporal directory,
run ring builder commands under it, and delete it after testing
for each test cases, to fix temp file leaking.

Change-Id: I6f59fe095ea6485af0e60b5a8e8fc3892e0a0f90
This commit is contained in:
Takashi Kajinami 2016-02-28 13:41:19 +09:00
parent 61928443ed
commit bd93d44bb4

View File

@ -22,6 +22,7 @@ import tempfile
import unittest
import uuid
import shlex
import shutil
from swift.cli import ringbuilder
from swift.common import exceptions
@ -76,12 +77,15 @@ class TestCommands(unittest.TestCase, RunSwiftRingBuilderMixin):
"127.0.0.1", "z0:6000", ":6000", "R127.0.0.1",
"127.0.0.1R127.0.0.1", "R:6000",
"_some meta data"]
tmpf = tempfile.NamedTemporaryFile()
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
tmpf = tempfile.NamedTemporaryFile(dir=self.tmpdir)
self.tempfile = self.tmpfile = tmpf.name
def tearDown(self):
try:
os.remove(self.tmpfile)
shutil.rmtree(self.tmpdir, True)
except OSError:
pass
@ -2092,12 +2096,15 @@ class TestRebalanceCommand(unittest.TestCase, RunSwiftRingBuilderMixin):
def __init__(self, *args, **kwargs):
super(TestRebalanceCommand, self).__init__(*args, **kwargs)
tmpf = tempfile.NamedTemporaryFile()
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
tmpf = tempfile.NamedTemporaryFile(dir=self.tmpdir)
self.tempfile = self.tmpfile = tmpf.name
def tearDown(self):
try:
os.remove(self.tempfile)
shutil.rmtree(self.tmpdir, True)
except OSError:
pass