Skip to content

Commit d62abd7

Browse files
committed
Switch from unstructured to structs in validation tests
The "k8s.io/apimachinery" module does not support "omitzero" until 1.34. See: kubernetes/kubernetes@41805af
1 parent fe0bd17 commit d62abd7

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

internal/testing/validation/postgrescluster_test.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ import (
1111

1212
"gotest.tools/v3/assert"
1313
apierrors "k8s.io/apimachinery/pkg/api/errors"
14-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1514
"k8s.io/apimachinery/pkg/util/intstr"
1615
"sigs.k8s.io/controller-runtime/pkg/client"
16+
"sigs.k8s.io/yaml"
1717

18-
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
1918
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
2019
"github.com/crunchydata/postgres-operator/internal/testing/require"
2120
v1 "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1"
@@ -271,13 +270,15 @@ func TestPostgresConfigParameters(t *testing.T) {
271270
key string
272271
value any
273272
}{
274-
{"archive_timeout", int64(100)},
273+
{"archive_timeout", 100},
275274
{"archive_timeout", "20s"},
276275
} {
277276
t.Run(tt.key, func(t *testing.T) {
278-
cluster := require.Value(runtime.ToUnstructuredObject(base))
279-
assert.NilError(t, unstructured.SetNestedField(cluster.Object,
280-
tt.value, "spec", "config", "parameters", tt.key))
277+
cluster := base.DeepCopy()
278+
require.UnmarshalInto(t, &cluster.Spec.Config,
279+
require.Value(yaml.Marshal(map[string]any{
280+
"parameters": map[string]any{tt.key: tt.value},
281+
})))
281282

282283
assert.NilError(t, cc.Create(ctx, cluster, client.DryRunAll))
283284
})
@@ -299,13 +300,15 @@ func TestPostgresConfigParameters(t *testing.T) {
299300
{key: "listen_addresses", value: ""},
300301
{key: "log_file_mode", value: ""},
301302
{key: "logging_collector", value: "off"},
302-
{key: "port", value: int64(5)},
303+
{key: "port", value: 5},
303304
{key: "wal_log_hints", value: "off"},
304305
} {
305306
t.Run(tt.key, func(t *testing.T) {
306-
cluster := require.Value(runtime.ToUnstructuredObject(base))
307-
assert.NilError(t, unstructured.SetNestedField(cluster.Object,
308-
tt.value, "spec", "config", "parameters", tt.key))
307+
cluster := base.DeepCopy()
308+
require.UnmarshalInto(t, &cluster.Spec.Config,
309+
require.Value(yaml.Marshal(map[string]any{
310+
"parameters": map[string]any{tt.key: tt.value},
311+
})))
309312

310313
err := cc.Create(ctx, cluster, client.DryRunAll)
311314
assert.Assert(t, apierrors.IsInvalid(err))
@@ -332,9 +335,11 @@ func TestPostgresConfigParameters(t *testing.T) {
332335
{key: "unix_socket_group", value: "two"},
333336
} {
334337
t.Run(tt.key, func(t *testing.T) {
335-
cluster := require.Value(runtime.ToUnstructuredObject(base))
336-
assert.NilError(t, unstructured.SetNestedField(cluster.Object,
337-
tt.value, "spec", "config", "parameters", tt.key))
338+
cluster := base.DeepCopy()
339+
require.UnmarshalInto(t, &cluster.Spec.Config,
340+
require.Value(yaml.Marshal(map[string]any{
341+
"parameters": map[string]any{tt.key: tt.value},
342+
})))
338343

339344
err := cc.Create(ctx, cluster, client.DryRunAll)
340345
assert.Assert(t, apierrors.IsInvalid(err))
@@ -354,9 +359,11 @@ func TestPostgresConfigParameters(t *testing.T) {
354359
{key: "recovery_target_name", value: "doot"},
355360
} {
356361
t.Run(tt.key, func(t *testing.T) {
357-
cluster := require.Value(runtime.ToUnstructuredObject(base))
358-
assert.NilError(t, unstructured.SetNestedField(cluster.Object,
359-
tt.value, "spec", "config", "parameters", tt.key))
362+
cluster := base.DeepCopy()
363+
require.UnmarshalInto(t, &cluster.Spec.Config,
364+
require.Value(yaml.Marshal(map[string]any{
365+
"parameters": map[string]any{tt.key: tt.value},
366+
})))
360367

361368
err := cc.Create(ctx, cluster, client.DryRunAll)
362369
assert.Assert(t, apierrors.IsInvalid(err))
@@ -408,9 +415,11 @@ func TestPostgresConfigParameters(t *testing.T) {
408415
{key: "recovery_min_apply_delay", value: ""},
409416
} {
410417
t.Run(tt.key, func(t *testing.T) {
411-
cluster := require.Value(runtime.ToUnstructuredObject(base))
412-
assert.NilError(t, unstructured.SetNestedField(cluster.Object,
413-
tt.value, "spec", "config", "parameters", tt.key))
418+
cluster := base.DeepCopy()
419+
require.UnmarshalInto(t, &cluster.Spec.Config,
420+
require.Value(yaml.Marshal(map[string]any{
421+
"parameters": map[string]any{tt.key: tt.value},
422+
})))
414423

415424
err := cc.Create(ctx, cluster, client.DryRunAll)
416425
assert.Assert(t, apierrors.IsInvalid(err))

0 commit comments

Comments
 (0)