Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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