Skip to content

Commit

Permalink
libpod/image: unit tests: use require.NoError
Browse files Browse the repository at this point in the history
In contrast to `assert.NoError`, `require.NoError` treats mismatches
fatally which in many cases is necessary to prevent subsequent checks
from segfaulting.

Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg authored and mheon committed Mar 29, 2021
1 parent a01b387 commit 396a59b
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions libpod/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/containers/storage/pkg/reexec"
"github.com/opencontainers/go-digest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
Expand Down Expand Up @@ -107,7 +108,7 @@ func TestImage_NewFromLocal(t *testing.T) {
assert.NoError(t, err)
for _, name := range image.names {
newImage, err := ir.NewFromLocal(name)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, newImage.ID(), image.img.ID())
}
}
Expand Down Expand Up @@ -142,7 +143,7 @@ func TestImage_New(t *testing.T) {
// after the pull
for _, img := range names {
newImage, err := ir.New(context.Background(), img, "", "", writer, nil, SigningOptions{}, nil, util.PullImageMissing, nil)
assert.NoError(t, err)
require.NoError(t, err)
assert.NotEqual(t, newImage.ID(), "")
err = newImage.Remove(context.Background(), false)
assert.NoError(t, err)
Expand All @@ -168,14 +169,14 @@ func TestImage_MatchRepoTag(t *testing.T) {
GraphRoot: workdir,
}
ir, err := NewImageRuntimeFromOptions(so)
assert.NoError(t, err)
require.NoError(t, err)
ir.Eventer = events.NewNullEventer()
newImage, err := ir.New(context.Background(), "busybox", "", "", os.Stdout, nil, SigningOptions{}, nil, util.PullImageMissing, nil)
assert.NoError(t, err)
require.NoError(t, err)
err = newImage.TagImage("foo:latest")
assert.NoError(t, err)
require.NoError(t, err)
err = newImage.TagImage("foo:bar")
assert.NoError(t, err)
require.NoError(t, err)

// Tests start here.
for _, name := range bbNames {
Expand All @@ -188,12 +189,12 @@ func TestImage_MatchRepoTag(t *testing.T) {

// foo should resolve to foo:latest
repoTag, err := newImage.MatchRepoTag("foo")
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "localhost/foo:latest", repoTag)

// foo:bar should resolve to foo:bar
repoTag, err = newImage.MatchRepoTag("foo:bar")
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "localhost/foo:bar", repoTag)
// Shutdown the runtime and remove the temporary storage
cleanup(workdir, ir)
Expand All @@ -202,9 +203,7 @@ func TestImage_MatchRepoTag(t *testing.T) {
// TestImage_RepoDigests tests RepoDigest generation.
func TestImage_RepoDigests(t *testing.T) {
dgst, err := digest.Parse("sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

for _, tt := range []struct {
name string
Expand Down Expand Up @@ -236,10 +235,7 @@ func TestImage_RepoDigests(t *testing.T) {
},
}
actual, err := image.RepoDigests()
if err != nil {
t.Fatal(err)
}

require.NoError(t, err)
assert.Equal(t, test.expected, actual)

image = &Image{
Expand All @@ -249,10 +245,7 @@ func TestImage_RepoDigests(t *testing.T) {
},
}
actual, err = image.RepoDigests()
if err != nil {
t.Fatal(err)
}

require.NoError(t, err)
assert.Equal(t, test.expected, actual)
})
}
Expand Down

0 comments on commit 396a59b

Please sign in to comment.