fix(digest): check container image info for nil (#1027)

fix/image-tag-from-hash
nils måsén 3 years ago committed by GitHub
parent 7221704638
commit 9bb8991a76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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,
@ -57,7 +62,7 @@ func CreateMockContainerWithImageInfo(id string, name string, image string, crea
}
return *container.NewContainer(
&content,
&imageInfo,
imageInfo,
)
}

@ -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)

@ -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() {
@ -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",

Loading…
Cancel
Save