def test_delete(self): from google.protobuf import empty_pb2 from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = client.instance(self.INSTANCE_ID) app_profile = self._make_one(self.APP_PROFILE_ID, instance) # Create response_pb response_pb = empty_pb2.Empty() # Patch the stub used by the API method. client._instance_admin_client = instance_api instance_stub = ( client._instance_admin_client.bigtable_instance_admin_stub) instance_stub.DeleteCluster.side_effect = [response_pb] # Create expected_result. expected_result = None # delete() has no return value. # Perform the method and check the result. result = app_profile.delete() self.assertEqual(result, expected_result)
def test_reload_routing_any(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable_admin_v2.proto import (instance_pb2 as data_v2_pb2) from google.cloud.bigtable.enums import RoutingPolicyType api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = _Instance(self.INSTANCE_ID, client) routing = RoutingPolicyType.ANY description = 'routing policy any' app_profile = self._make_one(self.APP_PROFILE_ID, instance, routing_policy_type=routing, description=description) # Create response_pb description_from_server = 'routing policy switched to single' cluster_id_from_server = self.CLUSTER_ID allow_transactional_writes = True single_cluster_routing = (data_v2_pb2.AppProfile.SingleClusterRouting( cluster_id=cluster_id_from_server, allow_transactional_writes=allow_transactional_writes)) response_pb = data_v2_pb2.AppProfile( name=app_profile.name, single_cluster_routing=single_cluster_routing, description=description_from_server) # Patch the stub used by the API method. client._instance_admin_client = api instance_stub = ( client._instance_admin_client.bigtable_instance_admin_stub) instance_stub.GetCluster.side_effect = [response_pb] # Create expected_result. expected_result = None # reload() has no return value. # Check app_profile config values before. self.assertEqual(app_profile.routing_policy_type, routing) self.assertEqual(app_profile.description, description) self.assertIsNone(app_profile.cluster_id) self.assertIsNone(app_profile.allow_transactional_writes) # Perform the method and check the result. result = app_profile.reload() self.assertEqual(result, expected_result) self.assertEqual(app_profile.routing_policy_type, RoutingPolicyType.SINGLE) self.assertEqual(app_profile.description, description_from_server) self.assertEqual(app_profile.cluster_id, cluster_id_from_server) self.assertEqual(app_profile.allow_transactional_writes, allow_transactional_writes)
def test_delete(self): from google.protobuf import empty_pb2 from google.cloud.bigtable.cluster import DEFAULT_SERVE_NODES from google.cloud.bigtable.instance import Instance from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = Instance(self.INSTANCE_ID, client) cluster = self._make_one(self.CLUSTER_ID, instance, self.LOCATION_ID, serve_nodes=DEFAULT_SERVE_NODES) # Create response_pb response_pb = empty_pb2.Empty() # Patch the stub used by the API method. client._instance_admin_client = api instance_admin_client = client._instance_admin_client instance_stub = instance_admin_client.bigtable_instance_admin_stub instance_stub.DeleteCluster.side_effect = [response_pb] # Create expected_result. expected_result = None # delete() has no return value. # Perform the method and check the result. result = cluster.delete() self.assertEqual(result, expected_result)
def test_list_instances(self): from google.cloud.bigtable_admin_v2.proto import (instance_pb2 as data_v2_pb2) from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud.bigtable_admin_v2.gapic import \ bigtable_instance_admin_client from google.cloud.bigtable.instance import Instance FAILED_LOCATION = 'FAILED' INSTANCE_ID1 = 'instance-id1' INSTANCE_ID2 = 'instance-id2' INSTANCE_NAME1 = ('projects/' + self.PROJECT + '/instances/' + INSTANCE_ID1) INSTANCE_NAME2 = ('projects/' + self.PROJECT + '/instances/' + INSTANCE_ID2) credentials = _make_credentials() api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) client = self._make_one(project=self.PROJECT, credentials=credentials, admin=True) # Create response_pb response_pb = messages_v2_pb2.ListInstancesResponse( failed_locations=[ FAILED_LOCATION, ], instances=[ data_v2_pb2.Instance( name=INSTANCE_NAME1, display_name=INSTANCE_NAME1, ), data_v2_pb2.Instance( name=INSTANCE_NAME2, display_name=INSTANCE_NAME2, ), ], ) # Patch the stub used by the API method. client._instance_admin_client = api bigtable_instance_stub = (client.instance_admin_client.transport) bigtable_instance_stub.list_instances.side_effect = [response_pb] # Perform the method and check the result. instances, failed_locations = client.list_instances() instance_1, instance_2 = instances self.assertIsInstance(instance_1, Instance) self.assertEqual(instance_1.name, INSTANCE_NAME1) self.assertTrue(instance_1._client is client) self.assertIsInstance(instance_2, Instance) self.assertEqual(instance_2.name, INSTANCE_NAME2) self.assertTrue(instance_2._client is client) self.assertEqual(failed_locations, [FAILED_LOCATION])
def test_create_w_explicit_serve_nodes(self): from google.api_core import operation from google.longrunning import operations_pb2 from tests.unit._testing import _FakeStub from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client, self.LOCATION_ID) # Create response_pb response_pb = operations_pb2.Operation(name=self.OP_NAME) # Patch the stub used by the API method. stub = _FakeStub(response_pb) client._instance_admin_client = api client._instance_admin_client.bigtable_instance_admin_stub = stub # Perform the method and check the result. result = instance.create() self.assertIsInstance(result, operation.Operation)
def test_exists(self): from google.cloud.bigtable_admin_v2.gapic import bigtable_instance_admin_client from google.cloud.bigtable_admin_v2.proto import instance_pb2 as data_v2_pb2 from google.api_core import exceptions instance_api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = client.instance(self.INSTANCE_ID) # Create response_pb response_pb = data_v2_pb2.AppProfile(name=self.APP_PROFILE_NAME) client._instance_admin_client = instance_api # Patch the stub used by the API method. client._instance_admin_client = instance_api instance_stub = client._instance_admin_client.transport instance_stub.get_app_profile.side_effect = [ response_pb, exceptions.NotFound("testing"), exceptions.BadRequest("testing"), ] # Perform the method and check the result. non_existing_app_profile_id = "other-app-profile-id" app_profile = self._make_one(self.APP_PROFILE_ID, instance) alt_app_profile = self._make_one(non_existing_app_profile_id, instance) self.assertTrue(app_profile.exists()) self.assertFalse(alt_app_profile.exists()) with self.assertRaises(exceptions.BadRequest): alt_app_profile.exists()
def test_test_iam_permissions(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.iam.v1 import iam_policy_pb2 credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) permissions = ["bigtable.tables.create", "bigtable.clusters.create"] expected_request = iam_policy_pb2.TestIamPermissionsRequest( resource=instance.name, permissions=permissions) # Patch the stub used by the API method. channel = ChannelStub(responses=[expected_request]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) client._instance_admin_client = instance_api result = instance.test_iam_permissions(permissions) actual_request = channel.requests[0][1] self.assertEqual(actual_request, expected_request) self.assertEqual(result, permissions)
def test_create(self): from google.api_core import operation from google.longrunning import operations_pb2 from google.cloud.bigtable.instance import Instance from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = Instance(self.INSTANCE_ID, client) cluster = self._make_one(self.CLUSTER_ID, instance, self.LOCATION_ID) # Create response_pb OP_ID = 5678 OP_NAME = ( 'operations/projects/{}/instances/{}/clusters/{}/operations/{}' .format(self.PROJECT, self.INSTANCE_ID, self.CLUSTER_ID, OP_ID)) response_pb = operations_pb2.Operation(name=OP_NAME) # Patch the stub used by the API method. client._instance_admin_client = api instance_admin_client = client._instance_admin_client instance_stub = instance_admin_client.bigtable_instance_admin_stub instance_stub.CreateCluster.side_effect = [response_pb] # Perform the method and check the result. result = cluster.create() self.assertIsInstance(result, operation.Operation) self.assertEqual(result.operation.name, OP_NAME) self.assertIsNone(result.metadata)
def test_update(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client, self.LOCATION_ID, display_name=self.DISPLAY_NAME) # Mock api calls client._instance_admin_client = api # Create expected_result. expected_result = None # Perform the method and check the result. result = instance.update() self.assertEqual(result, expected_result)
def test_list_clusters(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud.bigtable_admin_v2.proto import ( instance_pb2 as data_v2_pb2) from google.cloud.bigtable.instance import Instance from google.cloud.bigtable.instance import Cluster instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = Instance(self.INSTANCE_ID, client) failed_location = 'FAILED' cluster_id1 = 'cluster-id1' cluster_id2 = 'cluster-id2' cluster_name1 = (client.instance_admin_client.cluster_path( self.PROJECT, self.INSTANCE_ID, cluster_id1)) cluster_name2 = (client.instance_admin_client.cluster_path( self.PROJECT, self.INSTANCE_ID, cluster_id2)) # Create response_pb response_pb = messages_v2_pb2.ListClustersResponse( failed_locations=[ failed_location ], clusters=[ data_v2_pb2.Cluster( name=cluster_name1, ), data_v2_pb2.Cluster( name=cluster_name2, ), ], ) # Patch the stub used by the API method. client._instance_admin_client = instance_api instance_admin_client = client._instance_admin_client instance_stub = instance_admin_client.transport instance_stub.list_clusters.side_effect = [response_pb] # Perform the method and check the result. clusters, failed_locations = instance.list_clusters() cluster_1, cluster_2 = clusters self.assertIsInstance(cluster_1, Cluster) self.assertEqual(cluster_1.name, cluster_name1) self.assertIsInstance(cluster_2, Cluster) self.assertEqual(cluster_2.name, cluster_name2) self.assertEqual(failed_locations, [failed_location])
def test_list_app_profiles(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud.bigtable_admin_v2.proto import ( instance_pb2 as data_v2_pb2) from google.cloud.bigtable.app_profile import AppProfile instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) # Setup Expected Response next_page_token = '' app_profile_id1 = 'app-profile-id1' app_profile_id2 = 'app-profile-id2' app_profile_name1 = (client.instance_admin_client.app_profile_path( self.PROJECT, self.INSTANCE_ID, app_profile_id1)) app_profile_name2 = (client.instance_admin_client.app_profile_path( self.PROJECT, self.INSTANCE_ID, app_profile_id2)) routing_policy = data_v2_pb2.AppProfile.MultiClusterRoutingUseAny() expected_response = messages_v2_pb2.ListAppProfilesResponse( next_page_token=next_page_token, app_profiles=[ data_v2_pb2.AppProfile( name=app_profile_name1, multi_cluster_routing_use_any=routing_policy, ), data_v2_pb2.AppProfile( name=app_profile_name2, multi_cluster_routing_use_any=routing_policy, ) ], ) # Patch the stub used by the API method. client._instance_admin_client = instance_api bigtable_instance_stub = ( client._instance_admin_client.transport) bigtable_instance_stub.list_app_profiles.side_effect = [ expected_response] # Perform the method and check the result. app_profiles = instance.list_app_profiles() app_profile_1, app_profile_2 = app_profiles self.assertIsInstance(app_profile_1, AppProfile) self.assertEqual(app_profile_1.name, app_profile_name1) self.assertIsInstance(app_profile_2, AppProfile) self.assertEqual(app_profile_2.name, app_profile_name2)
def test_update(self): import datetime from google.api_core import operation from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2, ) from google.cloud.bigtable_admin_v2.types import instance_pb2 from google.cloud.bigtable_admin_v2.gapic import bigtable_instance_admin_client from google.cloud.bigtable.enums import StorageType NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() client = self._make_client( project=self.PROJECT, credentials=credentials, admin=True ) STORAGE_TYPE_SSD = StorageType.SSD instance = _Instance(self.INSTANCE_ID, client) cluster = self._make_one( self.CLUSTER_ID, instance, location_id=self.LOCATION_ID, serve_nodes=self.SERVE_NODES, default_storage_type=STORAGE_TYPE_SSD, ) # Create expected_request expected_request = instance_pb2.Cluster( name=cluster.name, serve_nodes=self.SERVE_NODES ) metadata = messages_v2_pb2.UpdateClusterMetadata(request_time=NOW_PB) type_url = "type.googleapis.com/{}".format( messages_v2_pb2.UpdateClusterMetadata.DESCRIPTOR.full_name ) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any(type_url=type_url, value=metadata.SerializeToString()), ) # Patch the stub used by the API method. channel = ChannelStub(responses=[response_pb]) api = bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel ) client._instance_admin_client = api # Perform the method and check the result. result = cluster.update() actual_request = channel.requests[0][1] self.assertEqual(actual_request, expected_request) self.assertIsInstance(result, operation.Operation) self.assertEqual(result.operation.name, self.OP_NAME) self.assertIsInstance(result.metadata, messages_v2_pb2.UpdateClusterMetadata)
def test_update_app_profile_routing_single(self): from google.api_core import operation from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud.bigtable.enums import RoutingPolicyType from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.protobuf import field_mask_pb2 credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = client.instance(self.INSTANCE_ID) routing = RoutingPolicyType.ANY app_profile = self._make_one(self.APP_PROFILE_ID, instance, routing_policy_type=routing) # Create response_pb metadata = messages_v2_pb2.UpdateAppProfileMetadata() type_url = 'type.googleapis.com/{}'.format( messages_v2_pb2.UpdateAppProfileMetadata.DESCRIPTOR.full_name) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any( type_url=type_url, value=metadata.SerializeToString(), )) # Patch the stub used by the API method. channel = ChannelStub(responses=[response_pb]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) # Mock api calls client._instance_admin_client = instance_api # Perform the method and check the result. ignore_warnings = True expected_request_update_mask = field_mask_pb2.FieldMask( paths=['multi_cluster_routing_use_any']) expected_request = messages_v2_pb2.UpdateAppProfileRequest( app_profile=app_profile._to_pb(), update_mask=expected_request_update_mask, ignore_warnings=ignore_warnings) result = app_profile.update(ignore_warnings=ignore_warnings) actual_request = channel.requests[0][1] self.assertEqual(actual_request, expected_request) self.assertIsInstance(result, operation.Operation) self.assertEqual(result.operation.name, self.OP_NAME) self.assertIsInstance(result.metadata, messages_v2_pb2.UpdateAppProfileMetadata)
def test_reload(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable_admin_v2.proto import ( instance_pb2 as data_v2_pb2) from google.cloud.bigtable.enums import StorageType from google.cloud.bigtable.enums import Cluster api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) STORAGE_TYPE_SSD = StorageType.SSD instance = _Instance(self.INSTANCE_ID, client) cluster = self._make_one(self.CLUSTER_ID, instance, location_id=self.LOCATION_ID, serve_nodes=self.SERVE_NODES, default_storage_type=STORAGE_TYPE_SSD) # Create response_pb LOCATION_ID_FROM_SERVER = 'new-location-id' STATE = Cluster.State.READY SERVE_NODES_FROM_SERVER = 10 STORAGE_TYPE_FROM_SERVER = StorageType.HDD response_pb = data_v2_pb2.Cluster( name=cluster.name, location=self.LOCATION_PATH + LOCATION_ID_FROM_SERVER, state=STATE, serve_nodes=SERVE_NODES_FROM_SERVER, default_storage_type=STORAGE_TYPE_FROM_SERVER ) # Patch the stub used by the API method. client._instance_admin_client = api instance_admin_client = client._instance_admin_client instance_stub = instance_admin_client.bigtable_instance_admin_stub instance_stub.GetCluster.side_effect = [response_pb] # Create expected_result. expected_result = None # reload() has no return value. # Check Cluster optional config values before. self.assertEqual(cluster.location_id, self.LOCATION_ID) self.assertIsNone(cluster.state) self.assertEqual(cluster.serve_nodes, self.SERVE_NODES) self.assertEqual(cluster.default_storage_type, STORAGE_TYPE_SSD) # Perform the method and check the result. result = cluster.reload() self.assertEqual(result, expected_result) self.assertEqual(cluster.location_id, LOCATION_ID_FROM_SERVER) self.assertEqual(cluster.state, STATE) self.assertEqual(cluster.serve_nodes, SERVE_NODES_FROM_SERVER) self.assertEqual(cluster.default_storage_type, STORAGE_TYPE_FROM_SERVER)
def test_create(self): import datetime from google.api_core import operation from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.bigtable_admin_v2 import enums from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable.cluster import DEFAULT_SERVE_NODES NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client, display_name=self.DISPLAY_NAME) # Create response_pb metadata = messages_v2_pb2.CreateInstanceMetadata(request_time=NOW_PB) type_url = 'type.googleapis.com/%s' % ( messages_v2_pb2.CreateInstanceMetadata.DESCRIPTOR.full_name, ) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any( type_url=type_url, value=metadata.SerializeToString(), )) # Patch the stub used by the API method. channel = ChannelStub(responses=[response_pb]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) client._instance_admin_client = instance_api # Perform the method and check the result. result = instance.create(location_id=self.LOCATION_ID) actual_request = channel.requests[0][1] cluster_id = '{}-cluster'.format(self.INSTANCE_ID) cluster = self._create_cluster( instance_api, cluster_id, self.LOCATION_ID, DEFAULT_SERVE_NODES, enums.StorageType.STORAGE_TYPE_UNSPECIFIED) expected_request = self._create_instance_request( self.DISPLAY_NAME, {cluster_id: cluster}) self.assertEqual(expected_request, actual_request) self.assertIsInstance(result, operation.Operation) # self.assertEqual(result.operation.name, self.OP_NAME) self.assertIsInstance(result.metadata, messages_v2_pb2.CreateInstanceMetadata)
def test_create_app_profile_with_single_routing_policy(self): from google.cloud.bigtable_admin_v2.proto import instance_pb2 from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable.enums import RoutingPolicyType credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) description = 'description-1724546052' app_profile_id = 'appProfileId1262094415' cluster_id = 'cluster-id' expected_response = { 'name': self.APP_PROFILE_PATH + app_profile_id, 'description': description, 'single_cluster_routing': instance_pb2.AppProfile.SingleClusterRouting( cluster_id=cluster_id, allow_transactional_writes=False) } expected_request = { 'app_profile_id': app_profile_id, 'routing_policy_type': RoutingPolicyType.SINGLE, 'description': description, 'cluster_id': cluster_id } expected_response = instance_pb2.AppProfile(**expected_response) channel = ChannelStub(responses=[expected_response]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) # Patch the stub used by the API method. client._instance_admin_client = instance_api # Perform the method and check the result. result = instance.create_app_profile(**expected_request) parent = client._instance_admin_client.instance_path( self.PROJECT, self.INSTANCE_ID) expected_request = _CreateAppProfileRequestPB( parent=parent, app_profile_id=app_profile_id, app_profile=expected_response, ) actual_request = channel.requests[0][1] self.assertEqual(expected_request, actual_request) self.assertEqual(result, expected_response)
def test_create_routing_single(self): from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2, ) from google.cloud.bigtable.enums import RoutingPolicyType from google.cloud.bigtable_admin_v2.gapic import bigtable_instance_admin_client credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = client.instance(self.INSTANCE_ID) routing = RoutingPolicyType.SINGLE description = "routing policy single" allow_writes = False ignore_warnings = True app_profile = self._make_one( self.APP_PROFILE_ID, instance, routing_policy_type=routing, description=description, cluster_id=self.CLUSTER_ID, allow_transactional_writes=allow_writes, ) expected_request_app_profile = app_profile._to_pb() expected_request = messages_v2_pb2.CreateAppProfileRequest( parent=instance.name, app_profile_id=self.APP_PROFILE_ID, app_profile=expected_request_app_profile, ignore_warnings=ignore_warnings, ) # Patch the stub used by the API method. channel = ChannelStub(responses=[expected_request_app_profile]) instance_api = bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel) client._instance_admin_client = instance_api # Perform the method and check the result. result = app_profile.create(ignore_warnings) actual_request = channel.requests[0][1] self.assertEqual(actual_request, expected_request) self.assertIsInstance(result, self._get_target_class()) self.assertEqual(result.app_profile_id, self.APP_PROFILE_ID) self.assertIs(result._instance, instance) self.assertEqual(result.routing_policy_type, routing) self.assertEqual(result.description, description) self.assertEqual(result.allow_transactional_writes, allow_writes) self.assertEqual(result.cluster_id, self.CLUSTER_ID)
def test_create_app_profile_with_multi_routing_policy(self): from google.cloud.bigtable_admin_v2.proto import instance_pb2 from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) description = 'description-1724546052' app_profile_id = 'appProfileId1262094415' expected_response = { 'name': self.APP_PROFILE_PATH + app_profile_id, 'description': description, 'multi_cluster_routing_use_any': instance_pb2.AppProfile.MultiClusterRoutingUseAny() } expected_request = { 'app_profile_id': app_profile_id, 'routing_policy_type': 1, 'description': description } expected_response = instance_pb2.AppProfile(**expected_response) channel = ChannelStub(responses=[expected_response]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) # Patch the stub used by the API method. client._instance_admin_client = instance_api # Perform the method and check the result. result = instance.create_app_profile(**expected_request) parent = client._instance_admin_client.instance_path( self.PROJECT, self.INSTANCE_ID) expected_request = _CreateAppProfileRequestPB( parent=parent, app_profile_id=app_profile_id, app_profile=expected_response, ) actual_request = channel.requests[0][1] assert expected_request == actual_request self.assertEqual(result, expected_response)
def test_update_app_profile_with_single_routing_policy(self): import datetime from google.api_core import operation from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud._helpers import _datetime_to_pb_timestamp from tests.unit._testing import _FakeStub from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) # Create response_pb NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) metadata = messages_v2_pb2.CreateInstanceMetadata(request_time=NOW_PB) type_url = 'type.googleapis.com/%s' % ( messages_v2_pb2.CreateInstanceMetadata.DESCRIPTOR.full_name, ) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any( type_url=type_url, value=metadata.SerializeToString(), )) # Patch the stub used by the API method. client._instance_admin_client = instance_api stub = _FakeStub(response_pb) client._instance_admin_client.bigtable_instance_admin_stub = stub update_mask = [] # Perform the method and check the result. app_profile_id = 'appProfileId1262094415' cluster_id = 'cluster-id' result = instance.update_app_profile(app_profile_id, update_mask=update_mask, routing_policy_type=2, cluster_id=cluster_id) self.assertIsInstance(result, operation.Operation)
def test_name_property(self): from google.cloud.bigtable_admin_v2.gapic import bigtable_instance_admin_client api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) # Patch the the API method. client._instance_admin_client = api instance = self._make_one(self.INSTANCE_ID, client) self.assertEqual(instance.name, self.INSTANCE_NAME)
def test_create(self): import datetime from google.api_core import operation from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as messages_v2_pb2) from google.cloud._helpers import _datetime_to_pb_timestamp from tests.unit._testing import _FakeStub from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) NOW = datetime.datetime.utcnow() NOW_PB = _datetime_to_pb_timestamp(NOW) api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client, self.LOCATION_ID, display_name=self.DISPLAY_NAME) # Create response_pb metadata = messages_v2_pb2.CreateInstanceMetadata(request_time=NOW_PB) type_url = 'type.googleapis.com/%s' % ( messages_v2_pb2.CreateInstanceMetadata.DESCRIPTOR.full_name, ) response_pb = operations_pb2.Operation( name=self.OP_NAME, metadata=Any( type_url=type_url, value=metadata.SerializeToString(), )) # Patch the stub used by the API method. stub = _FakeStub(response_pb) client._instance_admin_client = api client._instance_admin_client.bigtable_instance_admin_stub = stub # Perform the method and check the result. result = instance.create() self.assertIsInstance(result, operation.Operation) # self.assertEqual(result.operation.name, self.OP_NAME) self.assertIsInstance(result.metadata, messages_v2_pb2.CreateInstanceMetadata)
def test_set_iam_policy(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.iam.v1 import iam_policy_pb2 from google.iam.v1 import policy_pb2 from google.cloud.bigtable.policy import Policy from google.cloud.bigtable.policy import BIGTABLE_ADMIN_ROLE credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) version = 1 etag = b'etag_v1' bindings = [{'role': BIGTABLE_ADMIN_ROLE, 'members': ['serviceAccount:[email protected]', 'user:[email protected]']}] expected_request_policy = policy_pb2.Policy(version=version, etag=etag, bindings=bindings) expected_request = iam_policy_pb2.SetIamPolicyRequest( resource=instance.name, policy=expected_request_policy ) # Patch the stub used by the API method. channel = ChannelStub(responses=[expected_request_policy]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) client._instance_admin_client = instance_api # Perform the method and check the result. policy_request = Policy(etag=etag, version=version) policy_request[BIGTABLE_ADMIN_ROLE] = [Policy.user("*****@*****.**"), Policy.service_account( "*****@*****.**")] result = instance.set_iam_policy(policy_request) actual_request = channel.requests[0][1] self.assertEqual(actual_request, expected_request) self.assertEqual(result.bigtable_admins, policy_request.bigtable_admins)
def _list_tables_helper(self, table_name=None): from google.cloud.bigtable_admin_v2.proto import ( table_pb2 as table_data_v2_pb2) from google.cloud.bigtable_admin_v2.proto import ( bigtable_table_admin_pb2 as table_messages_v1_pb2) from google.cloud.bigtable_admin_v2.gapic import ( bigtable_table_admin_client, bigtable_instance_admin_client) table_api = bigtable_table_admin_client.BigtableTableAdminClient( mock.Mock()) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) # Create response_pb if table_name is None: table_name = self.TABLE_NAME response_pb = table_messages_v1_pb2.ListTablesResponse( tables=[ table_data_v2_pb2.Table(name=table_name), ], ) # Patch the stub used by the API method. client._table_admin_client = table_api client._instance_admin_client = instance_api bigtable_table_stub = ( client._table_admin_client.transport) bigtable_table_stub.list_tables.side_effect = [response_pb] # Create expected_result. expected_table = instance.table(self.TABLE_ID) expected_result = [expected_table] # Perform the method and check the result. result = instance.list_tables() self.assertEqual(result, expected_result)
def test_update_empty(self): from google.api_core import operation from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.longrunning import operations_pb2 from google.protobuf import field_mask_pb2 from google.cloud.bigtable_admin_v2.types import instance_pb2 from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as instance_v2_pb2) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(None, client) expected_request_instance = instance_pb2.Instance( name=instance.name, display_name=instance.display_name, type=instance.type_, labels=instance.labels) expected_request_update_mask = field_mask_pb2.FieldMask() expected_request = instance_v2_pb2.PartialUpdateInstanceRequest( instance=expected_request_instance, update_mask=expected_request_update_mask) response_pb = operations_pb2.Operation(name=self.OP_NAME) channel = ChannelStub(responses=[response_pb]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) # Mock api calls client._instance_admin_client = instance_api # Perform the method and check the result. result = instance.update() actual_request = channel.requests[0][1] self.assertIsInstance(result, operation.Operation) self.assertEqual(actual_request, expected_request)
def test_get_app_profile(self): from google.cloud.bigtable_admin_v2.proto import (instance_pb2 as instance_data_v2_pb2) from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) name = 'name3373707' etag = 'etag3123477' description = 'description-1724546052' expected_response = { 'name': name, 'etag': etag, 'description': description } expected_response = instance_data_v2_pb2.AppProfile( **expected_response) response_pb = instance_data_v2_pb2.AppProfile(name=name, etag=etag, description=description) # Patch the stub used by the API method. client._instance_admin_client = instance_api bigtable_instance_stub = ( client._instance_admin_client.bigtable_instance_admin_stub) bigtable_instance_stub.GetAppProfile.side_effect = [response_pb] # Perform the method and check the result. app_profile_id = 'appProfileId1262094415' result = instance.get_app_profile(app_profile_id=app_profile_id) self.assertEqual(result, expected_response)
def test_create_w_explicit_serve_nodes(self): from google.api_core import operation from google.longrunning import operations_pb2 from google.cloud.bigtable_admin_v2 import enums from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) serve_nodes = 10 credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client, display_name=self.DISPLAY_NAME) # Create response_pb response_pb = operations_pb2.Operation(name=self.OP_NAME) # Patch the stub used by the API method. channel = ChannelStub(responses=[response_pb]) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( channel=channel)) client._instance_admin_client = instance_api # Perform the method and check the result. result = instance.create(location_id=self.LOCATION_ID, serve_nodes=serve_nodes, default_storage_type=enums.StorageType.SSD) actual_request = channel.requests[0][1] cluster_id = '{}-cluster'.format(self.INSTANCE_ID) cluster = self._create_cluster(instance_api, cluster_id, self.LOCATION_ID, serve_nodes, enums.StorageType.SSD) expected_request = self._create_instance_request( self.DISPLAY_NAME, {cluster_id: cluster}) self.assertEqual(expected_request, actual_request) self.assertIsInstance(result, operation.Operation)
def test_reload(self): from google.cloud.bigtable_admin_v2.proto import ( instance_pb2 as data_v2_pb2) from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable import enums api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) # Create response_pb DISPLAY_NAME = u'hey-hi-hello' instance_type = enums.Instance.Type.PRODUCTION response_pb = data_v2_pb2.Instance( display_name=DISPLAY_NAME, type=instance_type, labels=self.LABELS ) # Patch the stub used by the API method. client._instance_admin_client = api bigtable_instance_stub = ( client._instance_admin_client.transport) bigtable_instance_stub.get_instance.side_effect = [response_pb] # Create expected_result. expected_result = None # reload() has no return value. # Check Instance optional config values before. self.assertEqual(instance.display_name, self.INSTANCE_ID) # Perform the method and check the result. result = instance.reload() self.assertEqual(result, expected_result) # Check Instance optional config values before. self.assertEqual(instance.display_name, DISPLAY_NAME)
def test_delete(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) # Mock api calls client._instance_admin_client = api # Create expected_result. expected_result = None # delete() has no return value. # Perform the method and check the result. result = instance.delete() self.assertEqual(result, expected_result)
def test_exists(self): from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) from google.cloud.bigtable_admin_v2.proto import ( instance_pb2 as data_v2_pb2) from google.cloud.bigtable.instance import Instance from google.api_core import exceptions instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = Instance(self.INSTANCE_ID, client) # Create response_pb cluster_name = client.instance_admin_client.cluster_path( self.PROJECT, self.INSTANCE_ID, self.CLUSTER_ID) response_pb = data_v2_pb2.Cluster(name=cluster_name) # Patch the stub used by the API method. client._instance_admin_client = instance_api instance_admin_client = client._instance_admin_client instance_stub = instance_admin_client.bigtable_instance_admin_stub instance_stub.GetCluster.side_effect = [ response_pb, exceptions.NotFound('testing'), exceptions.BadRequest('testing') ] # Perform the method and check the result. non_existing_cluster_id = 'cluster-id-2' alt_cluster_1 = self._make_one(self.CLUSTER_ID, instance) alt_cluster_2 = self._make_one(non_existing_cluster_id, instance) self.assertTrue(alt_cluster_1.exists()) self.assertFalse(alt_cluster_2.exists()) with self.assertRaises(exceptions.BadRequest): alt_cluster_1.exists()
def test_list_app_profiles(self): from google.cloud.bigtable_admin_v2.proto import ( bigtable_instance_admin_pb2 as instance_messages_v1_pb2) from google.cloud.bigtable_admin_v2.gapic import ( bigtable_instance_admin_client) instance_api = ( bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance = self._make_one(self.INSTANCE_ID, client) # Setup Expected Response next_page_token = '' app_profiles_element = {} app_profiles = [app_profiles_element] expected_response = { 'next_page_token': next_page_token, 'app_profiles': app_profiles } expected_response = instance_messages_v1_pb2.ListAppProfilesResponse( **expected_response) # Patch the stub used by the API method. client._instance_admin_client = instance_api bigtable_instance_stub = ( client._instance_admin_client.bigtable_instance_admin_stub) bigtable_instance_stub.ListAppProfiles.side_effect = [ expected_response ] # Perform the method and check the result. response = instance.list_app_profiles() self.assertEqual(response[0], expected_response.app_profiles[0])