def test_replica_set(self): self.CONF.set_override('connection', str(tests_db.MongoDBFakeConnectionUrl()) + '?replicaSet=foobar', group='database') conn = impl_mongodb.Connection(self.CONF) self.assertTrue(conn.conn)
class TestPecanApp(FunctionalTest): database_connection = tests_db.MongoDBFakeConnectionUrl() def test_pecan_extension_guessing_unset(self): # check Pecan does not assume .jpg is an extension response = self.app.get(self.PATH_PREFIX + '/meters/meter.jpg') self.assertEqual(response.content_type, 'application/json')
class TestApiMiddleware(FunctionalTest): # This doesn't really matter database_connection = tests_db.MongoDBFakeConnectionUrl() def test_json_parsable_error_middleware_404(self): response = self.get_json('/invalid_path', expect_errors=True, headers={"Accept": "application/json"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json( '/invalid_path', expect_errors=True, headers={"Accept": "application/json,application/xml"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json('/invalid_path', expect_errors=True, headers={ "Accept": "application/xml;q=0.8, \ application/json" }) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json('/invalid_path', expect_errors=True) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json('/invalid_path', expect_errors=True, headers={"Accept": "text/html,*/*"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) def test_xml_parsable_error_middleware_404(self): response = self.get_json('/invalid_path', expect_errors=True, headers={"Accept": "application/xml,*/*"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') response = self.get_json('/invalid_path', expect_errors=True, headers={ "Accept": "application/json;q=0.8 \ ,application/xml" }) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message')
class TestApiMiddleware(FunctionalTest): # This doesn't really matter database_connection = tests_db.MongoDBFakeConnectionUrl() no_lang_translated_error = 'No lang translated error' en_US_translated_error = 'en-US translated error' def _fake_translate(self, message, user_locale): if user_locale is None: return self.no_lang_translated_error else: return self.en_US_translated_error def test_json_parsable_error_middleware_404(self): response = self.get_json('/invalid_path', expect_errors=True, headers={"Accept": "application/json"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json( '/invalid_path', expect_errors=True, headers={"Accept": "application/json,application/xml"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json('/invalid_path', expect_errors=True, headers={ "Accept": "application/xml;q=0.8, \ application/json" }) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json('/invalid_path', expect_errors=True) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json('/invalid_path', expect_errors=True, headers={"Accept": "text/html,*/*"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) def test_json_parsable_error_middleware_translation_400(self): # Ensure translated messages get placed properly into json faults with mock.patch.object(gettextutils, 'translate', side_effect=self._fake_translate): response = self.post_json('/alarms', params={ 'name': 'foobar', 'type': 'threshold' }, expect_errors=True, headers={"Accept": "application/json"}) self.assertEqual(response.status_int, 400) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) self.assertEqual(response.json['error_message']['faultstring'], self.no_lang_translated_error) def test_xml_parsable_error_middleware_404(self): response = self.get_json('/invalid_path', expect_errors=True, headers={"Accept": "application/xml,*/*"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') response = self.get_json('/invalid_path', expect_errors=True, headers={ "Accept": "application/json;q=0.8 \ ,application/xml" }) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') def test_xml_parsable_error_middleware_translation_400(self): # Ensure translated messages get placed properly into xml faults with mock.patch.object(gettextutils, 'translate', side_effect=self._fake_translate): response = self.post_json( '/alarms', params={ 'name': 'foobar', 'type': 'threshold' }, expect_errors=True, headers={"Accept": "application/xml,*/*"}) self.assertEqual(response.status_int, 400) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') fault = response.xml.findall('./error/faultstring') for fault_string in fault: self.assertEqual(fault_string.text, self.no_lang_translated_error) def test_best_match_language(self): # Ensure that we are actually invoking language negotiation with mock.patch.object(gettextutils, 'translate', side_effect=self._fake_translate): response = self.post_json('/alarms', params={ 'name': 'foobar', 'type': 'threshold' }, expect_errors=True, headers={ "Accept": "application/xml,*/*", "Accept-Language": "en-US" }) self.assertEqual(response.status_int, 400) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') fault = response.xml.findall('./error/faultstring') for fault_string in fault: self.assertEqual(fault_string.text, self.en_US_translated_error) def test_translated_then_untranslated_error(self): resp = self.get_json('/alarms/alarm-id-3', expect_errors=True) self.assertEqual(resp.status_code, 404) self.assertEqual( json.loads(resp.body)['error_message']['faultstring'], "Alarm alarm-id-3 Not Found") with mock.patch('ceilometer.api.controllers.v2.EntityNotFound') \ as CustomErrorClass: CustomErrorClass.return_value = wsme.exc.ClientSideError( "untranslated_error", status_code=404) resp = self.get_json('/alarms/alarm-id-5', expect_errors=True) self.assertEqual(resp.status_code, 404) self.assertEqual( json.loads(resp.body)['error_message']['faultstring'], "untranslated_error")
class TestApiMiddleware(FunctionalTest): # This doesn't really matter database_connection = tests_db.MongoDBFakeConnectionUrl() translated_error = 'Translated error' def _fake_get_localized_message(self, message, user_locale): return self.translated_error def test_json_parsable_error_middleware_404(self): response = self.get_json('/invalid_path', expect_errors=True, headers={"Accept": "application/json"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json( '/invalid_path', expect_errors=True, headers={"Accept": "application/json,application/xml"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json('/invalid_path', expect_errors=True, headers={ "Accept": "application/xml;q=0.8, \ application/json" }) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json('/invalid_path', expect_errors=True) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json('/invalid_path', expect_errors=True, headers={"Accept": "text/html,*/*"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) def test_json_parsable_error_middleware_translation_400(self): # Ensure translated messages get placed properly into json faults self.stubs.Set(gettextutils, 'get_localized_message', self._fake_get_localized_message) response = self.get_json('/alarms/-', expect_errors=True, headers={"Accept": "application/json"}) self.assertEqual(response.status_int, 400) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) fault = json.loads(response.json['error_message']) self.assertEqual(fault['faultstring'], self.translated_error) def test_xml_parsable_error_middleware_404(self): response = self.get_json('/invalid_path', expect_errors=True, headers={"Accept": "application/xml,*/*"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') response = self.get_json('/invalid_path', expect_errors=True, headers={ "Accept": "application/json;q=0.8 \ ,application/xml" }) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') def test_xml_parsable_error_middleware_translation_400(self): # Ensure translated messages get placed properly into xml faults self.stubs.Set(gettextutils, 'get_localized_message', self._fake_get_localized_message) response = self.get_json('/alarms/-', expect_errors=True, headers={"Accept": "application/xml,*/*"}) self.assertEqual(response.status_int, 400) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') fault = response.xml.findall('./error/faultstring') for fault_string in fault: self.assertEqual(fault_string.text, self.translated_error)
class MongoDBEngineTestBase(tests_db.TestBase): database_connection = tests_db.MongoDBFakeConnectionUrl()
class CompatibilityTest(test_storage_scenarios.DBTestBase, tests_db.MixinTestsWithBackendScenarios): scenarios = [ ('mongodb', dict(database_connection=tests_db.MongoDBFakeConnectionUrl())), ('db2', dict(database_connection=tests_db.DB2FakeConnectionUrl())), ] def prepare_data(self): def old_record_metering_data(self, data): self.db.user.update( {'_id': data['user_id']}, { '$addToSet': { 'source': data['source'], }, }, upsert=True, ) self.db.project.update( {'_id': data['project_id']}, { '$addToSet': { 'source': data['source'], }, }, upsert=True, ) received_timestamp = datetime.datetime.utcnow() self.db.resource.update( {'_id': data['resource_id']}, { '$set': { 'project_id': data['project_id'], 'user_id': data['user_id'], # Current metadata being used and when it was # last updated. 'timestamp': data['timestamp'], 'received_timestamp': received_timestamp, 'metadata': data['resource_metadata'], 'source': data['source'], }, '$addToSet': { 'meter': { 'counter_name': data['counter_name'], 'counter_type': data['counter_type'], }, }, }, upsert=True, ) record = copy.copy(data) self.db.meter.insert(record) # Stubout with the old version DB schema, the one w/o 'counter_unit' with patch.object(self.conn, 'record_metering_data', side_effect=old_record_metering_data): self.counters = [] c = sample.Sample( 'volume.size', 'gauge', 'GiB', 5, 'user-id', 'project1', 'resource-id', timestamp=datetime.datetime(2012, 9, 25, 10, 30), resource_metadata={ 'display_name': 'test-volume', 'tag': 'self.counter', }, source='test', ) self.counters.append(c) msg = utils.meter_message_from_counter(c, secret='not-so-secret') self.conn.record_metering_data(self.conn, msg) # Create the old format alarm with a dict instead of a # array for matching_metadata alarm = dict(alarm_id='0ld-4l3rt', enabled=True, name='old-alert', description='old-alert', timestamp=None, meter_name='cpu', user_id='me', project_id='and-da-boys', comparison_operator='lt', threshold=36, statistic='count', evaluation_periods=1, period=60, state="insufficient data", state_timestamp=None, ok_actions=[], alarm_actions=['http://nowhere/alarms'], insufficient_data_actions=[], repeat_actions=False, matching_metadata={'key': 'value'}) self.conn.db.alarm.update({'alarm_id': alarm['alarm_id']}, {'$set': alarm}, upsert=True) alarm['alarm_id'] = 'other-kind-of-0ld-4l3rt' alarm['name'] = 'other-old-alaert' alarm['matching_metadata'] = [{ 'key': 'key1', 'value': 'value1' }, { 'key': 'key2', 'value': 'value2' }] self.conn.db.alarm.update({'alarm_id': alarm['alarm_id']}, {'$set': alarm}, upsert=True) def test_alarm_get_old_format_matching_metadata_dict(self): old = list(self.conn.get_alarms(name='old-alert'))[0] self.assertEqual('threshold', old.type) self.assertEqual([{ 'field': 'key', 'op': 'eq', 'value': 'value', 'type': 'string' }], old.rule['query']) self.assertEqual(60, old.rule['period']) self.assertEqual('cpu', old.rule['meter_name']) self.assertEqual(1, old.rule['evaluation_periods']) self.assertEqual('count', old.rule['statistic']) self.assertEqual('lt', old.rule['comparison_operator']) self.assertEqual(36, old.rule['threshold']) def test_alarm_get_old_format_matching_metadata_array(self): old = list(self.conn.get_alarms(name='other-old-alaert'))[0] self.assertEqual('threshold', old.type) self.assertEqual( sorted([{ 'field': 'key1', 'op': 'eq', 'value': 'value1', 'type': 'string' }, { 'field': 'key2', 'op': 'eq', 'value': 'value2', 'type': 'string' }]), sorted(old.rule['query']), ) self.assertEqual('cpu', old.rule['meter_name']) self.assertEqual(60, old.rule['period']) self.assertEqual(1, old.rule['evaluation_periods']) self.assertEqual('count', old.rule['statistic']) self.assertEqual('lt', old.rule['comparison_operator']) self.assertEqual(36, old.rule['threshold']) def test_counter_unit(self): meters = list(self.conn.get_meters()) self.assertEqual(1, len(meters))
class TestApiMiddleware(FunctionalTest): # This doesn't really matter database_connection = tests_db.MongoDBFakeConnectionUrl() no_lang_translated_error = 'No lang translated error' en_US_translated_error = 'en-US translated error' def setUp(self): super(TestApiMiddleware, self).setUp() self.stubs = self.useFixture(moxstubout.MoxStubout()).stubs def _fake_get_localized_message(self, message, user_locale): if user_locale is None: return self.no_lang_translated_error else: return self.en_US_translated_error def test_json_parsable_error_middleware_404(self): response = self.get_json('/invalid_path', expect_errors=True, headers={"Accept": "application/json"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json( '/invalid_path', expect_errors=True, headers={"Accept": "application/json,application/xml"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json('/invalid_path', expect_errors=True, headers={ "Accept": "application/xml;q=0.8, \ application/json" }) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json('/invalid_path', expect_errors=True) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) response = self.get_json('/invalid_path', expect_errors=True, headers={"Accept": "text/html,*/*"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) def test_json_parsable_error_middleware_translation_400(self): # Ensure translated messages get placed properly into json faults self.stubs.Set(gettextutils, 'get_localized_message', self._fake_get_localized_message) response = self.post_json('/alarms', params={ 'name': 'foobar', 'type': 'threshold' }, expect_errors=True, headers={"Accept": "application/json"}) self.assertEqual(response.status_int, 400) self.assertEqual(response.content_type, "application/json") self.assertTrue(response.json['error_message']) self.assertEqual(response.json['error_message']['faultstring'], self.no_lang_translated_error) def test_xml_parsable_error_middleware_404(self): response = self.get_json('/invalid_path', expect_errors=True, headers={"Accept": "application/xml,*/*"}) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') response = self.get_json('/invalid_path', expect_errors=True, headers={ "Accept": "application/json;q=0.8 \ ,application/xml" }) self.assertEqual(response.status_int, 404) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') def test_xml_parsable_error_middleware_translation_400(self): # Ensure translated messages get placed properly into xml faults self.stubs.Set(gettextutils, 'get_localized_message', self._fake_get_localized_message) response = self.post_json('/alarms', params={ 'name': 'foobar', 'type': 'threshold' }, expect_errors=True, headers={"Accept": "application/xml,*/*"}) self.assertEqual(response.status_int, 400) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') fault = response.xml.findall('./error/faultstring') for fault_string in fault: self.assertEqual(fault_string.text, self.no_lang_translated_error) def test_best_match_language(self): # Ensure that we are actually invoking language negotiation self.stubs.Set(gettextutils, 'get_localized_message', self._fake_get_localized_message) response = self.post_json('/alarms', params={ 'name': 'foobar', 'type': 'threshold' }, expect_errors=True, headers={ "Accept": "application/xml,*/*", "Accept-Language": "en-US" }) self.assertEqual(response.status_int, 400) self.assertEqual(response.content_type, "application/xml") self.assertEqual(response.xml.tag, 'error_message') fault = response.xml.findall('./error/faultstring') for fault_string in fault: self.assertEqual(fault_string.text, self.en_US_translated_error)