Skip to content

Commit 599519a

Browse files
committed
chore: update agent test to minimize pulled images
Signed-off-by: Allen Conlon <[email protected]>
1 parent a32c2cb commit 599519a

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/internal/agent/hooks/common_test.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/defenseunicorns/pkg/helpers/v2"
12+
v1 "github.com/opencontainers/image-spec/specs-go/v1"
1213
"github.com/stretchr/testify/require"
1314
"github.com/zarf-dev/zarf/src/pkg/state"
1415
"github.com/zarf-dev/zarf/src/pkg/transform"
@@ -17,7 +18,7 @@ import (
1718
"oras.land/oras-go/v2/registry/remote"
1819
)
1920

20-
func populateLocalRegistry(t *testing.T, ctx context.Context, localUrl string, artifact transform.Image) {
21+
func populateLocalRegistry(t *testing.T, ctx context.Context, localUrl string, artifact transform.Image, copyOpts oras.CopyOptions) {
2122
localReg, err := remote.NewRegistry(localUrl)
2223
require.NoError(t, err)
2324

@@ -32,17 +33,17 @@ func populateLocalRegistry(t *testing.T, ctx context.Context, localUrl string, a
3233
dst, err := localReg.Repository(ctx, artifact.Path)
3334
require.NoError(t, err)
3435

35-
_, err = oras.Copy(ctx, src, artifact.Tag, dst, artifact.Tag, oras.DefaultCopyOptions)
36+
_, err = oras.Copy(ctx, src, artifact.Tag, dst, artifact.Tag, copyOpts)
3637
require.NoError(t, err)
3738

3839
hashedTag, err := transform.ImageTransformHost(localUrl, fmt.Sprintf("%s/%s:%s", artifact.Host, artifact.Path, artifact.Tag))
3940
require.NoError(t, err)
4041

41-
_, err = oras.Copy(ctx, src, artifact.Tag, dst, hashedTag, oras.DefaultCopyOptions)
42+
_, err = oras.Copy(ctx, src, artifact.Tag, dst, hashedTag, copyOpts)
4243
require.NoError(t, err)
4344
}
4445

45-
func setupRegistry(t *testing.T, ctx context.Context, port int, artifacts []transform.Image) (string, error) {
46+
func setupRegistry(t *testing.T, ctx context.Context, port int, artifacts []transform.Image, copyOpts oras.CopyOptions) (string, error) {
4647
localUrl := testutil.SetupInMemoryRegistry(ctx, t, port)
4748

4849
localReg, err := remote.NewRegistry(localUrl)
@@ -52,7 +53,7 @@ func setupRegistry(t *testing.T, ctx context.Context, port int, artifacts []tran
5253
}
5354

5455
for _, art := range artifacts {
55-
populateLocalRegistry(t, ctx, localUrl, art)
56+
populateLocalRegistry(t, ctx, localUrl, art, copyOpts)
5657
}
5758

5859
return localUrl, nil
@@ -63,19 +64,27 @@ type mediaTypeTest struct {
6364
image string
6465
expected string
6566
artifact []transform.Image
67+
Opts oras.CopyOptions
6668
}
6769

6870
func TestConfigMediaTypes(t *testing.T) {
6971
t.Parallel()
7072
port, err := helpers.GetAvailablePort()
7173
require.NoError(t, err)
7274

75+
linuxAmd64Opts := oras.DefaultCopyOptions
76+
linuxAmd64Opts.WithTargetPlatform(&v1.Platform{
77+
Architecture: "amd64",
78+
OS: "linux",
79+
})
80+
7381
tests := []mediaTypeTest{
7482
{
7583
// https://oci.dag.dev/?image=ghcr.io%2Fstefanprodan%2Fmanifests%2Fpodinfo%3A6.9.0
7684
name: "flux manifest",
7785
expected: "application/vnd.cncf.flux.config.v1+json",
7886
image: fmt.Sprintf("localhost:%d/stefanprodan/manifests/podinfo:6.9.0-zarf-2823281104", port),
87+
Opts: oras.DefaultCopyOptions,
7988
artifact: []transform.Image{
8089
{
8190
Host: "ghcr.io",
@@ -89,6 +98,7 @@ func TestConfigMediaTypes(t *testing.T) {
8998
name: "helm chart manifest",
9099
expected: "application/vnd.cncf.helm.config.v1+json",
91100
image: fmt.Sprintf("localhost:%d/stefanprodan/charts/podinfo:6.9.0", port),
101+
Opts: oras.DefaultCopyOptions,
92102
artifact: []transform.Image{
93103
{
94104
Host: "ghcr.io",
@@ -98,11 +108,11 @@ func TestConfigMediaTypes(t *testing.T) {
98108
},
99109
},
100110
{
101-
// docker images do not include a `.config.mediaType`
102-
// https://oci.dag.dev/?image=ghcr.io%2Fstefanprodan%2Fpodinfo%3A6.9.0
111+
//
103112
name: "docker image manifest",
104-
expected: "",
113+
expected: "application/vnd.oci.image.config.v1+json",
105114
image: fmt.Sprintf("localhost:%d/zarf-dev/images/hello-world:latest", port),
115+
Opts: linuxAmd64Opts,
106116
artifact: []transform.Image{
107117
{
108118
Host: "ghcr.io",
@@ -118,7 +128,7 @@ func TestConfigMediaTypes(t *testing.T) {
118128
t.Run(tt.name, func(t *testing.T) {
119129
t.Parallel()
120130
ctx := testutil.TestContext(t)
121-
url, err := setupRegistry(t, ctx, port, tt.artifact)
131+
url, err := setupRegistry(t, ctx, port, tt.artifact, tt.Opts)
122132
require.NoError(t, err)
123133

124134
s := &state.State{RegistryInfo: state.RegistryInfo{Address: url}}

src/internal/agent/hooks/flux-ocirepo_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
corev1 "k8s.io/api/core/v1"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/apimachinery/pkg/runtime"
26+
"oras.land/oras-go/v2"
2627
)
2728

2829
func createFluxOCIRepoAdmissionRequest(t *testing.T, op v1.Operation, fluxOCIRepo *flux.OCIRepository) *v1.AdmissionRequest {
@@ -343,15 +344,10 @@ func TestFluxOCIMutationWebhook(t *testing.T) {
343344
Path: "stefanprodan/manifests/podinfo",
344345
Tag: "6.9.0",
345346
},
346-
{
347-
Host: "ghcr.io",
348-
Path: "zarf-dev/images/hello-world",
349-
Tag: "latest",
350-
},
351347
}
352348

353349
ctx := context.Background()
354-
url, err := setupRegistry(t, ctx, port, artifacts)
350+
url, err := setupRegistry(t, ctx, port, artifacts, oras.DefaultCopyOptions)
355351
require.NoError(t, err)
356352

357353
for _, tt := range tests {

0 commit comments

Comments
 (0)