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 simple host test module. | 6 """A simple host test module. |
7 | 7 |
8 This module runs on the host machine and is responsible for creating 2 | 8 This module runs on the host machine and is responsible for creating 2 |
9 task machines, waiting for them, and running RPC calls on them. | 9 task machines, waiting for them, and running RPC calls on them. |
10 """ | 10 """ |
11 | 11 |
12 # Map the legion directory so we can import the host controller. | |
13 import sys | |
14 sys.path.append('../../') | |
15 | |
16 import argparse | 12 import argparse |
17 import logging | 13 import logging |
| 14 import os |
| 15 import sys |
18 import time | 16 import time |
19 | 17 |
20 import test_controller | 18 # Map the testing directory so we can import legion.legion_test. |
| 19 TESTING_DIR = os.path.join( |
| 20 os.path.dirname(os.path.abspath(__file__)), |
| 21 '..', '..', '..', '..', 'testing') |
| 22 sys.path.append(TESTING_DIR) |
| 23 |
| 24 from legion import legion_test_case |
21 | 25 |
22 | 26 |
23 class ExampleTestController(test_controller.TestController): | 27 class ExampleTestController(legion_test_case.TestCase): |
24 """A simple example controller for a test.""" | 28 """A simple example controller for a test.""" |
25 | 29 |
26 def __init__(self): | 30 @classmethod |
27 super(ExampleTestController, self).__init__() | 31 def CreateTestTask(cls): |
28 self.task1 = None | 32 """Create a new task.""" |
29 self.task2 = None | 33 parser = argparse.ArgumentParser() |
| 34 parser.add_argument('--task-hash') |
| 35 parser.add_argument('--os', default='Ubuntu-14.04') |
| 36 args, _ = parser.parse_known_args() |
30 | 37 |
31 def CreateTask(self, isolated_hash): | 38 task = cls.CreateTask( |
32 """Create a task object and set the proper values.""" | 39 isolated_hash=args.task_hash, |
33 task = self.CreateNewTask( | 40 dimensions={'os': args.os}, |
34 isolated_hash=isolated_hash, | 41 idle_timeout_secs=90, |
35 dimensions={'os': 'Ubuntu-14.04', 'pool': 'Legion'}, priority=200, | 42 connection_timeout_secs=90, |
36 idle_timeout_secs=90, connection_timeout_secs=90, | 43 verbosity=logging.DEBUG) |
37 verbosity=logging.INFO, | |
38 run_id=1) | |
39 task.Create() | 44 task.Create() |
40 return task | 45 return task |
41 | 46 |
42 def SetUp(self): | 47 @classmethod |
43 """Create the task machines and wait until they connect. | 48 def setUpClass(cls): |
| 49 """Creates the task machines and waits until they connect.""" |
| 50 cls.task1 = cls.CreateTestTask() |
| 51 cls.task2 = cls.CreateTestTask() |
| 52 cls.task1.WaitForConnection() |
| 53 cls.task2.WaitForConnection() |
44 | 54 |
45 In this call the actual creation of the task machines is done in parallel | 55 def testCallEcho(self): |
46 by the system. The WaitForConnect calls are performed in series but will | 56 """Tests rpc.Echo on a task.""" |
47 return as soon as the tasks connect. | 57 logging.info('Calling Echo on %s', self.task2.name) |
48 """ | 58 self.assertEqual(self.task2.rpc.Echo('foo'), 'echo foo') |
49 parser = argparse.ArgumentParser() | |
50 parser.add_argument('--task-hash') | |
51 args, _ = parser.parse_known_args() | |
52 | 59 |
53 self.task1 = self.CreateTask(args.task_hash) | 60 def testLaunchTaskBinary(self): |
54 self.task2 = self.CreateTask(args.task_hash) | 61 """Call task_test.py 'name' on the tasks.""" |
55 self.task1.WaitForConnection() | 62 self.VerifyTaskBinaryLaunched(self.task1) |
56 self.task2.WaitForConnection() | 63 self.VerifyTaskBinaryLaunched(self.task2) |
57 | 64 |
58 def RunTest(self): | 65 def VerifyTaskBinaryLaunched(self, task): |
59 """Main method to run the test code.""" | 66 logging.info( |
60 self.CallEcho(self.task1) | 67 'Calling Process to run "./task_test.py %s"', task.name) |
61 self.CallEcho(self.task2) | 68 proc = task.Process(['./task_test.py', task.name]) |
62 self.CallTaskTest(self.task1) | 69 proc.Wait() |
63 self.CallTaskTest(self.task2) | 70 self.assertEqual(proc.GetReturncode(), 0) |
64 | 71 self.assertIn(task.name, proc.ReadStdout()) |
65 def CallEcho(self, task): | 72 self.assertEquals(proc.ReadStderr(), '') |
66 """Call rpc.Echo on a task.""" | 73 proc.Delete() |
67 logging.info('Calling Echo on %s', task.name) | |
68 logging.info(task.rpc.Echo(task.name)) | |
69 | |
70 def CallTaskTest(self, task): | |
71 """Call task_test.py name on a task.""" | |
72 logging.info('Calling Subprocess to run "./task_test.py %s"', task.name) | |
73 proc = task.rpc.subprocess.Popen(['./task_test.py', task.name]) | |
74 task.rpc.subprocess.Wait(proc) | |
75 retcode = task.rpc.subprocess.GetReturncode(proc) | |
76 stdout = task.rpc.subprocess.ReadStdout(proc) | |
77 stderr = task.rpc.subprocess.ReadStderr(proc) | |
78 logging.info('retcode: %s, stdout: %s, stderr: %s', retcode, stdout, stderr) | |
79 | 74 |
80 | 75 |
81 if __name__ == '__main__': | 76 if __name__ == '__main__': |
82 ExampleTestController().RunController() | 77 legion_test_case.main() |
OLD | NEW |