def test_urlopen_requests_on_urlerror(self): def raise_urlerror(req, timeout): raise URLError('URL Error message') with mock.patch('opencensus.common.http_handler.urlopen') as urlopen_mock: urlopen_mock.side_effect = raise_urlerror self.assertIsNone(get_request(self.TEST_URL))
def test_urlopen_requests_on_sockettimeout(self): def raise_sockettimeout(req, timeout): raise socket.timeout('URL Timeout message') with mock.patch('opencensus.common.http_handler.urlopen') as urlopen_mock: urlopen_mock.side_effect = raise_sockettimeout self.assertIsNone(get_request(self.TEST_URL))
def test_urlopen_requests_on_httperror(self): def raise_httperror(req, timeout): raise HTTPError( url='http://127.0.0.1', code=400, msg='Bad request', hdrs=None, fp=None) with mock.patch('opencensus.common.http_handler.urlopen') as urlopen_mock: urlopen_mock.side_effect = raise_httperror self.assertIsNone(get_request(self.TEST_URL))
def _get_attribute(attribute_key): """ Fetch the requested instance metadata entry. :param attribute_uri: attribute_uri: attribute name relative to the computeMetadata/v1 prefix :return: The value read from the metadata service or None """ attribute_value = get_request( _GCP_METADATA_URI + _GKE_ATTRIBUTES[attribute_key], _GCP_METADATA_URI_HEADER) return attribute_value
def get_attribute(attribute_uri): """ Fetch the requested instance metadata entry. :param attribute_uri: attribute_uri: attribute name relative to the computeMetadata/v1 prefix :return: The value read from the metadata service or None """ attribute_value = get_request(_GCP_METADATA_URI + attribute_uri, _GCP_METADATA_URI_HEADER) if attribute_value is not None and isinstance(attribute_value, bytes): # At least in python3, bytes are are returned from # urllib (although the response is text), convert # to a normal string: attribute_value = attribute_value.decode('utf-8') return attribute_value
def test_urlopen(self, urlopen_mock, request_mock): mocked_response = { 'availabilityZone': 'us-west-2b', 'instanceId': 'i-1234567890abcdef0', 'imageId': 'ami-5fb8c835', 'privateIp': '10.158.112.84', 'pendingTime': '2016-11-19T16:32:11Z', 'accountId': '12345678901', 'region': 'us-west-2', 'marketplaceProductCodes': ["1abc2defghijklm3nopqrs4tu"], 'instanceType': 't2.micro', 'version': '2017-09-30', 'architecture': 'x86_64', } urlopen_mock.return_value = StringIO(json.dumps(mocked_response)) data = get_request(self.TEST_URL) self.assertIn('availabilityZone', data) self.assertEqual(urlopen_mock.call_count, 1)
def _initialize_aws_identity_document(cls): """This method, tries to establish an HTTP connection to AWS instance identity document url. If the application is running on an EC2 instance, we should be able to get back a valid JSON document. Make a http get request call and store data in local map. This method should only be called once. """ if cls.inited: return content = get_request(_AWS_INSTANCE_IDENTITY_DOCUMENT_URI) if content is not None: content = json.loads(content) for env_var, attribute_key in _AWS_ATTRIBUTES.items(): attribute_value = content.get(env_var) if attribute_value is not None: aws_metadata_map[attribute_key] = attribute_value cls.is_running = True cls.inited = True