If an invalid VM config exists, delete it
VMM#get can give three results:
1. There's no such VM.
2. There is such a VM and it is valid.
3. There is such a VM, but it is invalid.
We want to create a completely new VM, so we need to delete the
existing one in cases 2 and 3. Previously we didn't handle case 3 at
all.
Bug: 324650937
Test: atest MicrodroidTests
Change-Id: Ibeefe404628d2d21917ebd24d996c905e6e162ba
diff --git a/javalib/src/android/system/virtualmachine/VirtualMachineManager.java b/javalib/src/android/system/virtualmachine/VirtualMachineManager.java
index 1250bd1..1607c0a 100644
--- a/javalib/src/android/system/virtualmachine/VirtualMachineManager.java
+++ b/javalib/src/android/system/virtualmachine/VirtualMachineManager.java
@@ -211,7 +211,7 @@
*
* @see #getOrCreate
* @throws VirtualMachineException if the virtual machine exists but could not be successfully
- * retrieved.
+ * retrieved. This can be resolved by calling {@link #delete} on the VM.
* @hide
*/
@SystemApi
diff --git a/tests/helper/src/java/com/android/microdroid/test/device/MicrodroidDeviceTestBase.java b/tests/helper/src/java/com/android/microdroid/test/device/MicrodroidDeviceTestBase.java
index 15e175b..8e11218 100644
--- a/tests/helper/src/java/com/android/microdroid/test/device/MicrodroidDeviceTestBase.java
+++ b/tests/helper/src/java/com/android/microdroid/test/device/MicrodroidDeviceTestBase.java
@@ -150,8 +150,15 @@
public VirtualMachine forceCreateNewVirtualMachine(String name, VirtualMachineConfig config)
throws VirtualMachineException {
final VirtualMachineManager vmm = getVirtualMachineManager();
- VirtualMachine existingVm = vmm.get(name);
- if (existingVm != null) {
+ boolean deleteExisting;
+ try {
+ deleteExisting = vmm.get(name) != null;
+ } catch (VirtualMachineException e) {
+ // VM exists, i.e. there are some files for it, but they could not be successfully
+ // loaded.
+ deleteExisting = true;
+ }
+ if (deleteExisting) {
vmm.delete(name);
}
return vmm.create(name, config);