|
| 1 | +// Copyright 2021 - 2025 Crunchy Data Solutions, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package validation |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "gotest.tools/v3/assert" |
| 12 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 13 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | + "sigs.k8s.io/yaml" |
| 16 | + |
| 17 | + "github.com/crunchydata/postgres-operator/internal/controller/runtime" |
| 18 | + "github.com/crunchydata/postgres-operator/internal/testing/require" |
| 19 | + "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1" |
| 20 | +) |
| 21 | + |
| 22 | +func TestPGAdminInstrumentation(t *testing.T) { |
| 23 | + ctx := context.Background() |
| 24 | + cc := require.Kubernetes(t) |
| 25 | + t.Parallel() |
| 26 | + |
| 27 | + namespace := require.Namespace(t, cc) |
| 28 | + base := v1beta1.NewPGAdmin() |
| 29 | + base.Namespace = namespace.Name |
| 30 | + base.Name = "pgadmin-instrumentation" |
| 31 | + |
| 32 | + assert.NilError(t, cc.Create(ctx, base.DeepCopy(), client.DryRunAll), |
| 33 | + "expected this base to be valid") |
| 34 | + |
| 35 | + t.Run("LogsRetentionPeriod", func(t *testing.T) { |
| 36 | + pgadmin := base.DeepCopy() |
| 37 | + assert.NilError(t, yaml.UnmarshalStrict([]byte(`{ |
| 38 | + instrumentation: { |
| 39 | + logs: { retentionPeriod: 5m }, |
| 40 | + }, |
| 41 | + }`), &pgadmin.Spec)) |
| 42 | + |
| 43 | + err := cc.Create(ctx, pgadmin, client.DryRunAll) |
| 44 | + assert.Assert(t, apierrors.IsInvalid(err)) |
| 45 | + assert.ErrorContains(t, err, "retentionPeriod") |
| 46 | + assert.ErrorContains(t, err, "hour|day|week") |
| 47 | + assert.ErrorContains(t, err, "one hour") |
| 48 | + |
| 49 | + //nolint:errorlint // This is a test, and a panic is unlikely. |
| 50 | + status := err.(apierrors.APIStatus).Status() |
| 51 | + assert.Assert(t, status.Details != nil) |
| 52 | + assert.Equal(t, len(status.Details.Causes), 2) |
| 53 | + |
| 54 | + for _, cause := range status.Details.Causes { |
| 55 | + assert.Equal(t, cause.Field, "spec.instrumentation.logs.retentionPeriod") |
| 56 | + } |
| 57 | + |
| 58 | + t.Run("Valid", func(t *testing.T) { |
| 59 | + for _, tt := range []string{ |
| 60 | + "28 weeks", |
| 61 | + "90 DAY", |
| 62 | + "1 hr", |
| 63 | + "PT1D2H", |
| 64 | + "1 week 2 days", |
| 65 | + } { |
| 66 | + u, err := runtime.ToUnstructuredObject(pgadmin) |
| 67 | + assert.NilError(t, err) |
| 68 | + assert.NilError(t, unstructured.SetNestedField(u.Object, |
| 69 | + tt, "spec", "instrumentation", "logs", "retentionPeriod")) |
| 70 | + |
| 71 | + assert.NilError(t, cc.Create(ctx, u, client.DryRunAll), tt) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + t.Run("Invalid", func(t *testing.T) { |
| 76 | + for _, tt := range []string{ |
| 77 | + // Amount too small |
| 78 | + "0 days", |
| 79 | + "0", |
| 80 | + |
| 81 | + // Text too long |
| 82 | + "2 weeks 3 days 4 hours", |
| 83 | + } { |
| 84 | + u, err := runtime.ToUnstructuredObject(pgadmin) |
| 85 | + assert.NilError(t, err) |
| 86 | + assert.NilError(t, unstructured.SetNestedField(u.Object, |
| 87 | + tt, "spec", "instrumentation", "logs", "retentionPeriod")) |
| 88 | + |
| 89 | + err = cc.Create(ctx, u, client.DryRunAll) |
| 90 | + assert.Assert(t, apierrors.IsInvalid(err), tt) |
| 91 | + assert.ErrorContains(t, err, "retentionPeriod") |
| 92 | + } |
| 93 | + }) |
| 94 | + }) |
| 95 | +} |
0 commit comments