OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """A host test module demonstrating interacting with remote subprocesses.""" | 6 """A host test module demonstrating interacting with remote subprocesses.""" |
7 | 7 |
8 # Map the legion directory so we can import the host controller. | 8 # Map the legion directory so we can import the host controller. |
9 import sys | 9 import sys |
10 sys.path.append('../../') | 10 sys.path.append('../../') |
11 | 11 |
12 import logging | 12 import logging |
13 import time | 13 import time |
14 import xmlrpclib | 14 import xmlrpclib |
15 | 15 |
16 import test_controller | 16 import host_controller |
17 | 17 |
18 | 18 |
19 class ExampleTestController(test_controller.TestController): | 19 class ExampleController(host_controller.HostController): |
20 """An example controller using the remote subprocess functions.""" | 20 """An example controller using the remote subprocess functions.""" |
21 | 21 |
22 def __init__(self): | 22 def __init__(self): |
23 super(ExampleTestController, self).__init__() | 23 super(ExampleController, self).__init__() |
24 self.task = None | 24 self.client = None |
25 | 25 |
26 def SetUp(self): | 26 def SetUp(self): |
27 """Creates the task machine and waits until it connects.""" | 27 """Creates the client machine and waits until it connects.""" |
28 self.task = self.CreateNewTask( | 28 self.client = self.NewClient( |
29 isolate_file='task.isolate', | 29 isolate_file='client.isolate', |
30 config_vars={'multi_machine': '1'}, | 30 config_vars={'multi_machine': '1'}, |
31 dimensions={'os': 'legion-linux'}, | 31 dimensions={'os': 'legion-linux'}, |
32 idle_timeout_secs=90, connection_timeout_secs=90, | 32 idle_timeout_secs=90, connection_timeout_secs=90, |
33 verbosity=logging.DEBUG) | 33 verbosity=logging.DEBUG) |
34 self.task.Create() | 34 self.client.Create() |
35 self.task.WaitForConnection() | 35 self.client.WaitForConnection() |
36 | 36 |
37 def RunTest(self): | 37 def Task(self): |
38 """Main method to run the test code.""" | 38 """Main method to run the task code.""" |
39 self.TestLs() | 39 self.TestLs() |
40 self.TestTerminate() | 40 self.TestTerminate() |
41 self.TestMultipleProcesses() | 41 self.TestMultipleProcesses() |
42 | 42 |
43 def TestMultipleProcesses(self): | 43 def TestMultipleProcesses(self): |
44 start = time.time() | 44 start = time.time() |
45 | 45 |
46 sleep20 = self.task.rpc.subprocess.Popen(['sleep', '20']) | 46 sleep20 = self.client.rpc.subprocess.Popen(['sleep', '20']) |
47 sleep10 = self.task.rpc.subprocess.Popen(['sleep', '10']) | 47 sleep10 = self.client.rpc.subprocess.Popen(['sleep', '10']) |
48 | 48 |
49 self.task.rpc.subprocess.Wait(sleep10) | 49 self.client.rpc.subprocess.Wait(sleep10) |
50 elapsed = time.time() - start | 50 elapsed = time.time() - start |
51 assert elapsed >= 10 and elapsed < 11 | 51 assert elapsed >= 10 and elapsed < 11 |
52 | 52 |
53 self.task.rpc.subprocess.Wait(sleep20) | 53 self.client.rpc.subprocess.Wait(sleep20) |
54 elapsed = time.time() - start | 54 elapsed = time.time() - start |
55 assert elapsed >= 20 | 55 assert elapsed >= 20 |
56 | 56 |
57 self.task.rpc.subprocess.Delete(sleep20) | 57 self.client.rpc.subprocess.Delete(sleep20) |
58 self.task.rpc.subprocess.Delete(sleep10) | 58 self.client.rpc.subprocess.Delete(sleep10) |
59 | 59 |
60 def TestTerminate(self): | 60 def TestTerminate(self): |
61 start = time.time() | 61 start = time.time() |
62 proc = self.task.rpc.subprocess.Popen(['sleep', '20']) | 62 proc = self.client.rpc.subprocess.Popen(['sleep', '20']) |
63 self.task.rpc.subprocess.Terminate(proc) # Implicitly deleted | 63 self.client.rpc.subprocess.Terminate(proc) # Implicitly deleted |
64 try: | 64 try: |
65 self.task.rpc.subprocess.Wait(proc) | 65 self.client.rpc.subprocess.Wait(proc) |
66 except xmlrpclib.Fault: | 66 except xmlrpclib.Fault: |
67 pass | 67 pass |
68 assert time.time() - start < 20 | 68 assert time.time() - start < 20 |
69 | 69 |
70 def TestLs(self): | 70 def TestLs(self): |
71 proc = self.task.rpc.subprocess.Popen(['ls']) | 71 proc = self.client.rpc.subprocess.Popen(['ls']) |
72 self.task.rpc.subprocess.Wait(proc) | 72 self.client.rpc.subprocess.Wait(proc) |
73 assert self.task.rpc.subprocess.GetReturncode(proc) == 0 | 73 assert self.client.rpc.subprocess.GetReturncode(proc) == 0 |
74 assert 'task.isolate' in self.task.rpc.subprocess.ReadStdout(proc) | 74 assert 'client.isolate' in self.client.rpc.subprocess.ReadStdout(proc) |
75 self.task.rpc.subprocess.Delete(proc) | 75 self.client.rpc.subprocess.Delete(proc) |
76 | 76 |
77 | 77 |
78 if __name__ == '__main__': | 78 if __name__ == '__main__': |
79 ExampleTestController().RunController() | 79 ExampleController().RunController() |
OLD | NEW |