def test_get_internal_services_empty_list(rip_helper, mock_logger): mock_logger.return_value = MockLogger() mock_rip_helper = MockRIPHelperLocal() rip_helper.return_value.service.side_effect = mock_rip_helper.service mocked_service_identifiers = [] expected_services = [] assert rip.get_internal_services(mocked_service_identifiers) == expected_services
def test_get_internal_services_with_mixed_list(rip_helper, mock_logger): mock_logger.return_value = MockLogger() mock_rip_helper = MockRIPHelperLocal() rip_helper.return_value.service.side_effect = mock_rip_helper.service mocked_service_identifiers = [ "ecytu", "stree", "cloudcondensation", "will_iam", "netsky", "sword", "nirvana", "llamada" ] expected_services = ["ecytu", "stree", "cloudcondensation", "llamada"] assert rip.get_internal_services(mocked_service_identifiers) == expected_services
def test_get_internal_services_externalonly_list(rip_helper, mock_logger): mock_logger.return_value = MockLogger() mock_rip_helper = MockRIPHelperLocal() rip_helper.return_value.service.side_effect = mock_rip_helper.service mocked_service_identifiers = [ "will_iam", "cloudobserve", "netsky" ] expected_services = [] assert rip.get_internal_services(mocked_service_identifiers) == expected_services
def test_logger_is_used_as_expected(self): logger = MockLogger() # set the expected pattern of log message logger.set_expected_pattern(r".*add_user.*") db = UserDB(logger) db.add_user("Jane Doe") # final verification is delegated to the mock logger.verify()
def test_create_rms_service_dict_one_value(rip_helper, patched_mock_logger, mock_cpt_data): mock_rip_helper = MockRIPHelperLocal() rip_helper.return_value.service.side_effect = mock_rip_helper.service patched_mock_logger.return_value = MockLogger() service_dict = rms.create_rms_service_dict(mock_cpt_data, ["arn"]) expected_result = { "stree": { "arn": ["arn:aws:rmsv2:::test/002", "arn:aws:rmsv2:::test/004"] }, "ecytu": { "arn": ["arn:aws:rmsv2:::test/000"] } } assert service_dict == expected_result
# Tests that cover the Redis integration from mock_logger import MockLogger from typing import Optional import sys import pytest original_imports = set(sys.modules.keys()) k = 'foo' v = 'bar' logger = MockLogger() # Mock Redis client to use for testing. # The `should_fail` flag is used to force specific code branches for # consistent testing and more code coverage. class MockRedisClient: def __init__(self, should_fail: bool = False): self.cache = {} # store a cache internally self.should_fail = should_fail def set(self, post_id: str, server: str) -> bool: if not self.should_fail: self.cache[post_id] = server return True else: return False def exists(self, post_id: str) -> int: if post_id in self.cache: return 1 else:
def test_service_plan_categorization_caches_updated(get_logger): fake_services_seen_categories = {} fake_services_to_successors_data = {} fake_milestone_task_arns = [ "arn:aws:rmsv2:::test/000", "arn:aws:rmsv2:::test/001" ] fake_service = "ecytu" get_logger.return_value = MockLogger() graph = nx.DiGraph() ecytu1 = { "arn": "arn:aws:rmsv2:::test/000", "status": "STARTED", "dimension": "COMMERCIAL_PARTITION_TEMPLATE", "service": "ecytu" } ecytu2 = { "arn": "arn:aws:rmsv2:::test/001", "status": "STARTED", "dimension": "COMMERCIAL_PARTITION_TEMPLATE", "service": "ecytu" } stree = { "arn": "arn:aws:rmsv2:::milestones/002", "status": "STARTED", "dimension": "COMMERCIAL_PARTITION_TEMPLATE", "service": "stree" } stree3 = { "arn": "arn:aws:rmsv2:::milestones/003", "status": "STARTED", "dimension": "COMMERCIAL_PARTITION_TEMPLATE", "service": "stree3" } streetasks = { "arn": "arn:aws:rmsv2:::tasks/002", "status": "STARTED", "dimension": "COMMERCIAL_PARTITION_TEMPLATE", "service": "streetasks" } streetasks3 = { "arn": "arn:aws:rmsv2:::tasks/003", "status": "STARTED", "dimension": "COMMERCIAL_PARTITION_TEMPLATE", "service": "streetasks3" } node_ecytu1 = ("arn:aws:rmsv2:::test/000", { "data": RmsMilestoneOrTaskData(ecytu1, ServicePlan.UNCATEGORIZED) }) node_ecytu2 = ("arn:aws:rmsv2:::test/001", { "data": RmsMilestoneOrTaskData(ecytu2, ServicePlan.UNCATEGORIZED) }) node_stree = ("arn:aws:rmsv2:::milestones/002", { "data": RmsMilestoneOrTaskData(stree, ServicePlan.MANDATORY) }) node_stree3 = ("arn:aws:rmsv2:::milestones/003", { "data": RmsMilestoneOrTaskData(stree3, ServicePlan.NON_GLOBAL) }) node_streetasks = ("arn:aws:rmsv2:::tasks/002", { "data": RmsMilestoneOrTaskData(streetasks, ServicePlan.MANDATORY) }) node_streetasks3 = ("arn:aws:rmsv2:::tasks/003", { "data": RmsMilestoneOrTaskData(streetasks3, ServicePlan.LAUNCH_BLOCKING) }) graph.add_nodes_from([ node_ecytu1, node_ecytu2, node_stree, node_stree3, node_streetasks, node_streetasks3 ]) graph.add_edges_from([(node_ecytu1[0], node_stree[0]), (node_ecytu1[0], node_streetasks[0]), (node_ecytu2[0], node_stree3[0]), (node_ecytu2[0], node_streetasks3[0])]) expected_services_seen_categories = { "ecytu": ServicePlan.LAUNCH_BLOCKING, "stree": ServicePlan.MANDATORY, "stree3": ServicePlan.NON_GLOBAL, "streetasks": ServicePlan.MANDATORY, "streetasks3": ServicePlan.LAUNCH_BLOCKING } expected_services_to_successors_data = { "ecytu": [ RmsMilestoneOrTaskData(stree, ServicePlan.MANDATORY, node_ecytu1[0]), RmsMilestoneOrTaskData(streetasks, ServicePlan.MANDATORY, node_ecytu1[0]), RmsMilestoneOrTaskData(stree3, ServicePlan.NON_GLOBAL, node_ecytu2[0]), RmsMilestoneOrTaskData(streetasks3, ServicePlan.LAUNCH_BLOCKING, node_ecytu2[0]) ] } service_plan_catergorization(fake_service, fake_milestone_task_arns, fake_services_seen_categories, fake_services_to_successors_data, graph) assert fake_services_seen_categories == expected_services_seen_categories assert fake_services_to_successors_data == expected_services_to_successors_data
def run_logger(self): self.logger = MockLogger() self.logger.start()
class ClientHandlerTest(unittest.TestCase): logger = None logger_thread = None cli = None mm_server = None cli_thread = None mms_thread = None test_cli_id = b'ac5a49da-63f5-11e6-badf-d850e64f35c6' def setUp(self): self.msg1_unpacked = [b'mpwp_v1.0', b'100', b'0', b'1', b'12345', b'0', b'{\"content\": []}'] self.msg1_packed = '{\"data\": [\"mpwp_v1.0\", \"100\", \"0\", \"1\", ' \ '\"12345\", \"0\", \"{\\"content\\": []}\"]}' self.start_logger() self.start_mm_server() self.start_cli_handler() time.sleep(1) def tearDown(self): pass #self.mm_server.stop() #self.cli.stop() def start_logger(self): self.logger_thread = threading.Thread(target=self.run_logger, args=()) self.logger_thread.daemon = True self.logger_thread.start() def run_logger(self): self.logger = MockLogger() self.logger.start() def start_cli_handler(self): self.cli_thread = threading.Thread(target=self.run_cli_handler, args=()) self.cli_thread.daemon = True self.cli_thread.start() def start_mm_server(self): self.mms_thread = threading.Thread(target=self.run_mm_server, args=()) self.mms_thread.daemon = True self.mms_thread.start() def run_mm_server(self): self.mm_server = MatchmakerServer() self.mm_server.start() def run_cli_handler(self): self.cli = ClientHandler() self.cli.ID = self.test_cli_id self.cli.connect_to_matchmaker() def test_chatter(self): msg = self.cli.get_packet(b'0', b'0', b'') self.cli.log(1, msg) self.cli.forward_to_server(msg) time.sleep(1) msg = self.logger.log_buffer[0] self.assertEqual(b"[b'mpwp_v1.0', b'100', b'0', b'ac5a49da-63f5-11e6-badf-d850e64f35c6', b'0', b'0']", msg)
class ClientHandlerTest(unittest.TestCase): logger = None logger_thread = None cli = None mm_server = None cli_thread = None mms_thread = None test_cli_id = b'ac5a49da-63f5-11e6-badf-d850e64f35c6' def setUp(self): self.msg1_unpacked = [ b'mpwp_v1.0', b'100', b'0', b'1', b'12345', b'0', b'{\"content\": []}' ] self.msg1_packed = '{\"data\": [\"mpwp_v1.0\", \"100\", \"0\", \"1\", ' \ '\"12345\", \"0\", \"{\\"content\\": []}\"]}' self.start_logger() self.start_mm_server() self.start_cli_handler() time.sleep(1) def tearDown(self): pass #self.mm_server.stop() #self.cli.stop() def start_logger(self): self.logger_thread = threading.Thread(target=self.run_logger, args=()) self.logger_thread.daemon = True self.logger_thread.start() def run_logger(self): self.logger = MockLogger() self.logger.start() def start_cli_handler(self): self.cli_thread = threading.Thread(target=self.run_cli_handler, args=()) self.cli_thread.daemon = True self.cli_thread.start() def start_mm_server(self): self.mms_thread = threading.Thread(target=self.run_mm_server, args=()) self.mms_thread.daemon = True self.mms_thread.start() def run_mm_server(self): self.mm_server = MatchmakerServer() self.mm_server.start() def run_cli_handler(self): self.cli = ClientHandler() self.cli.ID = self.test_cli_id self.cli.connect_to_matchmaker() def test_chatter(self): msg = self.cli.get_packet(b'0', b'0', b'') self.cli.log(1, msg) self.cli.forward_to_server(msg) time.sleep(1) msg = self.logger.log_buffer[0] self.assertEqual( b"[b'mpwp_v1.0', b'100', b'0', b'ac5a49da-63f5-11e6-badf-d850e64f35c6', b'0', b'0']", msg)