|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +import unittest2 |
| 18 | + |
| 19 | + |
| 20 | +class TestClient(unittest2.TestCase): |
| 21 | + |
| 22 | + def _getTargetClass(self): |
| 23 | + from gcloud.error_reporting.client import Client |
| 24 | + return Client |
| 25 | + |
| 26 | + def _makeOne(self, *args, **kw): |
| 27 | + return self._getTargetClass()(*args, **kw) |
| 28 | + |
| 29 | + PROJECT = 'PROJECT' |
| 30 | + SERVICE = 'SERVICE' |
| 31 | + VERSION = 'myversion' |
| 32 | + |
| 33 | + def test_ctor_default(self): |
| 34 | + CREDENTIALS = _Credentials() |
| 35 | + target = self._makeOne(project=self.PROJECT, |
| 36 | + credentials=CREDENTIALS) |
| 37 | + self.assertEquals(target._get_default_service(), |
| 38 | + target.DEFAULT_SERVICE) |
| 39 | + |
| 40 | + def test_ctor_params(self): |
| 41 | + CREDENTIALS = _Credentials() |
| 42 | + target = self._makeOne(project=self.PROJECT, |
| 43 | + credentials=CREDENTIALS, |
| 44 | + service=self.SERVICE, |
| 45 | + version=self.VERSION) |
| 46 | + self.assertEquals(target.service, self.SERVICE) |
| 47 | + self.assertEquals(target._get_default_service(), self.SERVICE) |
| 48 | + |
| 49 | + def test_report(self): |
| 50 | + CREDENTIALS = _Credentials() |
| 51 | + target = self._makeOne(project=self.PROJECT, |
| 52 | + credentials=CREDENTIALS) |
| 53 | + MESSAGE = 'hello world' |
| 54 | + |
| 55 | + logger = _Logger() |
| 56 | + target.logging_client.logger = lambda _: logger |
| 57 | + |
| 58 | + try: |
| 59 | + raise NameError |
| 60 | + except NameError: |
| 61 | + target.report(MESSAGE) |
| 62 | + |
| 63 | + payload = logger.log_struct_called_with |
| 64 | + self.assertEquals(payload['serviceContext'], { |
| 65 | + 'service': target.DEFAULT_SERVICE, |
| 66 | + }) |
| 67 | + self.assertIn(MESSAGE, payload['message']) |
| 68 | + self.assertIn('test_report', payload['message']) |
| 69 | + self.assertIn('test_client.py', payload['message']) |
| 70 | + |
| 71 | + def test_report_specify_service(self): |
| 72 | + CREDENTIALS = _Credentials() |
| 73 | + target = self._makeOne(project=self.PROJECT, |
| 74 | + credentials=CREDENTIALS) |
| 75 | + MESSAGE = 'hello world' |
| 76 | + SERVICE = "notdefault" |
| 77 | + VERSION = "notdefaultversion" |
| 78 | + |
| 79 | + logger = _Logger() |
| 80 | + target.logging_client.logger = lambda _: logger |
| 81 | + |
| 82 | + try: |
| 83 | + raise NameError |
| 84 | + except NameError: |
| 85 | + target.report(MESSAGE, service=SERVICE, version=VERSION) |
| 86 | + |
| 87 | + payload = logger.log_struct_called_with |
| 88 | + self.assertEquals(payload['serviceContext'], { |
| 89 | + 'service': SERVICE, |
| 90 | + 'version': VERSION |
| 91 | + }) |
| 92 | + self.assertIn(MESSAGE, payload['message']) |
| 93 | + self.assertIn('test_report_specify_service', payload['message']) |
| 94 | + self.assertIn('test_client.py', payload['message']) |
| 95 | + |
| 96 | + def test_report_with_version_in_constructor(self): |
| 97 | + CREDENTIALS = _Credentials() |
| 98 | + VERSION = "notdefaultversion" |
| 99 | + target = self._makeOne(project=self.PROJECT, |
| 100 | + credentials=CREDENTIALS, |
| 101 | + version=VERSION) |
| 102 | + MESSAGE = 'hello world' |
| 103 | + SERVICE = "notdefault" |
| 104 | + |
| 105 | + logger = _Logger() |
| 106 | + target.logging_client.logger = lambda _: logger |
| 107 | + |
| 108 | + try: |
| 109 | + raise NameError |
| 110 | + except NameError: |
| 111 | + target.report(MESSAGE, service=SERVICE) |
| 112 | + |
| 113 | + payload = logger.log_struct_called_with |
| 114 | + self.assertEquals(payload['serviceContext'], { |
| 115 | + 'service': SERVICE, |
| 116 | + 'version': VERSION |
| 117 | + }) |
| 118 | + self.assertIn(MESSAGE, payload['message']) |
| 119 | + self.assertIn( |
| 120 | + 'test_report_with_version_in_constructor', payload['message']) |
| 121 | + self.assertIn('test_client.py', payload['message']) |
| 122 | + |
| 123 | + |
| 124 | +class _Credentials(object): |
| 125 | + |
| 126 | + _scopes = None |
| 127 | + |
| 128 | + @staticmethod |
| 129 | + def create_scoped_required(): |
| 130 | + return True |
| 131 | + |
| 132 | + def create_scoped(self, scope): |
| 133 | + self._scopes = scope |
| 134 | + return self |
| 135 | + |
| 136 | + |
| 137 | +class _Logger(object): |
| 138 | + |
| 139 | + def log_struct(self, payload, # pylint: disable=unused-argument |
| 140 | + client=None, # pylint: disable=unused-argument |
| 141 | + labels=None, # pylint: disable=unused-argument |
| 142 | + insert_id=None, # pylint: disable=unused-argument |
| 143 | + severity=None, # pylint: disable=unused-argument |
| 144 | + http_request=None): # pylint: disable=unused-argument |
| 145 | + self.log_struct_called_with = payload |
0 commit comments