def test_get_free_devices(self): # (note, device1 is not free) add_server('server') add_image('img1', id=23) add_device("device2", state='free', environment='foo') add_device("device3", state='free', environment='bar') # device4 has an outstanding request that's still open, even # though its state is free; it should not be returned add_device("device4", state='free', environment='bar') add_request('server', device='device4', image='img1') self.assertEqual(sorted([x['name'] for x in data.get_free_devices()]), sorted(['device2', 'device3'])) self.assertEqual(sorted([x['name'] for x in data.get_free_devices(environment='foo')]), sorted(['device2'])) self.assertEqual(sorted([x['name'] for x in data.get_free_devices(environment='bar')]), sorted(['device3'])) self.assertEqual(sorted([x['name'] for x in data.get_free_devices(environment='bing')]), sorted([])) self.assertEqual(sorted([x['name'] for x in data.get_free_devices( environment='bar', device_name='device2')]), sorted([])) self.assertEqual(sorted([x['name'] for x in data.get_free_devices( environment='bar', device_name='device3')]), sorted(['device3']))
def find_device(self): self.logger.info('Finding device.') device_name = None count = self.machine.increment_counter(self.state_name) request = data.request_config(self.machine.request_id) image_is_reusable = data.image_is_reusable(request['image']) free_devices = data.get_free_devices( environment=request['environment'], device_name=request['requested_device']) if free_devices: if image_is_reusable: devices_with_image = [x for x in free_devices if x['image'] == request['image'] and data.from_json(x['boot_config']) == data.from_json(request['boot_config'])] if devices_with_image: free_devices = devices_with_image # pick a device at random from the returned list device_name = random.choice(free_devices)['name'] self.logger.info('Assigning device %s.' % device_name) if data.reserve_device(self.machine.request_id, device_name): self.logger.info('Request succeeded.') self.machine.goto_state(contacting_lifeguard) else: self.logger.warn('Request failed!') if request['requested_device'] == 'any': if count >= self.MAX_ANY_REQUESTS: self.logger.warn('Hit maximum number of attempts to find ' 'a free device; giving up.') self.machine.goto_state(device_not_found) else: if count >= self.MAX_SPECIFIC_REQUESTS: self.logger.warn('Requested device %s is busy.' % device_name) self.machine.goto_state(device_busy)
def find_device(self): # FIXME: refactor. device_name = None count = self.machine.increment_counter(self.state_name) request = data.dump_requests(self.machine.request_id)[0] if request['requested_device'] == 'any': free_devices = data.get_free_devices() if free_devices: device_id = random.randint(0, len(free_devices) - 1) device_name = free_devices[device_id] logs.request_logs.add( self.machine.request_id, 'assigning device %s' % device_name) else: self.logger.info('no free devices') else: device_name = request['requested_device'] self.logger.info('assigning requested device %s' % device_name) if device_name and data.reserve_device(self.machine.request_id, device_name): self.logger.info('request succeeded') self.machine.goto_state(contacting_lifeguard) else: self.logger.warn('request failed!') if request['requested_device'] == 'any': if count >= self.MAX_ANY_REQUESTS: logs.request_logs.add( self.machine.request_id, 'hit maximum number of attempts; giving up') self.machine.goto_state(device_not_found) else: if count >= self.MAX_SPECIFIC_REQUESTS: logs.request_logs.add( self.machine.request_id, 'requested device %s is busy' % device_name) self.machine.goto_state(device_busy)
def find_device(self): # FIXME: refactor. device_name = None count = self.machine.increment_counter(self.state_name) request = data.dump_requests(self.machine.request_id)[0] if request['requested_device'] == 'any': free_devices = data.get_free_devices() if free_devices: device_id = random.randint(0, len(free_devices) - 1) device_name = free_devices[device_id] logs.request_logs.add(self.machine.request_id, 'assigning device %s' % device_name) else: self.logger.info('no free devices') else: device_name = request['requested_device'] self.logger.info('assigning requested device %s' % device_name) if device_name and data.reserve_device(self.machine.request_id, device_name): self.logger.info('request succeeded') self.machine.goto_state(contacting_lifeguard) else: self.logger.warn('request failed!') if request['requested_device'] == 'any': if count >= self.MAX_ANY_REQUESTS: logs.request_logs.add( self.machine.request_id, 'hit maximum number of attempts; giving up') self.machine.goto_state(device_not_found) else: if count >= self.MAX_SPECIFIC_REQUESTS: logs.request_logs.add( self.machine.request_id, 'requested device %s is busy' % device_name) self.machine.goto_state(device_busy)