Skip to content

Commit 2499735

Browse files
authored
Merge pull request #2260 from tseaver/pubsub-logging-post-2223-system-tests
Fix logging/pubub system tests
2 parents e407bfb + 1390fbd commit 2499735

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

system_tests/logging_.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import google.cloud.logging.handlers.handlers
2020
from google.cloud.logging.handlers.handlers import CloudLoggingHandler
2121
from google.cloud.logging.handlers.transports import SyncTransport
22+
from google.cloud.logging import client
2223
from google.cloud import _helpers
2324
from google.cloud.environment_vars import TESTS_PROJECT
2425

@@ -27,13 +28,8 @@
2728
from system_test_utils import unique_resource_id
2829

2930
_RESOURCE_ID = unique_resource_id('-')
30-
DEFAULT_METRIC_NAME = 'system-tests-metric%s' % (_RESOURCE_ID,)
31-
DEFAULT_SINK_NAME = 'system-tests-sink%s' % (_RESOURCE_ID,)
3231
DEFAULT_FILTER = 'logName:syslog AND severity>=INFO'
3332
DEFAULT_DESCRIPTION = 'System testing'
34-
BUCKET_NAME = 'google-cloud-python-system-testing%s' % (_RESOURCE_ID,)
35-
DATASET_NAME = ('system_testing_dataset' + _RESOURCE_ID).replace('-', '_')
36-
TOPIC_NAME = 'google-cloud-python-system-testing%s' % (_RESOURCE_ID,)
3733

3834

3935
def _retry_on_unavailable(exc):
@@ -57,7 +53,7 @@ class Config(object):
5753

5854
def setUpModule():
5955
_helpers.PROJECT = TESTS_PROJECT
60-
Config.CLIENT = google.cloud.logging.Client()
56+
Config.CLIENT = client.Client()
6157

6258

6359
class TestLogging(unittest.TestCase):
@@ -232,16 +228,18 @@ def test_log_struct_w_metadata(self):
232228
self.assertEqual(request['status'], STATUS)
233229

234230
def test_create_metric(self):
231+
METRIC_NAME = 'test-create-metric%s' % (_RESOURCE_ID,)
235232
metric = Config.CLIENT.metric(
236-
DEFAULT_METRIC_NAME, DEFAULT_FILTER, DEFAULT_DESCRIPTION)
233+
METRIC_NAME, DEFAULT_FILTER, DEFAULT_DESCRIPTION)
237234
self.assertFalse(metric.exists())
238235
metric.create()
239236
self.to_delete.append(metric)
240237
self.assertTrue(metric.exists())
241238

242239
def test_list_metrics(self):
240+
METRIC_NAME = 'test-list-metrics%s' % (_RESOURCE_ID,)
243241
metric = Config.CLIENT.metric(
244-
DEFAULT_METRIC_NAME, DEFAULT_FILTER, DEFAULT_DESCRIPTION)
242+
METRIC_NAME, DEFAULT_FILTER, DEFAULT_DESCRIPTION)
245243
self.assertFalse(metric.exists())
246244
before_metrics, _ = Config.CLIENT.list_metrics()
247245
before_names = set(metric.name for metric in before_metrics)
@@ -251,13 +249,14 @@ def test_list_metrics(self):
251249
after_metrics, _ = Config.CLIENT.list_metrics()
252250
after_names = set(metric.name for metric in after_metrics)
253251
self.assertEqual(after_names - before_names,
254-
set([DEFAULT_METRIC_NAME]))
252+
set([METRIC_NAME]))
255253

256254
def test_reload_metric(self):
257255
from google.cloud.exceptions import Conflict
256+
METRIC_NAME = 'test-reload-metric%s' % (_RESOURCE_ID,)
258257
retry = RetryErrors(Conflict)
259258
metric = Config.CLIENT.metric(
260-
DEFAULT_METRIC_NAME, DEFAULT_FILTER, DEFAULT_DESCRIPTION)
259+
METRIC_NAME, DEFAULT_FILTER, DEFAULT_DESCRIPTION)
261260
self.assertFalse(metric.exists())
262261
retry(metric.create)()
263262
self.to_delete.append(metric)
@@ -269,11 +268,12 @@ def test_reload_metric(self):
269268

