summaryrefslogtreecommitdiffstats
path: root/testing/legion/test_controller.py
blob: 2703fadf09398794c1b72821610f3d4ffaf22b80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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.

"""Defines the test controller base library.

This module is the basis on which test controllers are built and executed.
"""

import logging
import sys

#pylint: disable=relative-import
import common_lib
import task_controller
import task_registration_server


class TestController(object):
  """The base test controller class."""

  def __init__(self):
    self._registration_server = (
        task_registration_server.TaskRegistrationServer())

  def SetUp(self):
    """Setup method used by the subclass."""
    pass

  def RunTest(self):
    """Main test method used by the subclass."""
    raise NotImplementedError()

  def TearDown(self):
    """Teardown method used by the subclass."""
    pass

  def CreateNewTask(self, *args, **kwargs):
    task = task_controller.TaskController(*args, **kwargs)
    self._registration_server.RegisterTaskCallback(
        task.otp, task.OnConnect)
    return task

  def RunController(self):
    """Main entry point for the controller."""
    print ' '.join(sys.argv)
    common_lib.InitLogging()
    self._registration_server.Start()

    error = None
    tb = None
    try:
      self.SetUp()
      self.RunTest()
    except Exception as e:
      # Defer raising exceptions until after TearDown is called.
      error = e
      tb = sys.exc_info()[-1]
    try:
      self.TearDown()
    except Exception as e:
      if not tb:
        error = e
        tb = sys.exc_info()[-1]

    self._registration_server.Shutdown()
    task_controller.TaskController.ReleaseAllTasks()
    if error:
      raise error, None, tb  #pylint: disable=raising-bad-type