Skip to content

Commit

Permalink
Introduce mocks.ForbiddenUnparsedImage, use it to simplify tests
Browse files Browse the repository at this point in the history
Should not change (test) behavior.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
  • Loading branch information
mtrmac committed Jun 11, 2022
1 parent 29966d5 commit 2907fba
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
20 changes: 20 additions & 0 deletions internal/testing/mocks/unparsed_image.go
@@ -0,0 +1,20 @@
package mocks

import (
"context"

"github.com/containers/image/v5/types"
)

// ForbiddenUnparsedImage is used when we don't expect the UnparsedImage to be used in our tests.
type ForbiddenUnparsedImage struct{}

func (ref ForbiddenUnparsedImage) Reference() types.ImageReference {
panic("unexpected call to a mock function")
}
func (ref ForbiddenUnparsedImage) Manifest(ctx context.Context) ([]byte, string, error) {
panic("unexpected call to a mock function")
}
func (ref ForbiddenUnparsedImage) Signatures(context.Context) ([][]byte, error) {
panic("unexpected call to a mock function")
}
2 changes: 1 addition & 1 deletion signature/policy_eval_simple_test.go
Expand Up @@ -10,7 +10,7 @@ import (

// nameOnlyImageMock is a mock of types.UnparsedImage which only allows transports.ImageName to work
type nameOnlyImageMock struct {
forbiddenImageMock
mocks.ForbiddenUnparsedImage
}

func (nameOnlyImageMock) Reference() types.ImageReference {
Expand Down
47 changes: 12 additions & 35 deletions signature/policy_reference_match_test.go
@@ -1,7 +1,6 @@
package signature

import (
"context"
"fmt"
"testing"

Expand Down Expand Up @@ -29,13 +28,13 @@ func TestParseImageAndDockerReference(t *testing.T) {
// Success
ref, err := reference.ParseNormalizedNamed(ok1)
require.NoError(t, err)
r1, r2, err := parseImageAndDockerReference(refImageMock{ref}, ok2)
r1, r2, err := parseImageAndDockerReference(refImageMock{ref: ref}, ok2)
require.NoError(t, err)
assert.Equal(t, ok1, reference.FamiliarString(r1))
assert.Equal(t, ok2, reference.FamiliarString(r2))

// Unidentified images are rejected.
_, _, err = parseImageAndDockerReference(refImageMock{nil}, ok2)
_, _, err = parseImageAndDockerReference(refImageMock{ref: nil}, ok2)
require.Error(t, err)
assert.IsType(t, PolicyRequirementError(""), err)

Expand All @@ -47,30 +46,21 @@ func TestParseImageAndDockerReference(t *testing.T) {
} {
ref, err := reference.ParseNormalizedNamed(refs[0])
if err == nil {
_, _, err := parseImageAndDockerReference(refImageMock{ref}, refs[1])
_, _, err := parseImageAndDockerReference(refImageMock{ref: ref}, refs[1])
assert.Error(t, err)
}
}
}

// refImageMock is a mock of types.UnparsedImage which returns itself in Reference().DockerReference.
type refImageMock struct{ ref reference.Named }
type refImageMock struct {
mocks.ForbiddenUnparsedImage
ref reference.Named
}

func (ref refImageMock) Reference() types.ImageReference {
return refImageReferenceMock{ref: ref.ref}
}
func (ref refImageMock) Close() error {
panic("unexpected call to a mock function")
}
func (ref refImageMock) Manifest(ctx context.Context) ([]byte, string, error) {
panic("unexpected call to a mock function")
}
func (ref refImageMock) Signatures(context.Context) ([][]byte, error) {
panic("unexpected call to a mock function")
}
func (ref refImageMock) LayerInfosForCopy(ctx context.Context) ([]types.BlobInfo, error) {
panic("unexpected call to a mock function")
}

// refImageReferenceMock is a mock of types.ImageReference which returns itself in DockerReference.
type refImageReferenceMock struct {
Expand Down Expand Up @@ -218,7 +208,7 @@ func testImageAndSig(t *testing.T, prm PolicyReferenceMatch, imageRef, sigRef st
// and therefore values refused by reference.ParseNormalizedNamed can not happen in practice.
parsedImageRef, err := reference.ParseNormalizedNamed(imageRef)
require.NoError(t, err)
res := prm.matchesDockerReference(refImageMock{parsedImageRef}, sigRef)
res := prm.matchesDockerReference(refImageMock{ref: parsedImageRef}, sigRef)
assert.Equal(t, result, res, fmt.Sprintf("%s vs. %s", imageRef, sigRef))
}

Expand Down Expand Up @@ -282,7 +272,7 @@ func TestPRMMatchExactMatchesDockerReference(t *testing.T) {
testPossiblyInvalidImageAndSig(t, prm, test.refB, test.refA, test.result)
}
// Even if they are signed with an empty string as a reference, unidentified images are rejected.
res := prm.matchesDockerReference(refImageMock{nil}, "")
res := prm.matchesDockerReference(refImageMock{ref: nil}, "")
assert.False(t, res, `unidentified vs. ""`)
}

Expand Down Expand Up @@ -318,7 +308,7 @@ func TestPRMMatchRepositoryMatchesDockerReference(t *testing.T) {
testPossiblyInvalidImageAndSig(t, prm, test.refB, test.refA, test.result)
}
// Even if they are signed with an empty string as a reference, unidentified images are rejected.
res := prm.matchesDockerReference(refImageMock{nil}, "")
res := prm.matchesDockerReference(refImageMock{ref: nil}, "")
assert.False(t, res, `unidentified vs. ""`)
}

Expand Down Expand Up @@ -347,22 +337,9 @@ func TestParseDockerReferences(t *testing.T) {
}
}

// forbiddenImageMock is a mock of types.UnparsedImage which ensures Reference is not called
type forbiddenImageMock struct{}

func (ref forbiddenImageMock) Reference() types.ImageReference {
panic("unexpected call to a mock function")
}
func (ref forbiddenImageMock) Manifest(ctx context.Context) ([]byte, string, error) {
panic("unexpected call to a mock function")
}
func (ref forbiddenImageMock) Signatures(context.Context) ([][]byte, error) {
panic("unexpected call to a mock function")
}

func testExactPRMAndSig(t *testing.T, prmFactory func(string) PolicyReferenceMatch, imageRef, sigRef string, result bool) {
prm := prmFactory(imageRef)
res := prm.matchesDockerReference(forbiddenImageMock{}, sigRef)
res := prm.matchesDockerReference(mocks.ForbiddenUnparsedImage{}, sigRef)
assert.Equal(t, result, res, fmt.Sprintf("%s vs. %s", imageRef, sigRef))
}

Expand Down Expand Up @@ -556,7 +533,7 @@ func TestPRMRemapIdentityMatchesDockerReference(t *testing.T) {
// Even if they are signed with an empty string as a reference, unidentified images are rejected.
prm, err := NewPRMRemapIdentity("docker.io", "docker.io")
require.NoError(t, err)
res := prm.matchesDockerReference(refImageMock{nil}, "")
res := prm.matchesDockerReference(refImageMock{ref: nil}, "")
assert.False(t, res, `unidentified vs. ""`)

// Verify that the behavior is otherwise the same as for prmMatchRepoDigestOrExact:
Expand Down

0 comments on commit 2907fba

Please sign in to comment.