def restore_to_file_without_backing_file_test(dev, backup_target): length0 = 256 length1 = 128 offset0 = 0 offset1 = length1 + offset0 output_raw_path = file(OUTPUT_FILE_RAW) output_qcow2_path = file(OUTPUT_FILE_QCOW2) # create 1 empty snapshot for converting to init state. snap0 = cmd.snapshot_create() # create 1 snapshot with 256B data. # output = snap2(offset0, length1) snap1_data = common.random_string(length0) common.verify_data(dev, offset0, snap1_data) snap1 = cmd.snapshot_create() backup = create_backup(backup_target, snap1) cmd.restore_to_file(backup, "", output_raw_path, IMAGE_FORMAT_RAW) output1_raw = read_file(output_raw_path, offset0, length0) assert output1_raw == snap1_data os.remove(output_raw_path) assert not os.path.exists(output_raw_path) cmd.restore_to_file(backup, "", output_qcow2_path, IMAGE_FORMAT_QCOW2) output1_qcow2 = read_qcow2_file_without_backing_file( output_qcow2_path, offset0, length0) assert output1_qcow2 == snap1_data os.remove(output_qcow2_path) assert not os.path.exists(output_qcow2_path) cmd.snapshot_revert(snap0) rm_snaps([snap1]) rm_backups([backup]) # create 2 snapshots with 256B data and 128B data # output = snap2(offset0, length1 - length2) + # snap1(offset2, length2) snap1_data = common.random_string(length0) common.verify_data(dev, offset0, snap1_data) snap1 = cmd.snapshot_create() snap2_data = common.random_string(length1) common.verify_data(dev, offset0, snap2_data) snap2 = cmd.snapshot_create() backup = create_backup(backup_target, snap2) cmd.restore_to_file(backup, "", output_raw_path, IMAGE_FORMAT_RAW) output2_raw_snap2 = read_file( output_raw_path, offset0, length1) output2_raw_snap1 = read_file( output_raw_path, offset1, length0 - length1) assert output2_raw_snap2 == snap2_data assert output2_raw_snap1 == snap1_data[offset1: length0] cmd.restore_to_file(backup, "", output_qcow2_path, IMAGE_FORMAT_QCOW2) output2_qcow2_snap2 = read_qcow2_file_without_backing_file( output_qcow2_path, offset0, length1) output2_qcow2_snap1 = read_qcow2_file_without_backing_file( output_qcow2_path, offset1, length0 - length1) assert output2_qcow2_snap2 == snap2_data assert output2_qcow2_snap1 == snap1_data[offset1: length0] os.remove(output_qcow2_path) assert not os.path.exists(output_qcow2_path) cmd.snapshot_revert(snap0) rm_snaps([snap1, snap2]) rm_backups([backup])
def restore_to_file_with_backing_file_test(backing_dev, backup_target): length0 = 4 * 1024 length1 = 256 length2 = 128 offset0 = 0 offset1 = length1 + offset0 offset2 = length2 + offset0 output_raw_path = file(OUTPUT_FILE_RAW) output_qcow2_path = file(OUTPUT_FILE_QCOW2) # create 1 empty snapshot. # data in output image == data in backing check_backing() check_empty_volume(backing_dev) snap0 = cmd.snapshot_create() backup = create_backup(backup_target, snap0) volume_data = read_dev(backing_dev, offset0, length0) backing_data = read_from_backing_file(offset0, length0) dev_checksum = common.checksum_dev(backing_dev) assert volume_data != "" assert volume_data == backing_data cmd.restore_to_file(backup, file(BACKING_FILE_QCOW2), output_raw_path, IMAGE_FORMAT_RAW) output0_raw = read_file(output_raw_path, offset0, length0) output0_checksum = checksum_data( read_file(output_raw_path, 0, SIZE)) assert output0_raw == backing_data assert output0_checksum == dev_checksum os.remove(output_raw_path) assert not os.path.exists(output_raw_path) cmd.restore_to_file(backup, file(BACKING_FILE_QCOW2), output_qcow2_path, IMAGE_FORMAT_QCOW2) output0_qcow2 = read_qcow2_file_without_backing_file( output_qcow2_path, offset0, length0) output0_checksum = checksum_data( read_qcow2_file_without_backing_file(output_qcow2_path, 0, SIZE)) assert output0_qcow2 == backing_data assert output0_qcow2 == volume_data assert output0_checksum == dev_checksum os.remove(output_qcow2_path) assert not os.path.exists(output_qcow2_path) rm_backups([backup]) # create 1 snapshot with 256B data. # output = snap1(offset0, length1) + backing(offset1, ...) snap1_data = common.random_string(length1) common.verify_data(backing_dev, offset0, snap1_data) snap1 = cmd.snapshot_create() backup = create_backup(backup_target, snap1) volume_data = read_dev(backing_dev, offset0, length0) backing_data = read_from_backing_file( offset1, length0 - offset1) dev_checksum = common.checksum_dev(backing_dev) cmd.restore_to_file(backup, file(BACKING_FILE_QCOW2), output_raw_path, IMAGE_FORMAT_RAW) output1_raw_snap1 = read_file( output_raw_path, offset0, length1) output1_raw_backing = read_file( output_raw_path, offset1, length0 - offset1) output1_checksum = checksum_data( read_file(output_raw_path, 0, SIZE)) assert output1_raw_snap1 == snap1_data assert output1_raw_backing == backing_data assert output1_raw_snap1 + output1_raw_backing == volume_data assert output1_checksum == dev_checksum os.remove(output_raw_path) assert not os.path.exists(output_raw_path) cmd.restore_to_file(backup, file(BACKING_FILE_QCOW2), output_qcow2_path, IMAGE_FORMAT_QCOW2) output1_qcow2_snap1 = read_qcow2_file_without_backing_file( output_qcow2_path, offset0, length1) output1_qcow2_backing = read_qcow2_file_without_backing_file( output_qcow2_path, offset1, length0 - offset1) output1_checksum = checksum_data( read_qcow2_file_without_backing_file(output_qcow2_path, 0, SIZE)) assert output1_qcow2_snap1 == snap1_data assert output1_qcow2_backing == backing_data assert output1_qcow2_snap1 + output1_qcow2_backing == volume_data assert output1_checksum == dev_checksum os.remove(output_qcow2_path) assert not os.path.exists(output_qcow2_path) cmd.snapshot_revert(snap0) rm_snaps([snap1]) rm_backups([backup]) check_backing() check_empty_volume(backing_dev) # create 2 snapshots with 256B data and 128B data # output = snap2(offset0, length1 - length2) + # snap1(offset2, length2) + backing(offset2, ...) snap1_data = common.random_string(length1) common.verify_data(backing_dev, offset0, snap1_data) snap1 = cmd.snapshot_create() snap2_data = common.random_string(length2) common.verify_data(backing_dev, offset0, snap2_data) snap2 = cmd.snapshot_create() backup = create_backup(backup_target, snap2) volume_data = read_dev(backing_dev, offset0, length0) backing_data = read_from_backing_file( offset1, length0 - offset1) dev_checksum = common.checksum_dev(backing_dev) cmd.restore_to_file(backup, file(BACKING_FILE_QCOW2), output_raw_path, IMAGE_FORMAT_RAW) output2_raw_snap2 = read_file( output_raw_path, offset0, length2) output2_raw_snap1 = read_file( output_raw_path, offset2, length1 - length2) output2_raw_backing = read_file( output_raw_path, offset1, length0 - offset1) output2_checksum = checksum_data( read_file(output_raw_path, 0, SIZE)) assert output2_raw_snap2 == snap2_data assert output2_raw_snap1 == snap1_data[offset2: length1] assert output2_raw_backing == backing_data assert \ volume_data == \ output2_raw_snap2 + output2_raw_snap1 + output2_raw_backing assert output2_checksum == dev_checksum os.remove(output_raw_path) assert not os.path.exists(output_raw_path) cmd.restore_to_file(backup, file(BACKING_FILE_QCOW2), output_qcow2_path, IMAGE_FORMAT_QCOW2) output2_qcow2_snap2 = read_qcow2_file_without_backing_file( output_qcow2_path, offset0, length2) output2_qcow2_snap1 = read_qcow2_file_without_backing_file( output_qcow2_path, offset2, length1 - length2) output2_qcow2_backing = read_qcow2_file_without_backing_file( output_qcow2_path, offset1, length0 - offset1) output2_checksum = checksum_data( read_qcow2_file_without_backing_file(output_qcow2_path, 0, SIZE)) assert output2_qcow2_snap2 == snap2_data assert output2_qcow2_snap1 == snap1_data[offset2: length1] assert output2_qcow2_backing == backing_data assert \ volume_data == \ output2_qcow2_snap2 + output2_qcow2_snap1 + output1_qcow2_backing assert output2_checksum == dev_checksum os.remove(output_qcow2_path) assert not os.path.exists(output_qcow2_path) cmd.snapshot_revert(snap0) rm_snaps([snap1, snap2]) rm_backups([backup]) check_backing() check_empty_volume(backing_dev)
def restore_inc_test( controller, replica1, replica2, # NOQA sb_controller, sb_replica1, sb_replica2, backup_target): # NOQA launcher.start_engine_frontend(FRONTEND_TGT_BLOCKDEV, url=LAUNCHER) dev = common.get_dev(replica1, replica2, controller) zero_string = b'\x00'.decode('utf-8') # backup0: 256 random data in 1st block length0 = 256 snap0_data = common.random_string(length0) verify_data(dev, 0, snap0_data) verify_data(dev, BLOCK_SIZE, snap0_data) snap0 = cmd.snapshot_create() backup0 = create_backup(backup_target, snap0) backup0_name = cmd.backup_inspect(backup0)['Name'] # backup1: 32 random data + 32 zero data + 192 random data in 1st block length1 = 32 offset1 = 32 snap1_data = zero_string * length1 verify_data(dev, offset1, snap1_data) snap1 = cmd.snapshot_create() backup1 = create_backup(backup_target, snap1) backup1_name = cmd.backup_inspect(backup1)['Name'] # backup2: 32 random data + 256 random data in 1st block, # 256 random data in 2nd block length2 = 256 offset2 = 32 snap2_data = common.random_string(length2) verify_data(dev, offset2, snap2_data) verify_data(dev, BLOCK_SIZE, snap2_data) snap2 = cmd.snapshot_create() backup2 = create_backup(backup_target, snap2) backup2_name = cmd.backup_inspect(backup2)['Name'] # backup3: 64 zero data + 192 random data in 1st block length3 = 64 offset3 = 0 verify_data(dev, offset3, zero_string * length3) verify_data(dev, length2, zero_string * offset2) verify_data(dev, BLOCK_SIZE, zero_string * length2) snap3 = cmd.snapshot_create() backup3 = create_backup(backup_target, snap3) backup3_name = cmd.backup_inspect(backup3)['Name'] # backup4: 256 random data in 1st block length4 = 256 offset4 = 0 snap4_data = common.random_string(length4) verify_data(dev, offset4, snap4_data) snap4 = cmd.snapshot_create() backup4 = create_backup(backup_target, snap4) backup4_name = cmd.backup_inspect(backup4)['Name'] common.cleanup_replica(replica1) common.cleanup_replica(replica2) common.cleanup_controller(controller) launcher.shutdown_engine_frontend(url=LAUNCHER) # start no-frontend volume # start standby volume (no frontend) start_no_frontend_volume(sb_controller, sb_replica1, sb_replica2) restore_for_no_frontend_volume(backup0, sb_controller) verify_no_frontend_data(0, snap0_data, sb_controller) # mock restore crash/error delta_file1 = "volume-delta-" + backup0_name + ".img" if "vfs" in backup_target: command = ["find", VFS_DIR, "-type", "d", "-name", VOLUME_NAME] backup_volume_path = subprocess.check_output(command).strip() command = ["find", backup_volume_path, "-name", "*blk"] blocks = subprocess.check_output(command).split() assert len(blocks) != 0 for blk in blocks: command = ["mv", blk, blk + ".tmp"] subprocess.check_output(command).strip() with pytest.raises(subprocess.CalledProcessError): cmd.restore_inc(backup1, backup0_name, CONTROLLER_NO_FRONTEND) assert path.exists(STANDBY_REPLICA1_PATH + delta_file1) assert path.exists(STANDBY_REPLICA2_PATH + delta_file1) for blk in blocks: command = ["mv", blk + ".tmp", blk] subprocess.check_output(command) data1 = \ snap0_data[0:offset1] + snap1_data + \ snap0_data[offset1+length1:] cmd.restore_inc(backup1, backup0_name, CONTROLLER_NO_FRONTEND) verify_no_frontend_data(0, data1, sb_controller) assert not path.exists(STANDBY_REPLICA1_PATH + delta_file1) assert not path.exists(STANDBY_REPLICA2_PATH + delta_file1) volume_info = cmd.info(CONTROLLER_NO_FRONTEND) assert volume_info['lastRestored'] == backup1_name data2 = \ data1[0:offset2] + snap2_data + \ zero_string * (BLOCK_SIZE - length2 - offset2) + snap2_data cmd.restore_inc(backup2, backup1_name, CONTROLLER_NO_FRONTEND) verify_no_frontend_data(0, data2, sb_controller) delta_file2 = "volume-delta-" + backup1_name + ".img" assert not path.exists(STANDBY_REPLICA1_PATH + delta_file2) assert not path.exists(STANDBY_REPLICA2_PATH + delta_file2) volume_info = cmd.info(CONTROLLER_NO_FRONTEND) assert volume_info['lastRestored'] == backup2_name # mock race condition with pytest.raises(subprocess.CalledProcessError) as e: cmd.restore_inc(backup1, backup0_name, CONTROLLER_NO_FRONTEND) assert "doesn't match lastRestored" in e data3 = zero_string * length3 + data2[length3:length2] cmd.restore_inc(backup3, backup2_name, CONTROLLER_NO_FRONTEND) verify_no_frontend_data(0, data3, sb_controller) delta_file3 = "volume-delta-" + backup3_name + ".img" assert not path.exists(STANDBY_REPLICA1_PATH + delta_file3) assert not path.exists(STANDBY_REPLICA2_PATH + delta_file3) volume_info = cmd.info(CONTROLLER_NO_FRONTEND) assert volume_info['lastRestored'] == backup3_name # mock corner case: invalid last-restored backup rm_backups([backup3]) # actually it is full restoration cmd.restore_inc(backup4, backup3_name, CONTROLLER_NO_FRONTEND) verify_no_frontend_data(0, snap4_data, sb_controller) volume_info = cmd.info(CONTROLLER_NO_FRONTEND) assert volume_info['lastRestored'] == backup4_name if "vfs" in backup_target: command = ["find", VFS_DIR, "-type", "d", "-name", VOLUME_NAME] backup_volume_path = subprocess.check_output(command).strip() command = ["find", backup_volume_path, "-name", "*tempoary"] tmp_files = subprocess.check_output(command).split() assert len(tmp_files) == 0 cleanup_no_frontend_volume(sb_controller, sb_replica1, sb_replica2) rm_backups([backup0, backup1, backup2, backup4])