Make db archival return a meaningful result code

This helps with automating to know when it's done. Update our own
post-test-hook to use it.

Change-Id: I61c3b187edab3e0e612470735eecd2db59028413
This commit is contained in:
Dan Smith 2016-09-28 07:54:56 -07:00
parent d5f113de34
commit 85bf71b2ae
3 changed files with 21 additions and 9 deletions

View File

@ -832,16 +832,20 @@ class DbCommands(object):
def archive_deleted_rows(self, max_rows, verbose=False):
"""Move up to max_rows deleted rows from production tables to shadow
tables.
Returns 0 if nothing was archived, 1 if some number of rows were
archived, 2 if max_rows is invalid. If automating, this should be
run continuously while the result is 1, stopping at 0.
"""
if max_rows is not None:
max_rows = int(max_rows)
if max_rows < 0:
print(_("Must supply a positive value for max_rows"))
return(1)
return(2)
if max_rows > db.MAX_INT:
print(_('max rows must be <= %(max_value)d') %
{'max_value': db.MAX_INT})
return(1)
return(2)
table_to_rows_archived = db.archive_deleted_rows(max_rows)
if verbose:
if table_to_rows_archived:
@ -849,6 +853,8 @@ class DbCommands(object):
dict_value=_('Number of Rows Archived'))
else:
print(_('Nothing was archived.'))
# NOTE(danms): Return nonzero if we archived something
return int(bool(table_to_rows_archived))
@args('--delete', action='store_true', dest='delete',
help='If specified, automatically delete any records found where '

View File

@ -469,16 +469,16 @@ class DBCommandsTestCase(test.NoDBTestCase):
self.commands = manage.DbCommands()
def test_archive_deleted_rows_negative(self):
self.assertEqual(1, self.commands.archive_deleted_rows(-1))
self.assertEqual(2, self.commands.archive_deleted_rows(-1))
def test_archive_deleted_rows_large_number(self):
large_number = '1' * 100
self.assertEqual(1, self.commands.archive_deleted_rows(large_number))
self.assertEqual(2, self.commands.archive_deleted_rows(large_number))
@mock.patch.object(db, 'archive_deleted_rows',
return_value=dict(instances=10, consoles=5))
def _test_archive_deleted_rows(self, mock_db_archive, verbose=False):
self.commands.archive_deleted_rows(20, verbose=verbose)
result = self.commands.archive_deleted_rows(20, verbose=verbose)
mock_db_archive.assert_called_once_with(20)
output = self.output.getvalue()
if verbose:
@ -493,6 +493,7 @@ class DBCommandsTestCase(test.NoDBTestCase):
self.assertEqual(expected, output)
else:
self.assertEqual(0, len(output))
self.assertEqual(1, result)
def test_archive_deleted_rows(self):
# Tests that we don't show any table output (not verbose).
@ -504,10 +505,11 @@ class DBCommandsTestCase(test.NoDBTestCase):
@mock.patch.object(db, 'archive_deleted_rows', return_value={})
def test_archive_deleted_rows_verbose_no_results(self, mock_db_archive):
self.commands.archive_deleted_rows(20, verbose=True)
result = self.commands.archive_deleted_rows(20, verbose=True)
mock_db_archive.assert_called_once_with(20)
output = self.output.getvalue()
self.assertIn('Nothing was archived.', output)
self.assertEqual(0, result)
@mock.patch.object(migration, 'db_null_instance_uuid_scan',
return_value={'foo': 0})

View File

@ -6,9 +6,13 @@ function archive_deleted_rows {
# NOTE(danms): Run this a few times to make sure that we end
# up with nothing more to archive
for i in `seq 30`; do
out=$($MANAGE db archive_deleted_rows --verbose --max_rows 1000)
echo $?
if [[ $out =~ "Nothing was archived" ]]; then
$MANAGE db archive_deleted_rows --verbose --max_rows 1000
RET=$?
if [[ $RET -gt 1 ]]; then
echo Archiving failed with result $RET
return $RET
elif [[ $RET -eq 0 ]]; then
echo Archiving Complete
break;
fi
done