def _test_socket_listener(self, **kwargs): ''' A simple test to check that SocketListenerApps are indeed working as expected; that is, they write the data they receive into their output, and finish when the connection is closed from the client side The data flow diagram looks like this: A --> B --> C --> D ''' host = 'localhost' port = 9933 data = os.urandom(1025) a = SocketListenerApp('oid:A', 'uid:A', host=host, port=port, **kwargs) b = InMemoryDROP('oid:B', 'uid:B') c = SumupContainerChecksum('oid:C', 'uid:C') d = InMemoryDROP('oid:D', 'uid:D') a.addOutput(b) b.addConsumer(c) c.addOutput(d) # Create the socket, write, and close the connection, allowing # A to move to COMPLETED with DROPWaiterCtx(self, d, 3): # That's plenty of time a.async_execute() utils.write_to(host, port, data, 1) for drop in [a, b, c, d]: self.assertEqual(DROPStates.COMPLETED, drop.status) # Our expectations are fulfilled! bContents = droputils.allDropContents(b) dContents = int(droputils.allDropContents(d)) self.assertEqual(data, bContents) self.assertEqual(crc32(data, 0), dContents)
def test_fullRound(self): """ A test that exercises most of the REST interface exposed on top of the DataIslandManager """ sessionId = 'lala' restPort = 8888 args = ['--port', str(restPort), '-N', hostname, '-qqq'] dimProcess = tool.start_process('dim', args) with testutils.terminating(dimProcess, 10): # Wait until the REST server becomes alive self.assertTrue(utils.portIsOpen('localhost', restPort, 10), "REST server didn't come up in time") # The DIM is still empty sessions = testutils.get(self, '/sessions', restPort) self.assertEqual(0, len(sessions)) dimStatus = testutils.get(self, '', restPort) self.assertEqual(1, len(dimStatus['hosts'])) self.assertEqual(hostname, dimStatus['hosts'][0]) self.assertEqual(0, len(dimStatus['sessionIds'])) # Create a session and check it exists testutils.post(self, '/sessions', restPort, '{"sessionId":"%s"}' % (sessionId)) sessions = testutils.get(self, '/sessions', restPort) self.assertEqual(1, len(sessions)) self.assertEqual(sessionId, sessions[0]['sessionId']) self.assertDictEqual({hostname: SessionStates.PRISTINE}, sessions[0]['status']) # Add this complex graph spec to the session # The UID of the two leaf nodes of this complex.js graph are T and S # Since the original complexGraph doesn't have node information # we need to add it manually before submitting -- otherwise it will # get rejected by the DIM. with pkg_resources.resource_stream( 'test', 'graphs/complex.js') as f: # @UndefinedVariable complexGraphSpec = json.load(codecs.getreader('utf-8')(f)) for dropSpec in complexGraphSpec: dropSpec['node'] = hostname testutils.post(self, '/sessions/%s/graph/append' % (sessionId), restPort, json.dumps(complexGraphSpec)) self.assertEqual({hostname: SessionStates.BUILDING}, testutils.get(self, '/sessions/%s/status' % (sessionId), restPort)) # Now we deploy the graph... testutils.post(self, '/sessions/%s/deploy' % (sessionId), restPort, "completed=SL_A,SL_B,SL_C,SL_D,SL_K", mimeType='application/x-www-form-urlencoded') self.assertEqual({hostname: SessionStates.RUNNING}, testutils.get(self, '/sessions/%s/status' % (sessionId), restPort)) # ...and write to all 5 root nodes that are listening in ports # starting at 1111 msg = os.urandom(10) for i in range(5): utils.write_to( 'localhost', 1111 + i, msg, 2), "Couldn't write data to localhost:%d" % (1111 + i) # Wait until the graph has finished its execution. We'll know # it finished by polling the status of the session while SessionStates.RUNNING in testutils.get( self, '/sessions/%s/status' % (sessionId), restPort).values(): time.sleep(0.2) self.assertEqual({hostname: SessionStates.FINISHED}, testutils.get(self, '/sessions/%s/status' % (sessionId), restPort)) testutils.delete(self, '/sessions/%s' % (sessionId), restPort) sessions = testutils.get(self, '/sessions', restPort) self.assertEqual(0, len(sessions))
def test_fullRound(self): """ A test that exercises most of the REST interface exposed on top of the DataIslandManager """ sessionId = "lala" restPort = 8989 # don't interfere with EAGLE default port args = ["--port", str(restPort), "-N", hostname, "-qqq"] dimProcess = tool.start_process("dim", args) with testutils.terminating(dimProcess, timeout=10): # Wait until the REST server becomes alive self.assertTrue( utils.portIsOpen("localhost", restPort, timeout=10), "REST server didn't come up in time", ) # The DIM is still empty sessions = testutils.get(self, "/sessions", restPort) self.assertEqual(0, len(sessions)) dimStatus = testutils.get(self, "", restPort) self.assertEqual(1, len(dimStatus["hosts"])) self.assertEqual(hostname, dimStatus["hosts"][0]) self.assertEqual(0, len(dimStatus["sessionIds"])) # Create a session and check it exists testutils.post( self, "/sessions", restPort, '{"sessionId":"%s"}' % (sessionId) ) sessions = testutils.get(self, "/sessions", restPort) self.assertEqual(1, len(sessions)) self.assertEqual(sessionId, sessions[0]["sessionId"]) self.assertDictEqual( {hostname: SessionStates.PRISTINE}, sessions[0]["status"] ) # Add this complex graph spec to the session # The UID of the two leaf nodes of this complex.js graph are T and S # Since the original complexGraph doesn't have node information # we need to add it manually before submitting -- otherwise it will # get rejected by the DIM. with pkg_resources.resource_stream( "test", "graphs/complex.js" ) as f: # @UndefinedVariable complexGraphSpec = json.load(codecs.getreader("utf-8")(f)) logger.debug(f"Loaded graph: {f}") for dropSpec in complexGraphSpec: dropSpec["node"] = hostname testutils.post( self, "/sessions/%s/graph/append" % (sessionId), restPort, json.dumps(complexGraphSpec), ) self.assertEqual( {hostname: SessionStates.BUILDING}, testutils.get(self, "/sessions/%s/status" % (sessionId), restPort), ) # Now we deploy the graph... testutils.post( self, "/sessions/%s/deploy" % (sessionId), restPort, "completed=SL_A,SL_B,SL_C,SL_D,SL_K", mimeType="application/x-www-form-urlencoded", ) self.assertEqual( {hostname: SessionStates.RUNNING}, testutils.get(self, "/sessions/%s/status" % (sessionId), restPort), ) # ...and write to all 5 root nodes that are listening in ports # starting at 1111 msg = os.urandom(10) for i in range(5): utils.write_to( "localhost", 1111 + i, msg, 2 ), "Couldn't write data to localhost:%d" % (1111 + i) # Wait until the graph has finished its execution. We'll know # it finished by polling the status of the session while ( SessionStates.RUNNING in testutils.get( self, "/sessions/%s/status" % (sessionId), restPort ).values() ): time.sleep(0.2) self.assertEqual( {hostname: SessionStates.FINISHED}, testutils.get(self, "/sessions/%s/status" % (sessionId), restPort), ) testutils.delete(self, "/sessions/%s" % (sessionId), restPort) sessions = testutils.get(self, "/sessions", restPort) self.assertEqual(0, len(sessions))