Skip to content

Commit 7608e58

Browse files
committed
refactor: use cmp.Diff for comparision
1 parent 0ddebe7 commit 7608e58

2 files changed

Lines changed: 10 additions & 19 deletions

File tree

lib/resourcebuilder/core_test.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111
"text/template"
1212

13+
"github.com/google/go-cmp/cmp"
1314
"sigs.k8s.io/yaml"
1415

1516
corev1 "k8s.io/api/core/v1"
@@ -258,18 +259,10 @@ func validateGenericOperatorConfigTLSInjected(modified *corev1.ConfigMap, fieldN
258259
return fmt.Errorf("servingInfo.cipherSuites not found")
259260
}
260261

261-
// Verify expected ciphers are present
262-
for _, expectedCipher := range expectedCiphers {
263-
found := false
264-
for _, cipher := range cipherSuites {
265-
if cipher == expectedCipher {
266-
found = true
267-
break
268-
}
269-
}
270-
if !found {
271-
return fmt.Errorf("expected cipher %s not found in cipherSuites: %v", expectedCipher, cipherSuites)
272-
}
262+
sort.Strings(expectedCiphers)
263+
sort.Strings(cipherSuites)
264+
if diff := cmp.Diff(expectedCiphers, cipherSuites); diff != "" {
265+
return fmt.Errorf("list of ciphers mismatch (-want +got):\n%s", diff)
273266
}
274267

275268
return nil
@@ -519,11 +512,8 @@ servingInfo:
519512
apiServer: makeAPIServerConfig(withCustomTLSProfile(testOpenSSLCipherSuitesReversed, configv1.VersionTLS13)),
520513
expectError: false,
521514
validateConfigMap: func(t *testing.T, original, modified *corev1.ConfigMap) {
522-
// Validate through pure string comparison
523-
originalData := original.Data[genericOperatorConfigCMKey]
524-
modifiedData := modified.Data[genericOperatorConfigCMKey]
525-
if originalData != modifiedData {
526-
t.Errorf("ConfigMap data was modified when it should not have been.\nOriginal:\n%s\nModified:\n%s", originalData, modifiedData)
515+
if diff := cmp.Diff(original.Data[genericOperatorConfigCMKey], modified.Data[genericOperatorConfigCMKey]); diff != "" {
516+
t.Errorf("ConfigMap YAML mismatch (-want +got):\n%s", diff)
527517
}
528518
},
529519
},

lib/resourcebuilder/resourcebuilder_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"testing"
77

8+
"github.com/google/go-cmp/cmp"
89
"sigs.k8s.io/yaml"
910

1011
corev1 "k8s.io/api/core/v1"
@@ -100,8 +101,8 @@ func TestModifyConfigMapWithBuilder(t *testing.T) {
100101

101102
// Verify the result matches expected YAML
102103
modifiedYAML := appliedConfigMap.Data["config.yaml"]
103-
if modifiedYAML != tt.expectYAML {
104-
t.Errorf("ConfigMap YAML does not match expected.\nExpected:\n%s\nGot:\n%s", tt.expectYAML, modifiedYAML)
104+
if diff := cmp.Diff(tt.expectYAML, modifiedYAML); diff != "" {
105+
t.Errorf("ConfigMap YAML mismatch (-want +got):\n%s", diff)
105106
}
106107
})
107108
}

0 commit comments

Comments
 (0)