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: remove validate pkg arch e2e test #2563

Merged
merged 1 commit into from
May 30, 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
134 changes: 57 additions & 77 deletions src/pkg/packager/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package packager

import (
"context"
"errors"
"fmt"
"testing"

Expand All @@ -16,126 +15,107 @@ import (
"github.com/defenseunicorns/zarf/src/types"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
k8sTesting "k8s.io/client-go/testing"
)

// TestValidatePackageArchitecture verifies that Zarf validates package architecture against cluster architecture correctly.
func TestValidatePackageArchitecture(t *testing.T) {
t.Parallel()

type testCase struct {
name string
pkgArch string
clusterArchs []string
images []string
expectedError error
getArchError error
}

testCases := []testCase{
tests := []struct {
name string
pkgArch string
clusterArchs []string
images []string
wantErr error
}{
{
name: "architecture match",
pkgArch: "amd64",
clusterArchs: []string{"amd64"},
images: []string{"nginx"},
expectedError: nil,
name: "architecture match",
pkgArch: "amd64",
clusterArchs: []string{"amd64"},
images: []string{"nginx"},
wantErr: nil,
},
{
name: "architecture mismatch",
pkgArch: "arm64",
clusterArchs: []string{"amd64"},
images: []string{"nginx"},
expectedError: fmt.Errorf(lang.CmdPackageDeployValidateArchitectureErr, "arm64", "amd64"),
name: "architecture mismatch",
pkgArch: "arm64",
clusterArchs: []string{"amd64"},
images: []string{"nginx"},
wantErr: fmt.Errorf(lang.CmdPackageDeployValidateArchitectureErr, "arm64", "amd64"),
},
{
name: "multiple cluster architectures",
pkgArch: "arm64",
clusterArchs: []string{"amd64", "arm64"},
images: []string{"nginx"},
expectedError: nil,
name: "multiple cluster architectures",
pkgArch: "arm64",
clusterArchs: []string{"amd64", "arm64"},
images: []string{"nginx"},
wantErr: nil,
},
{
name: "ignore validation when package arch equals 'multi'",
pkgArch: "multi",
clusterArchs: []string{"not evaluated"},
expectedError: nil,
name: "ignore validation when package arch equals 'multi'",
pkgArch: "multi",
clusterArchs: []string{"not evaluated"},
wantErr: nil,
},
{
name: "ignore validation when a package doesn't contain images",
pkgArch: "amd64",
images: []string{},
clusterArchs: []string{"not evaluated"},
expectedError: nil,
name: "ignore validation when a package doesn't contain images",
pkgArch: "amd64",
images: []string{},
clusterArchs: []string{"not evaluated"},
wantErr: nil,
},
{
name: "test the error path when fetching cluster architecture fails",
pkgArch: "amd64",
images: []string{"nginx"},
getArchError: errors.New("error fetching cluster architecture"),
expectedError: lang.ErrUnableToCheckArch,
name: "test the error path when fetching cluster architecture fails",
pkgArch: "amd64",
images: []string{"nginx"},
wantErr: lang.ErrUnableToCheckArch,
},
}

for _, testCase := range testCases {
testCase := testCase
for _, tt := range tests {
tt := tt

t.Run(testCase.name, func(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

mockClient := fake.NewSimpleClientset()
cs := fake.NewSimpleClientset()
logger := func(string, ...interface{}) {}

// Create a Packager instance with package architecture set and a mock Kubernetes client.
p := &Packager{
cluster: &cluster.Cluster{
K8s: &k8s.K8s{
Clientset: mockClient,
Clientset: cs,
Log: logger,
},
},
cfg: &types.PackagerConfig{
Pkg: types.ZarfPackage{
Metadata: types.ZarfMetadata{Architecture: testCase.pkgArch},
Metadata: types.ZarfMetadata{Architecture: tt.pkgArch},
Components: []types.ZarfComponent{
{
Images: testCase.images,
Images: tt.images,
},
},
},
},
}

// Set up test data for fetching cluster architecture.
mockClient.Fake.PrependReactor("list", "nodes", func(_ k8sTesting.Action) (bool, runtime.Object, error) {
// Return an error for cases that test this error path.
if testCase.getArchError != nil {
return true, nil, testCase.getArchError
}

nodeItems := []v1.Node{}

for _, arch := range testCase.clusterArchs {
nodeItems = append(nodeItems, v1.Node{
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
Architecture: arch,
},
for i, arch := range tt.clusterArchs {
node := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("node-%d-%s", i, tt.name),
},
Status: v1.NodeStatus{
NodeInfo: v1.NodeSystemInfo{
Architecture: arch,
},
})
}

// Create a NodeList object to fetch cluster architecture with the mock client.
nodeList := &v1.NodeList{
Items: nodeItems,
},
}
return true, nodeList, nil
})

err := p.validatePackageArchitecture(context.TODO())
_, err := cs.CoreV1().Nodes().Create(context.Background(), node, metav1.CreateOptions{})
require.NoError(t, err)
}

require.Equal(t, testCase.expectedError, err)
err := p.validatePackageArchitecture(context.Background())
require.Equal(t, tt.wantErr, err)
})
}
}
Expand Down
23 changes: 0 additions & 23 deletions src/test/e2e/29_mismatched_checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,6 @@ import (
"github.com/stretchr/testify/require"
)

// TestMismatchedArchitectures ensures that zarf produces an error
// when the package architecture doesn't match the target cluster architecture.
func TestMismatchedArchitectures(t *testing.T) {
t.Log("E2E: Mismatched architectures")
e2e.SetupWithCluster(t)

var (
mismatchedArch = e2e.GetMismatchedArch()
mismatchedGamesPackage = fmt.Sprintf("zarf-package-dos-games-%s-1.0.0.tar.zst", mismatchedArch)
expectedErrorMessage = fmt.Sprintf("this package architecture is %s", mismatchedArch)
)

// Build dos-games package with different arch than the cluster arch.
stdOut, stdErr, err := e2e.Zarf("package", "create", "examples/dos-games/", "--architecture", mismatchedArch, "--confirm")
require.NoError(t, err, stdOut, stdErr)
defer e2e.CleanFiles(mismatchedGamesPackage)

// Ensure zarf package deploy returns an error because of the mismatched architectures.
_, stdErr, err = e2e.Zarf("package", "deploy", mismatchedGamesPackage, "--confirm")
require.Error(t, err, stdErr)
require.Contains(t, e2e.StripMessageFormatting(stdErr), expectedErrorMessage)
}

// TestMismatchedVersions ensures that zarf produces a warning
// when the initialized version of Zarf doesn't match the current CLI
func TestMismatchedVersions(t *testing.T) {
Expand Down
Loading