From 1621f4d262de54d06e30660792deb14edd0a43f0 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Thu, 21 Nov 2024 10:29:41 +0100 Subject: [PATCH] blueprint: when doing GetPartitioning() also validate This commit follows the pattern of `GetRepositories()` to do validation early as part of getting the customization. --- pkg/blueprint/customizations.go | 10 +++++-- pkg/blueprint/customizations_test.go | 42 ++++++++++++++++++++++++++++ pkg/distro/fedora/distro_test.go | 1 + pkg/distro/fedora/imagetype.go | 12 ++++++-- 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/pkg/blueprint/customizations.go b/pkg/blueprint/customizations.go index dfba252cb2..b863e61bd8 100644 --- a/pkg/blueprint/customizations.go +++ b/pkg/blueprint/customizations.go @@ -314,11 +314,15 @@ func (c *Customizations) GetFilesystemsMinSize() uint64 { return agg } -func (c *Customizations) GetPartitioning() *DiskCustomization { +func (c *Customizations) GetPartitioning() (*DiskCustomization, error) { if c == nil { - return nil + return nil, nil } - return c.Disk + if err := c.Disk.Validate(); err != nil { + return nil, err + } + + return c.Disk, nil } func (c *Customizations) GetInstallationDevice() string { diff --git a/pkg/blueprint/customizations_test.go b/pkg/blueprint/customizations_test.go index e3df87d69d..d2da30d20d 100644 --- a/pkg/blueprint/customizations_test.go +++ b/pkg/blueprint/customizations_test.go @@ -378,6 +378,7 @@ func TestGetRPM(t *testing.T) { assert.EqualValues(t, expectedRPM, *retRPM) } + func TestGetRHSM(t *testing.T) { expectedRHSM := RHSMCustomization{ Config: &RHSMConfig{ @@ -493,3 +494,44 @@ func TestGetInstallerErrors(t *testing.T) { }) } } + +func TestGetPartitioningTrivial(t *testing.T) { + expected := DiskCustomization{ + Partitions: []PartitionCustomization{ + { + MinSize: 19, + FilesystemTypedCustomization: FilesystemTypedCustomization{ + FSType: "ext4", + Mountpoint: "/home", + }, + }, + }, + } + + TestCustomizations := Customizations{ + Disk: &expected, + } + + ret, err := TestCustomizations.GetPartitioning() + assert.NoError(t, err) + assert.EqualValues(t, expected, *ret) +} + +func TestGetPartitioningUnhappy(t *testing.T) { + expected := DiskCustomization{ + Partitions: []PartitionCustomization{ + { + FilesystemTypedCustomization: FilesystemTypedCustomization{ + FSType: "ext4", + }, + }, + }, + } + + TestCustomizations := Customizations{ + Disk: &expected, + } + + _, err := TestCustomizations.GetPartitioning() + assert.ErrorContains(t, err, "mountpoint is empty") +} diff --git a/pkg/distro/fedora/distro_test.go b/pkg/distro/fedora/distro_test.go index e75deea029..e7a4e65d21 100644 --- a/pkg/distro/fedora/distro_test.go +++ b/pkg/distro/fedora/distro_test.go @@ -951,6 +951,7 @@ func TestDistro_PartitioningConflict(t *testing.T) { { MinSize: 19, FilesystemTypedCustomization: blueprint.FilesystemTypedCustomization{ + FSType: "ext4", Mountpoint: "/home", }, }, diff --git a/pkg/distro/fedora/imagetype.go b/pkg/distro/fedora/imagetype.go index 60aea17cf7..720e863b81 100644 --- a/pkg/distro/fedora/imagetype.go +++ b/pkg/distro/fedora/imagetype.go @@ -148,7 +148,10 @@ func (t *imageType) getPartitionTable( } imageSize := t.Size(options.Size) - partitioning := customizations.GetPartitioning() + partitioning, err := customizations.GetPartitioning() + if err != nil { + return nil, err + } if partitioning != nil { // Use the new custom partition table to create a PT fully based on the user's customizations. // This overrides FilesystemCustomizations, but we should never have both defined. @@ -374,7 +377,10 @@ func (t *imageType) checkOptions(bp *blueprint.Blueprint, options distro.ImageOp } mountpoints := customizations.GetFilesystems() - partitioning := customizations.GetPartitioning() + partitioning, err := customizations.GetPartitioning() + if err != nil { + return nil, err + } if (len(mountpoints) > 0 || partitioning != nil) && t.rpmOstree { return nil, fmt.Errorf("Custom mountpoints and partitioning are not supported for ostree types") } @@ -406,7 +412,7 @@ func (t *imageType) checkOptions(bp *blueprint.Blueprint, options distro.ImageOp dc := customizations.GetDirectories() fc := customizations.GetFiles() - err := blueprint.ValidateDirFileCustomizations(dc, fc) + err = blueprint.ValidateDirFileCustomizations(dc, fc) if err != nil { return nil, err }