Skip to content

Commit 70b8f2f

Browse files
committed
Add an internal test helper that compares to YAML
Multiple packages have copies of the same function, "marshalMatches". A few packages used an equivalent combination of functions, "marshalEquals" with "strings.Trim". Move the implementation to a common package, and replace the lesser-used "marshalEquals" with it. Replacing every call to "marshalMatches" is very likely to conflict with other work, so leave those names in place for now.
1 parent 2276cf7 commit 70b8f2f

File tree

13 files changed

+47
-136
lines changed

13 files changed

+47
-136
lines changed

internal/controller/postgrescluster/helpers_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,20 @@ import (
2020
"os"
2121
"path/filepath"
2222
"strconv"
23-
"strings"
2423
"testing"
2524
"time"
2625

27-
"gotest.tools/v3/assert/cmp"
2826
corev1 "k8s.io/api/core/v1"
2927
"k8s.io/apimachinery/pkg/api/resource"
3028
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3129
"k8s.io/client-go/rest"
3230
"sigs.k8s.io/controller-runtime/pkg/client"
3331
"sigs.k8s.io/controller-runtime/pkg/envtest"
3432
"sigs.k8s.io/controller-runtime/pkg/manager"
35-
"sigs.k8s.io/yaml"
3633

3734
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
3835
"github.com/crunchydata/postgres-operator/internal/initialize"
36+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
3937
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
4038
)
4139

