def test_get_backup(self):
        # Setup Expected Response
        database = "database1789464955"
        name_2 = "name2-1052831874"
        size_bytes = 1796325715
        expected_response = {
            "database": database,
            "name": name_2,
            "size_bytes": size_bytes,
        }
        expected_response = backup_pb2.Backup(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = spanner_admin_database_v1.DatabaseAdminClient()

        # Setup Request
        name = client.backup_path("[PROJECT]", "[INSTANCE]", "[BACKUP]")

        response = client.get_backup(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = backup_pb2.GetBackupRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_update_backup(self):
        # Setup Expected Response
        database = "database1789464955"
        name = "name3373707"
        size_bytes = 1796325715
        expected_response = {
            "database": database,
            "name": name,
            "size_bytes": size_bytes,
        }
        expected_response = backup_pb2.Backup(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = spanner_admin_database_v1.DatabaseAdminClient()

        # Setup Request
        backup = {}
        update_mask = {}

        response = client.update_backup(backup, update_mask)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = backup_pb2.UpdateBackupRequest(
            backup=backup, update_mask=update_mask)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 3
0
    def test_reload_success(self):
        from google.cloud.spanner_admin_database_v1.proto import backup_pb2
        from google.cloud.spanner_admin_database_v1.gapic import enums
        from google.cloud._helpers import _datetime_to_pb_timestamp

        timestamp = self._make_timestamp()

        client = _Client()
        backup_pb = backup_pb2.Backup(
            name=self.BACKUP_NAME,
            database=self.DATABASE_NAME,
            expire_time=_datetime_to_pb_timestamp(timestamp),
            create_time=_datetime_to_pb_timestamp(timestamp),
            size_bytes=10,
            state=1,
            referencing_databases=[],
        )
        api = client.database_admin_api = self._make_database_admin_api()
        api.get_backup.return_value = backup_pb
        instance = _Instance(self.INSTANCE_NAME, client=client)
        backup = self._make_one(self.BACKUP_ID, instance)

        backup.reload()
        self.assertEqual(backup.name, self.BACKUP_NAME)
        self.assertEqual(backup.database, self.DATABASE_NAME)
        self.assertEqual(backup.expire_time, timestamp)
        self.assertEqual(backup.create_time, timestamp)
        self.assertEqual(backup.size_bytes, 10)
        self.assertEqual(backup.state, enums.Backup.State.CREATING)
        self.assertEqual(backup.referencing_databases, [])

        api.get_backup.assert_called_once_with(
            self.BACKUP_NAME,
            metadata=[("google-cloud-resource-prefix", backup.name)])
Exemplo n.º 4
0
    def test_from_pb_invalid_name(self):
        from google.cloud.spanner_admin_database_v1.proto import backup_pb2

        client = _Client()
        instance = _Instance(self.INSTANCE_NAME, client)
        backup_pb = backup_pb2.Backup(name="invalid_format")
        backup_class = self._get_target_class()

        with self.assertRaises(ValueError):
            backup_class.from_pb(backup_pb, instance)
Exemplo n.º 5
0
    def test_from_pb_project_mismatch(self):
        from google.cloud.spanner_admin_database_v1.proto import backup_pb2

        ALT_PROJECT = "ALT_PROJECT"
        client = _Client(project=ALT_PROJECT)
        instance = _Instance(self.INSTANCE_NAME, client)
        backup_pb = backup_pb2.Backup(name=self.BACKUP_NAME)
        backup_class = self._get_target_class()

        with self.assertRaises(ValueError):
            backup_class.from_pb(backup_pb, instance)
Exemplo n.º 6
0
    def test_from_pb_instance_mismatch(self):
        from google.cloud.spanner_admin_database_v1.proto import backup_pb2

        ALT_INSTANCE = "/projects/%s/instances/ALT-INSTANCE" % (
            self.PROJECT_ID, )
        client = _Client()
        instance = _Instance(ALT_INSTANCE, client)
        backup_pb = backup_pb2.Backup(name=self.BACKUP_NAME)
        backup_class = self._get_target_class()

        with self.assertRaises(ValueError):
            backup_class.from_pb(backup_pb, instance)
Exemplo n.º 7
0
    def test_list_backups_w_options(self):
        from google.cloud.spanner_admin_database_v1.gapic import database_admin_client
        from google.cloud.spanner_admin_database_v1.proto import backup_pb2
        from google.cloud.spanner_v1.backup import Backup

        api = database_admin_client.DatabaseAdminClient(mock.Mock())
        client = _Client(self.PROJECT)
        client.database_admin_api = api
        instance = self._make_one(self.INSTANCE_ID, client)

        backups_pb = backup_pb2.ListBackupsResponse(
            backups=[
                backup_pb2.Backup(name=instance.name + "/backups/op1"),
                backup_pb2.Backup(name=instance.name + "/backups/op2"),
                backup_pb2.Backup(name=instance.name + "/backups/op3"),
            ]
        )

        ldo_api = api._inner_api_calls["list_backups"] = mock.Mock(
            return_value=backups_pb
        )

        backups = instance.list_backups(filter_="filter", page_size=10)

        for backup in backups:
            self.assertIsInstance(backup, Backup)

        expected_metadata = [
            ("google-cloud-resource-prefix", instance.name),
            ("x-goog-request-params", "parent={}".format(instance.name)),
        ]
        ldo_api.assert_called_once_with(
            backup_pb2.ListBackupsRequest(
                parent=self.INSTANCE_NAME, filter="filter", page_size=10
            ),
            metadata=expected_metadata,
            retry=mock.ANY,
            timeout=mock.ANY,
        )
Exemplo n.º 8
0
    def test_from_pb_success(self):
        from google.cloud.spanner_admin_database_v1.proto import backup_pb2

        client = _Client()
        instance = _Instance(self.INSTANCE_NAME, client)
        backup_pb = backup_pb2.Backup(name=self.BACKUP_NAME)
        backup_class = self._get_target_class()

        backup = backup_class.from_pb(backup_pb, instance)

        self.assertIsInstance(backup, backup_class)
        self.assertEqual(backup._instance, instance)
        self.assertEqual(backup.backup_id, self.BACKUP_ID)
        self.assertEqual(backup._database, "")
        self.assertIsNone(backup._expire_time)
Exemplo n.º 9
0
    def test_exists_success(self):
        from google.cloud.spanner_admin_database_v1.proto import backup_pb2

        client = _Client()
        backup_pb = backup_pb2.Backup(name=self.BACKUP_NAME)
        api = client.database_admin_api = self._make_database_admin_api()
        api.get_backup.return_value = backup_pb

        instance = _Instance(self.INSTANCE_NAME, client=client)
        backup = self._make_one(self.BACKUP_ID, instance)

        self.assertTrue(backup.exists())

        api.get_backup.assert_called_once_with(
            self.BACKUP_NAME,
            metadata=[("google-cloud-resource-prefix", backup.name)])
    def test_create_backup(self):
        # Setup Expected Response
        database = "database1789464955"
        name = "name3373707"
        size_bytes = 1796325715
        expected_response = {
            "database": database,
            "name": name,
            "size_bytes": size_bytes,
        }
        expected_response = backup_pb2.Backup(**expected_response)
        operation = operations_pb2.Operation(
            name="operations/test_create_backup", done=True)
        operation.response.Pack(expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = spanner_admin_database_v1.DatabaseAdminClient()

        # Setup Request
        parent = client.instance_path("[PROJECT]", "[INSTANCE]")
        backup_id = "backupId1355353272"
        backup = {}

        response = client.create_backup(parent, backup_id, backup)
        result = response.result()
        assert expected_response == result

        assert len(channel.requests) == 1
        expected_request = backup_pb2.CreateBackupRequest(parent=parent,
                                                          backup_id=backup_id,
                                                          backup=backup)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 11
0
    def test_update_expire_time_success(self):
        from google.cloud._helpers import _datetime_to_pb_timestamp
        from google.cloud.spanner_admin_database_v1.proto import backup_pb2

        client = _Client()
        api = client.database_admin_api = self._make_database_admin_api()
        api.update_backup.return_type = backup_pb2.Backup(
            name=self.BACKUP_NAME)
        instance = _Instance(self.INSTANCE_NAME, client=client)
        backup = self._make_one(self.BACKUP_ID, instance)
        expire_time = self._make_timestamp()

        backup.update_expire_time(expire_time)

        backup_update = {
            "name": self.BACKUP_NAME,
            "expire_time": _datetime_to_pb_timestamp(expire_time),
        }
        update_mask = {"paths": ["expire_time"]}
        api.update_backup.assert_called_once_with(
            backup_update,
            update_mask,
            metadata=[("google-cloud-resource-prefix", backup.name)],
        )