buffet_config.py revision 275a2b309babaf95fd092bbf6dcfa4e857a6b80e
1# Copyright 2015 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
5import dbus
6import time
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib import utils
10from autotest_lib.client.common_lib.cros import dbus_send
11from autotest_lib.client.common_lib.cros.fake_device_server import fake_oauth
12from autotest_lib.client.common_lib.cros.fake_device_server import server
13
14BUFFET_CONFIG_PATH = '/tmp/buffet.fake.conf'
15BUFFET_STATE_PATH = '/tmp/buffet.fake.state'
16
17SERVICE_NAME = 'org.chromium.Buffet'
18
19MANAGER_INTERFACE = 'org.chromium.Buffet.Manager'
20MANAGER_OBJECT_PATH = '/org/chromium/Buffet/Manager'
21
22TEST_MESSAGE = 'Hello world!'
23
24LOCAL_SERVER_PORT = server.PORT
25LOCAL_OAUTH_URL = 'http://localhost:%d/%s/' % (LOCAL_SERVER_PORT,
26                                               fake_oauth.OAUTH_PATH)
27LOCAL_SERVICE_URL = 'http://localhost:%d/' % LOCAL_SERVER_PORT
28TEST_API_KEY = 'this_is_an_api_key'
29
30LOCAL_CLOUD_FAKES = {
31        'client_id': 'this_is_my_client_id',
32        'client_secret': 'this_is_my_client_secret',
33        'api_key': TEST_API_KEY,
34        'oauth_url': LOCAL_OAUTH_URL,
35        'service_url': LOCAL_SERVICE_URL,
36}
37
38
39class BuffetConfig(object):
40    """An object that knows how to restart buffet in various configurations."""
41
42    @staticmethod
43    def naive_restart(host=None):
44        """Restart Buffet without configuring it in any way.
45
46        @param host: Host object if we're interested in a remote host.
47
48        """
49        run = utils.run if host is None else host.run
50        run('stop buffet', ignore_status=True)
51        run('start buffet')
52
53
54    def __init__(self,
55                 log_verbosity=None,
56                 clean_state=True,
57                 use_local_cloud_fakes=True):
58        self.log_verbosity = log_verbosity
59        self.clean_state = clean_state
60        self.use_local_cloud_fakes = use_local_cloud_fakes
61
62
63    def restart_with_config(self, host=None, timeout_seconds=10):
64        """Restart Buffet with this configuration.
65
66        @param host: Host object if we're interested in a remote host.
67        @param timeout_seconds: number of seconds to wait for Buffet to
68                come up.
69
70        """
71        run = utils.run if host is None else host.run
72        run('stop buffet', ignore_status=True)
73        flag_list = []
74        if self.log_verbosity:
75            flag_list.append('BUFFET_LOG_LEVEL=%d' % self.log_verbosity)
76        if self.use_local_cloud_fakes:
77            conf_lines = ['%s=%s' % pair
78                          for pair in LOCAL_CLOUD_FAKES.iteritems()]
79            # Go through this convoluted shell magic here because we need to
80            # create this file on both remote and local hosts (see how run() is
81            # defined).
82            run('cat <<EOF >%s\n%s\nEOF\n' %
83                (BUFFET_CONFIG_PATH, '\n'.join(conf_lines)))
84            flag_list.append('BUFFET_CONFIG_PATH=%s' % BUFFET_CONFIG_PATH)
85        if self.clean_state:
86            run('echo > %s' % BUFFET_STATE_PATH)
87            run('chown buffet:buffet %s' % BUFFET_STATE_PATH)
88            flag_list.append('BUFFET_STATE_PATH=%s' % BUFFET_STATE_PATH)
89        run('start buffet %s' % ' '.join(flag_list))
90        start_time = time.time()
91        while time.time() - start_time < timeout_seconds:
92            result = dbus_send.dbus_send(
93                    SERVICE_NAME, MANAGER_INTERFACE, MANAGER_OBJECT_PATH,
94                    'TestMethod', args=[dbus.String(TEST_MESSAGE)],
95                    host=host, tolerate_failures=True)
96            if result and result.response == TEST_MESSAGE:
97                return
98            time.sleep(0.5)
99
100        raise error.TestFail('Buffet failed to restart in time.')
101