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. | |
9 import sys | |
10 sys.path.append('../../') | |
11 | |
12 import argparse | 8 import argparse |
13 import logging | 9 import logging |
| 10 import os |
| 11 import sys |
14 import time | 12 import time |
15 import xmlrpclib | 13 import xmlrpclib |
16 | 14 |
17 import test_controller | 15 # Map the testing directory so we can import legion.legion_test. |
| 16 TESTING_DIR = os.path.join( |
| 17 os.path.dirname(os.path.abspath(__file__)), |
| 18 '..', '..', '..', '..', 'testing') |
| 19 sys.path.append(TESTING_DIR) |
| 20 |
| 21 from legion import legion_test_case |
18 | 22 |
19 | 23 |
20 class ExampleTestController(test_controller.TestController): | 24 class ExampleTestController(legion_test_case.TestCase): |
21 """An example controller using the remote subprocess functions.""" | 25 """An example controller using the remote subprocess functions.""" |
22 | 26 |
23 def __init__(self): | 27 @classmethod |
24 super(ExampleTestController, self).__init__() | 28 def setUpClass(cls): |
25 self.task = None | |
26 | |
27 def SetUp(self): | |
28 """Creates the task machine and waits until it connects.""" | 29 """Creates the task machine and waits until it connects.""" |
29 parser = argparse.ArgumentParser() | 30 parser = argparse.ArgumentParser() |
30 parser.add_argument('--task-hash') | 31 parser.add_argument('--task-hash') |
| 32 parser.add_argument('--os', default='Ubuntu-14.04') |
31 args, _ = parser.parse_known_args() | 33 args, _ = parser.parse_known_args() |
32 | 34 |
33 self.task = self.CreateNewTask( | 35 cls.task = cls.CreateTask( |
34 isolated_hash=args.task_hash, | 36 isolated_hash=args.task_hash, |
35 dimensions={'os': 'Ubuntu-14.04', 'pool': 'Chromoting'}, | 37 dimensions={'os': args.os}, |
36 idle_timeout_secs=90, connection_timeout_secs=90, | 38 idle_timeout_secs=90, |
| 39 connection_timeout_secs=90, |
37 verbosity=logging.DEBUG) | 40 verbosity=logging.DEBUG) |
38 self.task.Create() | 41 cls.task.Create() |
39 self.task.WaitForConnection() | 42 cls.task.WaitForConnection() |
40 | 43 |
41 def RunTest(self): | 44 def testMultipleProcesses(self): |
42 """Main method to run the test code.""" | 45 """Tests that processes can be run and controlled simultaneously.""" |
43 self.TestLs() | 46 start = time.time() |
44 self.TestTerminate() | 47 logging.info('Starting "sleep 10" and "sleep 20"') |
45 self.TestMultipleProcesses() | 48 sleep10 = self.task.Process(['sleep', '10']) |
| 49 sleep20 = self.task.Process(['sleep', '20']) |
46 | 50 |
47 def TestMultipleProcesses(self): | 51 logging.info('Waiting for sleep 10 to finish and verifying timing') |
| 52 sleep10.Wait() |
| 53 elapsed = time.time() - start |
| 54 self.assertGreaterEqual(elapsed, 10) |
| 55 self.assertLess(elapsed, 11) |
| 56 |
| 57 logging.info('Waiting for sleep 20 to finish and verifying timing') |
| 58 sleep20.Wait() |
| 59 elapsed = time.time() - start |
| 60 self.assertGreaterEqual(elapsed, 20) |
| 61 |
| 62 sleep10.Delete() |
| 63 sleep20.Delete() |
| 64 |
| 65 def testTerminate(self): |
| 66 """Tests that a process can be correctly terminated.""" |
48 start = time.time() | 67 start = time.time() |
49 | 68 |
50 sleep20 = self.task.rpc.subprocess.Process(['sleep', '20']) | 69 logging.info('Starting "sleep 20"') |
51 self.task.rpc.subprocess.Start(sleep20) | 70 sleep20 = self.task.Process(['sleep', '20']) |
52 sleep10 = self.task.rpc.subprocess.Process(['sleep', '10']) | 71 logging.info('Calling Terminate()') |
53 self.task.rpc.subprocess.Start(sleep10) | 72 sleep20.Terminate() |
54 | |
55 self.task.rpc.subprocess.Wait(sleep10) | |
56 elapsed = time.time() - start | |
57 assert elapsed >= 10 and elapsed < 11 | |
58 | |
59 self.task.rpc.subprocess.Wait(sleep20) | |
60 elapsed = time.time() - start | |
61 assert elapsed >= 20 | |
62 | |
63 self.task.rpc.subprocess.Delete(sleep20) | |
64 self.task.rpc.subprocess.Delete(sleep10) | |
65 | |
66 def TestTerminate(self): | |
67 start = time.time() | |
68 proc = self.task.rpc.subprocess.Process(['sleep', '20']) | |
69 self.task.rpc.subprocess.Start(proc) | |
70 self.task.rpc.subprocess.Terminate(proc) | |
71 try: | 73 try: |
72 self.task.rpc.subprocess.Wait(proc) | 74 logging.info('Trying to wait for sleep 20 to complete') |
| 75 sleep20.Wait() |
73 except xmlrpclib.Fault: | 76 except xmlrpclib.Fault: |
74 pass | 77 pass |
75 finally: | 78 finally: |
76 self.task.rpc.subprocess.Delete(proc) | 79 sleep20.Delete() |
77 assert time.time() - start < 20 | 80 logging.info('Checking to make sure sleep 20 was actually terminated') |
| 81 self.assertLess(time.time() - start, 20) |
78 | 82 |
79 def TestLs(self): | 83 def testLs(self): |
80 proc = self.task.rpc.subprocess.Process(['ls']) | 84 """Tests that the returned results from a process are correct.""" |
81 self.task.rpc.subprocess.Start(proc) | 85 logging.info('Calling "ls"') |
82 self.task.rpc.subprocess.Wait(proc) | 86 ls = self.task.Process(['ls']) |
83 assert self.task.rpc.subprocess.GetReturncode(proc) == 0 | 87 logging.info('Trying to wait for ls to complete') |
84 assert 'task.isolate' in self.task.rpc.subprocess.ReadStdout(proc) | 88 ls.Wait() |
85 self.task.rpc.subprocess.Delete(proc) | 89 logging.info('Checking that ls completed and returned the correct results') |
| 90 self.assertEqual(ls.GetReturncode(), 0) |
| 91 self.assertIn('task.isolate', ls.ReadStdout()) |
86 | 92 |
87 | 93 |
88 if __name__ == '__main__': | 94 if __name__ == '__main__': |
89 ExampleTestController().RunController() | 95 legion_test_case.main() |
OLD | NEW |