2015-02-11 02:46:58 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from app import app
|
|
|
|
from util.imagetree import ImageTree
|
|
|
|
from initdb import setup_database_for_testing, finished_database_for_testing
|
|
|
|
from data import model
|
|
|
|
|
|
|
|
NAMESPACE = 'devtable'
|
|
|
|
SIMPLE_REPO = 'simple'
|
|
|
|
COMPLEX_REPO = 'complex'
|
|
|
|
|
|
|
|
class TestImageTree(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
setup_database_for_testing(self)
|
|
|
|
self.app = app.test_client()
|
|
|
|
self.ctx = app.test_request_context()
|
|
|
|
self.ctx.__enter__()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
finished_database_for_testing(self)
|
|
|
|
self.ctx.__exit__(True, None, None)
|
|
|
|
|
2016-09-01 14:49:14 +00:00
|
|
|
@staticmethod
|
|
|
|
def _get_base_image(all_images):
|
2015-02-11 02:46:58 +00:00
|
|
|
for image in all_images:
|
|
|
|
if image.ancestors == '/':
|
|
|
|
return image
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def test_longest_path_simple_repo(self):
|
2015-07-15 21:25:41 +00:00
|
|
|
all_images = list(model.image.get_repository_images(NAMESPACE, SIMPLE_REPO))
|
|
|
|
all_tags = list(model.tag.list_repository_tags(NAMESPACE, SIMPLE_REPO))
|
2015-02-11 02:46:58 +00:00
|
|
|
tree = ImageTree(all_images, all_tags)
|
|
|
|
|
|
|
|
base_image = self._get_base_image(all_images)
|
|
|
|
tag_image = all_tags[0].image
|
|
|
|
|
|
|
|
def checker(index, image):
|
|
|
|
return True
|
|
|
|
|
|
|
|
ancestors = tag_image.ancestors.split('/')[2:-1] # Skip the first image.
|
|
|
|
result = tree.find_longest_path(base_image.id, checker)
|
|
|
|
self.assertEquals(3, len(result))
|
|
|
|
for index in range(0, 2):
|
|
|
|
self.assertEquals(int(ancestors[index]), result[index].id)
|
|
|
|
|
|
|
|
self.assertEquals('latest', tree.tag_containing_image(result[-1]))
|
|
|
|
|
|
|
|
def test_longest_path_complex_repo(self):
|
2015-07-15 21:25:41 +00:00
|
|
|
all_images = list(model.image.get_repository_images(NAMESPACE, COMPLEX_REPO))
|
|
|
|
all_tags = list(model.tag.list_repository_tags(NAMESPACE, COMPLEX_REPO))
|
2015-02-11 02:46:58 +00:00
|
|
|
tree = ImageTree(all_images, all_tags)
|
|
|
|
|
|
|
|
base_image = self._get_base_image(all_images)
|
|
|
|
|
|
|
|
def checker(index, image):
|
|
|
|
return True
|
|
|
|
|
|
|
|
result = tree.find_longest_path(base_image.id, checker)
|
|
|
|
self.assertEquals(4, len(result))
|
2015-03-17 16:13:01 +00:00
|
|
|
self.assertEquals('prod', tree.tag_containing_image(result[-1]))
|
2015-02-11 02:46:58 +00:00
|
|
|
|
|
|
|
def test_filtering(self):
|
2015-07-15 21:25:41 +00:00
|
|
|
all_images = list(model.image.get_repository_images(NAMESPACE, COMPLEX_REPO))
|
|
|
|
all_tags = list(model.tag.list_repository_tags(NAMESPACE, COMPLEX_REPO))
|
2015-02-11 03:02:39 +00:00
|
|
|
tree = ImageTree(all_images, all_tags, base_filter=1245)
|
2015-02-11 02:46:58 +00:00
|
|
|
|
|
|
|
base_image = self._get_base_image(all_images)
|
|
|
|
|
|
|
|
def checker(index, image):
|
|
|
|
return True
|
|
|
|
|
|
|
|
result = tree.find_longest_path(base_image.id, checker)
|
|
|
|
self.assertEquals(0, len(result))
|
|
|
|
|
|
|
|
def test_find_tag_parent_image(self):
|
2015-07-15 21:25:41 +00:00
|
|
|
all_images = list(model.image.get_repository_images(NAMESPACE, COMPLEX_REPO))
|
|
|
|
all_tags = list(model.tag.list_repository_tags(NAMESPACE, COMPLEX_REPO))
|
2015-02-11 02:46:58 +00:00
|
|
|
tree = ImageTree(all_images, all_tags)
|
|
|
|
|
|
|
|
base_image = self._get_base_image(all_images)
|
|
|
|
|
|
|
|
def checker(index, image):
|
|
|
|
return True
|
|
|
|
|
|
|
|
result = tree.find_longest_path(base_image.id, checker)
|
|
|
|
self.assertEquals(4, len(result))
|
|
|
|
|
|
|
|
# Only use the first two images. They don't have tags, but the method should
|
|
|
|
# still return the tag that contains them.
|
2015-03-17 16:13:01 +00:00
|
|
|
self.assertEquals('staging', tree.tag_containing_image(result[0]))
|
2015-02-11 02:46:58 +00:00
|
|
|
|
2015-04-24 20:22:19 +00:00
|
|
|
def test_longest_path_simple_repo_direct_lookup(self):
|
2015-07-15 21:25:41 +00:00
|
|
|
repository = model.repository.get_repository(NAMESPACE, SIMPLE_REPO)
|
|
|
|
all_images = list(model.image.get_repository_images(NAMESPACE, SIMPLE_REPO))
|
|
|
|
all_tags = list(model.tag.list_repository_tags(NAMESPACE, SIMPLE_REPO))
|
2015-04-24 20:22:19 +00:00
|
|
|
|
|
|
|
base_image = self._get_base_image(all_images)
|
|
|
|
|
|
|
|
def checker(index, image):
|
|
|
|
return True
|
|
|
|
|
2016-09-01 14:49:14 +00:00
|
|
|
filtered_images = model.image.get_repository_images_without_placements(
|
|
|
|
repository,
|
|
|
|
with_ancestor=base_image)
|
2015-04-24 20:22:19 +00:00
|
|
|
self.assertEquals(set([f.id for f in filtered_images]), set([a.id for a in all_images]))
|
|
|
|
|
|
|
|
tree = ImageTree(filtered_images, all_tags)
|
|
|
|
|
|
|
|
result = tree.find_longest_path(base_image.id, checker)
|
|
|
|
self.assertEquals(3, len(result))
|
|
|
|
self.assertEquals('latest', tree.tag_containing_image(result[-1]))
|
|
|
|
|
2016-09-01 14:49:14 +00:00
|
|
|
|
2015-02-11 02:46:58 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|