def test_table_backup( admin_client, unique_suffix, instance_labels, location_id, data_instance_populated, data_cluster_id, instances_to_delete, tables_to_delete, backups_to_delete, skip_on_emulator, ): from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.bigtable import enums temp_table_id = "test-backup-table" temp_table = data_instance_populated.table(temp_table_id) temp_table.create() tables_to_delete.append(temp_table) temp_backup_id = "test-backup" # TODO: consider using `datetime.datetime.now().timestamp()` # when support for Python 2 is fully dropped expire = int(time.mktime(datetime.datetime.now().timetuple())) + 604800 # Testing `Table.backup()` factory temp_backup = temp_table.backup( temp_backup_id, cluster_id=data_cluster_id, expire_time=datetime.datetime.utcfromtimestamp(expire), ) # Reinitialize the admin client. This is to test `_table_admin_client` # returns a client object (and not NoneType) temp_backup._instance._client = admin_client # Sanity check for `Backup.exists()` method assert not temp_backup.exists() # Testing `Backup.create()` method backup_op = temp_backup.create() backup_op.result(timeout=30) # Implicit testing of `Backup.delete()` method backups_to_delete.append(temp_backup) # Testing `Backup.exists()` method assert temp_backup.exists() # Testing `Table.list_backups()` method temp_table_backup = temp_table.list_backups()[0] assert temp_backup_id == temp_table_backup.backup_id assert data_cluster_id == temp_table_backup.cluster assert expire == temp_table_backup.expire_time.seconds assert (temp_table_backup.encryption_info.encryption_type == enums.EncryptionInfo.EncryptionType.GOOGLE_DEFAULT_ENCRYPTION) # Testing `Backup.update_expire_time()` method expire += 3600 # A one-hour change in the `expire_time` parameter updated_time = datetime.datetime.utcfromtimestamp(expire) temp_backup.update_expire_time(updated_time) test = _datetime_to_pb_timestamp(updated_time) # Testing `Backup.get()` method temp_table_backup = temp_backup.get() assert test.seconds == DatetimeWithNanoseconds.timestamp( temp_table_backup.expire_time) # Testing `Table.restore()` and `Backup.retore()` methods restored_table_id = "test-backup-table-restored" restored_table = data_instance_populated.table(restored_table_id) local_restore_op = temp_table.restore(restored_table_id, cluster_id=data_cluster_id, backup_id=temp_backup_id) local_restore_op.result(timeout=30) tables = data_instance_populated.list_tables() assert restored_table in tables restored_table.delete() # Testing `Backup.restore()` into a different instance: # Setting up another instance... alt_instance_id = f"gcp-alt-{unique_suffix}" alt_cluster_id = f"{alt_instance_id}-cluster" alt_instance = admin_client.instance(alt_instance_id, labels=instance_labels) alt_cluster = alt_instance.cluster( cluster_id=alt_cluster_id, location_id=location_id, serve_nodes=1, ) create_op = alt_instance.create(clusters=[alt_cluster]) instances_to_delete.append(alt_instance) create_op.result(timeout=30) # Testing `restore()`... restore_op = temp_backup.restore(restored_table_id, alt_instance_id) restore_op.result(timeout=30) restored_table = alt_instance.table(restored_table_id) assert restored_table in alt_instance.list_tables() restored_table.delete()
def test_backup(self): if Config.IN_EMULATOR: self.skipTest("backups are not supported in the emulator") from google.cloud._helpers import _datetime_to_pb_timestamp temp_table_id = "test-backup-table" temp_table = Config.INSTANCE_DATA.table(temp_table_id) temp_table.create() self.tables_to_delete.append(temp_table) temp_backup_id = "test-backup" # TODO: consider using `datetime.datetime.now().timestamp()` # when support for Python 2 is fully dropped expire = int(time.mktime(datetime.datetime.now().timetuple())) + 604800 # Testing `Table.backup()` factory temp_backup = temp_table.backup( temp_backup_id, cluster_id=CLUSTER_ID_DATA, expire_time=datetime.datetime.utcfromtimestamp(expire), ) # Sanity check for `Backup.exists()` method self.assertFalse(temp_backup.exists()) # Testing `Backup.create()` method temp_backup.create().result() # Implicit testing of `Backup.delete()` method self.backups_to_delete.append(temp_backup) # Testing `Backup.exists()` method self.assertTrue(temp_backup.exists()) # Testing `Table.list_backups()` method temp_table_backup = temp_table.list_backups()[0] self.assertEqual(temp_backup_id, temp_table_backup.backup_id) self.assertEqual(CLUSTER_ID_DATA, temp_table_backup.cluster) self.assertEqual(expire, temp_table_backup.expire_time.seconds) # Testing `Backup.update_expire_time()` method expire += 3600 # A one-hour change in the `expire_time` parameter updated_time = datetime.datetime.utcfromtimestamp(expire) temp_backup.update_expire_time(updated_time) test = _datetime_to_pb_timestamp(updated_time) # Testing `Backup.get()` method temp_table_backup = temp_backup.get() self.assertEqual( test.seconds, DatetimeWithNanoseconds.timestamp(temp_table_backup.expire_time), ) # Testing `Table.restore()` and `Backup.retore()` methods restored_table_id = "test-backup-table-restored" restored_table = Config.INSTANCE_DATA.table(restored_table_id) temp_table.restore(restored_table_id, cluster_id=CLUSTER_ID_DATA, backup_id=temp_backup_id).result() tables = Config.INSTANCE_DATA.list_tables() self.assertIn(restored_table, tables) restored_table.delete()