Skip to content

Commit f2521ca

Browse files
committed
Use cmp.MarshalMatches everywhere. Delete unnecessary marshalMatches helper functions.
1 parent 7a78474 commit f2521ca

19 files changed

+119
-196
lines changed

internal/controller/pgupgrade/jobs_test.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,16 @@ import (
2121
"testing"
2222

2323
"gotest.tools/v3/assert"
24-
"gotest.tools/v3/assert/cmp"
2524
appsv1 "k8s.io/api/apps/v1"
2625
corev1 "k8s.io/api/core/v1"
2726
"k8s.io/apimachinery/pkg/api/resource"
2827
"sigs.k8s.io/yaml"
2928

3029
"github.com/crunchydata/postgres-operator/internal/initialize"
30+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
3131
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
3232
)
3333

34-
// marshalMatches converts actual to YAML and compares that to expected.
35-
func marshalMatches(actual interface{}, expected string) cmp.Comparison {
36-
b, err := yaml.Marshal(actual)
37-
if err != nil {
38-
return func() cmp.Result { return cmp.ResultFromError(err) }
39-
}
40-
return cmp.DeepEqual(string(b), strings.Trim(expected, "\t\n")+"\n")
41-
}
42-
4334
func TestGenerateUpgradeJob(t *testing.T) {
4435
ctx := context.Background()
4536
reconciler := &PGUpgradeReconciler{}
@@ -77,7 +68,7 @@ func TestGenerateUpgradeJob(t *testing.T) {
7768
}
7869

7970
job := reconciler.generateUpgradeJob(ctx, upgrade, startup, "")
80-
assert.Assert(t, marshalMatches(job, `
71+
assert.Assert(t, cmp.MarshalMatches(job, `
8172
apiVersion: batch/v1
8273
kind: Job
8374
metadata:
@@ -208,7 +199,7 @@ func TestGenerateRemoveDataJob(t *testing.T) {
208199
}
209200

210201
job := reconciler.generateRemoveDataJob(ctx, upgrade, sts)
211-
assert.Assert(t, marshalMatches(job, `
202+
assert.Assert(t, cmp.MarshalMatches(job, `
212203
apiVersion: batch/v1
213204
kind: Job
214205
metadata:

internal/controller/postgrescluster/cluster_test.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636

3737
"github.com/crunchydata/postgres-operator/internal/initialize"
3838
"github.com/crunchydata/postgres-operator/internal/naming"
39+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
3940
"github.com/crunchydata/postgres-operator/internal/testing/require"
4041
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
4142
)
@@ -611,11 +612,11 @@ func TestGenerateClusterPrimaryService(t *testing.T) {
611612
assert.ErrorContains(t, err, "not implemented")
612613

613614
alwaysExpect := func(t testing.TB, service *corev1.Service, endpoints *corev1.Endpoints) {
614-
assert.Assert(t, marshalMatches(service.TypeMeta, `
615+
assert.Assert(t, cmp.MarshalMatches(service.TypeMeta, `
615616
apiVersion: v1
616617
kind: Service
617618
`))
618-
assert.Assert(t, marshalMatches(service.ObjectMeta, `
619+
assert.Assert(t, cmp.MarshalMatches(service.ObjectMeta, `
619620
creationTimestamp: null
620621
labels:
621622
postgres-operator.crunchydata.com/cluster: pg5
@@ -630,7 +631,7 @@ ownerReferences:
630631
name: pg5
631632
uid: ""
632633
`))
633-
assert.Assert(t, marshalMatches(service.Spec.Ports, `
634+
assert.Assert(t, cmp.MarshalMatches(service.Spec.Ports, `
634635
- name: postgres
635636
port: 2600
636637
protocol: TCP
@@ -641,7 +642,7 @@ ownerReferences:
641642
assert.Assert(t, service.Spec.Selector == nil,
642643
"got %v", service.Spec.Selector)
643644

644-
assert.Assert(t, marshalMatches(endpoints, `
645+
assert.Assert(t, cmp.MarshalMatches(endpoints, `
645646
apiVersion: v1
646647
kind: Endpoints
647648
metadata:
@@ -730,11 +731,11 @@ func TestGenerateClusterReplicaServiceIntent(t *testing.T) {
730731
assert.NilError(t, err)
731732

732733
alwaysExpect := func(t testing.TB, service *corev1.Service) {
733-
assert.Assert(t, marshalMatches(service.TypeMeta, `
734+
assert.Assert(t, cmp.MarshalMatches(service.TypeMeta, `
734735
apiVersion: v1
735736
kind: Service
736737
`))
737-
assert.Assert(t, marshalMatches(service.ObjectMeta, `
738+
assert.Assert(t, cmp.MarshalMatches(service.ObjectMeta, `
738739
creationTimestamp: null
739740
labels:
740741
postgres-operator.crunchydata.com/cluster: pg2
@@ -752,7 +753,7 @@ ownerReferences:
752753
}
753754

754755
alwaysExpect(t, service)
755-
assert.Assert(t, marshalMatches(service.Spec, `
756+
assert.Assert(t, cmp.MarshalMatches(service.Spec, `
756757
ports:
757758
- name: postgres
758759
port: 9876
@@ -788,7 +789,7 @@ type: ClusterIP
788789
assert.NilError(t, err)
789790
alwaysExpect(t, service)
790791
test.Expect(t, service)
791-
assert.Assert(t, marshalMatches(service.Spec.Ports, `
792+
assert.Assert(t, cmp.MarshalMatches(service.Spec.Ports, `
792793
- name: postgres
793794
port: 9876
794795
protocol: TCP
@@ -808,19 +809,19 @@ type: ClusterIP
808809
assert.NilError(t, err)
809810

810811
// Annotations present in the metadata.
811-
assert.Assert(t, marshalMatches(service.ObjectMeta.Annotations, `
812+
assert.Assert(t, cmp.MarshalMatches(service.ObjectMeta.Annotations, `
812813
some: note
813814
`))
814815

815816
// Labels present in the metadata.
816-
assert.Assert(t, marshalMatches(service.ObjectMeta.Labels, `
817+
assert.Assert(t, cmp.MarshalMatches(service.ObjectMeta.Labels, `
817818
happy: label
818819
postgres-operator.crunchydata.com/cluster: pg2
819820
postgres-operator.crunchydata.com/role: replica
820821
`))
821822

822823
// Labels not in the selector.
823-
assert.Assert(t, marshalMatches(service.Spec.Selector, `
824+
assert.Assert(t, cmp.MarshalMatches(service.Spec.Selector, `
824825
postgres-operator.crunchydata.com/cluster: pg2
825826
postgres-operator.crunchydata.com/role: replica
826827
`))

internal/controller/postgrescluster/helpers_test.go

-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232

3333
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
3434
"github.com/crunchydata/postgres-operator/internal/initialize"
35-
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
3635
"github.com/crunchydata/postgres-operator/internal/testing/require"
3736
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
3837
)
@@ -63,11 +62,6 @@ func init() {
6362
}
6463
}
6564

66-
// marshalMatches converts actual to YAML and compares that to expected.
67-
func marshalMatches(actual interface{}, expected string) cmp.Comparison {
68-
return cmp.MarshalMatches(actual, expected)
69-
}
70-
7165
// setupKubernetes starts or connects to a Kubernetes API and returns a client
7266
// that uses it. See [require.Kubernetes] for more details.
7367
func setupKubernetes(t testing.TB) (*rest.Config, client.Client) {

internal/controller/postgrescluster/instance_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
569569

570570
// Only database container has mounts.
571571
// Other containers are ignored.
572-
assert.Assert(t, marshalMatches(out.Containers, `
572+
assert.Assert(t, cmp.MarshalMatches(out.Containers, `
573573
- name: database
574574
resources: {}
575575
volumeMounts:
@@ -661,7 +661,7 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
661661

662662
// Instance configuration files with certificates.
663663
// Other volumes are ignored.
664-
assert.Assert(t, marshalMatches(out.Volumes, `
664+
assert.Assert(t, cmp.MarshalMatches(out.Volumes, `
665665
- name: other
666666
- name: postgres-data
667667
- name: postgres-wal
@@ -709,7 +709,7 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
709709
// Instance configuration files plus client and server certificates.
710710
// The server certificate comes from the instance Secret.
711711
// Other volumes are untouched.
712-
assert.Assert(t, marshalMatches(result.Volumes, `
712+
assert.Assert(t, cmp.MarshalMatches(result.Volumes, `
713713
- name: other
714714
- name: postgres-data
715715
- name: postgres-wal
@@ -763,7 +763,7 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
763763

764764
// The TLS server is added and configuration mounted.
765765
// It has PostgreSQL volumes mounted while other volumes are ignored.
766-
assert.Assert(t, marshalMatches(out.Containers, `
766+
assert.Assert(t, cmp.MarshalMatches(out.Containers, `
767767
- name: database
768768
resources: {}
769769
volumeMounts:
@@ -879,7 +879,7 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
879879
assert.DeepEqual(t, before.Containers[:2], out.Containers[:2])
880880

881881
// It has the custom resources.
882-
assert.Assert(t, marshalMatches(out.Containers[2:], `
882+
assert.Assert(t, cmp.MarshalMatches(out.Containers[2:], `
883883
- command:
884884
- pgbackrest
885885
- server
@@ -1576,7 +1576,7 @@ func TestGenerateInstanceStatefulSetIntent(t *testing.T) {
15761576
name: "check default scheduling constraints are added",
15771577
run: func(t *testing.T, ss *appsv1.StatefulSet) {
15781578
assert.Equal(t, len(ss.Spec.Template.Spec.TopologySpreadConstraints), 2)
1579-
assert.Assert(t, marshalMatches(ss.Spec.Template.Spec.TopologySpreadConstraints, `
1579+
assert.Assert(t, cmp.MarshalMatches(ss.Spec.Template.Spec.TopologySpreadConstraints, `
15801580
- labelSelector:
15811581
matchExpressions:
15821582
- key: postgres-operator.crunchydata.com/data
@@ -1623,7 +1623,7 @@ func TestGenerateInstanceStatefulSetIntent(t *testing.T) {
16231623
},
16241624
run: func(t *testing.T, ss *appsv1.StatefulSet) {
16251625
assert.Equal(t, len(ss.Spec.Template.Spec.TopologySpreadConstraints), 3)
1626-
assert.Assert(t, marshalMatches(ss.Spec.Template.Spec.TopologySpreadConstraints, `
1626+
assert.Assert(t, cmp.MarshalMatches(ss.Spec.Template.Spec.TopologySpreadConstraints, `
16271627
- labelSelector:
16281628
matchExpressions:
16291629
- key: postgres-operator.crunchydata.com/cluster
@@ -1706,7 +1706,7 @@ func TestGenerateInstanceStatefulSetIntent(t *testing.T) {
17061706
},
17071707
run: func(t *testing.T, ss *appsv1.StatefulSet) {
17081708
assert.Equal(t, len(ss.Spec.Template.Spec.TopologySpreadConstraints), 1)
1709-
assert.Assert(t, marshalMatches(ss.Spec.Template.Spec.TopologySpreadConstraints,
1709+
assert.Assert(t, cmp.MarshalMatches(ss.Spec.Template.Spec.TopologySpreadConstraints,
17101710
`- labelSelector:
17111711
matchExpressions:
17121712
- key: postgres-operator.crunchydata.com/cluster

internal/controller/postgrescluster/patroni_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737

3838
"github.com/crunchydata/postgres-operator/internal/initialize"
3939
"github.com/crunchydata/postgres-operator/internal/naming"
40+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
4041
"github.com/crunchydata/postgres-operator/internal/testing/require"
4142
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
4243
)
@@ -56,11 +57,11 @@ func TestGeneratePatroniLeaderLeaseService(t *testing.T) {
5657
cluster.Spec.Port = initialize.Int32(9876)
5758

5859
alwaysExpect := func(t testing.TB, service *corev1.Service) {
59-
assert.Assert(t, marshalMatches(service.TypeMeta, `
60+
assert.Assert(t, cmp.MarshalMatches(service.TypeMeta, `
6061
apiVersion: v1
6162
kind: Service
6263
`))
63-
assert.Assert(t, marshalMatches(service.ObjectMeta, `
64+
assert.Assert(t, cmp.MarshalMatches(service.ObjectMeta, `
6465
creationTimestamp: null
6566
labels:
6667
postgres-operator.crunchydata.com/cluster: pg2
@@ -88,7 +89,7 @@ ownerReferences:
8889
alwaysExpect(t, service)
8990
// Defaults to ClusterIP.
9091
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeClusterIP)
91-
assert.Assert(t, marshalMatches(service.Spec.Ports, `
92+
assert.Assert(t, cmp.MarshalMatches(service.Spec.Ports, `
9293
- name: postgres
9394
port: 9876
9495
protocol: TCP
@@ -177,7 +178,7 @@ ownerReferences:
177178
assert.NilError(t, err)
178179
alwaysExpect(t, service)
179180
test.Expect(t, service)
180-
assert.Assert(t, marshalMatches(service.Spec.Ports, `
181+
assert.Assert(t, cmp.MarshalMatches(service.Spec.Ports, `
181182
- name: postgres
182183
port: 9876
183184
protocol: TCP
@@ -202,7 +203,7 @@ ownerReferences:
202203
assert.NilError(t, err)
203204
alwaysExpect(t, service)
204205
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeNodePort)
205-
assert.Assert(t, marshalMatches(service.Spec.Ports, `
206+
assert.Assert(t, cmp.MarshalMatches(service.Spec.Ports, `
206207
- name: postgres
207208
nodePort: 32001
208209
port: 9876
@@ -215,7 +216,7 @@ ownerReferences:
215216
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeLoadBalancer)
216217
assert.NilError(t, err)
217218
alwaysExpect(t, service)
218-
assert.Assert(t, marshalMatches(service.Spec.Ports, `
219+
assert.Assert(t, cmp.MarshalMatches(service.Spec.Ports, `
219220
- name: postgres
220221
nodePort: 32002
221222
port: 9876

internal/controller/postgrescluster/pgadmin_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func TestGeneratePGAdminService(t *testing.T) {
152152
assert.NilError(t, err)
153153
assert.Assert(t, !specified)
154154

155-
assert.Assert(t, marshalMatches(service.ObjectMeta, `
155+
assert.Assert(t, cmp.MarshalMatches(service.ObjectMeta, `
156156
creationTimestamp: null
157157
name: my-cluster-pgadmin
158158
namespace: my-ns
@@ -165,11 +165,11 @@ namespace: my-ns
165165
}
166166

167167
alwaysExpect := func(t testing.TB, service *corev1.Service) {
168-
assert.Assert(t, marshalMatches(service.TypeMeta, `
168+
assert.Assert(t, cmp.MarshalMatches(service.TypeMeta, `
169169
apiVersion: v1
170170
kind: Service
171171
`))
172-
assert.Assert(t, marshalMatches(service.ObjectMeta, `
172+
assert.Assert(t, cmp.MarshalMatches(service.ObjectMeta, `
173173
creationTimestamp: null
174174
labels:
175175
postgres-operator.crunchydata.com/cluster: my-cluster
@@ -263,7 +263,7 @@ ownerReferences:
263263
alwaysExpect(t, service)
264264
// Defaults to ClusterIP.
265265
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeClusterIP)
266-
assert.Assert(t, marshalMatches(service.Spec.Ports, `
266+
assert.Assert(t, cmp.MarshalMatches(service.Spec.Ports, `
267267
- name: pgadmin
268268
port: 5050
269269
protocol: TCP
@@ -296,7 +296,7 @@ ownerReferences:
296296
assert.Assert(t, specified)
297297
alwaysExpect(t, service)
298298
test.Expect(t, service)
299-
assert.Assert(t, marshalMatches(service.Spec.Ports, `
299+
assert.Assert(t, cmp.MarshalMatches(service.Spec.Ports, `
300300
- name: pgadmin
301301
port: 5050
302302
protocol: TCP
@@ -321,7 +321,7 @@ ownerReferences:
321321
assert.NilError(t, err)
322322
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeNodePort)
323323
alwaysExpect(t, service)
324-
assert.Assert(t, marshalMatches(service.Spec.Ports, `
324+
assert.Assert(t, cmp.MarshalMatches(service.Spec.Ports, `
325325
- name: pgadmin
326326
nodePort: 32001
327327
port: 5050
@@ -334,7 +334,7 @@ ownerReferences:
334334
assert.NilError(t, err)
335335
assert.Equal(t, service.Spec.Type, corev1.ServiceTypeLoadBalancer)
336336
alwaysExpect(t, service)
337-
assert.Assert(t, marshalMatches(service.Spec.Ports, `
337+
assert.Assert(t, cmp.MarshalMatches(service.Spec.Ports, `
338338
- name: pgadmin
339339
nodePort: 32002
340340
port: 5050
@@ -698,7 +698,7 @@ func TestReconcilePGAdminDataVolume(t *testing.T) {
698698
assert.Equal(t, pvc.Labels[naming.LabelRole], naming.RolePGAdmin)
699699
assert.Equal(t, pvc.Labels[naming.LabelData], naming.DataPGAdmin)
700700

701-
assert.Assert(t, marshalMatches(pvc.Spec, `
701+
assert.Assert(t, cmp.MarshalMatches(pvc.Spec, `
702702
accessModes:
703703
- ReadWriteOnce
704704
resources:

internal/controller/postgrescluster/pgbackrest_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import (
5252
"github.com/crunchydata/postgres-operator/internal/naming"
5353
"github.com/crunchydata/postgres-operator/internal/pgbackrest"
5454
"github.com/crunchydata/postgres-operator/internal/pki"
55+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
5556
"github.com/crunchydata/postgres-operator/internal/testing/require"
5657
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
5758
)
@@ -327,7 +328,7 @@ func TestReconcilePGBackRest(t *testing.T) {
327328
// TODO(tjmoore4): Add additional tests to test appending existing
328329
// topology spread constraints and spec.disableDefaultPodScheduling being
329330
// set to true (as done in instance StatefulSet tests).
330-
assert.Assert(t, marshalMatches(template.Spec, `
331+
assert.Assert(t, cmp.MarshalMatches(template.Spec, `
331332
affinity: {}
332333
automountServiceAccountToken: false
333334
containers: null
@@ -2157,7 +2158,7 @@ func TestReconcileCloudBasedDataSource(t *testing.T) {
21572158
assert.Assert(t, apierrors.IsNotFound(err), "expected NotFound, got %#v", err)
21582159
} else {
21592160
assert.NilError(t, err)
2160-
assert.Assert(t, marshalMatches(restoreConfig.Data["pgbackrest_instance.conf"], tc.result.conf))
2161+
assert.Assert(t, cmp.MarshalMatches(restoreConfig.Data["pgbackrest_instance.conf"], tc.result.conf))
21612162
}
21622163

21632164
restoreJobs := &batchv1.JobList{}
@@ -2454,7 +2455,7 @@ func TestGenerateBackupJobIntent(t *testing.T) {
24542455
"",
24552456
nil, nil,
24562457
)
2457-
assert.Assert(t, marshalMatches(spec.Template.Spec, `
2458+
assert.Assert(t, cmp.MarshalMatches(spec.Template.Spec, `
24582459
containers:
24592460
- command:
24602461
- /opt/crunchy/bin/pgbackrest

0 commit comments

Comments
 (0)