Beispiel #1
0
    def test_get_annotation(self, mock_cloud_interface):
        """
        Tests getting the value of a single annotation.

        Because cloud storage only cares about keys, and because we mock the cloud
        interface to return a specific response for a specific key, we do not have
        separate tests here for getting one of multiple annotations.
        """
        mock_cloud_interface.path = None
        annotation_manager = AnnotationManagerCloud(mock_cloud_interface, "test_server")
        mock_cloud_interface.list_bucket.return_value = iter(
            [
                "test_server/base/%s/annotations/test_annotation" % test_backup_id,
            ]
        )
        mock_cloud_interface.remote_open.return_value = io.BytesIO(
            "annotation_value".encode("utf-8")
        )
        assert (
            annotation_manager.get_annotation(test_backup_id, "test_annotation")
            == "annotation_value"
        )
        mock_cloud_interface.remote_open.assert_called_once_with(
            "test_server/base/%s/annotations/test_annotation" % (test_backup_id)
        )
Beispiel #2
0
 def test__get_annotation_path(self, mock_cloud_interface):
     """Tests _get_annotation_path when CloudInterface has no .path"""
     mock_cloud_interface.path = None
     annotation_manager = AnnotationManagerCloud(mock_cloud_interface, "test_server")
     assert (
         annotation_manager._get_annotation_path(test_backup_id, "test_annotation")
         == "test_server/base/%s/annotations/test_annotation" % test_backup_id
     )
Beispiel #3
0
 def test__get_annotation_path_with_cloud_interface_path(self, mock_cloud_interface):
     """Tests _get_annotation_path when CloudInterface has a .path"""
     mock_cloud_interface.path = "a/path/in/the/cloud"
     annotation_manager = AnnotationManagerCloud(mock_cloud_interface, "test_server")
     assert (
         annotation_manager._get_annotation_path(test_backup_id, "test_annotation")
         == "a/path/in/the/cloud/test_server/base/%s/annotations/test_annotation"
         % test_backup_id
     )
    def test_get_missing_annotation(self, mock_cloud_interface):
        """
        Getting a missing annotation returns None.

        Because cloud storage only cares about keys and has no hard notion of
        directories we do not need a separate test case for missing backups.
        """
        annotation_manager = AnnotationManagerCloud(mock_cloud_interface,
                                                    "test_server")
        mock_cloud_interface.remote_open.return_value = None
        assert (annotation_manager.get_annotation(test_backup_id,
                                                  "test_annotation") is None)
Beispiel #5
0
    def test_delete_annotation(self, mock_cloud_interface):
        """
        Tests we delete an annotation successfully.

        As with test_put_annotation, we only test that the expected arguments are
        passed to the CloudInterface and do not verify implementation details at the
        CloudInterface level.
        """
        mock_cloud_interface.path = None
        annotation_manager = AnnotationManagerCloud(mock_cloud_interface, "test_server")
        annotation_manager.delete_annotation(test_backup_id, "test_annotation")
        mock_cloud_interface.delete_objects.assert_called_once_with(
            ["test_server/base/%s/annotations/test_annotation" % test_backup_id]
        )
 def test_get_missing_annotation_cache_optimisation(self,
                                                    mock_cloud_interface):
     """
     Tests that we avoid remote_open calls for missing annotations.
     """
     mock_cloud_interface.path = None
     annotation_manager = AnnotationManagerCloud(mock_cloud_interface,
                                                 "test_server")
     mock_cloud_interface.list_bucket.return_value = iter([
         "test_server/base/%s/annotations/test_annotation" % test_backup_id,
     ])
     # Deliberately try to fetch an annotation which isn't there
     assert (annotation_manager.get_annotation(test_backup_id,
                                               "test_annotation_2") is None)
     # The AnnotationManager did not have to open the annotation to determine it
     # was missing
     mock_cloud_interface.remote_open.assert_not_called()
 def test_get_missing_annotation_bypass_cache_optimisation(
         self, mock_cloud_interface):
     """
     Tests that we bypass the cache optimisation for missing annotations by default.
     """
     mock_cloud_interface.path = None
     annotation_manager = AnnotationManagerCloud(mock_cloud_interface,
                                                 "test_server")
     # Deliberately try to fetch an annotation which isn't there without using the
     # cache optimization
     annotation_manager.get_annotation(test_backup_id,
                                       "test_annotation_2",
                                       use_cache=False)
     mock_cloud_interface.remote_open.assert_called_once_with(
         "test_server/base/%s/annotations/test_annotation_2" %
         test_backup_id)
     # The AnnotationManager did not have to list the bucket to populate the cache
     mock_cloud_interface.list_bucket.assert_not_called()
Beispiel #8
0
    def test_put_annotation(self, mock_cloud_interface):
        """
        Tests a single annotation is stored.

        We do not test beyond ensuring we pass the expected arguments on to
        CloudInterface because the behaviour of upload_fileobj with respect to
        idempotency and overwriting values is an implementation concern at the
        CloudInterface level.
        """
        mock_cloud_interface.path = None
        annotation_manager = AnnotationManagerCloud(mock_cloud_interface, "test_server")
        annotation_manager.put_annotation(
            test_backup_id, "test_annotation", "annotation_value"
        )
        mock_cloud_interface.upload_fileobj.assert_called_once()
        upload_value = mock_cloud_interface.upload_fileobj.call_args_list[0][0][0]
        assert upload_value.read() == b"annotation_value"
        upload_key = mock_cloud_interface.upload_fileobj.call_args_list[0][0][1]
        assert upload_key == "test_server/base/%s/annotations/test_annotation" % (
            test_backup_id
        )