def setUp(self) -> None: self.logger = logging.getLogger('dummy') self.db = TestUserConf.mongo_db_name self.host = TestUserConf.mongo_host self.port = TestUserConf.mongo_port self.user = TestUserConf.mongo_user self.password = TestUserConf.mongo_pass self.mongo = MongoApi(self.logger, self.db, self.host, self.port, username=self.user, password=self.password) # Ping Mongo try: self.mongo.ping_unsafe() except PyMongoError: self.fail('Mongo is not online.') # Clear test database self.mongo.drop_db() self.col1 = 'collection1' self.col2 = 'collection2' self.val1 = {'a': 'b', 'c': 'd'} self.val2 = {'e': 'f', 'g': 'h'} self.val3 = {'i': 'j'} self.val4 = {'k': 'l', 'm': {'n': ['o', 'p', True, False, 1, 2.1]}} self.time = timedelta(seconds=3) self.time_with_error_margin = timedelta(seconds=4) self.default_str = 'DEFAULT' self.default_int = 789 self.default_bool = False
def setUp(self) -> None: self.logger = logging.getLogger('dummy') self.db = TestUserConf.mongo_db_name self.host = TestUserConf.mongo_host self.port = TestUserConf.mongo_port self.live_check_time_interval = timedelta(seconds=3) self.live_check_time_interval_with_error_margin = timedelta(seconds=3.5) self.mongo = MongoApi(self.logger, self.db, self.host, self.port, live_check_time_interval= self.live_check_time_interval)
def setUp(self) -> None: self.logger = logging.getLogger('dummy') self.db = TestUserConf.mongo_db_name self.host = 'dummyhost' self.port = TestUserConf.mongo_port self.user = TestUserConf.mongo_user self.password = TestUserConf.mongo_pass self.mongo = MongoApi(self.logger, self.db, self.host, self.port, timeout_ms=1) # timeout_ms is set to 1ms to speed up tests. It cannot be 0 :p self.col1 = 'collection1' self.val1 = {'a': 'b', 'c': 'd'} self.val2 = {'e': 'f', 'g': 'h'} self.val3 = {'i': 'j'}
def setUpClass(cls) -> None: # Same as in setUp(), to avoid running all tests if Mongo is offline logger = logging.getLogger('dummy') db = TestUserConf.mongo_db_name host = TestUserConf.mongo_host port = TestUserConf.mongo_port user = TestUserConf.mongo_user password = TestUserConf.mongo_pass mongo = MongoApi(logger, db, host, port, username=user, password=password) # Ping Mongo try: mongo.ping_unsafe() except PyMongoError: raise Exception('Mongo is not online.')
def run() -> None: # Check if Mongo enabled if not UserConf.mongo_enabled: raise InitialisationException('Mongo is not set up. Run the setup ' 'script to configure Mongo.') logger = create_logger(InternalConf.mongo_log_file, 'mongo', InternalConf.logging_level) db_name = UserConf.mongo_db_name print('Deleting "{}" database from MongoDB.'.format(db_name)) # Attempt to delete database try: MongoApi(logger, UserConf.mongo_db_name, UserConf.mongo_host, UserConf.mongo_port, UserConf.mongo_user, UserConf.mongo_pass).drop_db() except Exception as e: sys.exit(e) print('Done deleting "{}" database from MongoDB.'.format(db_name))
def test_mongo(host: str, port: str, username: str, password: str) \ -> TestOutcome: mongo = MongoApi(DUMMY_LOGGER, 'dummy_db_name', host, int(port), username=username, password=password) try: mongo.ping_unsafe() if len(username) != 0: mongo.ping_auth(username, password) print('Test completed successfully.') return TestOutcome.OK except Exception as e: print('Something went wrong: {}'.format(e)) if yn_prompt('Retry Mongo setup? (Y/n)\n'): return TestOutcome.RestartSetup else: return TestOutcome.SkipSetup
class TestMongoApiLiveAndDownFeaturesWithMongoOffline(unittest.TestCase): def setUp(self) -> None: self.logger = logging.getLogger('dummy') self.db = TestUserConf.mongo_db_name self.host = TestUserConf.mongo_host self.port = TestUserConf.mongo_port self.live_check_time_interval = timedelta(seconds=3) self.live_check_time_interval_with_error_margin = timedelta( seconds=3.5) self.mongo = MongoApi( self.logger, self.db, self.host, self.port, live_check_time_interval=self.live_check_time_interval) def test_is_live_returns_true_by_default(self): self.assertTrue(self.mongo.is_live) def test_set_as_live_changes_is_live_to_true(self): self.mongo._is_live = False self.assertFalse(self.mongo.is_live) self.mongo._set_as_live() self.assertTrue(self.mongo._is_live) def test_set_as_live_leaves_is_live_as_true_if_already_true(self): self.mongo._is_live = True self.assertTrue(self.mongo.is_live) self.mongo._set_as_live() self.assertTrue(self.mongo._is_live) def test_set_as_down_changes_is_live_to_false(self): self.mongo._set_as_down() self.assertFalse(self.mongo.is_live) def test_set_as_down_leaves_is_live_as_false_if_already_false(self): self.mongo._is_live = False self.assertFalse(self.mongo.is_live) self.mongo._set_as_down() self.assertFalse(self.mongo.is_live) def test_allowed_to_use_by_default(self): # noinspection PyBroadException try: self.mongo._do_not_use_if_recently_went_down() except Exception: self.fail('Expected to be allowed to use Mongo.') def test_not_allowed_to_use_if_set_as_down_and_within_time_interval(self): self.mongo._set_as_down() # noinspection PyBroadException try: self.mongo._do_not_use_if_recently_went_down() self.fail('Expected to not be allowed to use Mongo.') except Exception: pass def test_allowed_to_use_if_set_as_down_and_within_time_interval(self): self.mongo._set_as_down() sleep(self.live_check_time_interval_with_error_margin.seconds) # noinspection PyBroadException try: self.mongo._do_not_use_if_recently_went_down() except Exception: self.fail('Expected to be allowed to use Mongo.')
class TestMongoApiWithMongoOffline(unittest.TestCase): def setUp(self) -> None: self.logger = logging.getLogger('dummy') self.db = TestUserConf.mongo_db_name self.host = 'dummyhost' self.port = TestUserConf.mongo_port self.user = TestUserConf.mongo_user self.password = TestUserConf.mongo_pass self.mongo = MongoApi(self.logger, self.db, self.host, self.port, timeout_ms=1) # timeout_ms is set to 1ms to speed up tests. It cannot be 0 :p self.col1 = 'collection1' self.val1 = {'a': 'b', 'c': 'd'} self.val2 = {'e': 'f', 'g': 'h'} self.val3 = {'i': 'j'} def test_insert_one_throws_exception_first_time_round(self): try: self.mongo.insert_one(self.col1, self.val1) self.fail('Expected ServerSelectionTimeoutError to be thrown.') except ServerSelectionTimeoutError: pass def test_insert_many_throws_exception_first_time_round(self): try: self.mongo.insert_many(self.col1, [self.val1, self.val2, self.val3]) self.fail('Expected ServerSelectionTimeoutError to be thrown.') except ServerSelectionTimeoutError: pass def test_get_all_throws_exception_first_time_round(self): try: self.mongo.get_all(self.col1) self.fail('Expected ServerSelectionTimeoutError to be thrown.') except ServerSelectionTimeoutError: pass def test_drop_collection_throws_exception_first_time_round(self): try: self.mongo.drop_collection(self.col1) self.fail('Expected ServerSelectionTimeoutError to be thrown.') except ServerSelectionTimeoutError: pass def test_drop_db_throws_exception_first_time_round(self): try: self.mongo.drop_db() self.fail('Expected ServerSelectionTimeoutError to be thrown.') except ServerSelectionTimeoutError: pass def test_ping_unsafe_throws_exception_first_time_round(self): try: self.mongo.ping_unsafe() self.fail('Expected ServerSelectionTimeoutError to be thrown.') except ServerSelectionTimeoutError: pass def test_ping_auth_throws_exception_first_time_round(self): try: self.mongo.ping_auth(username=self.user, password=self.password) self.fail('Expected ServerSelectionTimeoutError to be thrown.') except ServerSelectionTimeoutError: pass def test_insert_one_returns_none_if_mongo_already_down(self): self.mongo._set_as_down() self.assertIsNone(self.mongo.insert_one(self.col1, self.val1)) def test_insert_many_returns_none_if_mongo_already_down(self): self.mongo._set_as_down() documents = [self.val1, self.val2, self.val3] self.assertIsNone(self.mongo.insert_many(self.col1, documents)) def test_get_all_returns_none_if_mongo_already_down(self): self.mongo._set_as_down() self.assertIsNone(self.mongo.get_all(self.col1)) def test_drop_collection_returns_none_if_mongo_already_down(self): self.mongo._set_as_down() self.assertIsNone(self.mongo.drop_collection(self.col1)) def test_drop_db_returns_none_if_mongo_already_down(self): self.mongo._set_as_down() self.assertIsNone(self.mongo.drop_db()) def test_ping_unsafe_throws_exception_if_mongo_already_down(self): self.mongo._set_as_down() try: self.mongo.ping_unsafe() self.fail('Expected ServerSelectionTimeoutError to be thrown.') except ServerSelectionTimeoutError: pass def test_ping_auth_throws_exception_if_mongo_already_down(self): self.mongo._set_as_down() try: self.mongo.ping_auth(username=self.user, password=self.password) self.fail('Expected ServerSelectionTimeoutError to be thrown.') except ServerSelectionTimeoutError: pass
class TestMongoApiWithMongoOnline(unittest.TestCase): @classmethod def setUpClass(cls) -> None: # Same as in setUp(), to avoid running all tests if Mongo is offline logger = logging.getLogger('dummy') db = TestUserConf.mongo_db_name host = TestUserConf.mongo_host port = TestUserConf.mongo_port user = TestUserConf.mongo_user password = TestUserConf.mongo_pass mongo = MongoApi(logger, db, host, port, username=user, password=password) # Ping Mongo try: mongo.ping_unsafe() except PyMongoError: raise Exception('Mongo is not online.') def setUp(self) -> None: self.logger = logging.getLogger('dummy') self.db = TestUserConf.mongo_db_name self.host = TestUserConf.mongo_host self.port = TestUserConf.mongo_port self.user = TestUserConf.mongo_user self.password = TestUserConf.mongo_pass self.mongo = MongoApi(self.logger, self.db, self.host, self.port, username=self.user, password=self.password) # Ping Mongo try: self.mongo.ping_unsafe() except PyMongoError: self.fail('Mongo is not online.') # Clear test database self.mongo.drop_db() self.col1 = 'collection1' self.col2 = 'collection2' self.val1 = {'a': 'b', 'c': 'd'} self.val2 = {'e': 'f', 'g': 'h'} self.val3 = {'i': 'j'} self.val4 = {'k': 'l', 'm': {'n': ['o', 'p', True, False, 1, 2.1]}} self.time = timedelta(seconds=3) self.time_with_error_margin = timedelta(seconds=4) self.default_str = 'DEFAULT' self.default_int = 789 self.default_bool = False def tearDown(self) -> None: self.mongo.drop_db() def test_insert_one_inserts_value_into_the_specified_collection(self): # Check that col1 is empty get_result = list(self.mongo._db[self.col1].find({})) self.assertEqual(len(get_result), 0) # Insert val1 into col1 self.mongo.insert_one(self.col1, self.val1) # Check that value was added to col1 get_result = list(self.mongo._db[self.col1].find({})) self.assertEqual(len(get_result), 1) self.assertEqual(dict(get_result[0]), self.val1) def test_insert_one_supports_more_complex_documents(self): # Check that col1 is empty get_result = list(self.mongo._db[self.col1].find({})) self.assertEqual(len(get_result), 0) # Insert val4 into col1 self.mongo.insert_one(self.col1, self.val4) # Check that value was added to col1 get_result = list(self.mongo._db[self.col1].find({})) self.assertEqual(len(get_result), 1) self.assertEqual(dict(get_result[0]), self.val4) def test_insert_many_inserts_all_values_into_the_specified_collection( self): # Check that col1 is empty get_result = list(self.mongo._db[self.col1].find({})) self.assertEqual(len(get_result), 0) # Insert val1, val2, and val3 into col1 self.mongo.insert_many(self.col1, [self.val1, self.val2, self.val3]) # Check that the values was added to col1 get_result = list(self.mongo._db[self.col1].find({})) self.assertEqual(len(get_result), 3) self.assertEqual(dict(get_result[0]), self.val1) self.assertEqual(dict(get_result[1]), self.val2) self.assertEqual(dict(get_result[2]), self.val3) def test_get_all_returns_inserted_values_in_order_of_insert(self): # Check that col1 is empty get_result = self.mongo.get_all(self.col1) self.assertEqual(len(get_result), 0) # Insert val1, val2, and val3 into col1 self.mongo._db[self.col1].insert_many( [self.val1, self.val2, self.val3]) # Check that the values was added to col1 get_result = self.mongo.get_all(self.col1) self.assertEqual(len(get_result), 3) self.assertEqual(dict(get_result[0]), self.val1) self.assertEqual(dict(get_result[1]), self.val2) self.assertEqual(dict(get_result[2]), self.val3) def test_drop_collection_deletes_the_specified_collection(self): # Check that col1 and col2 are empty get_result1 = list(self.mongo._db[self.col1].find({})) get_result2 = list(self.mongo._db[self.col2].find({})) self.assertEqual(len(get_result1), 0) self.assertEqual(len(get_result2), 0) # Insert val1, val2, and val3 into col1 and val4 into col2 self.mongo._db[self.col1].insert_many( [self.val1, self.val2, self.val3]) self.mongo._db[self.col2].insert_one(self.val4) # Check that col1 and col2 are not empty get_result1 = list(self.mongo._db[self.col1].find({})) get_result2 = list(self.mongo._db[self.col2].find({})) self.assertEqual(len(get_result1), 3) self.assertEqual(len(get_result2), 1) # Delete col1 self.mongo.drop_collection(self.col1) # Check that col1 is back to being empty but col2 is not empty get_result1 = list(self.mongo._db[self.col1].find({})) get_result2 = list(self.mongo._db[self.col2].find({})) self.assertEqual(len(get_result1), 0) self.assertEqual(len(get_result2), 1) def test_drop_db_deletes_all_collections(self): # Check that col1 and col2 are empty get_result1 = list(self.mongo._db[self.col1].find({})) get_result2 = list(self.mongo._db[self.col2].find({})) self.assertEqual(len(get_result1), 0) self.assertEqual(len(get_result2), 0) # Insert val1, val2, and val3 into col1 and val4 into col2 self.mongo._db[self.col1].insert_many( [self.val1, self.val2, self.val3]) self.mongo._db[self.col2].insert_one(self.val4) # Check that col1 and col2 are not empty get_result1 = list(self.mongo._db[self.col1].find({})) get_result2 = list(self.mongo._db[self.col2].find({})) self.assertEqual(len(get_result1), 3) self.assertEqual(len(get_result2), 1) # Drop db self.mongo.drop_db() # Check that col1 and col2 are back to being empty get_result1 = list(self.mongo._db[self.col1].find({})) get_result2 = list(self.mongo._db[self.col2].find({})) self.assertEqual(len(get_result1), 0) self.assertEqual(len(get_result2), 0) def test_ping_returns_true(self): self.assertTrue(self.mongo.ping_unsafe()) def test_ping_auth_throws_value_error_for_empty_password(self): try: self.mongo.ping_auth(self.user, '') self.fail('Expected ValueError exception to be thrown.') except ValueError: pass def test_ping_auth_throws_operation_failure_for_wrong_password(self): try: self.mongo.ping_auth(self.user, 'incorrect_password') self.fail('Expected OperationFailure exception to be thrown.') except OperationFailure: pass
if UserConf.redis_enabled: REDIS = RedisApi(logger_redis, InternalConf.redis_database, UserConf.redis_host, UserConf.redis_port, password=UserConf.redis_password, namespace=UserConf.unique_alerter_identifier) else: REDIS = None # Set MONGO Object if it's enabled, pass the created Logger together # with details of the MONGO databse to the MONGO API Object if UserConf.mongo_enabled: MONGO = MongoApi(logger_mongo, UserConf.mongo_db_name, UserConf.mongo_host, UserConf.mongo_port, username=UserConf.mongo_user, password=UserConf.mongo_pass) else: MONGO = None # Alerters initialisation alerter_name = 'PANIC' # Returns the full list of channels and decides who is enabled or not. full_channel_set = get_full_channel_set(alerter_name, logger_general, REDIS, log_file_alerts, MONGO) # Returns list of enabled channels in string format log_and_print('Enabled alerting channels (general): {}'.format( full_channel_set.enabled_channels_list()))
from src.alerts.alerts import Alert from src.alerts.alerts import AlertCode from src.utils.alert_utils.get_channel_set import get_full_channel_set from src.utils.config_parsers.internal_parsed import InternalConf from src.utils.config_parsers.user_parsed import UserConf from src.utils.logging import DUMMY_LOGGER from src.store.mongo.mongo_api import MongoApi if __name__ == '__main__': mongo = MongoApi(logger=DUMMY_LOGGER, db_name=UserConf.mongo_db_name, host=UserConf.mongo_host, port=UserConf.mongo_port, username=UserConf.mongo_user, password=UserConf.mongo_pass) channel_set = get_full_channel_set( channel_name='TEST', logger_general=DUMMY_LOGGER, redis=None, alerts_log_file=InternalConf.alerts_log_file, mongo=mongo) channel_set.alert_info( Alert(AlertCode.TestAlert, 'This is a test INFO alert.')) channel_set.alert_warning( Alert(AlertCode.TestAlert, 'This is a test WARNING alert.')) channel_set.alert_critical( Alert(AlertCode.TestAlert, 'This is a test CRITICAL alert.')) channel_set.alert_error( Alert(AlertCode.TestAlert, 'This is a test ERROR alert.'))