270269
def test_update_metric(self):
271270
from google.cloud.exceptions import Conflict
271+
METRIC_NAME = 'test-update-metric%s' % (_RESOURCE_ID,)
272272
retry = RetryErrors(Conflict)
273273
NEW_FILTER = 'logName:other'
274274
NEW_DESCRIPTION = 'updated'
275275
metric = Config.CLIENT.metric(
276-
DEFAULT_METRIC_NAME, DEFAULT_FILTER, DEFAULT_DESCRIPTION)
276+
METRIC_NAME, DEFAULT_FILTER, DEFAULT_DESCRIPTION)
277277
self.assertFalse(metric.exists())
278278
retry(metric.create)()
279279
self.to_delete.append(metric)
@@ -282,12 +282,13 @@ def test_update_metric(self):
282282
metric.update()
283283
after_metrics, _ = Config.CLIENT.list_metrics()
284284
after_info = dict((metric.name, metric) for metric in after_metrics)
285-
after = after_info[DEFAULT_METRIC_NAME]
285+
after = after_info[METRIC_NAME]
286286
self.assertEqual(after.filter_, NEW_FILTER)
287287
self.assertEqual(after.description, NEW_DESCRIPTION)
288288

289289
def _init_storage_bucket(self):
290290
from google.cloud import storage
291+
BUCKET_NAME = 'g-c-python-testing%s' % (_RESOURCE_ID,)
291292
BUCKET_URI = 'storage.googleapis.com/%s' % (BUCKET_NAME,)
292293

293294
# Create the destination bucket, and set up the ACL to allow
@@ -305,19 +306,22 @@ def _init_storage_bucket(self):
305306

306307
def test_create_sink_storage_bucket(self):
307308
uri = self._init_storage_bucket()
309+
SINK_NAME = 'test-create-sink-bucket%s' % (_RESOURCE_ID,)
308310

309-
sink = Config.CLIENT.sink(DEFAULT_SINK_NAME, DEFAULT_FILTER, uri)
311+
sink = Config.CLIENT.sink(SINK_NAME, DEFAULT_FILTER, uri)
310312
self.assertFalse(sink.exists())
311313
sink.create()
312314
self.to_delete.append(sink)
313315
self.assertTrue(sink.exists())
314316

315317
def test_create_sink_pubsub_topic(self):
316-
from google.cloud import pubsub
318+
from google.cloud.pubsub import client as pubsub_client
319+
SINK_NAME = 'test-create-sink-topic%s' % (_RESOURCE_ID,)
320+
TOPIC_NAME = 'logging-test-sink%s' % (_RESOURCE_ID,)
317321

318322
# Create the destination topic, and set up the IAM policy to allow
319323
# Stackdriver Logging to write into it.
320-
pubsub_client = pubsub.Client()
324+
pubsub_client = pubsub_client.Client()
321325
topic = pubsub_client.topic(TOPIC_NAME)
322326
topic.create()
323327
self.to_delete.append(topic)
@@ -327,8 +331,7 @@ def test_create_sink_pubsub_topic(self):
327331

328332
TOPIC_URI = 'pubsub.googleapis.com/%s' % (topic.full_name,)
329333

330-
sink = Config.CLIENT.sink(
331-
DEFAULT_SINK_NAME, DEFAULT_FILTER, TOPIC_URI)
334+
sink = Config.CLIENT.sink(SINK_NAME, DEFAULT_FILTER, TOPIC_URI)
332335
self.assertFalse(sink.exists())
333336
sink.create()
334337
self.to_delete.append(sink)
@@ -337,6 +340,8 @@ def test_create_sink_pubsub_topic(self):
337340
def _init_bigquery_dataset(self):
338341
from google.cloud import bigquery
339342
from google.cloud.bigquery.dataset import AccessGrant
343+
DATASET_NAME = (
344+
'system_testing_dataset' + _RESOURCE_ID).replace('-', '_')
340345
DATASET_URI = 'bigquery.googleapis.com/projects/%s/datasets/%s' % (
341346
Config.CLIENT.project, DATASET_NAME,)
342347

