Skip to content

Commit

Permalink
osbuild: include context in skopedo error messages
Browse files Browse the repository at this point in the history
When trying to create enough data for a full pipeline run for
#287 I struggled a bit
because it was not clear which items where faulty.

This commit adds more context to the errors. Sadly I had to
implement `assertPanicsWithErrorRegexp` because of
stretchr/testify#1304

I can contribute it upstream but for that it needs some tests
around it first :)
  • Loading branch information
mvo5 committed Nov 29, 2023
1 parent 3e1c4e3 commit b8cf781
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
7 changes: 3 additions & 4 deletions pkg/osbuild/skopeo_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ func NewSkopeoSourceItem(name, digest string, tlsVerify *bool) SkopeoSourceItem
}

func (item SkopeoSourceItem) validate() error {

if item.Image.Name == "" {
return fmt.Errorf("source item has empty name")
return fmt.Errorf("source item %#v has empty name", item)
}

if !skopeoDigestPattern.MatchString(item.Image.Digest) {
return fmt.Errorf("source item has invalid digest")
return fmt.Errorf("source item %#v has invalid digest", item)
}

return nil
Expand All @@ -63,7 +62,7 @@ func NewSkopeoSource() *SkopeoSource {
func (source *SkopeoSource) AddItem(name, digest, image string, tlsVerify *bool) {
item := NewSkopeoSourceItem(name, digest, tlsVerify)
if !skopeoDigestPattern.MatchString(image) {
panic("item has invalid image id")
panic(fmt.Errorf("item %#v has invalid image id", image))
}
source.Items[image] = item
}
32 changes: 27 additions & 5 deletions pkg/osbuild/skopeo_source_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package osbuild

import (
"fmt"
"regexp"
"testing"

"github.com/stretchr/testify/assert"

"github.com/osbuild/images/internal/common"
)

// needed until https://github.com/stretchr/testify/issues/1304 is fixed
func assertPanicsWithErrorRegexp(t *testing.T, reg *regexp.Regexp, f assert.PanicTestFunc) (assertOk bool) {
defer func() {
var message interface{} // nolint: gosimple

message = recover()
err, ok := message.(error)
if !ok || err == nil {
assert.Fail(t, fmt.Sprintf("func %#v should return an error but got: %[1]v (type %[1]T)", f, message))
}
assertOk = assert.Regexp(t, reg, err.Error())
}()

f()
return assert.Fail(t, fmt.Sprintf("func %#v should panic but did not", f))
}

func TestNewSkopeoSource(t *testing.T) {
testDigest := "sha256:f29b6cd42a94a574583439addcd6694e6224f0e4b32044c9e3aee4c4856c2a50"
imageID := "sha256:c2ecf25cf190e76b12b07436ad5140d4ba53d8a136d498705e57a006837a720f"
Expand All @@ -33,27 +52,30 @@ func TestNewSkopeoSource(t *testing.T) {
assert.Nil(t, item.Image.TLSVerify)

// empty name
assert.Panics(t, func() {
expectedErr := regexp.MustCompile(`source item osbuild.SkopeoSourceItem.* has empty name`)
assertPanicsWithErrorRegexp(t, expectedErr, func() {
source.AddItem("", testDigest, imageID, nil)
})

// empty digest
assert.Panics(t, func() {
expectedErr = regexp.MustCompile(`source item osbuild.SkopeoSourceItem.* has invalid digest`)
assertPanicsWithErrorRegexp(t, expectedErr, func() {
source.AddItem("name", "", imageID, nil)
})

// empty image id
assert.Panics(t, func() {
assert.PanicsWithError(t, `item "" has invalid image id`, func() {
source.AddItem("name", testDigest, "", nil)
})

// invalid digest
assert.Panics(t, func() {
expectedErr = regexp.MustCompile(`item osbuild.SkopeoSourceItem.* has invalid digest`)
assertPanicsWithErrorRegexp(t, expectedErr, func() {
source.AddItem("name", "foo", imageID, nil)
})

// invalid image id
assert.Panics(t, func() {
assert.PanicsWithError(t, `item "sha256:foo" has invalid image id`, func() {
source.AddItem("name", testDigest, "sha256:foo", nil)
})
}

0 comments on commit b8cf781

Please sign in to comment.