Merge "Create policy_stat table in auditor if missing"

This commit is contained in:
Zuul 2018-06-14 07:58:26 +00:00 committed by Gerrit Code Review
commit de3c5054fa
2 changed files with 71 additions and 12 deletions

View File

@ -336,7 +336,12 @@ class AccountBroker(DatabaseBroker):
else: else:
columns.remove('container_count') columns.remove('container_count')
info = run_query() info = run_query()
elif "no such table: policy_stat" not in str(err): elif "no such table: policy_stat" in str(err):
if do_migrations:
self.create_policy_stat_table(conn)
info = run_query()
# else, pass and let the results be empty
else:
raise raise
policy_stats = {} policy_stats = {}

View File

@ -1174,22 +1174,28 @@ class TestAccountBrokerBeforeSPI(TestAccountBroker):
# first init an acct DB without the policy_stat table present # first init an acct DB without the policy_stat table present
broker = AccountBroker(db_path, account='a') broker = AccountBroker(db_path, account='a')
broker.initialize(Timestamp('1').internal) broker.initialize(Timestamp('1').internal)
with broker.get() as conn:
try: def confirm_no_table():
conn.execute(''' with broker.get() as conn:
SELECT * FROM policy_stat try:
''').fetchone()[0] conn.execute('''
except sqlite3.OperationalError as err: SELECT * FROM policy_stat
# confirm that the table really isn't there ''').fetchone()[0]
self.assertIn('no such table: policy_stat', str(err)) except sqlite3.OperationalError as err:
else: # confirm that the table really isn't there
self.fail('broker did not raise sqlite3.OperationalError ' self.assertIn('no such table: policy_stat', str(err))
'trying to select from policy_stat table!') else:
self.fail('broker did not raise sqlite3.OperationalError '
'trying to select from policy_stat table!')
confirm_no_table()
# make sure we can HEAD this thing w/o the table # make sure we can HEAD this thing w/o the table
stats = broker.get_policy_stats() stats = broker.get_policy_stats()
self.assertEqual(len(stats), 0) self.assertEqual(len(stats), 0)
confirm_no_table()
# now do a PUT to create the table # now do a PUT to create the table
broker.put_container('o', Timestamp.now().internal, 0, 0, 0, broker.put_container('o', Timestamp.now().internal, 0, 0, 0,
POLICIES.default.idx) POLICIES.default.idx)
@ -1202,6 +1208,54 @@ class TestAccountBrokerBeforeSPI(TestAccountBroker):
stats = broker.get_policy_stats() stats = broker.get_policy_stats()
self.assertEqual(len(stats), 1) self.assertEqual(len(stats), 1)
@with_tempdir
def test_policy_table_migration_in_get_policy_stats(self, tempdir):
db_path = os.path.join(tempdir, 'account.db')
# first init an acct DB without the policy_stat table present
broker = AccountBroker(db_path, account='a')
broker.initialize(Timestamp('1').internal)
# And manually add some container records for the default policy
with broker.get() as conn:
conn.execute('''
INSERT INTO container (
name, put_timestamp, delete_timestamp,
object_count, bytes_used
) VALUES (
'c', '%s', '0', 0, 0
)''' % Timestamp.now().internal).fetchone()
conn.commit()
def confirm_no_table():
with broker.get() as conn:
try:
conn.execute('''
SELECT * FROM policy_stat
''').fetchone()[0]
except sqlite3.OperationalError as err:
# confirm that the table really isn't there
self.assertIn('no such table: policy_stat', str(err))
else:
self.fail('broker did not raise sqlite3.OperationalError '
'trying to select from policy_stat table!')
confirm_no_table()
# make sure we can HEAD this thing w/o the table
stats = broker.get_policy_stats()
self.assertEqual(len(stats), 0)
confirm_no_table()
# but if we pass in do_migrations (like in the auditor), it comes in
stats = broker.get_policy_stats(do_migrations=True)
self.assertEqual(len(stats), 1)
# double check that it really exists
with broker.get() as conn:
conn.execute('SELECT * FROM policy_stat')
@patch_policies @patch_policies
@with_tempdir @with_tempdir
def test_container_table_migration(self, tempdir): def test_container_table_migration(self, tempdir):