From 8be2c5c89ab1d4fc33872adfc2dd6f671e65ad62 Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Wed, 27 Apr 2022 19:29:15 +0200 Subject: [PATCH] disk: test minimum directory size calculations / and /usr have minimum sizes defined (1 GiB and 2 GiB respectively). When /usr is not defined, the minimum size of /usr gets added to the minimum size for /. This new test runs through a few scenarios and checks whether the sizes fit. --- internal/disk/disk_test.go | 115 +++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/internal/disk/disk_test.go b/internal/disk/disk_test.go index 2699c8e58..6f9b38d36 100644 --- a/internal/disk/disk_test.go +++ b/internal/disk/disk_test.go @@ -502,6 +502,121 @@ func TestCreatePartitionTableLVMify(t *testing.T) { } } +func TestMinimumSizes(t *testing.T) { + assert := assert.New(t) + + // math/rand is good enough in this case + /* #nosec G404 */ + rng := rand.New(rand.NewSource(13)) + pt := testPartitionTables["plain"] + + type testCase struct { + Blueprint []blueprint.FilesystemCustomization + ExpectedMinSizes map[string]uint64 + } + + testCases := []testCase{ + { // specify small /usr -> / and /usr get default size + Blueprint: []blueprint.FilesystemCustomization{ + { + Mountpoint: "/usr", + MinSize: 1 * MiB, + }, + }, + ExpectedMinSizes: map[string]uint64{ + "/usr": 2 * GiB, + "/": 1 * GiB, + }, + }, + { // specify small / and /usr -> / and /usr get default size + Blueprint: []blueprint.FilesystemCustomization{ + { + Mountpoint: "/", + MinSize: 1 * MiB, + }, + { + Mountpoint: "/usr", + MinSize: 1 * KiB, + }, + }, + ExpectedMinSizes: map[string]uint64{ + "/usr": 2 * GiB, + "/": 1 * GiB, + }, + }, + { // big /usr -> / gets default size + Blueprint: []blueprint.FilesystemCustomization{ + { + Mountpoint: "/usr", + MinSize: 10 * GiB, + }, + }, + ExpectedMinSizes: map[string]uint64{ + "/usr": 10 * GiB, + "/": 1 * GiB, + }, + }, + { + Blueprint: []blueprint.FilesystemCustomization{ + { + Mountpoint: "/", + MinSize: 10 * GiB, + }, + { + Mountpoint: "/home", + MinSize: 1 * MiB, + }, + }, + ExpectedMinSizes: map[string]uint64{ + "/": 10 * GiB, + "/home": 1 * GiB, + }, + }, + { // no separate /usr and no size for / -> / gets sum of default sizes for / and /usr + Blueprint: []blueprint.FilesystemCustomization{ + { + Mountpoint: "/opt", + MinSize: 10 * GiB, + }, + }, + ExpectedMinSizes: map[string]uint64{ + "/opt": 10 * GiB, + "/": 3 * GiB, + }, + }, + } + + for idx, tc := range testCases { + { // without LVM + mpt, err := NewPartitionTable(&pt, tc.Blueprint, uint64(3*GiB), false, rng) + assert.NoError(err) + for mnt, minSize := range tc.ExpectedMinSizes { + path := entityPath(mpt, mnt) + assert.NotNil(path, "[%d] mountpoint %q not found", idx, mnt) + parent := path[1] + part, ok := parent.(*Partition) + assert.True(ok, "%q parent (%v) is not a partition", mnt, parent) + assert.GreaterOrEqual(part.GetSize(), minSize, + "[%d] %q size %d should be greater or equal to %d", idx, mnt, part.GetSize(), minSize) + } + } + + { // with LVM + mpt, err := NewPartitionTable(&pt, tc.Blueprint, uint64(3*GiB), true, rng) + assert.NoError(err) + for mnt, minSize := range tc.ExpectedMinSizes { + path := entityPath(mpt, mnt) + assert.NotNil(path, "[%d] mountpoint %q not found", idx, mnt) + parent := path[1] + part, ok := parent.(*LVMLogicalVolume) + assert.True(ok, "[%d] %q parent (%v) is not an LVM logical volume", idx, mnt, parent) + assert.GreaterOrEqual(part.GetSize(), minSize, + "[%d] %q size %d should be greater or equal to %d", idx, mnt, part.GetSize(), minSize) + } + } + } +} + func collectEntities(pt *PartitionTable) []Entity { entities := make([]Entity, 0) collector := func(ent Entity, path []Entity) error {