'Test attachment_delete.'

@mock.patch('cinder.volume.api.check_policy')
@mock.patch('cinder.volume.rpcapi.VolumeAPI.attachment_delete')
@mock.patch('cinder.volume.rpcapi.VolumeAPI.attachment_update')
def test_attachment_create_update_and_delete(self, mock_rpc_attachment_update, mock_rpc_attachment_delete, mock_policy):

    volume_params = {'status': 'available'}
    connection_info = {'fake_key': 'fake_value', 'fake_key2': ['fake_value1', 'fake_value2']}
    mock_rpc_attachment_update.return_value = connection_info
    vref = tests_utils.create_volume(self.context, **volume_params)
    aref = self.volume_api.attachment_create(self.context, vref, fake.UUID2)
    aref = objects.VolumeAttachment.get_by_id(self.context, aref.id)
    vref = objects.Volume.get_by_id(self.context, vref.id)
    connector = {'fake': 'connector'}
    self.volume_api.attachment_update(self.context, aref, connector)
    aref = objects.VolumeAttachment.get_by_id(self.context, aref.id)
    self.assertEqual(connection_info, aref.connection_info)
    values = {'volume_id': vref.id, 'volume_host': vref.host, 'attach_status': 'attached', 'instance_uuid': fake.UUID2}
    aref = db.volume_attach(self.context, values)
    aref = objects.VolumeAttachment.get_by_id(self.context, aref.id)
    self.assertEqual(vref.id, aref.volume_id)
    self.volume_api.attachment_delete(self.context, aref)
    mock_rpc_attachment_delete.assert_called_once_with(self.context, aref.id, mock.ANY)
