Skip to content

Commit

Permalink
added func to calculate image layers sha in e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: kumari tanushree <ktanushree@vmware.com>
  • Loading branch information
kumari tanushree committed Dec 29, 2023
1 parent 8eee075 commit 42fd005
Showing 1 changed file with 60 additions and 9 deletions.
69 changes: 60 additions & 9 deletions test/e2e/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"time"

"carvel.dev/imgpkg/test/helpers"
"github.com/google/go-containerregistry/pkg/authn"
regname "github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -68,41 +70,63 @@ images:
},
)
fmt.Printf("\n\nOutput: %s\n\n", stdout)
digestSha, err := GetImageSha(env.RelocationRepo + imageDigest)
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", env.RelocationRepo+imageDigest, err.Error()))
}
_ = digestSha
digestSha, err = GetImageSha(env.Image + imageDigest)
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", env.RelocationRepo+imageDigest, err.Error()))
}

assert.Contains(t, stdout, fmt.Sprintf(
` - Image: %s%s
Type: Image
Origin: %s%s
Layers:
- Digest: "sha256:a37f35f3e418ea6c1b339df0fc89c8d3155d937740445906ba71466996fac625"
Annotations:
- Digest: %s
some.annotation: some value
some.other.annotation: some other value
`, env.RelocationRepo, imageDigest, env.Image, imageDigest))
`, env.RelocationRepo, imageDigest, env.Image, imageDigest, digestSha))

digestSha, err = GetImageSha(env.RelocationRepo + "@" + imageDigest)
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", env.RelocationRepo+imageDigest, err.Error()))
}
assert.Contains(t, stdout, fmt.Sprintf(
` - Image: %s@%s
Type: Signature
Layers:
- Digest: "sha256:4197c5ac7fb0ba1e597a2377a1b58332d10f6de53dce9c89fd96a3f37034f88b"
- Digest: %s
Annotations:
tag: %s
`, env.RelocationRepo, imgSigDigest, imgSigTag))
`, env.RelocationRepo, imgSigDigest, imgSigTag, digestSha))

digestSha, err = GetImageSha(env.RelocationRepo + "@" + bundleSigDigest)
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", env.RelocationRepo+imageDigest, err.Error()))
}
assert.Contains(t, stdout, fmt.Sprintf(
` - Image: %s@%s
Type: Signature
Layers:
- Digest: "sha256:28014a7e4bef0b7c3f1da47d095f8bb131f474bd5fc96caa4d6125818220b00e"
- Digest: %s
Annotations:
tag: %s
`, env.RelocationRepo, bundleSigDigest, bundleSigTag))
`, env.RelocationRepo, bundleSigDigest, bundleSigTag, digestSha))

digestSha, err = GetImageSha(env.RelocationRepo + "@" + locationsImgDigest)
if err != nil {
panic(fmt.Sprintf("Internal inconsistency: image %s should be fully resolved, error: %s", env.RelocationRepo+imageDigest, err.Error()))
}
assert.Contains(t, stdout, fmt.Sprintf(
` - Image: %s@%s
Type: Internal
Layers:
-
Digest: "sha256:5d43e9fc8f1ad1b339b5f37bb050b150640ad2c1594345178f1fb38656583a94"
`, env.RelocationRepo, locationsImgDigest))
Digest: %s
`, env.RelocationRepo, locationsImgDigest, digestSha))
})
})

Expand Down Expand Up @@ -746,3 +770,30 @@ origin: %s%s
})
})
}

func GetImageSha(image string) (string, error) {
parsedImgRef, err := regname.ParseReference(image, regname.WeakValidation)
if err != nil {
return "", err
}

v1Img, err := remote.Image(parsedImgRef, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
return "", err
}

imgLayers, err := v1Img.Layers()
if err != nil {
return "", err
}
digestSha := ""
for _, imgLayer := range imgLayers {
digHash, err := imgLayer.Digest()
if err != nil {
return "", err
}
digestSha = digHash.String()
fmt.Printf("\nDigest sha of image: %s is %s\n", image, digestSha)
}
return digestSha, nil
}

0 comments on commit 42fd005

Please sign in to comment.