Mark image properties as deleted when deleting images.

Added a unit test to verify public images and their properties get deleted
when running a 'glance clear' command.
This commit is contained in:
Dan Prince 2011-04-20 11:07:44 -04:00
parent 5f3f566ea7
commit a13fa8a6bd
3 changed files with 54 additions and 1 deletions

View File

@ -122,6 +122,9 @@ def image_destroy(context, image_id):
image_ref = image_get(context, image_id, session=session)
image_ref.delete(session=session)
for prop_ref in image_ref.properties:
image_property_delete(context, prop_ref, session=session)
def image_get(context, image_id, session=None):
"""Get an image or raise if it does not exist."""

View File

@ -36,6 +36,8 @@ import urlparse
from tests.utils import execute, get_unused_port
from sqlalchemy import create_engine
class FunctionalTest(unittest.TestCase):
@ -64,7 +66,7 @@ class FunctionalTest(unittest.TestCase):
self.image_dir = "/tmp/test.%d/images" % self.test_id
self.sql_connection = os.environ.get('GLANCE_TEST_SQL_CONNECTION',
"sqlite://")
"sqlite:///glance.sqlite")
self.pid_files = [self.api_pid_file,
self.registry_pid_file]
self.files_to_destroy = []
@ -261,3 +263,7 @@ sql_idle_timeout = 3600
# went wrong...
if os.path.exists(self.test_dir):
shutil.rmtree(self.test_dir)
def run_sql_cmd(self, sql):
engine = create_engine(self.sql_connection, pool_recycle=30)
return engine.execute(sql)

View File

@ -150,3 +150,47 @@ class TestBinGlance(functional.FunctionalTest):
self.assertTrue('MyImage' in image_data_line)
self.stop_servers()
def test_add_clear(self):
"""
We test the following:
1. Add a couple images with metadata
2. Clear the images
"""
self.cleanup()
api_port, reg_port, conf_file = self.start_servers()
# 1. Add some images
for i in range(1, 5):
cmd = "bin/glance --port=%d add is_public=True name=MyName " \
" foo=bar" % api_port
exitcode, out, err = execute(cmd)
self.assertEqual(0, exitcode)
self.assertEqual('Added new image with ID: %i' % i, out.strip())
# 2. Clear all images
cmd = "bin/glance --port=%d clear" % api_port
exitcode, out, err = execute(cmd)
self.assertEqual(0, exitcode)
# 3. Verify no public images are found
cmd = "bin/glance --port=%d index" % api_port
exitcode, out, err = execute(cmd)
self.assertEqual(0, exitcode)
lines = out.split("\n")
first_line = lines[0]
self.assertEqual('No public images found.', first_line)
# 4. Lastly we manually verify with SQL that image properties are
# also getting marked as deleted.
sql = "SELECT COUNT(*) FROM image_properties WHERE deleted = 0"
recs = self.run_sql_cmd(sql)
for rec in recs:
self.assertEqual(0, rec[0])
self.stop_servers()