Raise tooz error when unexpected last entries found

When the file driver is deleting a group, the only
thing left in that group directory should be the
group metadata file, so before deleting the group
add a check that makes sure the leftover file is
actually the metadata file (and not something else).

Change-Id: I63a1e2cf95d524fea5e1c009ecc951670e2e8f5c
This commit is contained in:
Joshua Harlow
2016-07-26 23:54:09 -07:00
parent d6b710fe1b
commit 65f9f7a16a
2 changed files with 28 additions and 0 deletions

View File

@@ -449,6 +449,11 @@ class FileDriver(coordination._RunWatchersMixin,
else:
if len(entries) > 1:
raise coordination.GroupNotEmpty(group_id)
elif len(entries) == 1 and entries != ['.metadata']:
raise coordination.ToozError(
"Unexpected path '%s' found in"
" group directory '%s' (expected to only find"
" a '.metadata' path)" % (entries[0], group_dir))
else:
try:
shutil.rmtree(group_dir)

View File

@@ -15,6 +15,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import fixtures
import mock
from testtools import testcase
@@ -32,6 +35,26 @@ class TestFileDriver(testcase.TestCase):
coord = coordination.get_coordinator(url, self._FAKE_MEMBER_ID)
self.assertEqual(file_path, coord._dir)
def test_leftover_file(self):
fixture = self.useFixture(fixtures.TempDir())
file_path = fixture.path
url = 'file://%s' % file_path
coord = coordination.get_coordinator(url, self._FAKE_MEMBER_ID)
coord.start()
self.addCleanup(coord.stop)
coord.create_group(b"my_group").get()
safe_group_id = coord._make_filesystem_safe(b"my_group")
with open(os.path.join(file_path, 'groups',
safe_group_id, "junk.txt"), "wb"):
pass
os.unlink(os.path.join(file_path, 'groups',
safe_group_id, '.metadata'))
self.assertRaises(coordination.ToozError,
coord.delete_group(b"my_group").get)
@mock.patch('os.path.normpath', lambda x: x.replace('/', '\\'))
@mock.patch('sys.platform', 'win32')
def test_base_dir_win32(self):