Skip to content

Commit

Permalink
add unit tests for pull
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 committed Dec 9, 2024
1 parent f2a417d commit ed03aaa
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions src/internal/packager/images/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,53 @@ func TestCheckForIndex(t *testing.T) {
}

func TestPull(t *testing.T) {
t.Run("basic pulling of an image", func(t *testing.T) {
ref, err := transform.ParseImageRef("ghcr.io/zarf-dev/zarf/agent:v0.32.6@sha256:b3fabdc7d4ecd0f396016ef78da19002c39e3ace352ea0ae4baa2ce9d5958376")
require.NoError(t, err)
destDir := t.TempDir()
pullConfig := PullConfig{
DestinationDirectory: destDir,
ImageList: []transform.Image{
ref,
},
}
t.Parallel()
testCases := []struct {
name string
ref string
expectedErr string
}{
{
name: "pull an image",
ref: "ghcr.io/zarf-dev/zarf/agent:v0.32.6@sha256:b3fabdc7d4ecd0f396016ef78da19002c39e3ace352ea0ae4baa2ce9d5958376",
expectedErr: "",
},
{
name: "error when pulling an image that doesn't exist",
ref: "ghcr.io/zarf-dev/zarf/imagethatdoesntexist:v1.1.1",
expectedErr: "No such image",
},
}

pulled, err := Pull(context.Background(), pullConfig)
require.NoError(t, err)
layers, err := pulled[ref].Layers()
require.NoError(t, err)
// Make sure are all the layers of the image are pulled in
for _, layer := range layers {
digestHash, err := layer.Digest()
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ref, err := transform.ParseImageRef(tc.ref)
require.NoError(t, err)
digest, _ := strings.CutPrefix(digestHash.String(), "sha256:")
require.FileExists(t, filepath.Join(destDir, fmt.Sprintf("blobs/sha256/%s", digest)))
}
})
destDir := t.TempDir()
pullConfig := PullConfig{
DestinationDirectory: destDir,
ImageList: []transform.Image{
ref,
},
}

pulled, err := Pull(context.Background(), pullConfig)
if tc.expectedErr != "" {
require.ErrorContains(t, err, tc.expectedErr)
return
}
require.NoError(t, err)
layers, err := pulled[ref].Layers()
require.NoError(t, err)
// Make sure all the layers of the image are pulled in
for _, layer := range layers {
digestHash, err := layer.Digest()
require.NoError(t, err)
digest, _ := strings.CutPrefix(digestHash.String(), "sha256:")
require.FileExists(t, filepath.Join(destDir, fmt.Sprintf("blobs/sha256/%s", digest)))
}
})
}

t.Run("pulling a cosign image is successful and doesn't add anything to the cache", func(t *testing.T) {
ref, err := transform.ParseImageRef("ghcr.io/stefanprodan/podinfo:sha256-57a654ace69ec02ba8973093b6a786faa15640575fbf0dbb603db55aca2ccec8.sig")
Expand Down

0 comments on commit ed03aaa

Please sign in to comment.