'A ``VolumeBusy`` is raised if a volume\'s attachment state is "busy"
when attempting to detach the volume. This can occur if an attempt
is made to detach a volume that has not been unmounted.'

@capture_logging(None)
def test_volume_busy_error_on_busy_state(self, logger):

    try:
        config = get_blockdevice_config()
    except InvalidConfig as e:
        self.skipTest(str(e))
    dataset_id = uuid4()
    flocker_volume = self.api.create_volume(dataset_id=dataset_id, size=self.minimum_allocatable_size)
    ec2_client = get_ec2_client_for_test(config)
    instance_id = self.api.compute_instance_id()
    self.api.attach_volume(flocker_volume.blockdevice_id, instance_id)
    volume = ec2_client.connection.Volume(flocker_volume.blockdevice_id)
    def clean_volume(volume):
        volume.detach_from_instance()
        _wait_for_volume_state_change(VolumeOperations.DETACH, volume)
        volume.delete()
    self.addCleanup(clean_volume, volume)
    def busy_ebs_volume(volume_id):
        busy_volume = ec2_client.connection.Volume(volume_id)
        busy_volume.load()
        if busy_volume.attachments:
            busy_volume.attachments[0]['State'] = 'busy'
        return busy_volume
    self.patch(self.api, '_get_ebs_volume', busy_ebs_volume)
    self.assertRaises(VolumeBusy, self.api.detach_volume, flocker_volume.blockdevice_id)
    expected_keys = set(['volume_id', 'attachments'])
    messages = [message.message for message in LoggedMessage.of_type(logger.messages, VOLUME_BUSY_MESSAGE)]
    self.assertThat(messages, AllMatch(ContainsAll(expected_keys)))
