diff --git a/samples-and-tests/i-am-a-developer/tests.py b/samples-and-tests/i-am-a-developer/tests.py index b3211d76d3..cb79c4f76b 100755 --- a/samples-and-tests/i-am-a-developer/tests.py +++ b/samples-and-tests/i-am-a-developer/tests.py @@ -16,13 +16,14 @@ # --- TESTS +DEFAULTS = { + 'host': '127.0.0.1', + 'http.port': '9001', +} class IamADeveloper(unittest.TestCase): - DEFAULTS = { - 'host': '127.0.0.1', - 'http.port': '9001', - } + play = None @unittest.skip def testSSLConfig(self): @@ -48,7 +49,7 @@ def testSSLConfig(self): insert(app, "app/controllers/Application.java", 13, ' Logger.info("I am ssl secured!");') edit(app, "conf/application.conf", 32, 'http.port=-1') - edit(app, "conf/application.conf", 33, 'https.port=' + self.DEFAULTS['http.port']) + edit(app, "conf/application.conf", 33, 'https.port=' + DEFAULTS['http.port']) edit(app, "conf/application.conf", 232, 'play.ssl.netty.pipeline = play.server.FlashPolicyHandler,org.jboss.netty.handler.codec.http.HttpRequestDecoder,play.server.StreamChunkAggregator,org.jboss.netty.handler.codec.http.HttpResponseEncoder,org.jboss.netty.handler.codec.http.HttpContentCompressor,org.jboss.netty.handler.stream.ChunkedWriteHandler,play.server.ssl.SslPlayHandler') create(app, 'conf/host.key') @@ -145,7 +146,7 @@ def testSSLConfig(self): step("Send request to https") browser = mechanize.Browser() - response = browser.open('https://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + response = browser.open('https://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) step("check that ssl message is logged") self.assertTrue(waitFor(self.play, 'I am ssl secured!')) @@ -169,8 +170,7 @@ def testSSLConfig(self): step("Send request to https") - browser = mechanize.Browser() - response = browser.open('https://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + browserOpen('https://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) step("check that ssl message is logged") self.assertTrue(waitFor(self.play, 'I am ssl secured!')) @@ -196,7 +196,7 @@ def testLogLevelsAndLog4jConfig(self): self.play.wait() app = '%s/loglevelsapp' % self.working_directory - edit(app, "conf/application.conf", 32, 'http.port=' + self.DEFAULTS['http.port']) + edit(app, "conf/application.conf", 32, 'http.port=' + DEFAULTS['http.port']) # inserting some log-statements in our controller insert(app, "app/controllers/Application.java", 13, ' Logger.debug("I am a debug message");') @@ -210,14 +210,11 @@ def testLogLevelsAndLog4jConfig(self): with callPlay(self, ['run', app]) as self.play: # wait for play to be ready - self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port'])) + self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port'])) step("Send request to trigger some logging") - browser = mechanize.Browser() - browser.set_handle_robots(False) - - response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + response = browserOpen('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) step("check that only info log message is logged") self.assertTrue(waitForWithFail(self.play, 'I am an info message', 'I am a debug message')) @@ -249,12 +246,11 @@ def testLogLevelsAndLog4jConfig(self): with callPlay(self, ['run', app]) as self.play: # wait for play to be ready - self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port'])) + self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port'])) step("Send request to trigger some logging") - browser = mechanize.Browser() - response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + response = browserOpen('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) step("check that both debug and info message is logged") self.assertTrue(waitFor(self.play, 'I am a debug message')) @@ -280,7 +276,7 @@ def testCreateAndRunForJobProject(self): self.play.wait() app = '%s/jobapp' % self.working_directory - edit(app, "conf/application.conf", 32, 'https.port=' + self.DEFAULTS['http.port']) + edit(app, "conf/application.conf", 32, 'https.port=' + DEFAULTS['http.port']) # create our first job - which is executed sync on startup with @OnApplicationStart createDir(app, 'app/jobs') @@ -305,12 +301,11 @@ def testCreateAndRunForJobProject(self): self.play = callPlay(self, ['run', app]) # wait for play to be ready - self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port'])) + self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port'])) step("Send request to start app") - browser = mechanize.Browser() - response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + response = browserOpen('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) step("check that job completed before processing request") self.assertTrue(waitFor(self.play, 'Job done')) @@ -329,12 +324,10 @@ def testCreateAndRunForJobProject(self): self.play = callPlay(self, ['run', app]) # wait for play to be ready - self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port'])) + self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port'])) step("Send request to start app") - - browser = mechanize.Browser() - response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + response = browserOpen('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) step("check that the request is processed before the job finishes") self.assertTrue(waitFor(self.play, 'Processing request')) @@ -379,7 +372,7 @@ def testSimpleProjectCreation(self): step('Run the newly created application') self.play = callPlay(self, ['run', app]) - self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port'])) + self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port'])) # Start a browser step('Start a browser') @@ -389,7 +382,7 @@ def testSimpleProjectCreation(self): # Open the home page step('Open the home page') - response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port']) + response = browser.open('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port']) self.assertTrue(waitFor(self.play, "Application 'YOP' is now started !")) self.assertTrue(browser.viewing_html()) self.assertTrue(browser.title() == 'Your application is ready !') @@ -402,7 +395,7 @@ def testSimpleProjectCreation(self): browser.addheaders = [("Accept-Language", "en")] response = browser.open( - 'http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'] + '/@documentation') + 'http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'] + '/@documentation') self.assertTrue(browser.viewing_html()) self.assertTrue(browser.title() == 'Play manual - Documentation') html = response.get_data() @@ -498,7 +491,7 @@ def testSimpleProjectCreation(self): self.assertTrue(html.count(b'Hello !!')) response = browser.open( - 'http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'] + '/?name=Guillaume') + 'http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'] + '/?name=Guillaume') self.assertTrue(browser.viewing_html()) self.assertTrue(browser.title() == 'Hello world app') html = response.get_data() @@ -667,7 +660,7 @@ def testSimpleProjectCreation(self): insert(app, 'conf/routes', 7, "GET /hello Hello.hello") try: - response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'] + '/hello') + response = browser.open('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'] + '/hello') self.fail() except urllib.error.HTTPError as error: self.assertTrue(browser.viewing_html()) @@ -855,53 +848,52 @@ def timeout(process): timeoutOccurred = True -def killPlay(process, http='http'): +def killPlay(process, http='http', host=DEFAULTS['host'], port=DEFAULTS['http.port'], ): print("kill %s" % process.pid) try: - urllib.request.urlopen('%s://localhost:9000/@kill' % http) + url = "{}://{}:{}/@kill".format(http, host, port) + p = subprocess.Popen(["curl", url], stdout=subprocess.PIPE) + + print(p.communicate()) + + # urllib.request.urlopen("{}://{}:{}/@kill".format(http, host, port)) + + + # print("terminate" ) + # process.terminate() + # + # print("process 1 (thread): kill process 2 (pid %s)" % process.pid) + # process.kill() + # print("process 1 (thread): close process 2 stdout pipe (fd %s)" % process.stdout.fileno()) + # + # print("stdout" ) + # if process.stdout: + # process.stdout.close() + # + # if process.stderr: + # process.stderr.close() + # + # if process.stdin: + # process.stdin.close() + # + # + # print("wait" ) + # process.wait(10) + # print("wait" ) + # process.wait() + # + # print("KILLED") + # # kill subprocess tree, because calling urllib.urlopen(f"{http}://localhost:9000/@kill") is not enough + # while True: + # if process.poll() is None: + # print("Kill Play subprocess") + # os.kill(process.pid, SIGTERM) + # process.wait(3) + # else: + # print("KILLED") + # return except: pass -# urllib.request.urlopen(f"{http}://" + 'localhost' + ':' + '9001' + '/@kill') - -# print("terminate" ) -# process.terminate() -# -# print("process 1 (thread): kill process 2 (pid %s)" % process.pid) -# process.kill() -# print("process 1 (thread): close process 2 stdout pipe (fd %s)" % process.stdout.fileno()) -# -# print("stdout" ) -# if process.stdout: -# process.stdout.close() -# -# if process.stderr: -# process.stderr.close() -# -# if process.stdin: -# process.stdin.close() -# -# -# print("wait" ) -# process.wait(10) -# print("wait" ) -# process.wait() -# # -# id = process.pid -# -# print("os.kill %s" % id ) -# -# # os.kill(id, SIGTERM) -# -# print("KILLED") -# # kill subprocess tree, because calling urllib.urlopen(f"{http}://localhost:9000/@kill") is not enough -# while True: -# if process.poll() is None: -# print("Kill Play subprocess") -# os.kill(process.pid, SIGTERM) -# process.wait(3) -# else: -# print("KILLED") -# return def step(msg): @@ -962,6 +954,13 @@ def rename(app, fro, to): os.rename(os.path.join(app, fro), os.path.join(app, to)) +def browserOpen(url): + browser = mechanize.Browser() + browser.set_handle_robots(False) + response = browser.open(url) + return response + + if __name__ == '__main__': # thanks to: https://stackoverflow.com/a/35960702/3221476 try: