diff options
author | mmeade <mmeade@chromium.org> | 2015-07-28 09:20:23 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-28 16:21:24 +0000 |
commit | 9c045d1be60a44dbd926bb6535dc8630d47fd623 (patch) | |
tree | d6e2cb0881ea448a187c748975f6669477835e8a /testing/legion | |
parent | bfa736ea015575cff0b21d4249ceb9d45d4df254 (diff) | |
download | chromium_src-9c045d1be60a44dbd926bb6535dc8630d47fd623.zip chromium_src-9c045d1be60a44dbd926bb6535dc8630d47fd623.tar.gz chromium_src-9c045d1be60a44dbd926bb6535dc8630d47fd623.tar.bz2 |
Adding an http_test example test to accompany the How To doc.
BUG=
Review URL: https://codereview.chromium.org/1260743004
Cr-Commit-Position: refs/heads/master@{#340698}
Diffstat (limited to 'testing/legion')
-rw-r--r-- | testing/legion/examples/http_example/http_client.isolate | 18 | ||||
-rw-r--r-- | testing/legion/examples/http_example/http_client.py | 28 | ||||
-rw-r--r-- | testing/legion/examples/http_example/http_server.isolate | 18 | ||||
-rw-r--r-- | testing/legion/examples/http_example/http_server.py | 44 | ||||
-rw-r--r-- | testing/legion/examples/http_example/http_test.isolate | 18 | ||||
-rw-r--r-- | testing/legion/examples/http_example/http_test.py | 74 | ||||
-rw-r--r-- | testing/legion/rpc_methods.py | 9 |
7 files changed, 209 insertions, 0 deletions
diff --git a/testing/legion/examples/http_example/http_client.isolate b/testing/legion/examples/http_example/http_client.isolate new file mode 100644 index 0000000..abba454 --- /dev/null +++ b/testing/legion/examples/http_example/http_client.isolate @@ -0,0 +1,18 @@ +# Copyright 2015 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'includes': [ + '../../../legion/legion.isolate' + ], + 'variables': { + 'command': [ + 'python', + '../../../legion/run_task.py', + ], + 'files': [ + 'http_client.py', + ], + }, +} diff --git a/testing/legion/examples/http_example/http_client.py b/testing/legion/examples/http_example/http_client.py new file mode 100644 index 0000000..830ae62 --- /dev/null +++ b/testing/legion/examples/http_example/http_client.py @@ -0,0 +1,28 @@ +# Copyright 2015 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import argparse +import httplib +import sys + + +def GetArgs(): + """Returns the specified command line args.""" + parser = argparse.ArgumentParser() + parser.add_argument('--server', required=True) + parser.add_argument('--port', required=True, type=int) + return parser.parse_args() + + +def main(): + """Get the webpage and assert the text == 'SUCCESS!'.""" + args = GetArgs() + conn = httplib.HTTPConnection(args.server, args.port) + conn.request('GET', '/') + response = conn.getresponse() + assert response.read() == 'SUCCESS!' + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/testing/legion/examples/http_example/http_server.isolate b/testing/legion/examples/http_example/http_server.isolate new file mode 100644 index 0000000..0fc3230 --- /dev/null +++ b/testing/legion/examples/http_example/http_server.isolate @@ -0,0 +1,18 @@ +# Copyright 2015 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'includes': [ + '../../../legion/legion.isolate' + ], + 'variables': { + 'command': [ + 'python', + '../../../legion/run_task.py', + ], + 'files': [ + 'http_server.py', + ], + }, +} diff --git a/testing/legion/examples/http_example/http_server.py b/testing/legion/examples/http_example/http_server.py new file mode 100644 index 0000000..deda879 --- /dev/null +++ b/testing/legion/examples/http_example/http_server.py @@ -0,0 +1,44 @@ +# Copyright 2015 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import argparse +import SimpleHTTPServer +import SocketServer +import sys +import threading +import time + + +class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): + + def do_GET(self): + self.wfile.write('SUCCESS!') + + +def GetArgs(): + """Returns the specified command line args.""" + parser = argparse.ArgumentParser() + parser.add_argument('--port', required=True, type=int) + parser.add_argument('--timeout', type=int, default=60) + return parser.parse_args() + + +def main(): + """Run a webserver until the process is killed.""" + server = None + args = GetArgs() + try: + server = SocketServer.TCPServer(('', args.port), Handler) + thread = threading.Thread(target=server.serve_forever) + thread.start() + start = time.time() + while time.time() < start + args.timeout: + time.sleep(1) + finally: + if server: + server.shutdown() + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/testing/legion/examples/http_example/http_test.isolate b/testing/legion/examples/http_example/http_test.isolate new file mode 100644 index 0000000..605551b --- /dev/null +++ b/testing/legion/examples/http_example/http_test.isolate @@ -0,0 +1,18 @@ +# Copyright 2015 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'includes': [ + '../../../legion/legion.isolate' + ], + 'variables': { + 'command': [ + 'python', + 'http_test.py', + ], + 'files': [ + 'http_test.py', + ], + }, +} diff --git a/testing/legion/examples/http_example/http_test.py b/testing/legion/examples/http_example/http_test.py new file mode 100644 index 0000000..fe6e767 --- /dev/null +++ b/testing/legion/examples/http_example/http_test.py @@ -0,0 +1,74 @@ +# Copyright 2015 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import argparse +import os +import sys + +TESTING_DIR = os.path.join( + os.path.dirname(os.path.abspath(__file__)), '..', '..', '..') +sys.path.append(TESTING_DIR) + +from legion import legion_test_case + + +class HttpTest(legion_test_case.TestCase): + """Example HTTP test case.""" + + @classmethod + def GetArgs(cls): + """Get command line args.""" + parser = argparse.ArgumentParser() + parser.add_argument('--http-server') + parser.add_argument('--http-client') + parser.add_argument('--os', default='Ubuntu-14.04') + args, _ = parser.parse_known_args() + return args + + @classmethod + def CreateTask(cls, name, task_hash, os_type): + """Create a new task.""" + #pylint: disable=unexpected-keyword-arg,no-value-for-parameter + task = super(HttpTest, cls).CreateTask( + name=name, + isolated_hash=task_hash, + dimensions={'os': os_type}) + task.Create() + return task + + @classmethod + def setUpClass(cls): + """Creates the task machines and waits until they connect.""" + args = cls.GetArgs() + cls.http_server = cls.CreateTask( + 'http_server', args.http_server, args.os) + cls.http_client = cls.CreateTask( + 'http_client', args.http_client, args.os) + cls.http_server.WaitForConnection() + cls.http_client.WaitForConnection() + + def testHttpWorks(self): + server_port = '8080' + server_proc = None + client_proc = None + try: + server_ip = self.http_server.rpc.GetIpAddress() + server_proc = self.http_server.Process( + ['python', 'http_server.py', '--port', server_port]) + client_proc = self.http_client.Process( + ['python', 'http_client.py', '--server', server_ip, + '--port', server_port]) + client_proc.Wait() + self.assertEqual(client_proc.GetReturncode(), 0) + finally: + if server_proc: + server_proc.Kill() + server_proc.Delete() + if client_proc: + client_proc.Kill() + client_proc.Delete() + + +if __name__ == '__main__': + legion_test_case.main() diff --git a/testing/legion/rpc_methods.py b/testing/legion/rpc_methods.py index aaab7e0..b846269 100644 --- a/testing/legion/rpc_methods.py +++ b/testing/legion/rpc_methods.py @@ -6,6 +6,7 @@ import logging import os +import socket import sys import threading @@ -77,3 +78,11 @@ class RPCMethods(object): def ListDir(self, path): """Returns the results of os.listdir.""" return os.listdir(path) + + def GetIpAddress(self): + """Returns the local IPv4 address.""" + return socket.gethostbyname(socket.gethostname()) + + def GetHostname(self): + """Returns the hostname.""" + return socket.gethostname() |