def test_failed_delete_checkpoint(): error_msg = "Internal delete error" dom = FakeDomainAdapter() checkpoint_2 = FakeCheckpoint(CHECKPOINT_2_XML, CHECKPOINT_2_ID, dom=dom) # simulating an error that raised when calling the delete method # of a specific checkpoint checkpoint_2.errors["delete"] = fake.libvirt_error( [libvirt.VIR_ERR_INTERNAL_ERROR, '', error_msg], "Fake libvirt error") dom.output_checkpoints = [ FakeCheckpoint(CHECKPOINT_1_XML, CHECKPOINT_1_ID, dom=dom), checkpoint_2 ] vm = FakeVm() res = backup.delete_checkpoints(vm, dom, [CHECKPOINT_1_ID, CHECKPOINT_2_ID]) expected_result = { 'checkpoint_ids': [CHECKPOINT_1_ID], 'error': { 'code': 1, 'message': error_msg } } assert res["result"] == expected_result res = backup.list_checkpoints(vm, dom) assert res["result"] == [CHECKPOINT_2_ID]
def test_dump_checkpoint_lookup_failed(): dom = FakeDomainAdapter() dom.errors["checkpointLookupByName"] = fake.libvirt_error( [libvirt.VIR_ERR_INTERNAL_ERROR], "Fake libvirt error") dom.output_checkpoints = [ FakeCheckpoint(CHECKPOINT_1_XML, CHECKPOINT_1_ID), FakeCheckpoint(CHECKPOINT_2_XML, CHECKPOINT_2_ID) ] with pytest.raises(libvirt.libvirtError) as e: backup.dump_checkpoint(dom, CHECKPOINT_1_ID) assert e.value.get_error_code() == libvirt.VIR_ERR_INTERNAL_ERROR
def test_dump_checkpoint(): dom = FakeDomainAdapter() dom.output_checkpoints = [ FakeCheckpoint(CHECKPOINT_1_XML, CHECKPOINT_1_ID), FakeCheckpoint(CHECKPOINT_2_XML, CHECKPOINT_2_ID) ] for checkpoint_cfg in FAKE_CHECKPOINT_CFG: res = backup.dump_checkpoint(dom, checkpoint_cfg['id']) expected_result = {'checkpoint': checkpoint_cfg['xml']} assert res["result"] == expected_result
def test_delete_one_checkpoint(): dom = FakeDomainAdapter() dom.output_checkpoints = [ FakeCheckpoint(CHECKPOINT_1_XML, CHECKPOINT_1_ID, dom=dom), FakeCheckpoint(CHECKPOINT_2_XML, CHECKPOINT_2_ID, dom=dom) ] vm = FakeVm() res = backup.delete_checkpoints(vm, dom, [CHECKPOINT_1_ID]) expected_result = {'checkpoint_ids': [CHECKPOINT_1_ID]} assert res["result"] == expected_result res = backup.list_checkpoints(vm, dom) assert res["result"] == [CHECKPOINT_2_ID]
def test_dump_checkpoint_get_xml_failed(): checkpoint_2 = FakeCheckpoint(CHECKPOINT_2_XML, CHECKPOINT_2_ID) # simulating an error that raised when calling the getXMLDesc method # of a specific checkpoint checkpoint_2.errors["getXMLDesc"] = fake.libvirt_error( [libvirt.VIR_ERR_INTERNAL_ERROR, '', 'Internal get XML error'], "Fake libvirt error") dom = FakeDomainAdapter() dom.output_checkpoints = [ FakeCheckpoint(CHECKPOINT_1_XML, CHECKPOINT_1_ID), checkpoint_2 ] with pytest.raises(libvirt.libvirtError) as e: backup.dump_checkpoint(dom, CHECKPOINT_2_ID) assert e.value.get_error_code() == libvirt.VIR_ERR_INTERNAL_ERROR
def test_dump_missing_checkpoint(): dom = FakeDomainAdapter() dom.output_checkpoints = [ FakeCheckpoint(CHECKPOINT_1_XML, CHECKPOINT_1_ID), ] with pytest.raises(exception.NoSuchCheckpointError): backup.dump_checkpoint(dom, CHECKPOINT_2_ID)
def test_redefine_checkpoints_using_config(): checkpoint_1 = FakeCheckpoint(CHECKPOINT_1_WITH_CREATION_TIME_XML, CHECKPOINT_1_ID) checkpoint_2 = FakeCheckpoint(CHECKPOINT_2_WITH_CREATION_TIME_XML, CHECKPOINT_2_ID) dom = FakeDomainAdapter(output_checkpoints=[checkpoint_1, checkpoint_2]) vm = FakeVm() fake_disks = create_fake_disks() fake_checkpoint_config_cfg = [ { 'id': CHECKPOINT_1_ID, 'config': { 'backup_id': BACKUP_1_ID, 'disks': fake_disks, 'to_checkpoint_id': CHECKPOINT_1_ID, 'creation_time': 1 } }, { 'id': CHECKPOINT_2_ID, 'config': { 'backup_id': BACKUP_2_ID, 'disks': fake_disks, 'from_checkpoint_id': CHECKPOINT_1_ID, 'to_checkpoint_id': CHECKPOINT_2_ID, 'creation_time': 2 } }, ] res = backup.redefine_checkpoints(vm, dom, fake_checkpoint_config_cfg) expected_result = { 'checkpoint_ids': [checkpoint_1.getName(), checkpoint_2.getName()], } assert res["result"] == expected_result
def test_delete_missing_checkpoint(): dom = FakeDomainAdapter() dom.output_checkpoints = [ FakeCheckpoint(CHECKPOINT_2_XML, CHECKPOINT_2_ID, dom=dom) ] vm = FakeVm() res = backup.delete_checkpoints(vm, dom, [CHECKPOINT_1_ID, CHECKPOINT_2_ID]) expected_result = {'checkpoint_ids': [CHECKPOINT_1_ID, CHECKPOINT_2_ID]} # validate that the missing checkpoint reported as # successfully removed assert res["result"] == expected_result res = backup.list_checkpoints(vm, dom) assert res["result"] == []
def test_redefine_diskless_checkpoints_using_config(): diskless_checkpoint = FakeCheckpoint(DISKLESS_CHECKPOINT_XML, DISKLESS_CHECKPOINT_ID) dom = FakeDomainAdapter(output_checkpoints=[diskless_checkpoint]) vm = FakeVm() fake_checkpoint_config_cfg = [ { 'id': DISKLESS_CHECKPOINT_ID, 'config': { 'backup_id': BACKUP_1_ID, 'disks': [], 'to_checkpoint_id': DISKLESS_CHECKPOINT_ID, 'creation_time': 1 } }, ] res = backup.redefine_checkpoints(vm, dom, fake_checkpoint_config_cfg) expected_result = { 'checkpoint_ids': [diskless_checkpoint.getName()], } assert res["result"] == expected_result
<disk name='vda' checkpoint='bitmap'/> </disks> </domaincheckpoint> """.format(CHECKPOINT_2_ID, BACKUP_2_ID, CHECKPOINT_1_ID) MIXED_CHECKPOINT_XML = """ <domaincheckpoint> <name>{}</name> <description>checkpoint for backup '{}'</description> <disks> <disk name='sda' checkpoint='bitmap'/> </disks> </domaincheckpoint> """.format(CHECKPOINT_1_ID, BACKUP_1_ID) CHECKPOINT_1 = FakeCheckpoint(CHECKPOINT_1_XML, CHECKPOINT_1_ID) CHECKPOINT_2 = FakeCheckpoint(CHECKPOINT_2_XML, CHECKPOINT_2_ID) class FakeDrive(object): def __init__(self, name, imageID, path=''): self.name = name self.imageID = imageID self.diskType = 'file' self.path = path self.format = 'cow' self.domainID = 'domain_id' class FakeHSM(hsm.HSM): def __init__(self):