def test_get_value_for_attribute_with_exception(self, mock_meta_req): """Test get_value_for_attribute returns correctly. Setup: * Have _issue_http_request raise errors.MetadataServerHttpError Expected results: * A matching string. """ mock_meta_req.side_effect = _MockMetadataServerHttpError('Unreachable') actual_response = metadata_server.get_value_for_attribute('') self.assertIsNone(actual_response)
def test_get_value_for_attribute_with_a_present_attribute(self, mock_meta_req): """Test get_value_for_attribute returns correctly. Setup: * Mock out a httplib.HTTPResponse . * Return that from _issue_http_request. Expected results: * A matching string. """ mock_response = 'expected_response' with mock.patch('httplib.HTTPResponse', mock.mock_open(read_data=mock_response)) as mock_http_resp: mock_http_resp.return_value.status = httplib.OK mock_meta_req.side_effect = mock_http_resp actual_response = metadata_server.get_value_for_attribute('') self.assertEqual(actual_response, mock_response)