Skip to content

Commit e45e07f

Browse files
liyanhui1228lukesneeringer
authored andcommitted
Remove deepcopy of client._http (#3954)
1 parent 1658e0c commit e45e07f

File tree

3 files changed

+32
-49
lines changed

3 files changed

+32
-49
lines changed

packages/google-cloud-logging/google/cloud/logging/handlers/transports/background_thread.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from __future__ import print_function
2121

2222
import atexit
23-
import copy
2423
import logging
2524
import threading
2625

@@ -254,9 +253,7 @@ class BackgroundThreadTransport(Transport):
254253

255254
def __init__(self, client, name, grace_period=_DEFAULT_GRACE_PERIOD,
256255
batch_size=_DEFAULT_MAX_BATCH_SIZE):
257-
http = copy.deepcopy(client._http)
258-
self.client = client.__class__(
259-
client.project, client._credentials, http)
256+
self.client = client
260257
logger = self.client.logger(name)
261258
self.worker = _Worker(logger)
262259
self.worker.start()

packages/google-cloud-logging/tests/system.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -414,30 +414,32 @@ def test_create_sink_storage_bucket(self):
414414
self.assertTrue(sink.exists())
415415

416416
def test_create_sink_pubsub_topic(self):
417-
from google.cloud.iam import OWNER_ROLE
418-
from google.cloud.pubsub import client as pubsub_client
417+
import uuid
418+
419+
from google.cloud import pubsub_v1
419420

420421
SINK_NAME = 'test-create-sink-topic%s' % (_RESOURCE_ID,)
421-
TOPIC_NAME = 'logging-test-sink%s' % (_RESOURCE_ID,)
422+
TOPIC_NAME = '%s-%s' % ('systest', str(uuid.uuid4())[0:8])
422423

423424
# Create the destination topic, and set up the IAM policy to allow
424425
# Stackdriver Logging to write into it.
425-
pubsub_client = pubsub_client.Client()
426-
topic = pubsub_client.topic(TOPIC_NAME)
427-
topic.create()
428-
self.to_delete.append(topic)
429-
policy = topic.get_iam_policy()
430-
new_owners = set([policy.group('cloud-logs@google.com')])
431-
new_owners.update(policy.owners)
432-
policy[OWNER_ROLE] = new_owners
433-
topic.set_iam_policy(policy)
434-
435-
TOPIC_URI = 'pubsub.googleapis.com/%s' % (topic.full_name,)
426+
publisher = pubsub_v1.PublisherClient()
427+
topic_path = publisher.topic_path(Config.CLIENT.project, TOPIC_NAME)
428+
publisher.create_topic(topic_path)
429+
430+
policy = publisher.get_iam_policy(topic_path)
431+
policy.bindings.add(
432+
role='roles/owner',
433+
members=['group:cloud-logs@google.com']
434+
)
435+
publisher.set_iam_policy(topic_path, policy)
436+
437+
TOPIC_URI = 'pubsub.googleapis.com/%s' % (topic_path,)
436438

437439
sink = Config.CLIENT.sink(SINK_NAME, DEFAULT_FILTER, TOPIC_URI)
438440
self.assertFalse(sink.exists())
439441
sink.create()
440-
self.to_delete.append(sink)
442+
publisher.delete_topic(topic_path)
441443
self.assertTrue(sink.exists())
442444

443445
def _init_bigquery_dataset(self):

packages/google-cloud-logging/tests/unit/test_client.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -560,23 +560,18 @@ def test_list_metrics_with_paging(self):
560560
})
561561

562562
def test_get_default_handler_app_engine(self):
563-
import requests
564563
import os
565564
from google.cloud._testing import _Monkey
566565
from google.cloud.logging.client import _APPENGINE_FLEXIBLE_ENV_VM
567566
from google.cloud.logging.handlers import AppEngineHandler
568567

569-
http_mock = mock.Mock(spec=requests.Session)
570568
credentials = _make_credentials()
571-
deepcopy = mock.Mock(return_value=http_mock)
572569

573570
with _Monkey(os, environ={_APPENGINE_FLEXIBLE_ENV_VM: 'True'}):
574-
with mock.patch('copy.deepcopy', new=deepcopy):
575-
client = self._make_one(project=self.PROJECT,
576-
credentials=credentials,
577-
_use_grpc=False)
578-
handler = client.get_default_handler()
579-
deepcopy.assert_called_once_with(client._http)
571+
client = self._make_one(project=self.PROJECT,
572+
credentials=credentials,
573+
_use_grpc=False)
574+
handler = client.get_default_handler()
580575

581576
self.assertIsInstance(handler, AppEngineHandler)
582577

@@ -598,39 +593,28 @@ def test_get_default_handler_container_engine(self):
598593
self.assertIsInstance(handler, ContainerEngineHandler)
599594

600595
def test_get_default_handler_general(self):
601-
import requests
602596
from google.cloud.logging.handlers import CloudLoggingHandler
603597

604-
http_mock = mock.Mock(spec=requests.Session)
605598
credentials = _make_credentials()
606-
deepcopy = mock.Mock(return_value=http_mock)
607599

608-
with mock.patch('copy.deepcopy', new=deepcopy):
609-
client = self._make_one(project=self.PROJECT,
610-
credentials=credentials,
611-
_use_grpc=False)
612-
handler = client.get_default_handler()
613-
deepcopy.assert_called_once_with(client._http)
600+
client = self._make_one(project=self.PROJECT,
601+
credentials=credentials,
602+
_use_grpc=False)
603+
handler = client.get_default_handler()
614604

615605
self.assertIsInstance(handler, CloudLoggingHandler)
616606

617607
def test_setup_logging(self):
618-
import requests
619-
620-
http_mock = mock.Mock(spec=requests.Session)
621-
deepcopy = mock.Mock(return_value=http_mock)
622608
setup_logging = mock.Mock(spec=[])
623609

624610
credentials = _make_credentials()
625611

626-
with mock.patch('copy.deepcopy', new=deepcopy):
627-
with mock.patch('google.cloud.logging.client.setup_logging',
628-
new=setup_logging):
629-
client = self._make_one(project=self.PROJECT,
630-
credentials=credentials,
631-
_use_grpc=False)
632-
client.setup_logging()
633-
deepcopy.assert_called_once_with(client._http)
612+
with mock.patch('google.cloud.logging.client.setup_logging',
613+
new=setup_logging):
614+
client = self._make_one(project=self.PROJECT,
615+
credentials=credentials,
616+
_use_grpc=False)
617+
client.setup_logging()
634618

635619
setup_logging.assert_called()
636620

0 commit comments

Comments
 (0)