Skip to content

Commit 8fb1788

Browse files
committed
Add a Comparison for YAML that contains a string
1 parent 8dbae3c commit 8fb1788

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

internal/controller/pgupgrade/jobs_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ status: {}
150150
`))
151151

152152
tdeJob := reconciler.generateUpgradeJob(ctx, upgrade, startup, "echo testKey")
153-
b, _ := yaml.Marshal(tdeJob)
154-
assert.Assert(t, strings.Contains(string(b),
153+
assert.Assert(t, cmp.MarshalContains(tdeJob,
155154
`/usr/pgsql-"${new_version}"/bin/initdb -k -D /pgdata/pg"${new_version}" --encryption-key-command "echo testKey"`))
156155
}
157156

internal/pgbackrest/config_test.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ import (
1313
"testing"
1414

1515
"gotest.tools/v3/assert"
16-
"gotest.tools/v3/assert/cmp"
1716
corev1 "k8s.io/api/core/v1"
1817
"k8s.io/apimachinery/pkg/api/resource"
19-
"sigs.k8s.io/yaml"
2018

2119
"github.com/crunchydata/postgres-operator/internal/initialize"
2220
"github.com/crunchydata/postgres-operator/internal/naming"
21+
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
2322
"github.com/crunchydata/postgres-operator/internal/testing/require"
2423
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2524
)
@@ -213,13 +212,13 @@ pg1-socket-path = /tmp/postgres
213212
[]string{"some-instance"})
214213

215214
assert.Assert(t,
216-
strings.Contains(configmap.Data["pgbackrest_instance.conf"],
215+
cmp.Contains(configmap.Data["pgbackrest_instance.conf"],
217216
"archive-header-check = n"))
218217
assert.Assert(t,
219-
strings.Contains(configmap.Data["pgbackrest_instance.conf"],
218+
cmp.Contains(configmap.Data["pgbackrest_instance.conf"],
220219
"page-header-check = n"))
221220
assert.Assert(t,
222-
strings.Contains(configmap.Data["pgbackrest_instance.conf"],
221+
cmp.Contains(configmap.Data["pgbackrest_instance.conf"],
223222
"pg-version-force"))
224223

225224
cluster.Spec.Backups.PGBackRest.Repos = []v1beta1.PGBackRestRepo{
@@ -234,13 +233,13 @@ pg1-socket-path = /tmp/postgres
234233
[]string{"some-instance"})
235234

236235
assert.Assert(t,
237-
strings.Contains(configmap.Data["pgbackrest_repo.conf"],
236+
cmp.Contains(configmap.Data["pgbackrest_repo.conf"],
238237
"archive-header-check = n"))
239238
assert.Assert(t,
240-
strings.Contains(configmap.Data["pgbackrest_repo.conf"],
239+
cmp.Contains(configmap.Data["pgbackrest_repo.conf"],
241240
"page-header-check = n"))
242241
assert.Assert(t,
243-
strings.Contains(configmap.Data["pgbackrest_repo.conf"],
242+
cmp.Contains(configmap.Data["pgbackrest_repo.conf"],
244243
"pg-version-force"))
245244
})
246245
}
@@ -323,10 +322,8 @@ func TestReloadCommand(t *testing.T) {
323322
}
324323

325324
func TestReloadCommandPrettyYAML(t *testing.T) {
326-
b, err := yaml.Marshal(reloadCommand("any"))
327-
assert.NilError(t, err)
328-
assert.Assert(t, strings.Contains(string(b), "\n- |"),
329-
"expected literal block scalar, got:\n%s", b)
325+
assert.Assert(t, cmp.MarshalContains(reloadCommand("any"), "\n- |"),
326+
"expected literal block scalar")
330327
}
331328

332329
func TestRestoreCommand(t *testing.T) {
@@ -351,19 +348,21 @@ func TestRestoreCommand(t *testing.T) {
351348
}
352349

353350
func TestRestoreCommandPrettyYAML(t *testing.T) {
354-
b, err := yaml.Marshal(RestoreCommand("/dir", "try", "", nil, "--options"))
355-
356-
assert.NilError(t, err)
357-
assert.Assert(t, strings.Contains(string(b), "\n- |"),
358-
"expected literal block scalar, got:\n%s", b)
351+
assert.Assert(t,
352+
cmp.MarshalContains(
353+
RestoreCommand("/dir", "try", "", nil, "--options"),
354+
"\n- |",
355+
),
356+
"expected literal block scalar")
359357
}
360358

361359
func TestRestoreCommandTDE(t *testing.T) {
362-
b, err := yaml.Marshal(RestoreCommand("/dir", "try", "echo testValue", nil, "--options"))
363-
364-
assert.NilError(t, err)
365-
assert.Assert(t, strings.Contains(string(b), "encryption_key_command = 'echo testValue'"),
366-
"expected encryption_key_command setting, got:\n%s", b)
360+
assert.Assert(t,
361+
cmp.MarshalContains(
362+
RestoreCommand("/dir", "try", "echo testValue", nil, "--options"),
363+
"encryption_key_command = 'echo testValue'",
364+
),
365+
"expected encryption_key_command setting")
367366
}
368367

369368
func TestDedicatedSnapshotVolumeRestoreCommand(t *testing.T) {
@@ -388,11 +387,12 @@ func TestDedicatedSnapshotVolumeRestoreCommand(t *testing.T) {
388387
}
389388

390389
func TestDedicatedSnapshotVolumeRestoreCommandPrettyYAML(t *testing.T) {
391-
b, err := yaml.Marshal(DedicatedSnapshotVolumeRestoreCommand("/dir", "--options"))
392-
393-
assert.NilError(t, err)
394-
assert.Assert(t, strings.Contains(string(b), "\n- |"),
395-
"expected literal block scalar, got:\n%s", b)
390+
assert.Assert(t,
391+
cmp.MarshalContains(
392+
DedicatedSnapshotVolumeRestoreCommand("/dir", "--options"),
393+
"\n- |",
394+
),
395+
"expected literal block scalar")
396396
}
397397

398398
func TestServerConfig(t *testing.T) {

internal/testing/cmp/cmp.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ func DeepEqual(x, y any, opts ...gocmp.Option) Comparison {
5050
return gotest.DeepEqual(x, y, opts...)
5151
}
5252

53+
// MarshalContains converts actual to YAML and succeeds if expected is in the result.
54+
func MarshalContains(actual any, expected string) Comparison {
55+
b, err := yaml.Marshal(actual)
56+
if err != nil {
57+
return func() gotest.Result { return gotest.ResultFromError(err) }
58+
}
59+
return Contains(string(b), expected)
60+
}
61+
5362
// MarshalMatches converts actual to YAML and compares that to expected.
5463
func MarshalMatches(actual any, expected string) Comparison {
5564
b, err := yaml.Marshal(actual)

0 commit comments

Comments
 (0)