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

refactor: remove use of reflections #2634

Merged
merged 3 commits into from
Jun 21, 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
5 changes: 3 additions & 2 deletions src/internal/packager/helm/post-render.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"bytes"
"context"
"fmt"
"maps"
"os"
"path/filepath"
"reflect"
"slices"

"github.com/defenseunicorns/pkg/helpers/v2"
Expand Down Expand Up @@ -163,7 +163,8 @@
validRegistrySecret := c.GenerateRegistryPullCreds(name, config.ZarfImagePullSecretName, r.state.RegistryInfo)
// TODO: Refactor as error is not checked instead of checking for not found error.
currentRegistrySecret, _ := c.Clientset.CoreV1().Secrets(name).Get(ctx, config.ZarfImagePullSecretName, metav1.GetOptions{})
if currentRegistrySecret.Name != config.ZarfImagePullSecretName || !reflect.DeepEqual(currentRegistrySecret.Data, validRegistrySecret.Data) {
sameSecretData := maps.EqualFunc(currentRegistrySecret.Data, validRegistrySecret.Data, func(v1, v2 []byte) bool { return bytes.Equal(v1, v2) })
if currentRegistrySecret.Name != config.ZarfImagePullSecretName || !sameSecretData {

Check warning on line 167 in src/internal/packager/helm/post-render.go

View check run for this annotation

Codecov / codecov/patch

src/internal/packager/helm/post-render.go#L166-L167

Added lines #L166 - L167 were not covered by tests
err := func() error {
_, err := c.Clientset.CoreV1().Secrets(validRegistrySecret.Namespace).Create(ctx, validRegistrySecret, metav1.CreateOptions{})
if err != nil && !kerrors.IsAlreadyExists(err) {
Expand Down
7 changes: 4 additions & 3 deletions src/pkg/cluster/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
package cluster

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"reflect"
"maps"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -123,7 +124,7 @@
spinner.Updatef("Updating existing Zarf-managed image secret for namespace: '%s'", namespace.Name)

newRegistrySecret := c.GenerateRegistryPullCreds(namespace.Name, config.ZarfImagePullSecretName, state.RegistryInfo)
if !reflect.DeepEqual(currentRegistrySecret.Data, newRegistrySecret.Data) {
if !maps.EqualFunc(currentRegistrySecret.Data, newRegistrySecret.Data, func(v1, v2 []byte) bool { return bytes.Equal(v1, v2) }) {

Check warning on line 127 in src/pkg/cluster/secrets.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/secrets.go#L127

Added line #L127 was not covered by tests
_, err := c.Clientset.CoreV1().Secrets(newRegistrySecret.Namespace).Update(ctx, newRegistrySecret, metav1.UpdateOptions{})
if err != nil {
message.WarnErrf(err, "Problem creating registry secret for the %s namespace", namespace.Name)
Expand Down Expand Up @@ -159,7 +160,7 @@

// Create the secret
newGitSecret := c.GenerateGitPullCreds(namespace.Name, config.ZarfGitServerSecretName, state.GitServer)
if !reflect.DeepEqual(currentGitSecret.StringData, newGitSecret.StringData) {
if !maps.Equal(currentGitSecret.StringData, newGitSecret.StringData) {

Check warning on line 163 in src/pkg/cluster/secrets.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/secrets.go#L163

Added line #L163 was not covered by tests
_, err := c.Clientset.CoreV1().Secrets(newGitSecret.Namespace).Update(ctx, newGitSecret, metav1.UpdateOptions{})
if err != nil {
message.WarnErrf(err, "Problem creating git server secret for the %s namespace", namespace.Name)
Expand Down
5 changes: 1 addition & 4 deletions src/pkg/packager/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package packager

import (
"reflect"
"testing"

"github.com/defenseunicorns/zarf/src/pkg/packager/sources"
Expand Down Expand Up @@ -222,9 +221,7 @@ func TestGenerateValuesOverrides(t *testing.T) {
if err != nil {
t.Errorf("%s: generateValuesOverrides() error = %v", tt.name, err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("%s: generateValuesOverrides() got = %v, want %v", tt.name, got, tt.want)
}
require.Equal(t, tt.want, got)
})
}
}
Expand Down
31 changes: 7 additions & 24 deletions src/pkg/packager/filters/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package filters

import (
"fmt"
"reflect"
"strings"
"testing"

Expand Down Expand Up @@ -115,7 +114,7 @@ func TestDeployFilter_Apply(t *testing.T) {

possibilities := componentMatrix(t)

testCases := map[string]struct {
tests := map[string]struct {
pkg types.ZarfPackage
optionalComponents string
want []types.ZarfComponent
Expand Down Expand Up @@ -196,35 +195,19 @@ func TestDeployFilter_Apply(t *testing.T) {
},
}

for name, tc := range testCases {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// we do not currently support interactive mode in unit tests
isInteractive := false
filter := ForDeploy(tc.optionalComponents, isInteractive)
filter := ForDeploy(tt.optionalComponents, isInteractive)

result, err := filter.Apply(tc.pkg)
if tc.expectedErr != nil {
require.ErrorIs(t, err, tc.expectedErr)
result, err := filter.Apply(tt.pkg)
if tt.expectedErr != nil {
require.ErrorIs(t, err, tt.expectedErr)
} else {
require.NoError(t, err)
}
equal := reflect.DeepEqual(tc.want, result)
if !equal {
left := []string{}
right := []string{}

for _, c := range tc.want {
left = append(left, c.Name)
}

for _, c := range result {
right = append(right, c.Name)
fmt.Printf("componentFromQuery(t, %q),\n", strings.TrimSpace(c.Name))
}

// cause the test to fail
require.FailNow(t, "expected and actual are not equal", "\n\nexpected: %#v\n\nactual: %#v", left, right)
}
require.Equal(t, tt.want, result)
})
}
}
5 changes: 1 addition & 4 deletions src/pkg/utils/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package utils

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -169,9 +168,7 @@ func TestSortDependencies(t *testing.T) {
} else {
require.Error(t, err)
}
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("expected %v but got %v", tt.expected, result)
}
require.Equal(t, tt.expected, result)
})
}
}
Loading