Skip to content

Commit

Permalink
test: add tests for FindImages (#2850)
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <[email protected]>
  • Loading branch information
phillebaba authored Aug 7, 2024
1 parent 95e9e45 commit 447d984
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 25 deletions.
19 changes: 2 additions & 17 deletions src/pkg/packager/creator/creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,17 @@ package creator

import (
"context"
"io/fs"
"os"
"path/filepath"
"testing"

"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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions src/pkg/packager/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down
42 changes: 42 additions & 0 deletions src/pkg/packager/prepare_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
}
33 changes: 33 additions & 0 deletions src/test/testutil/schema.go
Original file line number Diff line number Diff line change
@@ -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}
}
2 changes: 2 additions & 0 deletions src/test/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 447d984

Please sign in to comment.