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 logging
6import os
7
8from autotest_lib.client.bin import utils
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.cros import constants
11from autotest_lib.server import site_utils
12from autotest_lib.server import test
13from autotest_lib.server.cros.multimedia import remote_facade_factory
14from autotest_lib.site_utils import lxc
15
16
17class AudioTest(test.test):
18    """Base class for audio tests.
19
20    AudioTest provides a common warmup() function for the collection
21    of audio tests.
22    It is not mandatory to use this base class for audio tests, it is for
23    convenience only.
24
25    """
26
27    def warmup(self):
28        """Warmup for the test before executing main logic of the test."""
29        # test.test is an old-style class.
30        test.test.warmup(self)
31        audio_test_requirement()
32
33
34    def create_remote_facade_factory(self, host):
35        """Creates a remote facade factory to access multimedia server.
36
37        @param host: A CrosHost object to access Cros device.
38
39        @returns: A RemoteFacadeFactory object to create different facade for
40                  different functionalities provided by multimedia server.
41
42        """
43        return create_remote_facade_factory(host, self.resultsdir)
44
45
46def create_remote_facade_factory(host, result_dir):
47    """Creates a remote facade factory to access multimedia server.
48
49    @param host: A CrosHost object to access Cros device.
50    @param result_dir: A directory to store multimedia server init log.
51
52    @returns: A RemoteFacadeFactory object to create different facade for
53              different functionalities provided by multimedia server.
54
55    """
56    try:
57        factory = remote_facade_factory.RemoteFacadeFactory(host)
58    finally:
59        host.get_file(
60                constants.MULTIMEDIA_XMLRPC_SERVER_LOG_FILE,
61                os.path.join(
62                        result_dir,
63                        'multimedia_xmlrpc_server.log.init'))
64    return factory
65
66
67def audio_test_requirement():
68    """Installs sox and checks it is installed correctly."""
69    install_sox()
70    check_sox_installed()
71
72
73def install_sox():
74    """Install sox command on autotest drone."""
75    try:
76        lxc.install_package('sox')
77    except error.ContainerError:
78        logging.info('Can not install sox outside of container.')
79
80
81def check_sox_installed():
82    """Checks if sox is installed.
83
84    @raises: error.TestError if sox is not installed.
85
86    """
87    try:
88        utils.run('sox --help')
89        logging.info('Found sox executable.')
90    except error.CmdError:
91        error_message = 'sox command is not installed.'
92        if site_utils.is_inside_chroot():
93            error_message += ' sudo emerge sox to install sox in chroot'
94        raise error.TestError(error_message)
95