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: creator.ComposeComponents #2537

Merged
merged 6 commits into from
May 23, 2024
Merged
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
175 changes: 175 additions & 0 deletions src/pkg/packager/creator/compose_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package creator contains functions for creating Zarf packages.
package creator

import (
"testing"

"github.com/defenseunicorns/zarf/src/types"
"github.com/stretchr/testify/require"
)

func TestComposeComponents(t *testing.T) {
t.Parallel()

tests := []struct {
name string
pkg types.ZarfPackage
flavor string
expectedPkg types.ZarfPackage
expectedErr string
}{
{
name: "filter by architecture match",
pkg: types.ZarfPackage{
Metadata: types.ZarfMetadata{Architecture: "amd64"},
Components: []types.ZarfComponent{
{
Name: "component1",
Only: types.ZarfComponentOnlyTarget{
Cluster: types.ZarfComponentOnlyCluster{
Architecture: "amd64",
},
},
},
{
Name: "component2",
Only: types.ZarfComponentOnlyTarget{
Cluster: types.ZarfComponentOnlyCluster{
Architecture: "amd64",
},
},
},
},
},
expectedPkg: types.ZarfPackage{
Components: []types.ZarfComponent{
{Name: "component1"},
{Name: "component2"},
},
},
expectedErr: "",
},
{
name: "filter by architecture mismatch",
pkg: types.ZarfPackage{
Metadata: types.ZarfMetadata{Architecture: "amd64"},
Components: []types.ZarfComponent{
{
Name: "component1",
Only: types.ZarfComponentOnlyTarget{
Cluster: types.ZarfComponentOnlyCluster{
Architecture: "amd64",
},
},
},
{
Name: "component2",
Only: types.ZarfComponentOnlyTarget{
Cluster: types.ZarfComponentOnlyCluster{
Architecture: "arm64",
},
},
},
},
},
expectedPkg: types.ZarfPackage{
Components: []types.ZarfComponent{
{Name: "component1"},
},
},
expectedErr: "",
},
{
name: "filter by flavor match",
pkg: types.ZarfPackage{
Metadata: types.ZarfMetadata{Architecture: "amd64"},
Components: []types.ZarfComponent{
{
Name: "component1",
Only: types.ZarfComponentOnlyTarget{
Flavor: "default",
},
},
{
Name: "component2",
Only: types.ZarfComponentOnlyTarget{
Flavor: "default",
},
},
},
},
flavor: "default",
expectedPkg: types.ZarfPackage{
Components: []types.ZarfComponent{
{Name: "component1"},
{Name: "component2"},
},
},
expectedErr: "",
},
{
name: "filter by flavor mismatch",
pkg: types.ZarfPackage{
Metadata: types.ZarfMetadata{Architecture: "amd64"},
Components: []types.ZarfComponent{
{
Name: "component1",
Only: types.ZarfComponentOnlyTarget{
Flavor: "default",
},
},
{
Name: "component2",
Only: types.ZarfComponentOnlyTarget{
Flavor: "special",
},
},
},
},
flavor: "default",
expectedPkg: types.ZarfPackage{
Components: []types.ZarfComponent{
{Name: "component1"},
},
},
expectedErr: "",
},
{
name: "no architecture set error",
pkg: types.ZarfPackage{
Components: []types.ZarfComponent{
{
Name: "component1",
Only: types.ZarfComponentOnlyTarget{
Flavor: "default",
},
},
},
},
flavor: "default",
expectedPkg: types.ZarfPackage{},
expectedErr: "cannot build import chain: architecture must be provided",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

pkg, _, err := ComposeComponents(tt.pkg, tt.flavor)

if tt.expectedErr == "" {
require.NoError(t, err)
require.Equal(t, tt.expectedPkg.Components, pkg.Components)
return
}

require.EqualError(t, err, tt.expectedErr)
require.Empty(t, tt.expectedPkg)
})
}
}
Loading