Skip to content

Commit

Permalink
cmd/otk-resolve-ostree-commit: support mocking ostree resolve calls
Browse files Browse the repository at this point in the history
Read the OTK_UNDER_TEST env var, which we use in otk to enable test mode
for our resolvers, and return a mocked checksum.
  • Loading branch information
achilleas-k authored and mvo5 committed Sep 24, 2024
1 parent 1affbbc commit 890900a
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions cmd/otk-resolve-ostree-commit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"

"github.com/osbuild/images/internal/cmdutil"
"github.com/osbuild/images/pkg/ostree"
)

Expand Down Expand Up @@ -48,16 +49,31 @@ type OutputConst struct {
Checksum string `json:"checksum"`
}

// for mocking in testing
var osLookupEnv = os.LookupEnv

func underTest() bool {
testVar, found := osLookupEnv("OTK_UNDER_TEST")
return found && testVar == "1"
}

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

sourceSpec := ostree.SourceSpec(inputTree.Tree)
commitSpec, err := ostree.Resolve(sourceSpec)
if err != nil {
return fmt.Errorf("failed to resolve ostree commit: %w", err)

var commitSpec ostree.CommitSpec
if !underTest() {
var err error
commitSpec, err = ostree.Resolve(sourceSpec)
if err != nil {
return fmt.Errorf("failed to resolve ostree commit: %w", err)
}
} else {
commitSpec = cmdutil.MockOSTreeResolve(sourceSpec)
}

output := map[string]Output{
Expand Down

0 comments on commit 890900a

Please sign in to comment.