From 447d9846cc4ad6e76c3895d19836e6668c65cec5 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Wed, 7 Aug 2024 16:19:42 +0200 Subject: [PATCH] test: add tests for FindImages (#2850) Signed-off-by: Philip Laine --- src/pkg/packager/creator/creator_test.go | 19 ++--------- src/pkg/packager/prepare.go | 15 ++++----- src/pkg/packager/prepare_test.go | 42 ++++++++++++++++++++++++ src/test/testutil/schema.go | 33 +++++++++++++++++++ src/test/testutil/testutil.go | 2 ++ 5 files changed, 86 insertions(+), 25 deletions(-) create mode 100644 src/pkg/packager/prepare_test.go create mode 100644 src/test/testutil/schema.go diff --git a/src/pkg/packager/creator/creator_test.go b/src/pkg/packager/creator/creator_test.go index 9312fe0a2c..95803cc0f4 100644 --- a/src/pkg/packager/creator/creator_test.go +++ b/src/pkg/packager/creator/creator_test.go @@ -6,7 +6,6 @@ package creator import ( "context" - "io/fs" "os" "path/filepath" "testing" @@ -14,22 +13,10 @@ import ( "github.com/stretchr/testify/require" "github.com/zarf-dev/zarf/src/pkg/layout" "github.com/zarf-dev/zarf/src/pkg/lint" + "github.com/zarf-dev/zarf/src/test/testutil" "github.com/zarf-dev/zarf/src/types" ) -type mockSchemaLoader struct { - b []byte -} - -func (m *mockSchemaLoader) ReadFile(_ string) ([]byte, error) { - return m.b, nil -} - -// Satisfy fs.ReadFileFS interface -func (m *mockSchemaLoader) Open(_ string) (fs.File, error) { - return nil, nil -} - func TestLoadPackageDefinition(t *testing.T) { // TODO once creator is refactored to not expect to be in the same directory as the zarf.yaml file // this test can be re-parallelized @@ -64,9 +51,7 @@ func TestLoadPackageDefinition(t *testing.T) { creator: NewSkeletonCreator(types.ZarfCreateOptions{}, types.ZarfPublishOptions{}), }, } - b, err := os.ReadFile("../../../../zarf.schema.json") - require.NoError(t, err) - lint.ZarfSchema = &mockSchemaLoader{b: b} + lint.ZarfSchema = testutil.LoadSchema(t, "../../../../zarf.schema.json") for _, tt := range tests { tt := tt diff --git a/src/pkg/packager/prepare.go b/src/pkg/packager/prepare.go index d007e862bd..214258634a 100644 --- a/src/pkg/packager/prepare.go +++ b/src/pkg/packager/prepare.go @@ -13,10 +13,15 @@ import ( "sort" "strings" - "github.com/goccy/go-yaml" - "github.com/defenseunicorns/pkg/helpers/v2" + "github.com/goccy/go-yaml" "github.com/google/go-containerregistry/pkg/crane" + v1 "k8s.io/api/apps/v1" + batchv1 "k8s.io/api/batch/v1" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + "github.com/zarf-dev/zarf/src/api/v1alpha1" "github.com/zarf-dev/zarf/src/config/lang" "github.com/zarf-dev/zarf/src/internal/packager/helm" @@ -27,12 +32,6 @@ import ( "github.com/zarf-dev/zarf/src/pkg/packager/creator" "github.com/zarf-dev/zarf/src/pkg/utils" "github.com/zarf-dev/zarf/src/types" - v1 "k8s.io/api/apps/v1" - batchv1 "k8s.io/api/batch/v1" - corev1 "k8s.io/api/core/v1" - - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" ) // imageMap is a map of image/boolean pairs. diff --git a/src/pkg/packager/prepare_test.go b/src/pkg/packager/prepare_test.go new file mode 100644 index 0000000000..1f704fdfdc --- /dev/null +++ b/src/pkg/packager/prepare_test.go @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2021-Present The Zarf Authors + +package packager + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/zarf-dev/zarf/src/pkg/lint" + "github.com/zarf-dev/zarf/src/test/testutil" + "github.com/zarf-dev/zarf/src/types" +) + +func TestFindImages(t *testing.T) { + t.Parallel() + + ctx := testutil.TestContext(t) + + lint.ZarfSchema = testutil.LoadSchema(t, "../../../zarf.schema.json") + + cfg := &types.PackagerConfig{ + CreateOpts: types.ZarfCreateOptions{ + BaseDir: "../../../examples/dos-games/", + }, + } + p, err := New(cfg) + require.NoError(t, err) + images, err := p.FindImages(ctx) + require.NoError(t, err) + expectedImages := map[string][]string{ + "baseline": { + "defenseunicorns/zarf-game:multi-tile-dark", + "index.docker.io/defenseunicorns/zarf-game:sha256-0b694ca1c33afae97b7471488e07968599f1d2470c629f76af67145ca64428af.sig", + }, + } + require.Equal(t, len(expectedImages), len(images)) + for k, v := range expectedImages { + require.ElementsMatch(t, v, images[k]) + } +} diff --git a/src/test/testutil/schema.go b/src/test/testutil/schema.go new file mode 100644 index 0000000000..967e6e64ba --- /dev/null +++ b/src/test/testutil/schema.go @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2021-Present The Zarf Authors + +package testutil + +import ( + "io/fs" + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +type schemaFS struct { + b []byte +} + +func (m *schemaFS) ReadFile(_ string) ([]byte, error) { + return m.b, nil +} + +func (m *schemaFS) Open(_ string) (fs.File, error) { + return nil, nil +} + +// LoadSchema returns the schema file as a FS. +func LoadSchema(t *testing.T, path string) fs.ReadFileFS { + t.Helper() + + b, err := os.ReadFile(path) + require.NoError(t, err) + return &schemaFS{b: b} +} diff --git a/src/test/testutil/testutil.go b/src/test/testutil/testutil.go index 862bca937d..672f1ae194 100644 --- a/src/test/testutil/testutil.go +++ b/src/test/testutil/testutil.go @@ -12,6 +12,8 @@ import ( // TestContext takes a testing.T and returns a context that is // attached to the test by t.Cleanup() func TestContext(t *testing.T) context.Context { + t.Helper() + ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) return ctx