diff --git a/cmd/otk-make-fstab-stage/main.go b/cmd/otk-make-fstab-stage/main.go index 69123ea1fe..ff1a1a31da 100644 --- a/cmd/otk-make-fstab-stage/main.go +++ b/cmd/otk-make-fstab-stage/main.go @@ -20,6 +20,9 @@ func run(r io.Reader, w io.Writer) error { if err := json.NewDecoder(r).Decode(&inp); err != nil { return err } + if err := inp.Tree.Validate(); err != nil { + return fmt.Errorf("cannot validate input data: %w", err) + } opts, err := osbuild.NewFSTabStageOptions(inp.Tree.Const.Internal.PartitionTable) if err != nil { diff --git a/cmd/otk-make-fstab-stage/main_test.go b/cmd/otk-make-fstab-stage/main_test.go index a2c664be84..623bb1f11a 100644 --- a/cmd/otk-make-fstab-stage/main_test.go +++ b/cmd/otk-make-fstab-stage/main_test.go @@ -62,3 +62,10 @@ func TestIntegration(t *testing.T) { assert.Equal(t, expectedStages, fakeStdout.String()) } + +func TestIntegrationNoPartitionTable(t *testing.T) { + fakeStdin := bytes.NewBufferString(`{}`) + fakeStdout := bytes.NewBuffer(nil) + err := makefstab.Run(fakeStdin, fakeStdout) + assert.EqualError(t, err, "cannot validate input data: no partition table") +} diff --git a/cmd/otk-make-partition-mounts-devices/main.go b/cmd/otk-make-partition-mounts-devices/main.go index 3cfe02c970..80469b91ee 100644 --- a/cmd/otk-make-partition-mounts-devices/main.go +++ b/cmd/otk-make-partition-mounts-devices/main.go @@ -25,6 +25,9 @@ func run(r io.Reader, w io.Writer) error { if err := json.NewDecoder(r).Decode(&inp); err != nil { return err } + if err := inp.Tree.Validate(); err != nil { + return fmt.Errorf("cannot validate input data: %w", err) + } rootMntName, mounts, devices, err := osbuild.GenMountsDevicesFromPT(inp.Tree.Const.Filename, inp.Tree.Const.Internal.PartitionTable) if err != nil { diff --git a/cmd/otk-make-partition-mounts-devices/main_test.go b/cmd/otk-make-partition-mounts-devices/main_test.go index da2e68088f..2bf622e284 100644 --- a/cmd/otk-make-partition-mounts-devices/main_test.go +++ b/cmd/otk-make-partition-mounts-devices/main_test.go @@ -81,3 +81,10 @@ func TestIntegration(t *testing.T) { assert.NoError(t, err) assert.Equal(t, expectedOutput, fakeStdout.String()) } + +func TestIntegrationNoPartitionTable(t *testing.T) { + fakeStdin := bytes.NewBufferString(`{}`) + fakeStdout := bytes.NewBuffer(nil) + err := mkdevmnt.Run(fakeStdin, fakeStdout) + assert.EqualError(t, err, "cannot validate input data: no partition table") +} diff --git a/cmd/otk-make-partition-stages/main.go b/cmd/otk-make-partition-stages/main.go index 2631421731..62bc839010 100644 --- a/cmd/otk-make-partition-stages/main.go +++ b/cmd/otk-make-partition-stages/main.go @@ -34,6 +34,9 @@ func run(r io.Reader, w io.Writer) error { if err := json.NewDecoder(r).Decode(&inp); err != nil { return err } + if err := inp.Tree.Validate(); err != nil { + return fmt.Errorf("cannot validate input data: %w", err) + } stages, err := makeImagePrepareStages(inp, inp.Tree.Const.Filename) if err != nil { diff --git a/cmd/otk-make-partition-stages/main_test.go b/cmd/otk-make-partition-stages/main_test.go index d6e595d5c6..e429d4afce 100644 --- a/cmd/otk-make-partition-stages/main_test.go +++ b/cmd/otk-make-partition-stages/main_test.go @@ -125,3 +125,10 @@ func TestModificationFname(t *testing.T) { assert.Equal(t, expectedStages, fakeStdout.String()) } + +func TestIntegrationNoPartitionTable(t *testing.T) { + fakeStdin := bytes.NewBufferString(`{}`) + fakeStdout := bytes.NewBuffer(nil) + err := makestages.Run(fakeStdin, fakeStdout) + assert.EqualError(t, err, "cannot validate input data: no partition table") +} diff --git a/internal/otkdisk/partition.go b/internal/otkdisk/partition.go index 151884c52f..1803739085 100644 --- a/internal/otkdisk/partition.go +++ b/internal/otkdisk/partition.go @@ -15,6 +15,11 @@ type Data struct { Const Const `json:"const"` } +// Validate does basic validation of the data +func (d Data) Validate() error { + return d.Const.Internal.Validate() +} + // Const contains partition table data that is considered "constant", // i.e. that should not be modified by the consumer as there may be // inter-dependencies between the values @@ -52,6 +57,14 @@ type Internal struct { PartitionTable *disk.PartitionTable `json:"partition-table"` } +// Validate does basic validation of the internal data +func (i Internal) Validate() error { + if i.PartitionTable == nil { + return fmt.Errorf("no partition table") + } + return nil +} + // PartType represents a partition type type PartType string diff --git a/internal/otkdisk/partition_test.go b/internal/otkdisk/partition_test.go index b07b4e696b..f9aadd87f3 100644 --- a/internal/otkdisk/partition_test.go +++ b/internal/otkdisk/partition_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/osbuild/images/internal/otkdisk" + "github.com/osbuild/images/pkg/disk" ) func TestPartTypeValidationHappy(t *testing.T) { @@ -17,3 +18,13 @@ func TestPartTypeValidationSad(t *testing.T) { assert.EqualError(t, otkdisk.PartTypeUnset.Validate(), `unsupported partition type ""`) assert.EqualError(t, otkdisk.PartType("foo").Validate(), `unsupported partition type "foo"`) } + +func TestDataValidates(t *testing.T) { + // sad + d := otkdisk.Data{} + assert.EqualError(t, d.Validate(), "no partition table") + // happy + d.Const.Internal.PartitionTable = &disk.PartitionTable{} + err := d.Validate() + assert.NoError(t, err) +}