class XmlRPCClientTest(TestCase): """ The test cases to test the capibility of xmlrpclib. """ def setUp(self): self._mock_sampler = XmlRPCClientTest._MockedSampler() self._mock_sampler_server =\ NodeSamplerServer(port=8000, sampler=self._mock_sampler) def start_server(): self._mock_sampler_server.start() server_thread = Thread(target=start_server) server_thread.setDaemon(True) server_thread.start() self.xmlrpc_client = XmlRPCClient() self.fake_node_services =\ [Service(name="$$test_service_a$$", ip=TEST_IP, check_methods=TEST_METHOD), Service(name="$$test_service_b$$", ip=TEST_IP, check_methods=TEST_METHOD)] self.expected_respond = [{"name": "$$test_service_a$$", "ip": TEST_IP, "check_methods": TEST_METHOD}, {"name": "$$test_service_b$$", "ip": TEST_IP, "check_methods": TEST_METHOD}] def test_connect(self): """ The test case to check the capability of xmlrpclib when "ip" attribute of Service obj is string type. :return: """ node_result = self.xmlrpc_client.connect( ip=TEST_IP, group=TEST_GROUP, port=TEST_PORT, node_services=self.fake_node_services) self.assertEquals(node_result, self.expected_respond) class _MockedSampler(XmlRPCClient): """ The mocked NodeSampler inheriting from NodeSampler of XmlRPCClient contains stubs to check the communication inner status. """ def sample(self, services): return services
def setUp(self): self._mock_sampler = XmlRPCClientTest._MockedSampler() self._mock_sampler_server =\ NodeSamplerServer(port=8000, sampler=self._mock_sampler) def start_server(): self._mock_sampler_server.start() server_thread = Thread(target=start_server) server_thread.setDaemon(True) server_thread.start() self.xmlrpc_client = XmlRPCClient() self.fake_node_services =\ [Service(name="$$test_service_a$$", ip=TEST_IP, check_methods=TEST_METHOD), Service(name="$$test_service_b$$", ip=TEST_IP, check_methods=TEST_METHOD)] self.expected_respond = [{"name": "$$test_service_a$$", "ip": TEST_IP, "check_methods": TEST_METHOD}, {"name": "$$test_service_b$$", "ip": TEST_IP, "check_methods": TEST_METHOD}]
class NodeSamplerServerTest(TestCase): """ The test cases to test the capacity of Server start on some occassions. """ def setUp(self): _mocked_node_sampler = _MockedNodeSampler() self.node_sampler_server = NodeSamplerServer(8000, _mocked_node_sampler) def start_server(): self.node_sampler_server.start() server_thread = Thread(target=start_server) server_thread.setDaemon(True) server_thread.start() def test_sample_server(self): """ This test case to test sampler response. :return: """ fake_uri = "http://localhost:8000/" proxy = ServerProxy(fake_uri) dict_service_a = {"name": "$$test_service_a$$", "ip": TEST_IP, "check_methods": TEST_METHOD} dict_service_b = {"name": "$$test_service_b$$", "ip": TEST_IP, "check_methods": TEST_METHOD} fake_services = [dict_service_a, dict_service_b] self.assertEquals(proxy.sample(fake_services), 1, "Failed to return result from sampler.") def test_sampler_server_negative(self): """ This test case to test when transform value which cannot be found in config. :return: """ pass def tearDown(self): pass
def setUp(self): _mocked_node_sampler = _MockedNodeSampler() self.node_sampler_server = NodeSamplerServer(8000, _mocked_node_sampler) def start_server(): self.node_sampler_server.start() server_thread = Thread(target=start_server) server_thread.setDaemon(True) server_thread.start()
class NodeSamplerServerStartTest(TestCase): """ The test cases to test class NodeSamplerServer. """ def setUp(self): _mocked_node_sampler = _MockedNodeSampler() self.node_sampler_server =\ NodeSamplerServer(TEST_PORT, _mocked_node_sampler) def test_listen_port(self): """ The test case to test the server created by NodeSamplerServer class. :return: """ def start_server(): self.node_sampler_server.start() server_thread = Thread(target=start_server) server_thread.setDaemon(True) server_thread.start() proxy = ServerProxy("http://localhost:8000") service_a = Service(name="$$service_a$$", check_methods=TEST_METHOD, ip=TEST_IP) service_b = Service(name="$$service_b$$", check_methods=TEST_METHOD, ip=TEST_IP) service_c = Service(name="$$service_c$$", check_methods=TEST_METHOD, ip=TEST_IP) fake_services = [service_a, service_b, service_c] self.assertEquals( proxy.sample(fake_services), 1, "NodeSamplerServer cannot create object.")
def setUp(self): _mocked_node_sampler = _MockedNodeSampler() self.node_sampler_server =\ NodeSamplerServer(TEST_PORT, _mocked_node_sampler)