def test(self): expected_services = [Service(name=TEST_SERVICE_NAME, ip="localhost", check_methods=TEST_METHOD)] expected_nodes = [Node(services=expected_services, ip="localhost")] mocked_sampler = CommunicationTest._MockedNodeSampler() mocked_sampler.register_checker(TEST_METHOD, MockedMethodChecker) server = NodeSamplerServer(8000, mocked_sampler) def start_server(): server.start() server_thread = Thread(target=start_server) server_thread.setDaemon(True) server_thread.start() sedna_config_object = SednaConfig() sedna_config_object.port = "8000" sedna_config_object.nodes = expected_nodes master = Master() master.verify_nodes(sedna_config_object) self.assertTrue(mocked_sampler.is_sample_called, "The sampler is no called!") self.assertSequenceEqual(expected_services, mocked_sampler.actual_services)
def test_communicate(self): logger = LOG() logger_tester = logger.tester() sedna_config_value = SednaConfig(port=TEST_PORT, nodes=self.expected_nodes) logger_tester.info("{port:%s, nodes:%s}" % (sedna_config_value.port, sedna_config_value.nodes)) master = Master() results = master.verify_nodes(sedna_config_value) logger_tester.info("Sampler Server results: %s" % results) self.assertEquals(results[0].group, self.expected_node.group, "Fail when test_get_nodes information") self.assertEquals(results[0].ip, self.expected_node.ip, "Fail when test_get_nodes information") expected_result = Result(status=TEST_STATUS, service=self.expected_service) for result in results[0].result: self.assertEquals(result, expected_result)
class MasterTest(TestCase): """ The test case to test the Master Class. """ def setUp(self): """ The wrapper of sample method to build a fake Sampler and a fake Sedna config file with the relevant information of its test case. """ expected_service = Service(name=TEST_SERVICE_NAME, ip=TEST_IP, check_methods=TEST_METHOD) self.expected_services = [expected_service, expected_service] expected_node = Node(group=TEST_GROUP, ip=TEST_IP, services=self.expected_services) self.expected_nodes = [expected_node, expected_node, expected_node] self.mocked_xml_rpc_client = MasterTest._MockedXmlRPCClient() self.sedna_config_value =\ SednaConfig(port=TEST_PORT, nodes=self.expected_nodes) def test_master_all_node(self): """ The test case to test whether master class can set up a Master object and get result from all node. :return: """ self.master = Master(self.mocked_xml_rpc_client) master_verify_result =\ self.master.verify_nodes(self.sedna_config_value) self.assertTrue(self.mocked_xml_rpc_client.is_rpc_client_called, "The XMLRPCClient is not called!") self.assertEquals(self.expected_services, self.mocked_xml_rpc_client.node_services) self.assertEquals(len(master_verify_result), len(self.expected_nodes), "Fail to get result from all nodes!") def test_format_xmlrpc_response(self): sample_result = self.mocked_xml_rpc_client.connect( ip=TEST_IP, group=TEST_GROUP, port=TEST_PORT, node_services=self.expected_services) pass def teardown(self): pass class _MockedXmlRPCClient(XmlRPCClient): """ The MockedXmlRPCClient inheriting from XmlRPCClient contains stubs to check the communication inner status """ def __init__(self): self.is_rpc_client_called = False self.node_services = None super(MasterTest._MockedXmlRPCClient, self).__init__() def connect(self, ip, group, port, node_services): """ The method inheriting from NodeSampler can check whether the method called by Master and mock the return results of sampler. :param ip: :param group: :param port: :param node_services: :return: return the mocked results to check the communication """ self.is_rpc_client_called = True self.node_services = node_services self.register_checker(name=TEST_METHOD, klass=MockedMethodChecker) self.sample(services=node_services) return []