Skip to content

Commit

Permalink
osbuild-image-tests: call osbuild with export option
Browse files Browse the repository at this point in the history
Call osbuild with the export option.  For this we need to unmarshal the
manifest to determine its version and, in the case of v2, the name of
the last pipeline.
  • Loading branch information
achilleas-k committed Feb 24, 2021
1 parent 2006be9 commit 119fc7e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
8 changes: 6 additions & 2 deletions cmd/osbuild-image-tests/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ package constants

import "os/exec"

func GetOsbuildCommand(store, outputDirectory string) *exec.Cmd {
return exec.Command(
func GetOsbuildCommand(store, outputDirectory string, exports []string) *exec.Cmd {
cmd := exec.Command(
"osbuild",
"--store", store,
"--output-directory", outputDirectory,
"--json",
"-",
)
for _, export := range exports {
cmd.Args = append(cmd.Args, "--export", export)
}
return cmd
}

func GetImageInfoCommand(imagePath string) *exec.Cmd {
Expand Down
34 changes: 31 additions & 3 deletions cmd/osbuild-image-tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/osbuild/osbuild-composer/internal/boot/openstacktest"
"github.com/osbuild/osbuild-composer/internal/boot/vmwaretest"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/osbuild1"
"github.com/osbuild/osbuild-composer/internal/osbuild2"
"github.com/osbuild/osbuild-composer/internal/test"
"github.com/osbuild/osbuild-composer/internal/upload/vmware"
)
Expand All @@ -50,8 +52,8 @@ var disableLocalBoot = flag.Bool("disable-local-boot", false, "when this flag is
var failLocalBoot = flag.Bool("fail-local-boot", true, "when this flag is on (default), local boot will fail. Usually indicates missing cloud credentials")

// runOsbuild runs osbuild with the specified manifest and output-directory.
func runOsbuild(manifest []byte, store, outputDirectory string) error {
cmd := constants.GetOsbuildCommand(store, outputDirectory)
func runOsbuild(manifest []byte, store, outputDirectory string, exports []string) error {
cmd := constants.GetOsbuildCommand(store, outputDirectory, exports)

cmd.Stdin = bytes.NewReader(manifest)
var outBuffer bytes.Buffer
Expand Down Expand Up @@ -454,6 +456,29 @@ func testImage(t *testing.T, testcase testcaseStruct, imagePath string) {
}
}

func manifestExports(manifest json.RawMessage) ([]string, error) {
b := bytes.NewReader(manifest)
dec := json.NewDecoder(b)
dec.DisallowUnknownFields()
manifestV1 := new(osbuild1.Manifest)
errV1 := dec.Decode(manifestV1)
if errV1 == nil {
// manifest v1 only exports "assembler"
return []string{"assembler"}, nil
}

b.Reset(manifest)
manifestV2 := new(osbuild2.Manifest)
errV2 := dec.Decode(manifestV2)
if errV2 == nil && manifestV2.Version == "2" {
// manifest v2: export output of last pipeline
return []string{manifestV2.Pipelines[len(manifestV2.Pipelines)-1].Name}, nil
}

errors := fmt.Sprintf("v1 error: %s | v2 error: %s", errV1.Error(), errV2.Error())
return nil, fmt.Errorf("failed to unmarshal manifest: %s", errors)
}

// runTestcase builds the pipeline specified in the testcase and then it
// tests the result
func runTestcase(t *testing.T, testcase testcaseStruct, store string) {
Expand All @@ -466,7 +491,10 @@ func runTestcase(t *testing.T, testcase testcaseStruct, store string) {
require.NoError(t, err, "error removing temporary output directory")
}()

err = runOsbuild(testcase.Manifest, store, outputDirectory)
exports, err := manifestExports(testcase.Manifest)
require.NoError(t, err)

err = runOsbuild(testcase.Manifest, store, outputDirectory, exports)
require.NoError(t, err)

imagePath := fmt.Sprintf("%s/%s", outputDirectory, testcase.ComposeRequest.Filename)
Expand Down

0 comments on commit 119fc7e

Please sign in to comment.