def test_create_cluster(self): # Setup Expected Response name = 'name3373707' location = 'location1901043637' serve_nodes = 1288838783 expected_response = { 'name': name, 'location': location, 'serve_nodes': serve_nodes } expected_response = instance_pb2.Cluster(**expected_response) operation = operations_pb2.Operation( name='operations/test_create_cluster', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = bigtable_admin_v2.BigtableInstanceAdminClient(channel=channel) # Setup Request parent = client.instance_path('[PROJECT]', '[INSTANCE]') cluster_id = 'clusterId240280960' cluster = {} response = client.create_cluster(parent, cluster_id, cluster) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.CreateClusterRequest( parent=parent, cluster_id=cluster_id, cluster=cluster) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_cluster(self): # Setup Expected Response name = "name3373707" location = "location1901043637" serve_nodes = 1288838783 expected_response = { "name": name, "location": location, "serve_nodes": serve_nodes, } expected_response = instance_pb2.Cluster(**expected_response) operation = operations_pb2.Operation( name="operations/test_create_cluster", 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.BigtableInstanceAdminClient() # Setup Request parent = client.instance_path("[PROJECT]", "[INSTANCE]") cluster_id = "clusterId240280960" cluster = {} response = client.create_cluster(parent, cluster_id, cluster) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.CreateClusterRequest( parent=parent, cluster_id=cluster_id, cluster=cluster ) actual_request = channel.requests[0][1] assert expected_request == actual_request
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.instance import Instance 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_admin_v2.proto import ( bigtable_instance_admin_pb2 as instance_v2_pb2) 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 LOCATION = self.LOCATION_PATH + self.LOCATION_ID 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) expected_request_cluster = instance_pb2.Cluster( location=LOCATION, serve_nodes=cluster.serve_nodes, default_storage_type=cluster.default_storage_type) expected_request = instance_v2_pb2.CreateClusterRequest( parent=instance.name, cluster_id=self.CLUSTER_ID, cluster=expected_request_cluster) metadata = messages_v2_pb2.CreateClusterMetadata(request_time=NOW_PB) type_url = 'type.googleapis.com/{}'.format( messages_v2_pb2.CreateClusterMetadata.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.create() 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.CreateClusterMetadata)