def test___call__(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable.client import Client
        from gcloud_bigtable.client import DATA_SCOPE
        from gcloud_bigtable.client import DEFAULT_USER_AGENT

        access_token_expected = 'FOOBARBAZ'

        class _ReturnVal(object):
            access_token = access_token_expected

        scoped_creds = _MockWithAttachedMethods(_ReturnVal)
        credentials = _MockWithAttachedMethods(scoped_creds)
        project_id = 'PROJECT_ID'
        client = Client(credentials, project_id=project_id)

        transformer = self._makeOne(client)
        result = transformer(None)
        self.assertEqual(
            result,
            [
                ('Authorization', 'Bearer ' + access_token_expected),
                ('User-agent', DEFAULT_USER_AGENT),
            ])
        self.assertEqual(credentials._called, [
            ('create_scoped', ([DATA_SCOPE],), {}),
        ])
        self.assertEqual(scoped_creds._called, [('get_access_token', (), {})])
    def test_from_service_account_p12(self):
        from gcloud_bigtable._testing import _MockCalled
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable._testing import _Monkey
        from gcloud_bigtable import client as MUT

        klass = self._getTargetClass()
        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        signed_creds = _MockCalled(credentials)

        private_key = 'PRIVATE_KEY'
        mock_get_contents = _MockCalled(private_key)
        client_email = 'CLIENT_EMAIL'
        private_key_path = 'PRIVATE_KEY_PATH'

        with _Monkey(MUT, SignedJwtAssertionCredentials=signed_creds,
                     _get_contents=mock_get_contents):
            client = klass.from_service_account_p12(
                client_email, private_key_path, project_id=PROJECT_ID)

        self.assertEqual(client.project_id, PROJECT_ID)
        self.assertTrue(client._credentials is scoped_creds)
        expected_scopes = [MUT.DATA_SCOPE]
        self.assertEqual(credentials._called, [
            ('create_scoped', (expected_scopes,), {}),
        ])
        # SignedJwtAssertionCredentials() called with only kwargs
        signed_creds_kw = {
            'private_key': private_key,
            'service_account_name': client_email,
        }
        signed_creds.check_called(self, [()], [signed_creds_kw])
        # Load private key (via _get_contents) from the key path.
        mock_get_contents.check_called(self, [(private_key_path,)])
    def test_from_service_account_json(self):
        from gcloud_bigtable._testing import _MockCalled
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable._testing import _Monkey
        from gcloud_bigtable import client as MUT

        klass = self._getTargetClass()
        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        get_adc = _MockCalled(credentials)
        json_credentials_path = 'JSON_CREDENTIALS_PATH'

        with _Monkey(MUT,
                     _get_application_default_credential_from_file=get_adc):
            client = klass.from_service_account_json(
                json_credentials_path, project_id=PROJECT_ID)

        self.assertEqual(client.project_id, PROJECT_ID)
        self.assertTrue(client._credentials is scoped_creds)

        expected_scopes = [MUT.DATA_SCOPE]
        self.assertEqual(credentials._called, [
            ('create_scoped', (expected_scopes,), {}),
        ])
        # _get_application_default_credential_from_file only has pos. args.
        get_adc.check_called(self, [(json_credentials_path,)])
 def test_table_stub_getter(self):
     from gcloud_bigtable._testing import _MockWithAttachedMethods
     scoped_creds = object()
     credentials = _MockWithAttachedMethods(scoped_creds)
     client = self._makeOne(credentials, project_id=PROJECT_ID, admin=True)
     client._table_stub = object()
     self.assertTrue(client.table_stub is client._table_stub)
 def test_table_stub_unset_failure(self):
     from gcloud_bigtable._testing import _MockWithAttachedMethods
     scoped_creds = object()
     credentials = _MockWithAttachedMethods(scoped_creds)
     client = self._makeOne(credentials, project_id=PROJECT_ID, admin=True)
     with self.assertRaises(ValueError):
         getattr(client, 'table_stub')
    def _constructor_test_helper(self, expected_scopes, project_id=None,
                                 read_only=False, admin=False,
                                 user_agent=None):
        from gcloud_bigtable._testing import _MockCalled
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable._testing import _Monkey
        from gcloud_bigtable import client as MUT

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        determined_project_id = object()
        mock_determine_project_id = _MockCalled(determined_project_id)
        with _Monkey(MUT, _determine_project_id=mock_determine_project_id):
            client = self._makeOne(credentials, project_id=project_id,
                                   read_only=read_only, admin=admin,
                                   user_agent=user_agent)

        self.assertTrue(client._credentials is scoped_creds)
        self.assertEqual(credentials._called, [
            ('create_scoped', (expected_scopes,), {}),
        ])
        self.assertTrue(client._project_id is determined_project_id)
        self.assertEqual(client.timeout_seconds, MUT.DEFAULT_TIMEOUT_SECONDS)
        self.assertEqual(client.user_agent, user_agent)
        mock_determine_project_id.check_called(self, [(project_id,)])
    def test_it(self):
        from gcloud_bigtable._testing import _MockCalled
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable._testing import _Monkey
        from gcloud_bigtable import _helpers as MUT

        mock_result = object()
        custom_factory = _MockCalled(mock_result)
        transformed = object()
        transformer = _MockCalled(transformed)

        host = 'HOST'
        port = 1025
        certs = 'FOOBAR'
        client = _MockWithAttachedMethods()
        with _Monkey(MUT, get_certs=lambda: certs,
                     MetadataTransformer=transformer):
            result = self._callFUT(client, custom_factory, host, port)

        self.assertTrue(result is mock_result)
        custom_factory.check_called(
            self,
            [(host, port)],
            [{
                'metadata_transformer': transformed,
                'secure': True,
                'root_certificates': certs,
            }],
        )
        transformer.check_called(self, [(client,)])
        self.assertEqual(client._called, [])
    def test__make_table_stub(self):
        from gcloud_bigtable._testing import _MockCalled
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable._testing import _Monkey
        from gcloud_bigtable import client as MUT
        from gcloud_bigtable.client import TABLE_ADMIN_HOST
        from gcloud_bigtable.client import TABLE_ADMIN_PORT
        from gcloud_bigtable.client import TABLE_STUB_FACTORY

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = self._makeOne(credentials, project_id=PROJECT_ID)
        expected_result = object()
        mock_make_stub = _MockCalled(expected_result)
        with _Monkey(MUT, make_stub=mock_make_stub):
            result = client._make_table_stub()

        self.assertTrue(result is expected_result)
        make_stub_args = [
            (
                client,
                TABLE_STUB_FACTORY,
                TABLE_ADMIN_HOST,
                TABLE_ADMIN_PORT,
            ),
        ]
        mock_make_stub.check_called(self, make_stub_args)
    def _start_method_helper(self, admin):
        from gcloud_bigtable._testing import _MockCalled
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable._testing import _Monkey
        from gcloud_bigtable import client as MUT

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = self._makeOne(credentials, project_id=PROJECT_ID, admin=admin)
        stub = _FakeStub()
        mock_make_stub = _MockCalled(stub)
        with _Monkey(MUT, make_stub=mock_make_stub):
            client.start()

        self.assertTrue(client._data_stub is stub)
        if admin:
            self.assertTrue(client._cluster_stub is stub)
            self.assertTrue(client._operations_stub is stub)
            self.assertTrue(client._table_stub is stub)
            self.assertEqual(stub._entered, 4)
        else:
            self.assertTrue(client._cluster_stub is None)
            self.assertTrue(client._operations_stub is None)
            self.assertTrue(client._table_stub is None)
            self.assertEqual(stub._entered, 1)
        self.assertEqual(stub._exited, [])
    def _stop_method_helper(self, admin):
        from gcloud_bigtable._testing import _MockWithAttachedMethods

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = self._makeOne(credentials, project_id=PROJECT_ID, admin=admin)
        stub1 = _FakeStub()
        stub2 = _FakeStub()
        client._data_stub = stub1
        client._cluster_stub = stub2
        client._operations_stub = stub2
        client._table_stub = stub2
        client.stop()
        self.assertTrue(client._data_stub is None)
        self.assertTrue(client._cluster_stub is None)
        self.assertTrue(client._operations_stub is None)
        self.assertTrue(client._table_stub is None)
        self.assertEqual(stub1._entered, 0)
        self.assertEqual(stub2._entered, 0)
        exc_none_triple = (None, None, None)
        self.assertEqual(stub1._exited, [exc_none_triple])
        if admin:
            self.assertEqual(stub2._exited, [exc_none_triple] * 3)
        else:
            self.assertEqual(stub2._exited, [])
    def test_project_id_getter(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = self._makeOne(credentials, project_id=PROJECT_ID)
        self.assertEqual(client.project_id, PROJECT_ID)
    def test_copy(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods

        class Credentials(object):

            def __init__(self, value):
                self.value = value

            def __eq__(self, other):
                return self.value == other.value

        scoped_creds = Credentials('value')
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = self._makeOne(credentials, project_id=PROJECT_ID)
        # Put some fake stubs in place so that we can verify they
        # don't get copied.
        client._data_stub = object()
        client._cluster_stub = object()
        client._operations_stub = object()
        client._table_stub = object()

        new_client = client.copy()
        self.assertEqual(new_client._admin, client._admin)
        self.assertEqual(new_client._credentials, client._credentials)
        # Make sure credentials (a non-simple type) gets copied
        # to a new instance.
        self.assertFalse(new_client._credentials is client._credentials)
        self.assertEqual(new_client._project_id, client._project_id)
        self.assertEqual(new_client.user_agent, client.user_agent)
        self.assertEqual(new_client.timeout_seconds, client.timeout_seconds)
        # Make sure stubs are not preserved.
        self.assertEqual(new_client._data_stub, None)
        self.assertEqual(new_client._cluster_stub, None)
        self.assertEqual(new_client._operations_stub, None)
        self.assertEqual(new_client._table_stub, None)
    def _helper(self, status, raise_socket_err=False):
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable._testing import _Monkey
        from gcloud_bigtable import client as MUT

        fake_project_id = object()
        response = self._make_http_connection_response(
            status, fake_project_id, raise_socket_err=raise_socket_err)
        # The connection does the bulk of the work.
        mock_connection = _MockWithAttachedMethods(None, response, None)
        # The http_client module holds the connection constructor.
        mock_http_client = _MockWithAttachedMethods(mock_connection)
        # We need to put the client in place of it's location in six.
        mock_six = self._make_fake_six_module(mock_http_client)

        with _Monkey(MUT, six=mock_six):
            result = self._callFUT()

        if status == 200 and not raise_socket_err:
            self.assertEqual(result, fake_project_id)
        else:
            self.assertEqual(result, None)

        self.assertEqual(mock_connection._called, [
            (
                'request',
                ('GET', '/computeMetadata/v1/project/project-id'),
                {'headers': {'Metadata-Flavor': 'Google'}},
            ),
            (
                'getresponse',
                (),
                {},
            ),
            (
                'close',
                (),
                {},
            ),
        ])
        self.assertEqual(mock_http_client._called, [
            (
                'HTTPConnection',
                ('169.254.169.254',),
                {'timeout': 0.1},
            ),
        ])
    def test_project_name_property(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        project_name = 'projects/' + PROJECT_ID
        client = self._makeOne(credentials, project_id=PROJECT_ID)
        self.assertEqual(client.project_name, project_name)
    def test_project_id_getter(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable.client import Client

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = Client(credentials, project_id=PROJECT_ID)
        cluster = self._makeOne(ZONE, CLUSTER_ID, client)
        self.assertEqual(cluster.project_id, PROJECT_ID)
    def test_name_property(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable.client import Client

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = Client(credentials, project_id=PROJECT_ID)
        cluster = self._makeOne(ZONE, CLUSTER_ID, client)
        cluster_name = "projects/" + PROJECT_ID + "/zones/" + ZONE + "/clusters/" + CLUSTER_ID
        self.assertEqual(cluster.name, cluster_name)
    def test_is_started(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = self._makeOne(credentials, project_id=PROJECT_ID)
        self.assertFalse(client.is_started())
        client._data_stub = object()
        self.assertTrue(client.is_started())
        client._data_stub = None
        self.assertFalse(client.is_started())
    def test_start_while_started(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = self._makeOne(credentials, project_id=PROJECT_ID)
        client._data_stub = data_stub = object()
        self.assertTrue(client.is_started())
        client.start()

        # Make sure the stub did not change.
        self.assertEqual(client._data_stub, data_stub)
    def test_constructor_default(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable._testing import _Monkey
        from gcloud_bigtable import client as MUT

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        mock_creds_class = _MockWithAttachedMethods(credentials)

        with _Monkey(MUT, GoogleCredentials=mock_creds_class):
            client = self._makeOne(project_id=PROJECT_ID)

        self.assertEqual(client.project_id, PROJECT_ID)
        self.assertTrue(client._credentials is scoped_creds)
        self.assertEqual(client.user_agent, MUT.DEFAULT_USER_AGENT)
        self.assertEqual(mock_creds_class._called,
                         [('get_application_default', (), {})])
        expected_scopes = [MUT.DATA_SCOPE]
        self.assertEqual(credentials._called, [
            ('create_scoped', (expected_scopes,), {}),
        ])
    def test_with_app_engine(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable._testing import _Monkey
        from gcloud_bigtable import client as MUT

        fake_project_id = object()
        mock_app_identity = _MockWithAttachedMethods(fake_project_id)
        with _Monkey(MUT, app_identity=mock_app_identity):
            result = self._callFUT()

        self.assertTrue(result is fake_project_id)
        self.assertEqual(mock_app_identity._called,
                         [('get_application_id', (), {})])
    def test_it(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable._testing import _Monkey
        from gcloud_bigtable import client as MUT

        fake_project_id = object()
        mock_os = _MockWithAttachedMethods(fake_project_id)
        with _Monkey(MUT, os=mock_os):
            result = self._callFUT()

        self.assertTrue(result is fake_project_id)
        self.assertEqual(mock_os._called,
                         [('getenv', (MUT.PROJECT_ENV_VAR,), {})])
    def test_cluster_factory(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable.cluster import Cluster

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = self._makeOne(credentials, project_id=PROJECT_ID)

        zone = 'zone'
        cluster_id = 'cluster-id'
        cluster = client.cluster(zone, cluster_id)
        self.assertTrue(isinstance(cluster, Cluster))
        self.assertTrue(cluster.client is client)
        self.assertEqual(cluster.zone, zone)
        self.assertEqual(cluster.cluster_id, cluster_id)
    def test_stop_while_stopped(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = self._makeOne(credentials, project_id=PROJECT_ID)
        self.assertFalse(client.is_started())

        # This is a bit hacky. We set the cluster stub protected value
        # since it isn't used in is_started() and make sure that stop
        # doesn't reset this value to None.
        client._cluster_stub = cluster_stub = object()
        client.stop()
        # Make sure the cluster stub did not change.
        self.assertEqual(client._cluster_stub, cluster_stub)
    def test_constructor(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable.client import Client
        from gcloud_bigtable.client import DATA_SCOPE

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        project_id = 'PROJECT_ID'
        user_agent = 'USER_AGENT'
        client = Client(credentials, project_id=project_id,
                        user_agent=user_agent)
        transformer = self._makeOne(client)
        self.assertTrue(transformer._credentials is scoped_creds)
        self.assertEqual(transformer._user_agent, user_agent)
        self.assertEqual(credentials._called, [
            ('create_scoped', ([DATA_SCOPE],), {}),
        ])
    def test_from_pb_success(self):
        from gcloud_bigtable._generated import bigtable_cluster_data_pb2 as data_pb2
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable.client import Client

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = Client(credentials, project_id=PROJECT_ID)

        cluster_name = "projects/" + PROJECT_ID + "/zones/" + ZONE + "/clusters/" + CLUSTER_ID
        cluster_pb = data_pb2.Cluster(name=cluster_name, display_name=CLUSTER_ID, serve_nodes=3)

        klass = self._getTargetClass()
        cluster = klass.from_pb(cluster_pb, client)
        self.assertTrue(isinstance(cluster, klass))
        self.assertEqual(cluster.client, client)
        self.assertEqual(cluster.zone, ZONE)
        self.assertEqual(cluster.cluster_id, CLUSTER_ID)
    def test_from_pb_project_id_mistmatch(self):
        from gcloud_bigtable._generated import bigtable_cluster_data_pb2 as data_pb2
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable.client import Client

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        alt_project_id = "ALT_PROJECT_ID"
        client = Client(credentials, project_id=alt_project_id)

        self.assertNotEqual(PROJECT_ID, alt_project_id)

        cluster_name = "projects/" + PROJECT_ID + "/zones/" + ZONE + "/clusters/" + CLUSTER_ID
        cluster_pb = data_pb2.Cluster(name=cluster_name)

        klass = self._getTargetClass()
        with self.assertRaises(ValueError):
            klass.from_pb(cluster_pb, client)
    def _list_zones_helper(self, zone_status):
        from gcloud_bigtable._generated import (
            bigtable_cluster_data_pb2 as data_pb2)
        from gcloud_bigtable._generated import (
            bigtable_cluster_service_messages_pb2 as messages_pb2)
        from gcloud_bigtable._grpc_mocks import StubMock
        from gcloud_bigtable._testing import _MockWithAttachedMethods

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = self._makeOne(credentials, project_id=PROJECT_ID, admin=True)

        # Create request_pb
        request_pb = messages_pb2.ListZonesRequest(
            name='projects/' + PROJECT_ID,
        )

        # Create response_pb
        zone1 = 'foo'
        zone2 = 'bar'
        response_pb = messages_pb2.ListZonesResponse(
            zones=[
                data_pb2.Zone(display_name=zone1, status=zone_status),
                data_pb2.Zone(display_name=zone2, status=zone_status),
            ],
        )

        # Patch the stub used by the API method.
        client._cluster_stub = stub = StubMock(response_pb)

        # Create expected_result.
        expected_result = [zone1, zone2]

        # Perform the method and check the result.
        timeout_seconds = 281330
        result = client.list_zones(timeout_seconds=timeout_seconds)
        self.assertEqual(result, expected_result)
        self.assertEqual(stub.method_calls, [(
            'ListZones',
            (request_pb, timeout_seconds),
            {},
        )])
    def test_copy_partial_failure(self):
        from gcloud_bigtable._testing import _MockWithAttachedMethods
        from gcloud_bigtable._testing import _Monkey
        from gcloud_bigtable import client as MUT

        captured_stubs = {}

        def always_fail(client_to_copy):
            captured_stubs['data_stub'] = client_to_copy._data_stub
            captured_stubs['cluster_stub'] = client_to_copy._cluster_stub
            captured_stubs['operations_stub'] = client_to_copy._operations_stub
            captured_stubs['table_stub'] = client_to_copy._table_stub
            raise ValueError('cannot copy', client_to_copy)

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = self._makeOne(credentials, project_id=PROJECT_ID)

        client._data_stub = data_stub = object()
        client._cluster_stub = cluster_stub = object()
        client._operations_stub = operations_stub = object()
        client._table_stub = table_stub = object()
        with _Monkey(MUT.copy, deepcopy=always_fail):
            with self.assertRaises(ValueError):
                client.copy()

        # Make sure none of the stubs were present in the deepcopy().
        self.assertEqual(captured_stubs, {
            'data_stub': None,
            'cluster_stub': None,
            'operations_stub': None,
            'table_stub': None,
        })
        # Make sure **all** the stubs were restored after the failure.
        self.assertEqual(client._data_stub, data_stub)
        self.assertEqual(client._cluster_stub, cluster_stub)
        self.assertEqual(client._operations_stub, operations_stub)
        self.assertEqual(client._table_stub, table_stub)
    def test_list_clusters(self):
        from gcloud_bigtable._generated import (
            bigtable_cluster_data_pb2 as data_pb2)
        from gcloud_bigtable._generated import (
            bigtable_cluster_service_messages_pb2 as messages_pb2)
        from gcloud_bigtable._grpc_mocks import StubMock
        from gcloud_bigtable._testing import _MockWithAttachedMethods

        scoped_creds = object()
        credentials = _MockWithAttachedMethods(scoped_creds)
        client = self._makeOne(credentials, project_id=PROJECT_ID, admin=True)

        # Create request_pb
        request_pb = messages_pb2.ListClustersRequest(
            name='projects/' + PROJECT_ID,
        )

        # Create response_pb
        zone = 'foo'
        failed_zone = 'bar'
        cluster_id1 = 'cluster-id1'
        cluster_id2 = 'cluster-id2'
        cluster_name1 = ('projects/' + PROJECT_ID + '/zones/' + zone +
                         '/clusters/' + cluster_id1)
        cluster_name2 = ('projects/' + PROJECT_ID + '/zones/' + zone +
                         '/clusters/' + cluster_id2)
        response_pb = messages_pb2.ListClustersResponse(
            failed_zones=[
                data_pb2.Zone(display_name=failed_zone),
            ],
            clusters=[
                data_pb2.Cluster(
                    name=cluster_name1,
                    display_name=cluster_name1,
                    serve_nodes=3,
                ),
                data_pb2.Cluster(
                    name=cluster_name2,
                    display_name=cluster_name2,
                    serve_nodes=3,
                ),
            ],
        )

        # Patch the stub used by the API method.
        client._cluster_stub = stub = StubMock(response_pb)

        # Create expected_result.
        failed_zones = [failed_zone]
        clusters = [
            client.cluster(zone, cluster_id1),
            client.cluster(zone, cluster_id2),
        ]
        expected_result = (clusters, failed_zones)

        # Perform the method and check the result.
        timeout_seconds = 8004
        result = client.list_clusters(timeout_seconds=timeout_seconds)
        self.assertEqual(result, expected_result)
        self.assertEqual(stub.method_calls, [(
            'ListClusters',
            (request_pb, timeout_seconds),
            {},
        )])