def test_get_relevant_snapshot_images_simple(self, get_current_datetime): project = self.create_project() plan1 = self.create_plan(project) plan2 = self.create_plan(project) plan3 = self.create_plan(project) plan4 = self.create_plan(project) plan5 = self.create_plan(project) self.create_step(plan1, data={'cluster': 'cluster1'}) self.create_step(plan2, data={'cluster': 'cluster1'}) self.create_step(plan3, data={'cluster': 'cluster2'}) self.create_step(plan4, data={}) self.create_step(plan5) snapshot = self.create_snapshot(project) snapshot_image1 = self.create_snapshot_image(snapshot, plan1) snapshot_image2 = self.create_snapshot_image(snapshot, plan2) snapshot_image3 = self.create_snapshot_image(snapshot, plan3) snapshot_image4 = self.create_snapshot_image(snapshot, plan4) snapshot_image5 = self.create_snapshot_image(snapshot, plan5) get_current_datetime.return_value = self.mock_datetime gc.cache_snapshot(snapshot) images = gc.get_relevant_snapshot_images(snapshot.id) assert len(images) == 2 assert 'cluster1' in images assert 'cluster2' in images assert len(images['cluster1']) == 2 assert snapshot_image1 in images['cluster1'] assert snapshot_image2 in images['cluster1'] assert len(images['cluster2']) == 1 assert snapshot_image3 in images['cluster2']
def test_get_relevant_snapshot_images_across_projects(self, get_current_datetime): project1 = self.create_project() project2 = self.create_project() plan1_1 = self.create_plan(project1) plan1_2 = self.create_plan(project1) plan2_1 = self.create_plan(project2) plan2_2 = self.create_plan(project2) self.create_step(plan1_1, data={'cluster': 'cluster1'}) self.create_step(plan1_2, data={'cluster': 'cluster2'}) self.create_step(plan2_1, data={'cluster': 'cluster2'}) self.create_step(plan2_2, data={'cluster': 'cluster3'}) snapshot1 = self.create_snapshot(project1) snapshot2 = self.create_snapshot(project2) snapshot_image1_1 = self.create_snapshot_image(snapshot1, plan1_1) snapshot_image1_2 = self.create_snapshot_image(snapshot1, plan1_2) snapshot_image2_1 = self.create_snapshot_image(snapshot2, plan2_1) snapshot_image2_2 = self.create_snapshot_image(snapshot2, plan2_2) get_current_datetime.return_value = self.mock_datetime gc.cache_snapshot(snapshot2) gc.cache_snapshot(snapshot1) images = gc.get_relevant_snapshot_images(snapshot1.id) assert len(images) == 2 assert 'cluster1' in images assert 'cluster2' in images assert len(images['cluster1']) == 1 assert snapshot_image1_1 in images['cluster1'] assert len(images['cluster2']) == 2 assert snapshot_image1_2 in images['cluster2'] assert snapshot_image2_1 in images['cluster2']
def test_cache_snapshot_expires_old_snapshot(self, get_current_datetime): project = self.create_project() plans = [self.create_plan(project) for _ in range(3)] old_snapshot = self.create_snapshot(project) old_snapshot_images = [self.create_snapshot_image(old_snapshot, plan) for plan in plans] old_snapshot_image_ids = [image.id for image in old_snapshot_images] snapshot = self.create_snapshot(project) snapshot_images = [self.create_snapshot_image(snapshot, plan) for plan in plans] for old_snapshot_image in old_snapshot_images: self.create_cached_snapshot_image(old_snapshot_image) get_current_datetime.return_value = self.mock_datetime gc.cache_snapshot(snapshot) # Ensure that the old snapshots now expire sometime in the future assert not db.session.query(CachedSnapshotImage.query.filter( CachedSnapshotImage.id.in_(old_snapshot_image_ids), sqlalchemy.or_( CachedSnapshotImage.expiration_date == None, # NOQA CachedSnapshotImage.expiration_date <= self.mock_datetime ) ).exists()).scalar()
def test_get_relevant_snapshot_images_across_projects( self, get_current_datetime): project1 = self.create_project() project2 = self.create_project() plan1_1 = self.create_plan(project1) plan1_2 = self.create_plan(project1) plan2_1 = self.create_plan(project2) plan2_2 = self.create_plan(project2) self.create_step(plan1_1, data={'cluster': 'cluster1'}) self.create_step(plan1_2, data={'cluster': 'cluster2'}) self.create_step(plan2_1, data={'cluster': 'cluster2'}) self.create_step(plan2_2, data={'cluster': 'cluster3'}) snapshot1 = self.create_snapshot(project1) snapshot2 = self.create_snapshot(project2) snapshot_image1_1 = self.create_snapshot_image(snapshot1, plan1_1) snapshot_image1_2 = self.create_snapshot_image(snapshot1, plan1_2) snapshot_image2_1 = self.create_snapshot_image(snapshot2, plan2_1) snapshot_image2_2 = self.create_snapshot_image(snapshot2, plan2_2) get_current_datetime.return_value = self.mock_datetime gc.cache_snapshot(snapshot2) gc.cache_snapshot(snapshot1) images = gc.get_relevant_snapshot_images(snapshot1.id) assert len(images) == 2 assert 'cluster1' in images assert 'cluster2' in images assert len(images['cluster1']) == 1 assert snapshot_image1_1 in images['cluster1'] assert len(images['cluster2']) == 2 assert snapshot_image1_2 in images['cluster2'] assert snapshot_image2_1 in images['cluster2']
def test_get_relevant_snapshot_images_simple(self, get_current_datetime): project = self.create_project() plan1 = self.create_plan(project) plan2 = self.create_plan(project) plan3 = self.create_plan(project) plan4 = self.create_plan(project) plan5 = self.create_plan(project) self.create_step(plan1, data={'cluster': 'cluster1'}) self.create_step(plan2, data={'cluster': 'cluster1'}) self.create_step(plan3, data={'cluster': 'cluster2'}) self.create_step(plan4, data={}) self.create_step(plan5) snapshot = self.create_snapshot(project) snapshot_image1 = self.create_snapshot_image(snapshot, plan1) snapshot_image2 = self.create_snapshot_image(snapshot, plan2) snapshot_image3 = self.create_snapshot_image(snapshot, plan3) snapshot_image4 = self.create_snapshot_image(snapshot, plan4) snapshot_image5 = self.create_snapshot_image(snapshot, plan5) get_current_datetime.return_value = self.mock_datetime gc.cache_snapshot(snapshot) images = gc.get_relevant_snapshot_images(snapshot.id) assert len(images) == 2 assert 'cluster1' in images assert 'cluster2' in images assert len(images['cluster1']) == 2 assert snapshot_image1 in images['cluster1'] assert snapshot_image2 in images['cluster1'] assert len(images['cluster2']) == 1 assert snapshot_image3 in images['cluster2']
def test_recache_existing_cached_snapshot(self, get_current_datetime): """ In some cases we may want to re-cache an existing snapshot that already has an entry in the cache. This should be transparent. """ project = self.create_project() plan = self.create_plan(project) snapshot = self.create_snapshot(project) snapshot_image = self.create_snapshot_image(snapshot, plan) get_current_datetime.return_value = self.mock_datetime self.create_cached_snapshot_image(snapshot_image, expiration_date=self.mock_datetime - datetime.timedelta(0, 1)) gc.cache_snapshot(snapshot) # The old snapshot now has no expiration cached_snapshot_image = CachedSnapshotImage.query.get( snapshot_image.id) assert cached_snapshot_image is not None assert cached_snapshot_image.expiration_date is None # and is the only snapshot in existence assert CachedSnapshotImage.query.count() == 1
def test_cache_snapshot_expires_old_snapshot(self, get_current_datetime): project = self.create_project() plans = [self.create_plan(project) for _ in range(3)] old_snapshot = self.create_snapshot(project) old_snapshot_images = [ self.create_snapshot_image(old_snapshot, plan) for plan in plans ] old_snapshot_image_ids = [image.id for image in old_snapshot_images] snapshot = self.create_snapshot(project) snapshot_images = [ self.create_snapshot_image(snapshot, plan) for plan in plans ] for old_snapshot_image in old_snapshot_images: self.create_cached_snapshot_image(old_snapshot_image) get_current_datetime.return_value = self.mock_datetime gc.cache_snapshot(snapshot) # Ensure that the old snapshots now expire sometime in the future assert not db.session.query( CachedSnapshotImage.query.filter( CachedSnapshotImage.id.in_(old_snapshot_image_ids), sqlalchemy.or_( CachedSnapshotImage.expiration_date == None, # NOQA CachedSnapshotImage.expiration_date <= self.mock_datetime)).exists()).scalar()
def test_cache_snapshot_images_have_no_expiration(self, get_current_datetime): project = self.create_project() snapshot = self.create_snapshot(project) plans = [self.create_plan(project) for _ in range(3)] snapshot_images = [self.create_snapshot_image(snapshot, plan) for plan in plans] snapshot_image_ids = [image.id for image in snapshot_images] get_current_datetime.return_value = self.mock_datetime gc.cache_snapshot(snapshot) assert not db.session.query(CachedSnapshotImage.query.filter( CachedSnapshotImage.id.in_(snapshot_image_ids), CachedSnapshotImage.expiration_date != None, # NOQA ).exists()).scalar()
def test_cache_snapshot_images_have_no_expiration(self, get_current_datetime): project = self.create_project() snapshot = self.create_snapshot(project) plans = [self.create_plan(project) for _ in range(3)] snapshot_images = [ self.create_snapshot_image(snapshot, plan) for plan in plans ] snapshot_image_ids = [image.id for image in snapshot_images] get_current_datetime.return_value = self.mock_datetime gc.cache_snapshot(snapshot) assert not db.session.query( CachedSnapshotImage.query.filter( CachedSnapshotImage.id.in_(snapshot_image_ids), CachedSnapshotImage.expiration_date != None, # NOQA ).exists()).scalar()
def post(self, snapshot_id): """ Add the snapshot images of a given snapshot to the cache and respond with sync updates for all of the clusters associated with the snapshot that was just updated. """ snapshot = Snapshot.query.get(snapshot_id) if snapshot is None: return "", 404 # Add the snapshot to the cache, giving it no expiration gc.cache_snapshot(snapshot) # Send back the sync information for all clusters which require # an update. Because this reponse is intended to be used for # syncing we don't need to send anything but the snapshot # image ids. response = gc.get_relevant_snapshot_images(snapshot.id) return self.respond(self.unpack_snapshot_ids(response))
def post(self, snapshot_id): """ Add the snapshot images of a given snapshot to the cache and respond with sync updates for all of the clusters associated with the snapshot that was just updated. """ snapshot = Snapshot.query.get(snapshot_id) if snapshot is None: return '', 404 # Add the snapshot to the cache, giving it no expiration gc.cache_snapshot(snapshot) # Send back the sync information for all clusters which require # an update. Because this reponse is intended to be used for # syncing we don't need to send anything but the snapshot # image ids. response = gc.get_relevant_snapshot_images(snapshot.id) return self.respond(self.unpack_snapshot_ids(response))
def test_recache_existing_cached_snapshot(self, get_current_datetime): """ In some cases we may want to re-cache an existing snapshot that already has an entry in the cache. This should be transparent. """ project = self.create_project() plan = self.create_plan(project) snapshot = self.create_snapshot(project) snapshot_image = self.create_snapshot_image(snapshot, plan) get_current_datetime.return_value = self.mock_datetime self.create_cached_snapshot_image(snapshot_image, expiration_date=self.mock_datetime - datetime.timedelta(0, 1)) gc.cache_snapshot(snapshot) # The old snapshot now has no expiration cached_snapshot_image = CachedSnapshotImage.query.get(snapshot_image.id) assert cached_snapshot_image is not None assert cached_snapshot_image.expiration_date is None # and is the only snapshot in existence assert CachedSnapshotImage.query.count() == 1