@@ -355,16 +360,18 @@ def _init_bigquery_dataset(self):
355360
return DATASET_URI
356361

357362
def test_create_sink_bigquery_dataset(self):
363+
SINK_NAME = 'test-create-sink-dataset%s' % (_RESOURCE_ID,)
358364
uri = self._init_bigquery_dataset()
359-
sink = Config.CLIENT.sink(DEFAULT_SINK_NAME, DEFAULT_FILTER, uri)
365+
sink = Config.CLIENT.sink(SINK_NAME, DEFAULT_FILTER, uri)
360366
self.assertFalse(sink.exists())
361367
sink.create()
362368
self.to_delete.append(sink)
363369
self.assertTrue(sink.exists())
364370

365371
def test_list_sinks(self):
372+
SINK_NAME = 'test-list-sinks%s' % (_RESOURCE_ID,)
366373
uri = self._init_storage_bucket()
367-
sink = Config.CLIENT.sink(DEFAULT_SINK_NAME, DEFAULT_FILTER, uri)
374+
sink = Config.CLIENT.sink(SINK_NAME, DEFAULT_FILTER, uri)
368375
self.assertFalse(sink.exists())
369376
before_sinks, _ = Config.CLIENT.list_sinks()
370377
before_names = set(sink.name for sink in before_sinks)
@@ -374,13 +381,14 @@ def test_list_sinks(self):
374381
after_sinks, _ = Config.CLIENT.list_sinks()
375382
after_names = set(sink.name for sink in after_sinks)
376383
self.assertEqual(after_names - before_names,
377-
set([DEFAULT_SINK_NAME]))
384+
set([SINK_NAME]))
378385

379386
def test_reload_sink(self):
380387
from google.cloud.exceptions import Conflict
388+
SINK_NAME = 'test-reload-sink%s' % (_RESOURCE_ID,)
381389
retry = RetryErrors(Conflict)
382390
uri = self._init_bigquery_dataset()
383-
sink = Config.CLIENT.sink(DEFAULT_SINK_NAME, DEFAULT_FILTER, uri)
391+
sink = Config.CLIENT.sink(SINK_NAME, DEFAULT_FILTER, uri)
384392
self.assertFalse(sink.exists())
385393
retry(sink.create)()
386394
self.to_delete.append(sink)
@@ -392,12 +400,12 @@ def test_reload_sink(self):
392400

393401
def test_update_sink(self):
394402
from google.cloud.exceptions import Conflict
403+
SINK_NAME = 'test-update-sink%s' % (_RESOURCE_ID,)
395404
retry = RetryErrors(Conflict)
396405
bucket_uri = self._init_storage_bucket()
397406
dataset_uri = self._init_bigquery_dataset()
398407
UPDATED_FILTER = 'logName:syslog'
399-
sink = Config.CLIENT.sink(
400-
DEFAULT_SINK_NAME, DEFAULT_FILTER, bucket_uri)
408+
sink = Config.CLIENT.sink(SINK_NAME, DEFAULT_FILTER, bucket_uri)
401409
self.assertFalse(sink.exists())
402410
retry(sink.create)()
403411
self.to_delete.append(sink)

system_tests/pubsub.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from google.cloud import _helpers
2525
from google.cloud.environment_vars import PUBSUB_EMULATOR
2626
from google.cloud.environment_vars import TESTS_PROJECT
27-
from google.cloud import pubsub
27+
from google.cloud.pubsub import client
2828
# pylint: enable=ungrouped-imports
2929

3030
from retry import RetryInstanceState
@@ -53,11 +53,11 @@ class Config(object):
5353
def setUpModule():
5454
_helpers.PROJECT = TESTS_PROJECT
5555
if os.getenv(PUBSUB_EMULATOR) is None:
56-
Config.CLIENT = pubsub.Client()
56+
Config.CLIENT = client.Client()
5757
else:
5858
credentials = EmulatorCreds()
5959
http = httplib2.Http() # Un-authorized.
60-
Config.CLIENT = pubsub.Client(credentials=credentials,
60+
Config.CLIENT = client.Client(credentials=credentials,
6161
http=http)
6262

6363

0 commit comments

Comments
 (0)