Пример #1
0
    def test_update_expire_time_success(self):
        from google.cloud._helpers import _datetime_to_pb_timestamp
        from google.cloud.bigtable_admin_v2.proto import table_pb2
        from google.protobuf import field_mask_pb2

        client = _Client()
        api = client.table_admin_client = self._make_table_admin_client()
        api.update_backup.return_type = table_pb2.Backup(name=self.BACKUP_NAME)
        instance = _Instance(self.INSTANCE_NAME, client=client)
        backup = self._make_one(self.BACKUP_ID,
                                instance,
                                cluster_id=self.CLUSTER_ID)
        expire_time = self._make_timestamp()

        backup.update_expire_time(expire_time)

        backup_update = table_pb2.Backup(
            name=self.BACKUP_NAME,
            expire_time=_datetime_to_pb_timestamp(expire_time),
        )
        update_mask = field_mask_pb2.FieldMask(paths=["expire_time"])
        api.update_backup.assert_called_once_with(
            backup_update,
            update_mask,
        )
Пример #2
0
    def test_update_expire_time_not_found(self):
        from google.api_core.exceptions import NotFound
        from google.cloud._helpers import _datetime_to_pb_timestamp
        from google.cloud.bigtable_admin_v2.types import table_pb2
        from google.protobuf import field_mask_pb2

        client = _Client()
        api = client.table_admin_client = self._make_table_admin_client()
        api.update_backup.side_effect = NotFound("testing")
        instance = _Instance(self.INSTANCE_NAME, client=client)
        backup = self._make_one(self.BACKUP_ID,
                                instance,
                                cluster_id=self.CLUSTER_ID)
        expire_time = self._make_timestamp()

        with self.assertRaises(NotFound):
            backup.update_expire_time(expire_time)

        backup_update = table_pb2.Backup(
            name=self.BACKUP_NAME,
            expire_time=_datetime_to_pb_timestamp(expire_time),
        )
        update_mask = field_mask_pb2.FieldMask(paths=["expire_time"])
        api.update_backup.assert_called_once_with(
            backup_update,
            update_mask,
        )
Пример #3
0
    def test_create_success(self):
        from google.cloud._helpers import _datetime_to_pb_timestamp
        from google.cloud.bigtable_admin_v2.types import table_pb2

        op_future = object()
        client = _Client()
        api = client.table_admin_client = self._make_table_admin_client()
        api.create_backup.return_value = op_future

        timestamp = self._make_timestamp()
        backup = self._make_one(
            self.BACKUP_ID,
            _Instance(self.INSTANCE_NAME, client=client),
            table_id=self.TABLE_ID,
            expire_time=timestamp,
        )

        backup_pb = table_pb2.Backup(
            source_table=self.TABLE_NAME,
            expire_time=_datetime_to_pb_timestamp(timestamp),
        )

        future = backup.create(self.CLUSTER_ID)
        self.assertEqual(backup._cluster, self.CLUSTER_ID)
        self.assertIs(future, op_future)

        api.create_backup.assert_called_once_with(
            parent=self.CLUSTER_NAME,
            backup_id=self.BACKUP_ID,
            backup=backup_pb,
        )
Пример #4
0
    def test_reload(self):
        from google.cloud.bigtable_admin_v2.gapic import enums
        from google.cloud.bigtable_admin_v2.proto import table_pb2
        from google.cloud._helpers import _datetime_to_pb_timestamp

        timestamp = _datetime_to_pb_timestamp(self._make_timestamp())
        state = enums.Backup.State.READY

        client = _Client()
        backup_pb = table_pb2.Backup(
            name=self.BACKUP_NAME,
            source_table=self.TABLE_NAME,
            expire_time=timestamp,
            start_time=timestamp,
            end_time=timestamp,
            size_bytes=0,
            state=state,
        )
        api = client.table_admin_client = self._make_table_admin_client()
        api.get_backup.return_value = backup_pb

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

        backup.reload()
        self.assertEqual(backup._source_table, self.TABLE_NAME)
        self.assertEqual(backup._expire_time, timestamp)
        self.assertEqual(backup._start_time, timestamp)
        self.assertEqual(backup._end_time, timestamp)
        self.assertEqual(backup._size_bytes, 0)
        self.assertEqual(backup._state, state)
Пример #5
0
    def test_create_grpc_error(self):
        from google.api_core.exceptions import GoogleAPICallError
        from google.api_core.exceptions import Unknown
        from google.cloud._helpers import _datetime_to_pb_timestamp
        from google.cloud.bigtable_admin_v2.types import table_pb2

        client = _Client()
        api = client.table_admin_client = self._make_table_admin_client()
        api.create_backup.side_effect = Unknown("testing")

        timestamp = self._make_timestamp()
        backup = self._make_one(
            self.BACKUP_ID,
            _Instance(self.INSTANCE_NAME, client=client),
            table_id=self.TABLE_ID,
            expire_time=timestamp,
        )

        backup_pb = table_pb2.Backup(
            source_table=self.TABLE_NAME,
            expire_time=_datetime_to_pb_timestamp(timestamp),
        )

        with self.assertRaises(GoogleAPICallError):
            backup.create(self.CLUSTER_ID)

        api.create_backup.assert_called_once_with(
            parent=self.CLUSTER_NAME,
            backup_id=self.BACKUP_ID,
            backup=backup_pb,
        )
