def test_update_instance(self): # Setup Expected Response name_2 = 'name2-1052831874' display_name_2 = 'displayName21615000987' expected_response = {'name': name_2, 'display_name': display_name_2} expected_response = instance_pb2.Instance(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = bigtable_admin_v2.BigtableInstanceAdminClient(channel=channel) # Setup Request name = client.instance_path('[PROJECT]', '[INSTANCE]') display_name = 'displayName1615086568' type_ = enums.Instance.Type.TYPE_UNSPECIFIED labels = {} response = client.update_instance(name, display_name, type_, labels) assert expected_response == response assert len(channel.requests) == 1 expected_request = instance_pb2.Instance( name=name, display_name=display_name, type=type_, labels=labels) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_update_instance(self): # Setup Expected Response name_2 = "name2-1052831874" display_name_2 = "displayName21615000987" expected_response = {"name": name_2, "display_name": display_name_2} expected_response = instance_pb2.Instance(**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.instance_path("[PROJECT]", "[INSTANCE]") display_name = "displayName1615086568" type_ = enums.Instance.Type.TYPE_UNSPECIFIED labels = {} response = client.update_instance(name, display_name, type_, labels) assert expected_response == response assert len(channel.requests) == 1 expected_request = instance_pb2.Instance(name=name, display_name=display_name, type=type_, labels=labels) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_partial_update_instance(self): # Setup Expected Response name = 'name3373707' display_name = 'displayName1615086568' expected_response = {'name': name, 'display_name': display_name} expected_response = instance_pb2.Instance(**expected_response) operation = operations_pb2.Operation( name='operations/test_partial_update_instance', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = bigtable_admin_v2.BigtableInstanceAdminClient(channel=channel) # Setup Request instance = instance_pb2.Instance() update_mask = {} response = client.partial_update_instance(instance, update_mask) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.PartialUpdateInstanceRequest( instance=instance, update_mask=update_mask) actual_request = channel.requests[0][1] assert expected_request == actual_request
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_instance(self): # Setup Expected Response name = 'name3373707' display_name = 'displayName1615086568' expected_response = {'name': name, 'display_name': display_name} expected_response = instance_pb2.Instance(**expected_response) operation = operations_pb2.Operation( name='operations/test_create_instance', 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.project_path('[PROJECT]') instance_id = 'instanceId-2101995259' instance = {} clusters = {} response = client.create_instance(parent, instance_id, instance, clusters) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.CreateInstanceRequest( parent=parent, instance_id=instance_id, instance=instance, clusters=clusters) 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 credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) instance_type = enums.Instance.Type.PRODUCTION state = enums.Instance.State.READY instance_pb = data_v2_pb2.Instance( name=self.INSTANCE_NAME, display_name=self.INSTANCE_ID, type=instance_type, labels=self.LABELS, state=state, ) klass = self._get_target_class() instance = klass.from_pb(instance_pb, client) self.assertIsInstance(instance, klass) self.assertEqual(instance._client, client) self.assertEqual(instance.instance_id, self.INSTANCE_ID) self.assertEqual(instance.display_name, self.INSTANCE_ID) self.assertEqual(instance.type_, instance_type) self.assertEqual(instance.labels, self.LABELS) self.assertEqual(instance._state, state)
def test_create_instance(self): # Setup Expected Response name = "name3373707" display_name = "displayName1615086568" expected_response = {"name": name, "display_name": display_name} expected_response = instance_pb2.Instance(**expected_response) operation = operations_pb2.Operation( name="operations/test_create_instance", 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.project_path("[PROJECT]") instance_id = "instanceId-2101995259" instance = {} clusters = {} response = client.create_instance(parent, instance_id, instance, clusters) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.CreateInstanceRequest( parent=parent, instance_id=instance_id, instance=instance, clusters=clusters ) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_get_instance(self): # Setup Expected Response name_2 = "name2-1052831874" display_name = "displayName1615086568" expected_response = {"name": name_2, "display_name": display_name} expected_response = instance_pb2.Instance(**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.instance_path("[PROJECT]", "[INSTANCE]") response = client.get_instance(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.GetInstanceRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_partial_update_instance(self): # Setup Expected Response name = "name3373707" display_name = "displayName1615086568" expected_response = {"name": name, "display_name": display_name} expected_response = instance_pb2.Instance(**expected_response) operation = operations_pb2.Operation( name="operations/test_partial_update_instance", 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 instance = {} update_mask = {} response = client.partial_update_instance(instance, update_mask) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.PartialUpdateInstanceRequest( instance=instance, update_mask=update_mask) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test__update_from_pb_no_display_name(self): from google.cloud.bigtable_admin_v2.proto import instance_pb2 as data_v2_pb2 instance_pb = data_v2_pb2.Instance() instance = self._make_one(None, None) self.assertIsNone(instance.display_name) with self.assertRaises(ValueError): instance._update_from_pb(instance_pb)
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) 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) channel = _make_channel() client = self._make_one(project=self.PROJECT, channel=channel, 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. bigtable_instance_stub = ( client._instance_admin_client.bigtable_instance_admin_stub) bigtable_instance_stub.ListInstances.side_effect = [response_pb] expected_result = response_pb # Perform the method and check the result. result = client.list_instances() self.assertEqual(result, expected_result)
def test_from_pb_bad_instance_name(self): from google.cloud.bigtable_admin_v2.proto import instance_pb2 as data_v2_pb2 instance_name = "INCORRECT_FORMAT" instance_pb = data_v2_pb2.Instance(name=instance_name) klass = self._get_target_class() with self.assertRaises(ValueError): klass.from_pb(instance_pb, None)
def test__update_from_pb_success(self): from google.cloud.bigtable_admin_v2.proto import (instance_pb2 as data_v2_pb2) display_name = 'display_name' instance_pb = data_v2_pb2.Instance(display_name=display_name, ) instance = self._make_one(None, None, None, None) self.assertIsNone(instance.display_name) instance._update_from_pb(instance_pb) self.assertEqual(instance.display_name, display_name)
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) self.assertNotEqual(self.PROJECT, ALT_PROJECT) instance_pb = data_v2_pb2.Instance(name=self.INSTANCE_NAME) klass = self._get_target_class() with self.assertRaises(ValueError): klass.from_pb(instance_pb, client)
def test__update_from_pb_success_defaults(self): from google.cloud.bigtable_admin_v2.proto import instance_pb2 as data_v2_pb2 from google.cloud.bigtable import enums instance_pb = data_v2_pb2.Instance(display_name=self.DISPLAY_NAME) instance = self._make_one(None, None) self.assertIsNone(instance.display_name) self.assertIsNone(instance.type_) self.assertIsNone(instance.labels) instance._update_from_pb(instance_pb) self.assertEqual(instance.display_name, self.DISPLAY_NAME) self.assertEqual(instance.type_, enums.Instance.Type.UNSPECIFIED) self.assertFalse(instance.labels)
def test_update_instance(self): # Setup Expected Response name = "name3373707" display_name_2 = "displayName21615000987" expected_response = {"name": name, "display_name": display_name_2} expected_response = instance_pb2.Instance(**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 display_name = "displayName1615086568" response = client.update_instance(display_name) assert expected_response == response assert len(channel.requests) == 1 expected_request = instance_pb2.Instance(display_name=display_name) actual_request = channel.requests[0][1] assert expected_request == actual_request
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" credentials = _make_credentials() client = self._make_client(project=ALT_PROJECT, credentials=credentials, admin=True) self.assertNotEqual(self.PROJECT, ALT_PROJECT) instance_pb = data_v2_pb2.Instance(name=self.INSTANCE_NAME) klass = self._get_target_class() with self.assertRaises(ValueError): klass.from_pb(instance_pb, client)
def test_from_pb_success(self): from google.cloud.bigtable_admin_v2.proto import (instance_pb2 as data_v2_pb2) client = _Client(project=self.PROJECT) instance_pb = data_v2_pb2.Instance( name=self.INSTANCE_NAME, display_name=self.INSTANCE_ID, ) klass = self._get_target_class() instance = klass.from_pb(instance_pb, client) self.assertIsInstance(instance, klass) self.assertEqual(instance._client, client) self.assertEqual(instance.instance_id, self.INSTANCE_ID)
def test__update_from_pb_success(self): from google.cloud.bigtable_admin_v2.proto import (instance_pb2 as data_v2_pb2) from google.cloud.bigtable import enums instance_type = enums.InstanceType.PRODUCTION instance_pb = data_v2_pb2.Instance(display_name=self.DISPLAY_NAME, type=instance_type, labels=self.LABELS) instance = self._make_one(None, None) self.assertIsNone(instance.display_name) self.assertIsNone(instance.type_) self.assertIsNone(instance.labels) instance._update_from_pb(instance_pb) self.assertEqual(instance.display_name, self.DISPLAY_NAME) self.assertEqual(instance.type_, instance_type) self.assertEqual(instance.labels, self.LABELS)
def test_partial_update_instance_exception(self): # Setup Response error = status_pb2.Status() operation = operations_pb2.Operation( name='operations/test_partial_update_instance_exception', done=True) operation.error.CopyFrom(error) # Mock the API response channel = ChannelStub(responses=[operation]) client = bigtable_admin_v2.BigtableInstanceAdminClient(channel=channel) # Setup Request instance = instance_pb2.Instance() update_mask = {} response = client.partial_update_instance(instance, update_mask) exception = response.exception() assert exception.errors[0] == error
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_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(project=self.PROJECT) instance_type = enums.InstanceType.PRODUCTION instance_pb = data_v2_pb2.Instance(name=self.INSTANCE_NAME, display_name=self.INSTANCE_ID, type=instance_type, labels=self.LABELS) klass = self._get_target_class() instance = klass.from_pb(instance_pb, client) self.assertIsInstance(instance, klass) self.assertEqual(instance._client, client) self.assertEqual(instance.instance_id, self.INSTANCE_ID) self.assertEqual(instance.display_name, self.INSTANCE_ID) self.assertEqual(instance.type_, instance_type) self.assertEqual(instance.labels, self.LABELS)
def test_get_instance(self): # Setup Expected Response name_2 = 'name2-1052831874' display_name = 'displayName1615086568' expected_response = {'name': name_2, 'display_name': display_name} expected_response = instance_pb2.Instance(**expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = bigtable_admin_v2.BigtableInstanceAdminClient(channel=channel) # Setup Request name = client.instance_path('[PROJECT]', '[INSTANCE]') response = client.get_instance(name) assert expected_response == response assert len(channel.requests) == 1 expected_request = bigtable_instance_admin_pb2.GetInstanceRequest( name=name) actual_request = channel.requests[0][1] assert expected_request == actual_request
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 api = (bigtable_instance_admin_client.BigtableInstanceAdminClient( mock.Mock())) credentials = _make_credentials() client = self._make_client(project=self.PROJECT, credentials=credentials, admin=True) # Create response_pb instance_name = client.instance_admin_client.instance_path( self.PROJECT, self.INSTANCE_ID) response_pb = data_v2_pb2.Instance(name=instance_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.transport instance_stub.get_instance.side_effect = [ response_pb, exceptions.NotFound('testing'), exceptions.BadRequest('testing') ] # Perform the method and check the result. non_existing_instance_id = 'instance-id-2' alt_instance_1 = self._make_one(self.INSTANCE_ID, client) alt_instance_2 = self._make_one(non_existing_instance_id, client) self.assertTrue(alt_instance_1.exists()) self.assertFalse(alt_instance_2.exists()) with self.assertRaises(exceptions.BadRequest): alt_instance_2.exists()