• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2017 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 common
6import logging
7
8from autotest_lib.server import frontend
9from autotest_lib.server import site_utils
10
11RF_SWITCH_LABEL = 'rf_switch'
12LOCK_REASON = 'Locked for the tests.'
13
14
15def allocate_rf_switch():
16    """Allocates a RF Switch.
17
18    Locks the available RF Switch if it was discovered via AFE to prevent tests
19    stomping on each other.
20
21    @return an instance of AFE host object or None.
22    """
23    afe = frontend.AFE(
24            debug=True, server=site_utils.get_global_afe_hostname())
25    # Get a list of hosts with label rf_switch that are not locked.
26    rf_switch_hosts = afe.get_hosts(label=RF_SWITCH_LABEL, locked=False)
27    if len(rf_switch_hosts) > 0:
28        for rf_switch in rf_switch_hosts:
29            # Lock the first available RF Switch in the list.
30            if afe.lock_host(rf_switch.hostname, LOCK_REASON):
31                return rf_switch
32            logging.error(
33                    'RF Switch %s could not be locked' % rf_switch.hostname)
34    else:
35        logging.debug('No RF Switches are available for tests.')
36
37
38def deallocate_rf_switch(rf_switch_host):
39    """Unlocks a RF Switch.
40
41    @param rf_switch_host: an instance of AFE Host object to unlock.
42
43    @return True if rf_switch is unlocked, False otherwise.
44    """
45    afe = frontend.AFE(
46            debug=True, server=site_utils.get_global_afe_hostname())
47    afe.unlock_hosts([rf_switch_host.hostname])
48    rf_switch = afe.get_hosts(hostnames=(rf_switch_host.hostname,))
49    return not rf_switch[0].locked
50