diff --git a/cmd/gen-manifests/main.go b/cmd/gen-manifests/main.go index 24a6d27939..64e717afd3 100644 --- a/cmd/gen-manifests/main.go +++ b/cmd/gen-manifests/main.go @@ -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 } diff --git a/cmd/otk-resolve-ostree-commit/export_test.go b/cmd/otk-resolve-ostree-commit/export_test.go index 77c5f1a937..2de3e0d412 100644 --- a/cmd/otk-resolve-ostree-commit/export_test.go +++ b/cmd/otk-resolve-ostree-commit/export_test.go @@ -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 + } +} diff --git a/cmd/otk-resolve-ostree-commit/main.go b/cmd/otk-resolve-ostree-commit/main.go index e874443a37..ee051ce9a3 100644 --- a/cmd/otk-resolve-ostree-commit/main.go +++ b/cmd/otk-resolve-ostree-commit/main.go @@ -6,6 +6,7 @@ import ( "io" "os" + "github.com/osbuild/images/internal/cmdutil" "github.com/osbuild/images/pkg/ostree" ) @@ -48,6 +49,14 @@ 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 { @@ -55,9 +64,16 @@ func run(r io.Reader, w io.Writer) error { } 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{ diff --git a/cmd/otk-resolve-ostree-commit/main_test.go b/cmd/otk-resolve-ostree-commit/main_test.go index 841ff5622f..2139dcfb43 100644 --- a/cmd/otk-resolve-ostree-commit/main_test.go +++ b/cmd/otk-resolve-ostree-commit/main_test.go @@ -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()) +} diff --git a/internal/cmdutil/mock.go b/internal/cmdutil/mock.go new file mode 100644 index 0000000000..17f3993f28 --- /dev/null +++ b/internal/cmdutil/mock.go @@ -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 +}