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_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 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))