@@ -67,11 +65,7 @@ func init() {
6765

6866
// marshalMatches converts actual to YAML and compares that to expected.
6967
func marshalMatches(actual interface{}, expected string) cmp.Comparison {
70-
b, err := yaml.Marshal(actual)
71-
if err != nil {
72-
return func() cmp.Result { return cmp.ResultFromError(err) }
73-
}
74-
return cmp.DeepEqual(string(b), strings.Trim(expected, "\t\n")+"\n")
68+
return cmp.MarshalMatches(actual, expected)
7569
}
7670

7771
func testVolumeClaimSpec() corev1.PersistentVolumeClaimSpec {

internal/patroni/certificates_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
package patroni
1717

1818
import (
19-
"strings"
2019
"testing"
2120

2221
"gotest.tools/v3/assert"
23-
"gotest.tools/v3/assert/cmp"
22+
gotest "gotest.tools/v3/assert/cmp"
2423
corev1 "k8s.io/api/core/v1"
2524

2625
"github.com/crunchydata/postgres-operator/internal/pki"
26+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
2727
)
2828

2929
const rootPEM = `-----BEGIN CERTIFICATE-----
@@ -63,7 +63,7 @@ func TestCertFile(t *testing.T) {
6363
// - https://docs.python.org/3/library/ssl.html#combined-key-and-certificate
6464
// - https://docs.python.org/3/library/ssl.html#certificate-chains
6565
assert.Assert(t,
66-
cmp.Regexp(`^`+
66+
gotest.Regexp(`^`+
6767
`-----BEGIN [^ ]+ PRIVATE KEY-----\n`+
6868
`([^-]+\n)+`+
6969
`-----END [^ ]+ PRIVATE KEY-----\n`+
@@ -81,13 +81,13 @@ func TestInstanceCertificates(t *testing.T) {
8181

8282
projections := instanceCertificates(certs)
8383

84-
assert.Assert(t, marshalEquals(projections, strings.TrimSpace(`
84+
assert.Assert(t, cmp.MarshalMatches(projections, `
8585
- secret:
8686
items:
8787
- key: patroni.ca-roots
8888
path: ~postgres-operator/patroni.ca-roots
8989
- key: patroni.crt-combined
9090
path: ~postgres-operator/patroni.crt+key
9191
name: some-name
92-
`)+"\n"))
92+
`))
9393
}

internal/patroni/config_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"sigs.k8s.io/yaml"
2929

3030
"github.com/crunchydata/postgres-operator/internal/postgres"
31+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
3132
"github.com/crunchydata/postgres-operator/internal/testing/require"
3233
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
3334
)
@@ -559,7 +560,7 @@ func TestInstanceConfigFiles(t *testing.T) {
559560

560561
projections := instanceConfigFiles(cm1, cm2)
561562

562-
assert.Assert(t, marshalEquals(projections, strings.TrimSpace(`
563+
assert.Assert(t, cmp.MarshalMatches(projections, `
563564
- configMap:
564565
items:
565566
- key: patroni.yaml
@@ -570,7 +571,7 @@ func TestInstanceConfigFiles(t *testing.T) {
570571
- key: patroni.yaml
571572
path: ~postgres-operator_instance.yaml
572573
name: cm2
573-
`)+"\n"))
574+
`))
574575
}
575576

576577
func TestInstanceEnvironment(t *testing.T) {
@@ -585,7 +586,7 @@ func TestInstanceEnvironment(t *testing.T) {
585586

586587
vars := instanceEnvironment(cluster, podService, leaderService, nil)
587588

588-
assert.Assert(t, marshalEquals(vars, strings.TrimSpace(`
589+
assert.Assert(t, cmp.MarshalMatches(vars, `
589590
- name: PATRONI_NAME
590591
valueFrom:
591592
fieldRef:
@@ -613,7 +614,7 @@ func TestInstanceEnvironment(t *testing.T) {
613614
value: '*:8008'
614615
- name: PATRONICTL_CONFIG_FILE
615616
value: /etc/patroni
616-
`)+"\n"))
617+
`))
617618

618619
t.Run("MatchingPorts", func(t *testing.T) {
619620
leaderService.Spec.Ports = []corev1.ServicePort{{Name: "postgres"}}
@@ -625,7 +626,7 @@ func TestInstanceEnvironment(t *testing.T) {
625626

626627
vars := instanceEnvironment(cluster, podService, leaderService, containers)
627628

628-
assert.Assert(t, marshalEquals(vars, strings.TrimSpace(`
629+
assert.Assert(t, cmp.MarshalMatches(vars, `
629630
- name: PATRONI_NAME
630631
valueFrom:
631632
fieldRef:
@@ -655,7 +656,7 @@ func TestInstanceEnvironment(t *testing.T) {
655656
value: '*:8008'
656657
- name: PATRONICTL_CONFIG_FILE
657658
value: /etc/patroni
658-
`)+"\n"))
659+
`))
659660
})
660661
}
661662

internal/patroni/rbac_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
package patroni
1717

1818
import (
19-
"strings"
2019
"testing"
2120

2221
"gotest.tools/v3/assert"
2322

23+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
2424
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2525
)
2626

@@ -49,7 +49,7 @@ func TestPermissions(t *testing.T) {
4949
assert.Assert(t, isUniqueAndSorted(rule.Verbs), "got %q", rule.Verbs)
5050
}
5151

52-
assert.Assert(t, marshalEquals(permissions, strings.Trim(`
52+
assert.Assert(t, cmp.MarshalMatches(permissions, `
5353
- apiGroups:
5454
- ""
5555
resources:
@@ -76,7 +76,7 @@ func TestPermissions(t *testing.T) {
7676
- services
7777
verbs:
7878
- create
79-
`, "\t\n")+"\n"))
79+
`))
8080
})
8181

8282
t.Run("OpenShift", func(t *testing.T) {
@@ -90,7 +90,7 @@ func TestPermissions(t *testing.T) {
9090
assert.Assert(t, isUniqueAndSorted(rule.Verbs), "got %q", rule.Verbs)
9191
}
9292

93-
assert.Assert(t, marshalEquals(permissions, strings.Trim(`
93+
assert.Assert(t, cmp.MarshalMatches(permissions, `
9494
- apiGroups:
9595
- ""
9696
resources:
@@ -123,6 +123,6 @@ func TestPermissions(t *testing.T) {
123123
- services
124124
verbs:
125125
- create
126-
`, "\t\n")+"\n"))
126+
`))
127127
})
128128
}

internal/patroni/reconcile_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package patroni
1717

1818
import (
1919
"context"
20-
"strings"
2120
"testing"
2221

2322
"gotest.tools/v3/assert"
@@ -27,6 +26,7 @@ import (
2726
"github.com/crunchydata/postgres-operator/internal/naming"
2827
"github.com/crunchydata/postgres-operator/internal/pki"
2928
"github.com/crunchydata/postgres-operator/internal/postgres"
29+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
3030
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
3131
)
3232

@@ -129,7 +129,7 @@ func TestInstancePod(t *testing.T) {
129129
Labels: map[string]string{naming.LabelPatroni: "some-such-ha"},
130130
})
131131

132-
assert.Assert(t, marshalEquals(template.Spec, strings.TrimSpace(`
132+
assert.Assert(t, cmp.MarshalMatches(template.Spec, `
133133
containers:
134134
- command:
135135
- patroni
@@ -206,7 +206,7 @@ volumes:
206206
path: ~postgres-operator/patroni.ca-roots
207207
- key: patroni.crt-combined
208208
path: ~postgres-operator/patroni.crt+key
209-
`)+"\n"))
209+
`))
210210
}
211211

212212
func TestPodIsStandbyLeader(t *testing.T) {

internal/pgadmin/assertions_test.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

internal/pgadmin/reconcile_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
package pgadmin
1717

1818
import (
19-
"strings"
2019
"testing"
2120

2221
"gotest.tools/v3/assert"
2322
corev1 "k8s.io/api/core/v1"
2423
"k8s.io/apimachinery/pkg/api/resource"
2524

25+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
2626
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2727
)
2828

@@ -50,7 +50,7 @@ func TestPod(t *testing.T) {
5050

5151
call()
5252

53-
assert.Assert(t, marshalEquals(pod, strings.Trim(`
53+
assert.Assert(t, cmp.MarshalMatches(pod, `
5454
containers:
5555
- env:
5656
- name: PGADMIN_SETUP_EMAIL
@@ -97,7 +97,7 @@ volumes:
9797
- name: pgadmin-data
9898
persistentVolumeClaim:
9999
claimName: ""
100-
`, "\t\n")+"\n"))
100+
`))
101101

102102
// No change when called again.
103103
before := pod.DeepCopy()
@@ -114,8 +114,7 @@ volumes:
114114

115115
call()
116116

117-
assert.Assert(t, marshalEquals(pod,
118-
strings.Trim(`
117+
assert.Assert(t, cmp.MarshalMatches(pod, `
119118
containers:
120119
- env:
121120
- name: PGADMIN_SETUP_EMAIL
@@ -166,6 +165,6 @@ volumes:
166165
- name: pgadmin-data
167166
persistentVolumeClaim:
168167
claimName: ""
169-
`, "\t\n")+"\n"))
168+
`))
170169
})
171170
}

internal/pgbackrest/assertions_test.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

internal/pgbackrest/helpers_test.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,10 @@
1616
package pgbackrest
1717

1818
import (
19-
"strings"
20-
21-
"gotest.tools/v3/assert/cmp"
22-
23-
// Google Kubernetes Engine / Google Cloud Platform authentication provider
24-
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
25-
"sigs.k8s.io/yaml"
19+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
2620
)
2721

2822
// marshalMatches converts actual to YAML and compares that to expected.
2923
func marshalMatches(actual interface{}, expected string) cmp.Comparison {
30-
b, err := yaml.Marshal(actual)
31-
if err != nil {
32-
return func() cmp.Result { return cmp.ResultFromError(err) }
33-
}
34-
return cmp.DeepEqual(string(b), strings.Trim(expected, "\t\n")+"\n")
24+
return cmp.MarshalMatches(actual, expected)
3525
}

internal/pgbackrest/rbac_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
package pgbackrest
1717

1818
import (
19-
"strings"
2019
"testing"
2120

2221
"gotest.tools/v3/assert"
2322

23+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
2424
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2525
)
2626

@@ -48,7 +48,7 @@ func TestPermissions(t *testing.T) {
4848
assert.Assert(t, isUniqueAndSorted(rule.Verbs), "got %q", rule.Verbs)
4949
}
5050

51-
assert.Assert(t, marshalEquals(permissions, strings.Trim(`
51+
assert.Assert(t, cmp.MarshalMatches(permissions, `
5252
- apiGroups:
5353
- ""
5454
resources:
@@ -61,5 +61,5 @@ func TestPermissions(t *testing.T) {
6161
- pods/exec
6262
verbs:
6363
- create
64-
`, "\t\n")+"\n"))
64+
`))
6565
}

0 commit comments

Comments
 (0)