Пример #6
0
    def test_create_instance_not_found(self):
        from google.cloud._helpers import _datetime_to_pb_timestamp
        from google.cloud.bigtable_admin_v2.types import table_pb2
        from google.cloud.exceptions import NotFound

        client = _Client()
        api = client.table_admin_client = self._make_table_admin_client()
        api.create_backup.side_effect = NotFound("testing")

        timestamp = self._make_timestamp()
        backup = self._make_one(
            self.BACKUP_ID,
            _Instance(self.INSTANCE_NAME, client=client),
            table_id=self.TABLE_ID,
            expire_time=timestamp,
        )

        backup_pb = table_pb2.Backup(
            source_table=self.TABLE_NAME,
            expire_time=_datetime_to_pb_timestamp(timestamp),
        )

        with self.assertRaises(NotFound):
            backup.create(self.CLUSTER_ID)

        api.create_backup.assert_called_once_with(
            parent=self.CLUSTER_NAME,
            backup_id=self.BACKUP_ID,
            backup=backup_pb,
        )
    def test_update_backup(self):
        # Setup Expected Response
        name = "name3373707"
        source_table = "sourceTable1670858410"
        size_bytes = 1796325715
        expected_response = {
            "name": name,
            "source_table": source_table,
            "size_bytes": size_bytes,
        }
        expected_response = table_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 = bigtable_admin_v2.BigtableTableAdminClient()

        # Setup Request
        backup = {}
        update_mask = {}

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

        assert len(channel.requests) == 1
        expected_request = bigtable_table_admin_pb2.UpdateBackupRequest(
            backup=backup, update_mask=update_mask)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Пример #8
0
    def test_from_pb_success(self):
        from google.cloud.bigtable_admin_v2.gapic import enums
        from google.cloud.bigtable_admin_v2.proto import table_pb2
        from google.cloud._helpers import _datetime_to_pb_timestamp

        client = _Client()
        instance = _Instance(self.INSTANCE_NAME, client)
        timestamp = _datetime_to_pb_timestamp(self._make_timestamp())
        size_bytes = 1234
        state = enums.Backup.State.READY
        backup_pb = table_pb2.Backup(
            name=self.BACKUP_NAME,
            source_table=self.TABLE_NAME,
            expire_time=timestamp,
            start_time=timestamp,
            end_time=timestamp,
            size_bytes=size_bytes,
            state=state,
        )
        klasse = self._get_target_class()

        backup = klasse.from_pb(backup_pb, instance)

        self.assertTrue(isinstance(backup, klasse))
        self.assertEqual(backup._instance, instance)
        self.assertEqual(backup.backup_id, self.BACKUP_ID)
        self.assertEqual(backup.cluster, self.CLUSTER_ID)
        self.assertEqual(backup.table_id, self.TABLE_ID)
        self.assertEqual(backup._expire_time, timestamp)
        self.assertEqual(backup._start_time, timestamp)
        self.assertEqual(backup._end_time, timestamp)
        self.assertEqual(backup._size_bytes, size_bytes)
        self.assertEqual(backup._state, state)
    def test_get_backup(self):
        # Setup Expected Response
        name_2 = "name2-1052831874"
        source_table = "sourceTable1670858410"
        size_bytes = 1796325715
        expected_response = {
            "name": name_2,
            "source_table": source_table,
            "size_bytes": size_bytes,
        }
        expected_response = table_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 = bigtable_admin_v2.BigtableTableAdminClient()

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

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

        assert len(channel.requests) == 1
        expected_request = bigtable_table_admin_pb2.GetBackupRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Пример #10
0
    def test_from_pb_bad_name(self):
        from google.cloud.bigtable_admin_v2.proto import table_pb2

        client = _Client()
        instance = _Instance(self.INSTANCE_NAME, client)
        backup_pb = table_pb2.Backup(name="invalid_name")
        klasse = self._get_target_class()

        with self.assertRaises(ValueError):
            klasse.from_pb(backup_pb, instance)
Пример #11
0
    def test_from_pb_project_mismatch(self):
        from google.cloud.bigtable_admin_v2.proto import table_pb2

        alt_project_id = "alt-project-id"
        client = _Client(project=alt_project_id)
        instance = _Instance(self.INSTANCE_NAME, client)
        backup_pb = table_pb2.Backup(name=self.BACKUP_NAME)
        klasse = self._get_target_class()

        with self.assertRaises(ValueError):
            klasse.from_pb(backup_pb, instance)
Пример #12
0
    def test_from_pb_instance_mismatch(self):
        from google.cloud.bigtable_admin_v2.proto import table_pb2

        alt_instance = "/projects/%s/instances/alt-instance" % self.PROJECT_ID
        client = _Client()
        instance = _Instance(alt_instance, client)
        backup_pb = table_pb2.Backup(name=self.BACKUP_NAME)
        klasse = self._get_target_class()

        with self.assertRaises(ValueError):
            klasse.from_pb(backup_pb, instance)
Пример #13
0
    def test_exists_success(self):
        from google.cloud.bigtable_admin_v2.proto import table_pb2

        client = _Client()
        backup_pb = table_pb2.Backup(name=self.BACKUP_NAME)
        api = client.table_admin_client = self._make_table_admin_client()
        api.get_backup.return_value = backup_pb

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

        self.assertTrue(backup.exists())

        api.get_backup.assert_called_once_with(self.BACKUP_NAME)
    def test_create_backup(self):
        # Setup Expected Response
        name = "name3373707"
        source_table = "sourceTable1670858410"
        size_bytes = 1796325715
        expected_response = {
            "name": name,
            "source_table": source_table,
            "size_bytes": size_bytes,
        }
        expected_response = table_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 = bigtable_admin_v2.BigtableTableAdminClient()

        # Setup Request
        parent = client.cluster_path("[PROJECT]", "[INSTANCE]", "[CLUSTER]")
        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 = bigtable_table_admin_pb2.CreateBackupRequest(
            parent=parent, backup_id=backup_id, backup=backup)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request