Skip to content

Commit

Permalink
fix(digest): check container image info for nil (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel authored Jul 25, 2021
1 parent 7221704 commit 9bb8991
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/actions/mocks/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func CreateMockContainer(id string, name string, image string, created time.Time

// CreateMockContainerWithImageInfo should only be used for testing
func CreateMockContainerWithImageInfo(id string, name string, image string, created time.Time, imageInfo types.ImageInspect) container.Container {
return CreateMockContainerWithImageInfoP(id, name, image, created, &imageInfo)
}

// CreateMockContainerWithImageInfoP should only be used for testing
func CreateMockContainerWithImageInfoP(id string, name string, image string, created time.Time, imageInfo *types.ImageInspect) container.Container {
content := types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
ID: id,
Expand All @@ -57,7 +62,7 @@ func CreateMockContainerWithImageInfo(id string, name string, image string, crea
}
return *container.NewContainer(
&content,
&imageInfo,
imageInfo,
)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/registry/digest/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const ContentDigestHeader = "Docker-Content-Digest"

// CompareDigest ...
func CompareDigest(container types.Container, registryAuth string) (bool, error) {
if !container.HasImageInfo() {
return false, errors.New("container image info missing")
}

var digest string

registryAuth = TransformAuth(registryAuth)
Expand Down
7 changes: 7 additions & 0 deletions pkg/registry/digest/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ var _ = Describe("Digests", func() {
mockCreated,
mockDigest)

mockContainerNoImage := mocks.CreateMockContainerWithImageInfoP(mockId, mockName, mockImage, mockCreated, nil)

When("a digest comparison is done", func() {
It("should return true if digests match",
SkipIfCredentialsEmpty(GHCRCredentials, func() {
Expand All @@ -75,6 +77,11 @@ var _ = Describe("Digests", func() {
It("should return an error if the registry isn't available", func() {

})
It("should return an error when container contains no image info", func() {
matches, err := digest.CompareDigest(mockContainerNoImage, `user:pass`)
Expect(err).To(HaveOccurred())
Expect(matches).To(Equal(false))
})
})
When("using different registries", func() {
It("should work with DockerHub",
Expand Down

0 comments on commit 9bb8991

Please sign in to comment.