Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for FindImages #2850

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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