diff --git a/tools/dashboard-vms-test.py b/tools/dashboard-vms-test.py
index e30d4332323e6b118f6bd3cec4879172bb00df6a..bd4c6f7fa696c33c3b468e614d904ec9a980c153 100644
--- a/tools/dashboard-vms-test.py
+++ b/tools/dashboard-vms-test.py
@@ -22,6 +22,47 @@ spec.loader.exec_module(host)
class VMTests(unittest.TestCase):
+ def test_media_replaces_a_guest_locked_cd_and_persists_until_ejected(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ directory = Path(temporary).resolve()
+ installer = directory / 'installer.iso'
+ installer.write_bytes(b'installer')
+ vm = directory / 'fixture'
+ vm.mkdir()
+ seed = vm / 'seed.iso'
+ seed.write_bytes(b'setup')
+ definition = f'''
+
+
+ '''
+
+ def virsh(action, *args):
+ nonlocal definition
+ if action == 'dumpxml':
+ return definition
+ if action == 'domstate':
+ return 'running'
+ self.assertEqual(action, 'update-device')
+ self.assertIn('--live', args)
+ self.assertIn('--config', args)
+ if '--force' not in args:
+ raise vms.subprocess.CalledProcessError(1, action, stderr='tray locked')
+ cd = vms.ET.parse(args[1]).getroot()
+ self.assertIsNotNone(cd.find('readonly'))
+ definition = '' + vms.ET.tostring(cd, encoding='unicode') + ''
+ return ''
+
+ with patch.object(vms, 'DISKS', directory), patch.object(vms, 'resolve_image', return_value=installer), \
+ patch.object(vms, 'virsh', side_effect=virsh):
+ vms.change_media({'name': 'fixture', 'image': 'installer.iso'})
+ attached = Path(vms.ET.fromstring(definition).find('./devices/disk/source').get('file'))
+ self.assertEqual(attached.read_bytes(), installer.read_bytes())
+ self.assertFalse(seed.exists())
+ vms.change_media({'name': 'fixture', 'image': ''})
+ self.assertIsNone(vms.ET.fromstring(definition).find('./devices/disk/source'))
+ self.assertFalse(attached.exists())
+ self.assertTrue(installer.exists())
+
def test_secure_preset_retains_efi_state_but_generates_new_login(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary).resolve()
diff --git a/tools/vms.py b/tools/vms.py
index 2f8b3abf959fa8d3c448bb715a11694043620b8e..a57a753445aaffed23aeba14cbe3e777e730cc24 100644
--- a/tools/vms.py
+++ b/tools/vms.py
@@ -525,7 +525,7 @@ def change_media(spec):
if frozen:
frozen.unlink(missing_ok=True)
raise ValueError("Shut down this VM before adding its first CD drive.")
- flags.append("--live")
+ flags.extend(["--live", "--force"])
xml = directory / "media.xml"
xml.write_bytes(ET.tostring(cd))
try: