Skip to content

Commit

Permalink
cmd/otk-gen-partition-table: fix to be wrapped in a "tree" json
Browse files Browse the repository at this point in the history
This is similar to osbuild#827 but
this time for the `otk-gen-partition-table` which was forgoten
in this other PR :(
  • Loading branch information
mvo5 authored and achilleas-k committed Sep 2, 2024
1 parent 5106a91 commit b399bd6
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 104 deletions.
14 changes: 11 additions & 3 deletions cmd/otk-gen-partition-table/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (
"github.com/osbuild/images/pkg/osbuild"
)

// Tree is the wrapper aroudn the "real" input
type Tree struct {
Tree Input `json:"tree"`
}

// Input represents the user provided inputs that will be used
// to generate the partition table
type Input struct {
Expand Down Expand Up @@ -239,19 +244,22 @@ func run(r io.Reader, w io.Writer) error {
/* #nosec G404 */
rng := rand.New(rand.NewSource(rngSeed))

var genPartInput Input
if err := json.NewDecoder(r).Decode(&genPartInput); err != nil {
var genPartInputTree Tree
if err := json.NewDecoder(r).Decode(&genPartInputTree); err != nil {
return err
}
// XXX: validate inputs, right now an empty "type" is not an error
// but it should either be an error or we should set a default

output, err := genPartitionTable(&genPartInput, rng)
generated, err := genPartitionTable(&genPartInputTree.Tree, rng)
if err != nil {
return fmt.Errorf("cannot generate partition table: %w", err)
}
// there is no need to output "nice" json, but it does make testing
// simpler
output := map[string]interface{}{
"tree": generated,
}
outputJson, err := json.MarshalIndent(output, "", " ")
if err != nil {
return fmt.Errorf("cannot marshal response: %w", err)
Expand Down
206 changes: 105 additions & 101 deletions cmd/otk-gen-partition-table/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,116 +173,120 @@ func TestUnmarshalOutput(t *testing.T) {

var partInputsSimple = `
{
"properties": {
"create": {
"bios_boot_partition": true,
"esp_partition": true,
"esp_partition_size": "2 GiB"
"tree": {
"properties": {
"create": {
"bios_boot_partition": true,
"esp_partition": true,
"esp_partition_size": "2 GiB"
},
"type": "gpt",
"default_size": "10 GiB"
},
"type": "gpt",
"default_size": "10 GiB"
},
"partitions": [
{
"name": "root",
"mountpoint": "/",
"label": "root",
"size": "7 GiB",
"type": "ext4"
},
{
"name": "home",
"mountpoint": "/home",
"label": "home",
"size": "2 GiB",
"type": "ext4"
}
]
"partitions": [
{
"name": "root",
"mountpoint": "/",
"label": "root",
"size": "7 GiB",
"type": "ext4"
},
{
"name": "home",
"mountpoint": "/home",
"label": "home",
"size": "2 GiB",
"type": "ext4"
}
]
}
}`

// XXX: anything under "internal" we don't actually need to test
// as we do not make any gurantees to the outside
var expectedSimplePartOutput = `{
"const": {
"kernel_opts_list": [],
"partition_map": {
"root": {
"uuid": "9851898e-0b30-437d-8fad-51ec16c3697f"
}
},
"internal": {
"partition-table": {
"Size": 11814305792,
"UUID": "dbd21911-1c4e-4107-8a9f-14fe6e751358",
"Type": "gpt",
"Partitions": [
{
"Start": 1048576,
"Size": 1048576,
"Type": "21686148-6449-6E6F-744E-656564454649",
"Bootable": true,
"UUID": "FAC7F1FB-3E8D-4137-A512-961DE09A5549",
"Payload": null,
"PayloadType": "no-payload"
},
{
"Start": 2097152,
"Size": 2147483648,
"Type": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
"Bootable": false,
"UUID": "68B2905B-DF3E-4FB3-80FA-49D1E773AA33",
"Payload": {
"Type": "vfat",
"UUID": "7B77-95E7",
"Label": "EFI-SYSTEM",
"Mountpoint": "/boot/efi",
"FSTabOptions": "defaults,uid=0,gid=0,umask=077,shortname=winnt",
"FSTabFreq": 0,
"FSTabPassNo": 2
"tree": {
"const": {
"kernel_opts_list": [],
"partition_map": {
"root": {
"uuid": "9851898e-0b30-437d-8fad-51ec16c3697f"
}
},
"internal": {
"partition-table": {
"Size": 11814305792,
"UUID": "dbd21911-1c4e-4107-8a9f-14fe6e751358",
"Type": "gpt",
"Partitions": [
{
"Start": 1048576,
"Size": 1048576,
"Type": "21686148-6449-6E6F-744E-656564454649",
"Bootable": true,
"UUID": "FAC7F1FB-3E8D-4137-A512-961DE09A5549",
"Payload": null,
"PayloadType": "no-payload"
},
"PayloadType": "filesystem"
},
{
"Start": 4297064448,
"Size": 7517224448,
"Type": "",
"Bootable": false,
"UUID": "ed130be6-c822-49af-83bb-4ea648bb2264",
"Payload": {
"Type": "ext4",
"UUID": "9851898e-0b30-437d-8fad-51ec16c3697f",
"Label": "root",
"Mountpoint": "/",
"FSTabOptions": "",
"FSTabFreq": 0,
"FSTabPassNo": 0
{
"Start": 2097152,
"Size": 2147483648,
"Type": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
"Bootable": false,
"UUID": "68B2905B-DF3E-4FB3-80FA-49D1E773AA33",
"Payload": {
"Type": "vfat",
"UUID": "7B77-95E7",
"Label": "EFI-SYSTEM",
"Mountpoint": "/boot/efi",
"FSTabOptions": "defaults,uid=0,gid=0,umask=077,shortname=winnt",
"FSTabFreq": 0,
"FSTabPassNo": 2
},
"PayloadType": "filesystem"
},
"PayloadType": "filesystem"
},
{
"Start": 2149580800,
"Size": 2147483648,
"Type": "",
"Bootable": false,
"UUID": "9f6173fd-edc9-4dbe-9313-632af556c607",
"Payload": {
"Type": "ext4",
"UUID": "d8bb61b8-81cf-4c85-937b-69439a23dc5e",
"Label": "home",
"Mountpoint": "/home",
"FSTabOptions": "",
"FSTabFreq": 0,
"FSTabPassNo": 0
{
"Start": 4297064448,
"Size": 7517224448,
"Type": "",
"Bootable": false,
"UUID": "ed130be6-c822-49af-83bb-4ea648bb2264",
"Payload": {
"Type": "ext4",
"UUID": "9851898e-0b30-437d-8fad-51ec16c3697f",
"Label": "root",
"Mountpoint": "/",
"FSTabOptions": "",
"FSTabFreq": 0,
"FSTabPassNo": 0
},
"PayloadType": "filesystem"
},
"PayloadType": "filesystem"
}
],
"SectorSize": 0,
"ExtraPadding": 0,
"StartOffset": 0
}
},
"filename": "disk.img"
{
"Start": 2149580800,
"Size": 2147483648,
"Type": "",
"Bootable": false,
"UUID": "9f6173fd-edc9-4dbe-9313-632af556c607",
"Payload": {
"Type": "ext4",
"UUID": "d8bb61b8-81cf-4c85-937b-69439a23dc5e",
"Label": "home",
"Mountpoint": "/home",
"FSTabOptions": "",
"FSTabFreq": 0,
"FSTabPassNo": 0
},
"PayloadType": "filesystem"
}
],
"SectorSize": 0,
"ExtraPadding": 0,
"StartOffset": 0
}
},
"filename": "disk.img"
}
}
}
`
Expand Down

0 comments on commit b399bd6

Please sign in to comment.