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/otk-resolve-ostree-commit: support mocking ostree resolve calls #945

Merged
merged 3 commits into from
Sep 24, 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
11 changes: 1 addition & 10 deletions cmd/gen-manifests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,7 @@ func mockResolveCommits(commitSources map[string][]ostree.SourceSpec) map[string
for name, commitSources := range commitSources {
commitSpecs := make([]ostree.CommitSpec, len(commitSources))
for idx, commitSource := range commitSources {
checksum := fmt.Sprintf("%x", sha256.Sum256([]byte(commitSource.URL+commitSource.Ref)))
spec := ostree.CommitSpec{
Ref: commitSource.Ref,
URL: commitSource.URL,
Checksum: checksum,
}
if commitSource.RHSM {
spec.Secrets = "org.osbuild.rhsm.consumer"
}
commitSpecs[idx] = spec
commitSpecs[idx] = cmdutil.MockOSTreeResolve(commitSource)
}
commits[name] = commitSpecs
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/otk-resolve-ostree-commit/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,16 @@ package main
var (
Run = run
)

func MockEnvLookup() (restore func()) {
saved := osLookupEnv
osLookupEnv = func(key string) (string, bool) {
if key == "OTK_UNDER_TEST" {
return "1", true
}
return "", false
}
return func() {
osLookupEnv = saved
}
}
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
32 changes: 32 additions & 0 deletions cmd/otk-resolve-ostree-commit/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,35 @@ func TestResolverErrors(t *testing.T) {
})
}
}

func TestMockResolve(t *testing.T) {
restore := resolver.MockEnvLookup()
defer restore()

assert := assert.New(t)

inputReq := `
{
"tree": {
"ref": "otk/ostree/test",
"url": "https://ostree.example.org/repo"
}
}
`
expOutput := `{
"tree": {
"const": {
"ref": "otk/ostree/test",
"url": "https://ostree.example.org/repo",
"checksum": "5c1c0655481926623eca85109dd4017c8dba320ea1473c42b43005db6d900a60"
}
}
}
`
input := bytes.NewBuffer([]byte(inputReq))
output := &bytes.Buffer{}

assert.NoError(resolver.Run(input, output))

assert.Equal(expOutput, output.String())
}
21 changes: 21 additions & 0 deletions internal/cmdutil/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmdutil

import (
"crypto/sha256"
"fmt"

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

func MockOSTreeResolve(commitSource ostree.SourceSpec) ostree.CommitSpec {
checksum := fmt.Sprintf("%x", sha256.Sum256([]byte(commitSource.URL+commitSource.Ref)))
spec := ostree.CommitSpec{
Ref: commitSource.Ref,
URL: commitSource.URL,
Checksum: checksum,
}
if commitSource.RHSM {
spec.Secrets = "org.osbuild.rhsm.consumer"
}
return spec
}
Loading