Skip to content

Commit

Permalink
identity: move config validation to load_autoinstall_data
Browse files Browse the repository at this point in the history
Moves the logic for checking if user-data or identity section is
provided (on server) in the autoinstall config to the
load_autoinstall_data function. Without this change, the exception
thrown in apply_autoinstall_config won't halt the installer until
the postinstall steps (LP: 2060547).
  • Loading branch information
Chris-Peterson444 committed Apr 8, 2024
1 parent 16bcfcf commit 6415536
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
27 changes: 20 additions & 7 deletions subiquity/server/controllers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from subiquity.common.apidef import API
from subiquity.common.resources import resource_path
from subiquity.common.types import IdentityData, UsernameValidation
from subiquity.server.autoinstall import AutoinstallError
from subiquity.server.controller import SubiquityController
from subiquitycore.context import with_context

Expand Down Expand Up @@ -79,18 +80,30 @@ def load_autoinstall_data(self, data):
crypted_password=data["password"],
)
self.model.add_user(identity_data)

@with_context()
async def apply_autoinstall_config(self, context=None):
if self.model.user:
return

# The identity section is required except if:
# 1. a user-data section is provided
# 2. we are on not-Server (e.g., Desktop)
# 3. we are only refreshing the reset partition
if "user-data" in self.app.autoinstall_config:
return
if self.app.base_model.target is None:
return
if self.app.base_model.source.current.variant != "server":
return
raise Exception("neither identity nor user-data provided")
# The identity controller doesn't figure this out until the apply
# step, so we are going to cheat and inspect the situation here
storage_config = self.app.autoinstall_config.get("storage")
if (
storage_config is not None
and storage_config.get("layout") is not None
and storage_config["layout"].get("reset-partition-only")
):
return
raise AutoinstallError("neither identity nor user-data provided")

@with_context()
async def apply_autoinstall_config(self, context=None):
pass

def make_autoinstall(self):
if self.model.user is None:
Expand Down
26 changes: 23 additions & 3 deletions subiquity/server/controllers/tests/test_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jsonschema
from jsonschema.validators import validator_for

from subiquity.server.autoinstall import AutoinstallError
from subiquity.server.controllers.identity import IdentityController
from subiquitycore.tests import SubiTestCase
from subiquitycore.tests.mocks import make_app
Expand All @@ -42,10 +43,29 @@ def setUp(self):

async def test_server_requires_identity_case_4a1(self):
self.app.base_model.source.current.variant = "server"
with self.assertRaises(Exception):
await self.ic.apply_autoinstall_config()
with self.assertRaises(AutoinstallError):
self.ic.load_autoinstall_data(None)

async def test_server_requires_identity_case_4a1__reset_only_true(self):
"""Test no require identity for reset-partition-only=yes."""
self.app.base_model.source.current.variant = "server"

# No raise if reset-parition-only specified
self.app.autoinstall_config = {
"storage": {"layout": {"reset-partition-only": True}}
}
self.ic.load_autoinstall_data(None)

async def test_server_requires_identity_case_4a1__reset_only_false(self):
"""Test require identity for reset-partition-only=no."""
self.app.base_model.source.current.variant = "server"

# raises if no reset-parition-only in storage:layout:
self.app.autoinstall_config = {"storage": {"layout": {}}}
with self.assertRaises(AutoinstallError):
self.ic.load_autoinstall_data(None)

async def test_desktop_does_not_require_identity_case_4a2(self):
self.app.base_model.source.current.variant = "desktop"
await self.ic.apply_autoinstall_config()
self.ic.load_autoinstall_data(None)
# should not raise

0 comments on commit 6415536

Please sign in to comment.