def create_daemon(self, *args, **kwargs): self._daemon = DfmsDaemon(*args, **kwargs) if 'noNM' not in kwargs or not kwargs['noNM']: self.assertTrue(utils.portIsOpen('localhost', constants.NODE_DEFAULT_REST_PORT, _TIMEOUT), 'The NM did not start successfully') if 'master' in kwargs and kwargs['master']: self.assertTrue(utils.portIsOpen('localhost', constants.MASTER_DEFAULT_REST_PORT, _TIMEOUT), 'The MM did not start successfully') self._daemon_t = threading.Thread(target=lambda: self._daemon.start('localhost', 9000)) self._daemon_t.start() # Wait until the daemon's server has started # We can't simply check if the port is opened, because the server binds # before it is returned to us. In some tests we don't interact with it, # and therefore the shutdown of the daemon can occur before the server # is even returned to us. This would happen because portIsOpen will # succeed with a bound server, even if we haven't serve_forever()'d it # yet. In these situations shutting down the daemon will not shut down # the http server, and therefore the test will fail when checking that # the self._daemon_t is not alive anymore # # To actually avoid this we need to do some actual HTTP talk, which will # ensure the server is actually serving requests, and therefore already # in the daemon's hand #self.assertTrue(utils.portIsOpen('localhost', 9000, _TIMEOUT)) try: restutils.RestClient('localhost', 9000, 10)._GET('/anything') except restutils.RestClientException: # We don't care about the result pass
def create_daemon(self, *args, **kwargs): self._daemon = DfmsDaemon(*args, **kwargs) if "noNM" not in kwargs or not kwargs["noNM"]: self.assertTrue( utils.portIsOpen("localhost", constants.NODE_DEFAULT_REST_PORT, _TIMEOUT), "The NM did not start successfully", ) if "master" in kwargs and kwargs["master"]: self.assertTrue( utils.portIsOpen("localhost", constants.MASTER_DEFAULT_REST_PORT, _TIMEOUT), "The MM did not start successfully", ) self._daemon_t = threading.Thread(target=lambda: self._daemon.start("localhost", 9000)) self._daemon_t.start() # Wait until the daemon's server has started # We can't simply check if the port is opened, because the server binds # before it is returned to us. In some tests we don't interact with it, # and therefore the shutdown of the daemon can occur before the server # is even returned to us. This would happen because portIsOpen will # succeed with a bound server, even if we haven't serve_forever()'d it # yet. In these situations shutting down the daemon will not shut down # the http server, and therefore the test will fail when checking that # the self._daemon_t is not alive anymore # # To actually avoid this we need to do some actual HTTP talk, which will # ensure the server is actually serving requests, and therefore already # in the daemon's hand # self.assertTrue(utils.portIsOpen('localhost', 9000, _TIMEOUT)) try: restutils.RestClient("localhost", 9000, 10)._GET("/anything") except restutils.RestClientException: # We don't care about the result pass
class TestDaemon(unittest.TestCase): def create_daemon(self, *args, **kwargs): self._daemon = DfmsDaemon(*args, **kwargs) if "noNM" not in kwargs or not kwargs["noNM"]: self.assertTrue( utils.portIsOpen("localhost", constants.NODE_DEFAULT_REST_PORT, _TIMEOUT), "The NM did not start successfully", ) if "master" in kwargs and kwargs["master"]: self.assertTrue( utils.portIsOpen("localhost", constants.MASTER_DEFAULT_REST_PORT, _TIMEOUT), "The MM did not start successfully", ) self._daemon_t = threading.Thread(target=lambda: self._daemon.start("localhost", 9000)) self._daemon_t.start() # Wait until the daemon's server has started # We can't simply check if the port is opened, because the server binds # before it is returned to us. In some tests we don't interact with it, # and therefore the shutdown of the daemon can occur before the server # is even returned to us. This would happen because portIsOpen will # succeed with a bound server, even if we haven't serve_forever()'d it # yet. In these situations shutting down the daemon will not shut down # the http server, and therefore the test will fail when checking that # the self._daemon_t is not alive anymore # # To actually avoid this we need to do some actual HTTP talk, which will # ensure the server is actually serving requests, and therefore already # in the daemon's hand # self.assertTrue(utils.portIsOpen('localhost', 9000, _TIMEOUT)) try: restutils.RestClient("localhost", 9000, 10)._GET("/anything") except restutils.RestClientException: # We don't care about the result pass def tearDown(self): unittest.TestCase.tearDown(self) self._daemon.stop(_TIMEOUT) self._daemon_t.join(_TIMEOUT) self.assertFalse(self._daemon_t.is_alive(), "Daemon running thread should have finished by now") self.assertFalse(utils.portIsOpen("localhost", 9000, 0), "DFMS Daemon REST interface should be off") def test_nm_starts(self): # Simplest case... self.create_daemon(master=False, noNM=False, disable_zeroconf=True) def test_mm_starts(self): # Start with the MM included self.create_daemon(master=True, noNM=False, disable_zeroconf=True) def test_nothing_starts(self): # Nothing should start now self.create_daemon(master=False, noNM=True, disable_zeroconf=True) self.assertFalse( utils.portIsOpen("localhost", constants.NODE_DEFAULT_REST_PORT, 0), "NM started but it should not have" ) self.assertFalse( utils.portIsOpen("localhost", constants.MASTER_DEFAULT_REST_PORT, 0), "NM started but it should not have" ) def test_start_master_via_rest(self): self.create_daemon(master=False, noNM=False, disable_zeroconf=True) # Check that the master starts self._start("master", httplib.OK) self.assertTrue( utils.portIsOpen("localhost", constants.MASTER_DEFAULT_REST_PORT, _TIMEOUT), "The MM did not start successfully", ) def test_zeroconf_discovery(self): self.create_daemon(master=True, noNM=False, disable_zeroconf=False) # Both managers started fine. If they zeroconf themselves correctly then # if we query the MM it should know about its nodes, which should have # one element nodes = self._get_nodes_from_master(_TIMEOUT) self.assertIsNotNone(nodes) self.assertEquals(1, len(nodes), "MasterManager didn't find the NodeManager running on the same node") def test_start_dataisland_via_rest(self): self.create_daemon(master=True, noNM=False, disable_zeroconf=False) # Both managers started fine. If they zeroconf themselves correctly then # if we query the MM it should know about its nodes, which should have # one element nodes = self._get_nodes_from_master(_TIMEOUT) self.assertIsNotNone(nodes) self.assertEquals(1, len(nodes), "MasterManager didn't find the NodeManager running on the same node") # Check that the DataIsland starts with the given nodes self._start("dataisland", httplib.OK, {"nodes": nodes}) self.assertTrue( utils.portIsOpen("localhost", constants.ISLAND_DEFAULT_REST_PORT, _TIMEOUT), "The DIM did not start successfully", ) def _start(self, manager_name, expected_code, payload=None): conn = httplib.HTTPConnection("localhost", 9000) headers = {} if payload: payload = json.dumps(payload) headers["Content-Type"] = "application/json" conn.request("POST", "/managers/%s" % (manager_name,), body=payload, headers=headers) response = conn.getresponse() self.assertEquals(expected_code, response.status, response.read()) response.close() conn.close() def _get_nodes_from_master(self, timeout): mc = MasterManagerClient() timeout_time = time.time() + timeout while time.time() < timeout_time: nodes = mc.nodes() if nodes: return nodes time.sleep(0.1)
class TestDaemon(unittest.TestCase): def create_daemon(self, *args, **kwargs): self._daemon = DfmsDaemon(*args, **kwargs) if 'noNM' not in kwargs or not kwargs['noNM']: self.assertTrue(utils.portIsOpen('localhost', constants.NODE_DEFAULT_REST_PORT, _TIMEOUT), 'The NM did not start successfully') if 'master' in kwargs and kwargs['master']: self.assertTrue(utils.portIsOpen('localhost', constants.MASTER_DEFAULT_REST_PORT, _TIMEOUT), 'The MM did not start successfully') self._daemon_t = threading.Thread(target=lambda: self._daemon.start('localhost', 9000)) self._daemon_t.start() # Wait until the daemon's server has started # We can't simply check if the port is opened, because the server binds # before it is returned to us. In some tests we don't interact with it, # and therefore the shutdown of the daemon can occur before the server # is even returned to us. This would happen because portIsOpen will # succeed with a bound server, even if we haven't serve_forever()'d it # yet. In these situations shutting down the daemon will not shut down # the http server, and therefore the test will fail when checking that # the self._daemon_t is not alive anymore # # To actually avoid this we need to do some actual HTTP talk, which will # ensure the server is actually serving requests, and therefore already # in the daemon's hand #self.assertTrue(utils.portIsOpen('localhost', 9000, _TIMEOUT)) try: restutils.RestClient('localhost', 9000, 10)._GET('/anything') except restutils.RestClientException: # We don't care about the result pass def tearDown(self): unittest.TestCase.tearDown(self) self._daemon.stop(_TIMEOUT) self._daemon_t.join(_TIMEOUT) self.assertFalse(self._daemon_t.is_alive(), "Daemon running thread should have finished by now") self.assertTrue(utils.portIsClosed('localhost', 9000, _TIMEOUT), 'DFMS Daemon REST interface should be off') def test_nm_starts(self): # Simplest case... self.create_daemon(master=False, noNM=False, disable_zeroconf=True) def test_mm_starts(self): # Start with the MM included self.create_daemon(master=True, noNM=False, disable_zeroconf=True) def test_nothing_starts(self): # Nothing should start now self.create_daemon(master=False, noNM=True, disable_zeroconf=True) self.assertFalse(utils.portIsOpen('localhost', constants.NODE_DEFAULT_REST_PORT, 0), 'NM started but it should not have') self.assertFalse(utils.portIsOpen('localhost', constants.MASTER_DEFAULT_REST_PORT, 0), 'NM started but it should not have') def test_start_master_via_rest(self): self.create_daemon(master=False, noNM=False, disable_zeroconf=True) # Check that the master starts self._start('master', httplib.OK) self.assertTrue(utils.portIsOpen('localhost', constants.MASTER_DEFAULT_REST_PORT, _TIMEOUT), 'The MM did not start successfully') def test_zeroconf_discovery(self): self.create_daemon(master=True, noNM=False, disable_zeroconf=False) # Both managers started fine. If they zeroconf themselves correctly then # if we query the MM it should know about its nodes, which should have # one element nodes = self._get_nodes_from_master(_TIMEOUT) self.assertIsNotNone(nodes) self.assertEqual(1, len(nodes), "MasterManager didn't find the NodeManager running on the same node") def test_start_dataisland_via_rest(self): self.create_daemon(master=True, noNM=False, disable_zeroconf=False) # Both managers started fine. If they zeroconf themselves correctly then # if we query the MM it should know about its nodes, which should have # one element nodes = self._get_nodes_from_master(_TIMEOUT) self.assertIsNotNone(nodes) self.assertEqual(1, len(nodes), "MasterManager didn't find the NodeManager running on the same node") # Check that the DataIsland starts with the given nodes self._start('dataisland', httplib.OK, {'nodes': nodes}) self.assertTrue(utils.portIsOpen('localhost', constants.ISLAND_DEFAULT_REST_PORT, _TIMEOUT), 'The DIM did not start successfully') def _start(self, manager_name, expected_code, payload=None): conn = httplib.HTTPConnection('localhost', 9000) headers = {} if payload: payload = json.dumps(payload) headers['Content-Type'] = 'application/json' conn.request('POST', '/managers/%s' % (manager_name,), body=payload, headers=headers) response = conn.getresponse() self.assertEqual(expected_code, response.status, response.read()) response.close() conn.close() def _get_nodes_from_master(self, timeout): mc = MasterManagerClient() timeout_time = time.time() + timeout while time.time() < timeout_time: nodes = mc.nodes() if nodes: return nodes time.sleep(0.1)