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