• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2013 The Chromium OS 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.
4
5"""
6Provides utility class for stopping and restarting services
7
8When using this class, one likely wishes to do the following:
9
10    def initialize(self):
11        self._services = service_stopper.ServiceStopper(['service'])
12        self._services.stop_services()
13
14
15    def cleanup(self):
16        self._services.start_services()
17
18As this ensures that the services will be off before the test code runs, and
19the test framework will ensure that the services are restarted through any
20code path out of the test.
21"""
22
23import logging
24
25from autotest_lib.client.bin import utils
26from autotest_lib.client.common_lib import error
27
28
29class ServiceStopper(object):
30    """Class to manage CrOS services.
31    Public attributes:
32      services_to_stop: list of services that should be stopped
33
34   Public constants:
35      POWER_DRAW_SERVICES: list of services that influence power test in
36    unpredictable/undesirable manners.
37
38    Public methods:
39      stop_sevices: stop running system services.
40      restore_services: restore services that were previously stopped.
41
42    Private attributes:
43      _services_stopped: list of services that were successfully stopped
44    """
45
46    POWER_DRAW_SERVICES = ['powerd', 'update-engine', 'bluetoothd', 'vnc']
47
48    def __init__(self, services_to_stop=[]):
49        """Initialize instance of class.
50
51        By Default sets an empty list of services.
52        """
53        self.services_to_stop = services_to_stop
54        self._services_stopped = []
55
56
57    def stop_services(self):
58        """Turn off managed services."""
59
60        for service in self.services_to_stop:
61            cmd = 'status %s' % service
62            out = utils.system_output(cmd, ignore_status=True)
63            is_stopped = 'start/running' not in out
64            if is_stopped:
65                continue
66            try:
67                utils.system('stop %s' % service)
68                self._services_stopped.append(service)
69            except error.CmdError as e:
70                logging.warning('Error stopping service %s. %s',
71                                service, str(e))
72
73
74    def restore_services(self):
75        """Restore services that were stopped."""
76        for service in reversed(self._services_stopped):
77            utils.system('start %s' % service, ignore_status=True)
78        self._services_stopped = []
79
80
81    def __enter__(self):
82        self.stop_services()
83        return self
84
85
86    def __exit__(self, exnval, exntype, exnstack):
87        self.restore_services()
88
89
90    def close(self):
91        """Equivalent to restore_services."""
92        self.restore_services()
93