Skip to content
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
13 changes: 13 additions & 0 deletions pkg/osbuild/osbuild-exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,16 @@ func OSBuildVersion() (string, error) {
version = strings.TrimSpace(version)
return version, nil
}

// OSBuildInspect converts a manifest to an inspected manifest.
func OSBuildInspect(manifest []byte) ([]byte, error) {
cmd := exec.Command("osbuild", "--inspect")
cmd.Stdin = bytes.NewBuffer(manifest)

out, err := cmd.Output()
if err != nil {
return nil, err
}

return out, nil
}
33 changes: 33 additions & 0 deletions pkg/osbuild/osbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// OSBuild (schema v2) types.
package osbuild

import (
"encoding/json"
"fmt"
)

const (
// should be "^\\/(?!\\.\\.)((?!\\/\\.\\.\\/).)+$" but Go doesn't support lookaheads
// therefore we have to instead check for the invalid cases, which is much simpler
Expand Down Expand Up @@ -51,3 +56,31 @@ func (p *Pipeline) AddStages(stages ...*Stage) {
p.AddStage(stage)
}
}

// Take some bytes and deserialize them into a Manifest; mostly used to take
// an inspected manifest
func NewManifestFromBytes(data []byte) (*Manifest, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this for a real manifest but it did not go very well, it dies with json: cannot unmarshal object into Go struct field Stage.pipelines.stages.inputs of type osbuild.Inputs and looking at our osbuild inputs it seems we need to add quite a bit of custom unmarshaling in this area. What is the idea here? Will we expand the osbuild package so that it can unmarshal things properly back? it would be nice for tests as well to be able to transform the json back into more managable structs.

manifest := &Manifest{}

if err := json.Unmarshal(data, &manifest); err != nil {
return nil, err
}

return manifest, nil
}

// GetID gets the pipeline identifiers for an *inspected* manifest. These are
// not available for non-inspected manifests and will return an error there.
func (p *Pipeline) GetID() (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a test for this or is it very difficult to do? We might also want to expand the docstring as AIUI this will only return something if the manifest was run through osbuild-inspect before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring expanded, test included; I'm using a fake manifest, we're not currently mocking any osbuild calls anywhere in our test cases that I could find so I'm doing this minimum for now so I can start integrating this and test the actual osbuild execution there.

if len(p.Stages) == 0 {
return "", fmt.Errorf("no stages in manifest")
}

lastStage := p.Stages[len(p.Stages)-1]

if len(lastStage.ID) == 0 {
return "", fmt.Errorf("un-inspected manifest, identifiers are not available")
}

return lastStage.ID, nil
}
32 changes: 32 additions & 0 deletions pkg/osbuild/osbuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,35 @@ func TestPipeline_AddStage(t *testing.T) {
assert.Equal(t, expectedPipeline, actualPipeline)
assert.Equal(t, 1, len(actualPipeline.Stages))
}

var fakeOsbuildManifestWithIdentifiers = []byte(`{
"version": "2",
"pipelines": [
{
"name": "build",
"stages": [
{
"id": "1234",
"type": "org.osbuild.rpm"
},
{
"id": "5678",
"type": "org.osbuild.mkdir"
}
]
}
]
}`)

func TestManifestFromBytes(t *testing.T) {
manifest, err := NewManifestFromBytes(fakeOsbuildManifestWithIdentifiers)
assert.NoError(t, err)

assert.Equal(t, manifest.Pipelines[0].Stages[0].ID, "1234")
assert.Equal(t, manifest.Pipelines[0].Stages[1].ID, "5678")

pID, err := manifest.Pipelines[0].GetID()
assert.NoError(t, err)

assert.Equal(t, pID, "5678")
}
3 changes: 3 additions & 0 deletions pkg/osbuild/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ type Stage struct {
// Well-known name in reverse domain-name notation, uniquely identifying
// the stage type.
Type string `json:"type"`

ID string `json:"id,omitempty"`

// Stage-type specific options fully determining the operations of the

Inputs Inputs `json:"inputs,omitempty"`
Expand Down
Loading