def test_update_cluster(self): # Setup Expected Response name_2 = 'name2-1052831874' location = 'location1901043637' serve_nodes_2 = 1623486220 expected_response = { 'name': name_2, 'location': location, 'serve_nodes': serve_nodes_2 } expected_response = instance_pb2.Cluster(**expected_response) operation = operations_pb2.Operation( name='operations/test_update_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 name = client.cluster_path('[PROJECT]', '[INSTANCE]', '[CLUSTER]') serve_nodes = 1288838783 response = client.update_cluster(name, serve_nodes) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = instance_pb2.Cluster( name=name, serve_nodes=serve_nodes) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_cluster(self): # Setup Expected Response name_2 = "name2-1052831874" location = "location1901043637" serve_nodes_2 = 1623486220 expected_response = { "name": name_2, "location": location, "serve_nodes": serve_nodes_2, } expected_response = instance_pb2.Cluster(**expected_response) operation = operations_pb2.Operation( name="operations/test_update_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 name = client.cluster_path("[PROJECT]", "[INSTANCE]", "[CLUSTER]") serve_nodes = 1288838783 response = client.update_cluster(name, serve_nodes) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = instance_pb2.Cluster(name=name, serve_nodes=serve_nodes) actual_request = channel.requests[0][1] assert expected_request == actual_request
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_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 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_path_template = "projects/{}/instances/{}/clusters/{}" cluster_name1 = cluster_path_template.format(self.PROJECT, self.INSTANCE_ID, cluster_id1) cluster_name2 = cluster_path_template.format(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. instance_api = mock.create_autospec( bigtable_instance_admin_client.BigtableInstanceAdminClient) instance_api.list_clusters.side_effect = [response_pb] instance_api.cluster_path = cluster_path_template.format client._instance_admin_client = instance_api # 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_get_cluster(self): # Setup Expected Response name_2 = "name2-1052831874" location = "location1901043637" serve_nodes = 1288838783 expected_response = { "name": name_2, "location": location, "serve_nodes": serve_nodes, } expected_response = instance_pb2.Cluster(**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.BigtableInstanceAdminClient() # Setup Request name = client.cluster_path("[PROJECT]", "[INSTANCE]", "[CLUSTER]") response = client.get_cluster(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.GetClusterRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_cluster(self): # Setup Expected Response name_2 = 'name2-1052831874' location = 'location1901043637' serve_nodes = 1288838783 expected_response = { 'name': name_2, 'location': location, 'serve_nodes': serve_nodes } expected_response = instance_pb2.Cluster(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = bigtable_admin_v2.BigtableInstanceAdminClient(channel=channel) # Setup Request name = client.cluster_path('[PROJECT]', '[INSTANCE]', '[CLUSTER]') response = client.get_cluster(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.GetClusterRequest( name=name) 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]) 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_from_pb_success(self): from google.cloud.bigtable_admin_v2.proto import ( instance_pb2 as data_v2_pb2) from google.cloud.bigtable import enums client = _Client(self.PROJECT) instance = _Instance(self.INSTANCE_ID, client) location = self.LOCATION_PATH + self.LOCATION_ID state = enums.Cluster.State.RESIZING storage_type = enums.StorageType.SSD cluster_pb = data_v2_pb2.Cluster( name=self.CLUSTER_NAME, location=location, state=state, serve_nodes=self.SERVE_NODES, default_storage_type=storage_type ) klass = self._get_target_class() cluster = klass.from_pb(cluster_pb, instance) self.assertIsInstance(cluster, klass) self.assertEqual(cluster._instance, instance) self.assertEqual(cluster.cluster_id, self.CLUSTER_ID) self.assertEqual(cluster.location_id, self.LOCATION_ID) self.assertEqual(cluster.state, state) self.assertEqual(cluster.serve_nodes, self.SERVE_NODES) self.assertEqual(cluster.default_storage_type, storage_type)
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_from_pb_bad_cluster_name(self): from google.cloud.bigtable_admin_v2.proto import instance_pb2 as data_v2_pb2 bad_cluster_name = "BAD_NAME" cluster_pb = data_v2_pb2.Cluster(name=bad_cluster_name) klass = self._get_target_class() with self.assertRaises(ValueError): klass.from_pb(cluster_pb, None)
def test_from_pb_project_mistmatch(self): from google.cloud.bigtable_admin_v2.proto import instance_pb2 as data_v2_pb2 ALT_PROJECT = "ALT_PROJECT" client = _Client(project=ALT_PROJECT) instance = _Instance(self.INSTANCE_ID, client) self.assertNotEqual(self.PROJECT, ALT_PROJECT) cluster_pb = data_v2_pb2.Cluster(name=self.CLUSTER_NAME) klass = self._get_target_class() with self.assertRaises(ValueError): klass.from_pb(cluster_pb, instance)
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_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 _ClusterPB(*args, **kw): from google.cloud.bigtable_admin_v2.proto import (instance_pb2 as instance_v2_pb2) return instance_v2_pb2.Cluster(*args, **kw)
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 Cluster instance_api = bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock()) credentials = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=credentials, admin=True) INSTANCE_ID1 = "instance-id1" INSTANCE_ID2 = "instance-id2" failed_location = "FAILED" cluster_id1 = "{}-cluster".format(INSTANCE_ID1) cluster_id2 = "{}-cluster-1".format(INSTANCE_ID2) cluster_id3 = "{}-cluster-2".format(INSTANCE_ID2) cluster_name1 = client.instance_admin_client.cluster_path( self.PROJECT, INSTANCE_ID1, cluster_id1) cluster_name2 = client.instance_admin_client.cluster_path( self.PROJECT, INSTANCE_ID2, cluster_id2) cluster_name3 = client.instance_admin_client.cluster_path( self.PROJECT, INSTANCE_ID2, cluster_id3) # 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), data_v2_pb2.Cluster(name=cluster_name3), ], ) # Patch the stub used by the API method. client._instance_admin_client = instance_api instance_stub = client._instance_admin_client.transport instance_stub.list_clusters.side_effect = [response_pb] # Perform the method and check the result. clusters, failed_locations = client.list_clusters() cluster_1, cluster_2, cluster_3 = clusters self.assertIsInstance(cluster_1, Cluster) self.assertEqual(cluster_1.name, cluster_name1) self.assertEqual(cluster_1._instance.instance_id, INSTANCE_ID1) self.assertIsInstance(cluster_2, Cluster) self.assertEqual(cluster_2.name, cluster_name2) self.assertEqual(cluster_2._instance.instance_id, INSTANCE_ID2) self.assertIsInstance(cluster_3, Cluster) self.assertEqual(cluster_3.name, cluster_name3) self.assertEqual(cluster_3._instance.instance_id, INSTANCE_ID2) self.assertEqual(failed_locations, [failed_location])