Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: add otk-make-fstab-stage helper #787

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/otk-make-fstab-stage/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

var Run = run
52 changes: 52 additions & 0 deletions cmd/otk-make-fstab-stage/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/osbuild/images/pkg/osbuild"

"github.com/osbuild/images/internal/otkdisk"
)

type Input = otkdisk.Data

func makeFstabStages(inp Input) (stages *osbuild.Stage, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("cannot generate image prepare stages: %v", r)
}
}()

pt := inp.Const.Internal.PartitionTable
fstabStage := osbuild.NewFSTabStage(osbuild.NewFSTabStageOptions(pt))
achilleas-k marked this conversation as resolved.
Show resolved Hide resolved
return fstabStage, nil
}

func run(r io.Reader, w io.Writer) error {
var inp Input
if err := json.NewDecoder(r).Decode(&inp); err != nil {
return err
}

stage, err := makeFstabStages(inp)
if err != nil {
return fmt.Errorf("cannot make partition stages: %w", err)
}

outputJson, err := json.MarshalIndent(stage, "", " ")
if err != nil {
return fmt.Errorf("cannot marshal response: %w", err)
}
fmt.Fprintf(w, "%s\n", outputJson)
return nil
}

func main() {
if err := run(os.Stdin, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err.Error())
os.Exit(1)
}
}
60 changes: 60 additions & 0 deletions cmd/otk-make-fstab-stage/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main_test

import (
"bytes"
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"

makefstab "github.com/osbuild/images/cmd/otk-make-fstab-stage"
"github.com/osbuild/images/internal/otkdisk"
"github.com/osbuild/images/internal/testdisk"
)

// this is not symetrical to the output, this is sad but also
// okay because the input is really just a dump of the internal
// disk.PartitionTable so encoding it in json here will not add
// a benefit for the test
var minimalInputBase = makefstab.Input{
Const: otkdisk.Const{
Internal: otkdisk.Internal{
PartitionTable: testdisk.MakeFakePartitionTable("/", "/var"),
},
},
}

var minimalExpectedStages = `{
"type": "org.osbuild.fstab",
"options": {
"filesystems": [
{
"uuid": "6264D520-3FB9-423F-8AB8-7A0A8E3D3562",
"vfs_type": "ext4",
"path": "/"
},
{
"uuid": "CB07C243-BC44-4717-853E-28852021225B",
"vfs_type": "ext4",
"path": "/var"
}
]
}
}
`

func TestIntegration(t *testing.T) {
minimalInput := minimalInputBase
minimalInput.Const.Filename = "disk.img"
expectedStages := minimalExpectedStages

inpJSON, err := json.Marshal(&minimalInput)
assert.NoError(t, err)
fakeStdin := bytes.NewBuffer(inpJSON)
fakeStdout := bytes.NewBuffer(nil)

err = makefstab.Run(fakeStdin, fakeStdout)
assert.NoError(t, err)

assert.Equal(t, expectedStages, fakeStdout.String())
}
Loading