'Test backup in-use volume using temp snapshot.'

@mock.patch('cinder.utils.brick_get_connector_properties')
@mock.patch('cinder.volume.rpcapi.VolumeAPI.get_backup_device')
@mock.patch('cinder.utils.temporary_chown')
@mock.patch('six.moves.builtins.open')
@mock.patch.object(os.path, 'isdir', return_value=False)
def test_create_backup_with_temp_snapshot(self, mock_isdir, mock_open, mock_temporary_chown, mock_get_backup_device, mock_get_conn):

    self.override_config('backup_use_same_host', True)
    vol_size = 1
    vol_id = self._create_volume_db_entry(size=vol_size, previous_status='in-use')
    backup = self._create_backup_db_entry(volume_id=vol_id)
    snap = self._create_snapshot_db_entry(volume_id=vol_id)
    vol = objects.Volume.get_by_id(self.ctxt, vol_id)
    mock_get_backup_device.return_value = objects.BackupDeviceInfo.from_primitive({'backup_device': snap, 'secure_enabled': False, 'is_snapshot': True}, self.ctxt, expected_attrs=['metadata'])
    attach_info = {'device': {'path': '/dev/null'}, 'conn': {'data': {}}, 'connector': fake.FakeConnector(None)}
    mock_terminate_connection_snapshot = self.mock_object(volume_rpcapi.VolumeAPI, 'terminate_connection_snapshot')
    mock_initialize_connection_snapshot = self.mock_object(volume_rpcapi.VolumeAPI, 'initialize_connection_snapshot')
    mock_connect_device = self.mock_object(manager.BackupManager, '_connect_device')
    mock_connect_device.return_value = attach_info
    properties = {}
    mock_get_conn.return_value = properties
    mock_open.return_value = open('/dev/null', 'rb')
    self.backup_mgr.create_backup(self.ctxt, backup)
    mock_temporary_chown.assert_called_once_with('/dev/null')
    mock_initialize_connection_snapshot.assert_called_once_with(self.ctxt, snap, properties)
    mock_get_backup_device.assert_called_once_with(self.ctxt, backup, vol)
    mock_get_conn.assert_called_once_with()
    mock_terminate_connection_snapshot.assert_called_once_with(self.ctxt, snap, properties, force=True)
    vol = objects.Volume.get_by_id(self.ctxt, vol_id)
    self.assertEqual('in-use', vol['status'])
    self.assertEqual('backing-up', vol['previous_status'])
    backup = objects.Backup.get_by_id(self.ctxt, backup.id)
    self.assertEqual(fields.BackupStatus.AVAILABLE, backup.status)
    self.assertEqual(vol_size, backup.size)
