Skip to content

Commit

Permalink
add tests for create_rp_boot_entry
Browse files Browse the repository at this point in the history
and fix the bugs it uncovered, yay
  • Loading branch information
mwhudson committed Jul 18, 2023
1 parent c991568 commit cf3de7f
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 3 deletions.
4 changes: 2 additions & 2 deletions subiquity/server/controllers/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,12 @@ async def create_rp_boot_entry(self, context, rp):
new_entry = efi_state_after.entries[new_bootnum]
was_dup = False
for entry in efi_state_before.entries.values():
if entry.path == new_entry.path and entry.label == new_entry.label:
if entry.path == new_entry.path and entry.name == new_entry.name:
was_dup = True
if was_dup:
cmd = [
'efibootmgr', '--delete-bootnum',
'--bootnum', new_entry,
'--bootnum', new_bootnum,
]
else:
cmd = [
Expand Down
123 changes: 122 additions & 1 deletion subiquity/server/controllers/tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@
from pathlib import Path
import subprocess
import unittest
from unittest.mock import ANY, Mock, mock_open, patch
from unittest.mock import (
ANY,
AsyncMock,
call,
Mock,
mock_open,
patch,
)

from curtin.util import EFIBootEntry, EFIBootState

from subiquity.common.types import PackageInstallState
from subiquity.models.tests.test_filesystem import make_model_and_partition
from subiquity.server.controllers.install import (
InstallController,
)
Expand Down Expand Up @@ -115,6 +126,64 @@ def test_generic_config(self):
})


efi_state_no_rp = EFIBootState(
current='0000',
timeout='0 seconds',
order=['0000', '0002'],
entries={
'0000': EFIBootEntry(
name='ubuntu',
path='HD(1,GPT,...)/File(\\EFI\\ubuntu\\shimx64.efi)'),
'0001': EFIBootEntry(
name='Windows Boot Manager',
path='HD(1,GPT,...,0x82000)/File(\\EFI\\bootmgfw.efi'),
'0002': EFIBootEntry(
name='Linux-Firmware-Updater',
path='HD(1,GPT,...,0x800,0x100000)/File(\\shimx64.efi)\\.fwupd'),
})

efi_state_with_rp = EFIBootState(
current='0000',
timeout='0 seconds',
order=['0000', '0002', '0003'],
entries={
'0000': EFIBootEntry(
name='ubuntu',
path='HD(1,GPT,...)/File(\\EFI\\ubuntu\\shimx64.efi)'),
'0001': EFIBootEntry(
name='Windows Boot Manager',
path='HD(1,GPT,...,0x82000)/File(\\EFI\\bootmgfw.efi'),
'0002': EFIBootEntry(
name='Linux-Firmware-Updater',
path='HD(1,GPT,...,0x800,0x100000)/File(\\shimx64.efi)\\.fwupd'),
'0003': EFIBootEntry(
name='Restore Ubuntu to factory state',
path='HD(1,GPT,...,0x800,0x100000)/File(\\shimx64.efi)'),
})

efi_state_with_dup_rp = EFIBootState(
current='0000',
timeout='0 seconds',
order=['0000', '0002', '0004'],
entries={
'0000': EFIBootEntry(
name='ubuntu',
path='HD(1,GPT,...)/File(\\EFI\\ubuntu\\shimx64.efi)'),
'0001': EFIBootEntry(
name='Windows Boot Manager',
path='HD(1,GPT,...,0x82000)/File(\\EFI\\bootmgfw.efi'),
'0002': EFIBootEntry(
name='Linux-Firmware-Updater',
path='HD(1,GPT,...,0x800,0x100000)/File(\\shimx64.efi)\\.fwupd'),
'0003': EFIBootEntry(
name='Restore Ubuntu to factory state',
path='HD(1,GPT,...,0x800,0x100000)/File(\\shimx64.efi)'),
'0004': EFIBootEntry(
name='Restore Ubuntu to factory state',
path='HD(1,GPT,...,0x800,0x100000)/File(\\shimx64.efi)'),
})


class TestInstallController(unittest.IsolatedAsyncioTestCase):
def setUp(self):
self.controller = InstallController(make_app())
Expand All @@ -141,3 +210,55 @@ async def test_install_package(self, m_sleep):
with patch(run_curtin, side_effect=(error, error, error, error)):
with self.assertRaises(subprocess.CalledProcessError):
await self.controller.install_package(package="git")

def setup_rp_test(self):
app = self.controller.app
fsc = app.controllers.Filesystem
fsc.reset_partition_only = True
app.package_installer = Mock()
app.command_runner = Mock()
self.run = app.command_runner.run = AsyncMock()
app.package_installer.install_pkg = AsyncMock()
app.package_installer.install_pkg.return_value = \
PackageInstallState.DONE
fsm, self.part = make_model_and_partition()

@patch("subiquity.server.controllers.install.get_efibootmgr")
async def test_create_rp_boot_entry_add(self, m_get_efibootmgr):
m_get_efibootmgr.side_effect = iter([
efi_state_no_rp, efi_state_with_rp])
self.setup_rp_test()
await self.controller.create_rp_boot_entry(rp=self.part)
calls = [
call([
'efibootmgr', '--create',
'--loader', '\\EFI\\boot\\shimx64.efi',
'--disk', self.part.device.path,
'--part', str(self.part.number),
'--label', "Restore Ubuntu to factory state",
]),
call([
'efibootmgr', '--bootorder', '0000,0002',
]),
]
self.run.assert_has_awaits(calls)

@patch("subiquity.server.controllers.install.get_efibootmgr")
async def test_create_rp_boot_entry_dup(self, m_get_efibootmgr):
m_get_efibootmgr.side_effect = iter([
efi_state_with_rp, efi_state_with_dup_rp])
self.setup_rp_test()
await self.controller.create_rp_boot_entry(rp=self.part)
calls = [
call([
'efibootmgr', '--create',
'--loader', '\\EFI\\boot\\shimx64.efi',
'--disk', self.part.device.path,
'--part', str(self.part.number),
'--label', "Restore Ubuntu to factory state",
]),
call([
'efibootmgr', '--delete-bootnum', '--bootnum', '0004',
]),
]
self.run.assert_has_awaits(calls)

0 comments on commit cf3de7f

Please sign in to comment.