1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
12 TESTING_DIR
= os
.path
.join(
13 os
.path
.dirname(os
.path
.abspath(__file__
)), '..', '..', '..')
14 sys
.path
.append(TESTING_DIR
)
16 from legion
import legion_test_case
19 class HttpTest(legion_test_case
.TestCase
):
20 """Example HTTP test case."""
24 """Get command line args."""
25 parser
= argparse
.ArgumentParser()
26 parser
.add_argument('--http-server')
27 parser
.add_argument('--http-client')
28 parser
.add_argument('--os', default
='Ubuntu-14.04')
29 args
, _
= parser
.parse_known_args()
33 def CreateTask(cls
, name
, task_hash
, os_type
):
34 """Create a new task."""
35 #pylint: disable=unexpected-keyword-arg,no-value-for-parameter
36 #pylint: disable=arguments-differ
37 task
= super(HttpTest
, cls
).CreateTask(
39 isolated_hash
=task_hash
,
40 dimensions
={'os': os_type
})
46 """Creates the task machines and waits until they connect."""
48 cls
.http_server
= cls
.CreateTask(
49 'http_server', args
.http_server
, args
.os
)
50 cls
.http_client
= cls
.CreateTask(
51 'http_client', args
.http_client
, args
.os
)
52 cls
.http_server
.WaitForConnection()
53 cls
.http_client
.WaitForConnection()
55 def CanConnectToServerPort(self
, server_port
):
56 """Connect to a port on the http_server.
59 True if the connection succeeded, False otherwise.
62 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
63 s
.connect((self
.http_server
.ip_address
, server_port
))
68 def FindOpenPortOnServer(self
):
69 """Find an open port on the server and return it.
72 The value of an open port.
74 for server_port
in xrange(2000, 20000):
75 if not self
.CanConnectToServerPort(server_port
):
77 self
.fail('Unable to find an open port on the server.')
79 def StartServer(self
, server_port
):
80 """Starts the http_server process.
86 timeout
= time
.time() + 5
87 while timeout
> time
.time():
88 if self
.CanConnectToServerPort(server_port
):
90 self
.fail('Server process failed to start')
93 self
.http_server
.executable
,
95 '--port', str(server_port
)
97 proc
= self
.http_server
.Process(cmd
)
101 def StartClient(self
, server_port
):
102 """Starts the http_client process.
108 self
.http_client
.executable
,
110 '--server', self
.http_server
.ip_address
,
111 '--port', str(server_port
)
113 return self
.http_client
.Process(cmd
)
115 def testHttpWorks(self
):
116 """Tests that the client process can talk to the server process."""
120 server_port
= self
.FindOpenPortOnServer()
121 logging
.info('Starting server at %s:%s', self
.http_server
.ip_address
,
123 server_proc
= self
.StartServer(server_port
)
124 logging
.info('Connecting to server at %s:%s', self
.http_server
.ip_address
,
126 client_proc
= self
.StartClient(server_port
)
128 logging
.info('client_proc.stdout: %s', client_proc
.ReadStdout())
129 logging
.info('client_proc.stderr: %s', client_proc
.ReadStderr())
130 self
.assertEqual(client_proc
.GetReturncode(), 0)
140 if __name__
== '__main__':
141 legion_test_case
.main()