Skip to content

Commit

Permalink
test: replace invalid address image pull e2e test with unit test (#3305)
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 authored Dec 10, 2024
1 parent 66deda7 commit eb123a7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/internal/packager/images/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func Pull(ctx context.Context, cfg PullConfig) (map[transform.Image]v1.Image, er
if err != nil {
return err
}
if cacheImg {
if cacheImg && cfg.CacheDirectory != "" {
img = cache.Image(img, cache.NewFilesystemCache(cfg.CacheDirectory))
}

Expand Down
48 changes: 48 additions & 0 deletions src/internal/packager/images/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

v1 "github.com/google/go-containerregistry/pkg/v1"
Expand Down Expand Up @@ -84,6 +85,53 @@ func TestCheckForIndex(t *testing.T) {
}

func TestPull(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
ref string
expectErr bool
}{
{
name: "pull an image",
ref: "ghcr.io/zarf-dev/zarf/agent:v0.32.6@sha256:b3fabdc7d4ecd0f396016ef78da19002c39e3ace352ea0ae4baa2ce9d5958376",
},
{
name: "error when pulling an image that doesn't exist",
ref: "ghcr.io/zarf-dev/zarf/imagethatdoesntexist:v1.1.1",
expectErr: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ref, err := transform.ParseImageRef(tc.ref)
require.NoError(t, err)
destDir := t.TempDir()
pullConfig := PullConfig{
DestinationDirectory: destDir,
ImageList: []transform.Image{
ref,
},
}

pulled, err := Pull(context.Background(), pullConfig)
if tc.expectErr {
require.Error(t, err, tc.expectErr)
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")
require.NoError(t, err)
Expand Down
10 changes: 0 additions & 10 deletions src/test/e2e/00_use_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,6 @@ func TestUseCLI(t *testing.T) {
require.Contains(t, stdErr, expectedOutString, "The log level should be changed to 'debug'")
})

t.Run("zarf package to test bad remote images", func(t *testing.T) {
_, stdErr, err := e2e.Zarf(t, "package", "create", "src/test/packages/00-remote-pull-fail", "--confirm")
// expecting zarf to have an error and output to stderr
require.Error(t, err)
// Make sure we print the get request error (only look for GET since the actual error changes based on login status)
require.Contains(t, stdErr, "failed to find the manifest on a remote: GET")
// And the docker error
require.Contains(t, stdErr, "response from daemon: No such image")
})

t.Run("zarf package to test archive path", func(t *testing.T) {
t.Parallel()
stdOut, stdErr, err := e2e.Zarf(t, "package", "create", "packages/distros/eks", "--confirm")
Expand Down
8 changes: 0 additions & 8 deletions src/test/packages/00-remote-pull-fail/zarf.yaml

This file was deleted.

0 comments on commit eb123a7

Please sign in to comment.