|
| 1 | +/* |
| 2 | + Copyright 2021 - 2024 Crunchy Data Solutions, Inc. |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package validation |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "gotest.tools/v3/assert" |
| 24 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 26 | + "sigs.k8s.io/yaml" |
| 27 | + |
| 28 | + "github.com/crunchydata/postgres-operator/internal/testing/cmp" |
| 29 | + "github.com/crunchydata/postgres-operator/internal/testing/require" |
| 30 | + "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1" |
| 31 | +) |
| 32 | + |
| 33 | +func TestPostgresUserOptions(t *testing.T) { |
| 34 | + ctx := context.Background() |
| 35 | + cc := require.Kubernetes(t) |
| 36 | + t.Parallel() |
| 37 | + |
| 38 | + namespace := require.Namespace(t, cc) |
| 39 | + base := v1beta1.NewPostgresCluster() |
| 40 | + |
| 41 | + // Start with a bunch of required fields. |
| 42 | + assert.NilError(t, yaml.Unmarshal([]byte(`{ |
| 43 | + postgresVersion: 16, |
| 44 | + backups: { |
| 45 | + pgbackrest: { |
| 46 | + repos: [{ name: repo1 }], |
| 47 | + }, |
| 48 | + }, |
| 49 | + instances: [{ |
| 50 | + dataVolumeClaimSpec: { |
| 51 | + accessModes: [ReadWriteOnce], |
| 52 | + resources: { requests: { storage: 1Mi } }, |
| 53 | + }, |
| 54 | + }], |
| 55 | + }`), &base.Spec)) |
| 56 | + |
| 57 | + base.Namespace = namespace.Name |
| 58 | + base.Name = "postgres-user-options" |
| 59 | + |
| 60 | + assert.NilError(t, cc.Create(ctx, base.DeepCopy(), client.DryRunAll), |
| 61 | + "expected this base cluster to be valid") |
| 62 | + |
| 63 | + // See [internal/controller/postgrescluster.TestValidatePostgresUsers] |
| 64 | + |
| 65 | + t.Run("NoComments", func(t *testing.T) { |
| 66 | + cluster := base.DeepCopy() |
| 67 | + cluster.Spec.Users = []v1beta1.PostgresUserSpec{ |
| 68 | + {Name: "dashes", Options: "ANY -- comment"}, |
| 69 | + {Name: "block-open", Options: "/* asdf"}, |
| 70 | + {Name: "block-close", Options: " qw */ rt"}, |
| 71 | + } |
| 72 | + |
| 73 | + err := cc.Create(ctx, cluster, client.DryRunAll) |
| 74 | + assert.Assert(t, apierrors.IsInvalid(err)) |
| 75 | + assert.ErrorContains(t, err, "cannot contain comments") |
| 76 | + |
| 77 | + //nolint:errorlint // This is a test, and a panic is unlikely. |
| 78 | + status := err.(apierrors.APIStatus).Status() |
| 79 | + assert.Assert(t, status.Details != nil) |
| 80 | + assert.Equal(t, len(status.Details.Causes), 3) |
| 81 | + |
| 82 | + for i, cause := range status.Details.Causes { |
| 83 | + assert.Equal(t, cause.Field, fmt.Sprintf("spec.users[%d].options", i)) |
| 84 | + assert.Assert(t, cmp.Contains(cause.Message, "cannot contain comments")) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("NoPassword", func(t *testing.T) { |
| 89 | + cluster := base.DeepCopy() |
| 90 | + cluster.Spec.Users = []v1beta1.PostgresUserSpec{ |
| 91 | + {Name: "uppercase", Options: "SUPERUSER PASSWORD ''"}, |
| 92 | + {Name: "lowercase", Options: "password 'asdf'"}, |
| 93 | + } |
| 94 | + |
| 95 | + err := cc.Create(ctx, cluster, client.DryRunAll) |
| 96 | + assert.Assert(t, apierrors.IsInvalid(err)) |
| 97 | + assert.ErrorContains(t, err, "cannot assign password") |
| 98 | + |
| 99 | + //nolint:errorlint // This is a test, and a panic is unlikely. |
| 100 | + status := err.(apierrors.APIStatus).Status() |
| 101 | + assert.Assert(t, status.Details != nil) |
| 102 | + assert.Equal(t, len(status.Details.Causes), 2) |
| 103 | + |
| 104 | + for i, cause := range status.Details.Causes { |
| 105 | + assert.Equal(t, cause.Field, fmt.Sprintf("spec.users[%d].options", i)) |
| 106 | + assert.Assert(t, cmp.Contains(cause.Message, "cannot assign password")) |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + t.Run("NoTerminators", func(t *testing.T) { |
| 111 | + cluster := base.DeepCopy() |
| 112 | + cluster.Spec.Users = []v1beta1.PostgresUserSpec{ |
| 113 | + {Name: "semicolon", Options: "some ;where"}, |
| 114 | + } |
| 115 | + |
| 116 | + err := cc.Create(ctx, cluster, client.DryRunAll) |
| 117 | + assert.Assert(t, apierrors.IsInvalid(err)) |
| 118 | + assert.ErrorContains(t, err, "should match") |
| 119 | + |
| 120 | + //nolint:errorlint // This is a test, and a panic is unlikely. |
| 121 | + status := err.(apierrors.APIStatus).Status() |
| 122 | + assert.Assert(t, status.Details != nil) |
| 123 | + assert.Equal(t, len(status.Details.Causes), 1) |
| 124 | + assert.Equal(t, status.Details.Causes[0].Field, "spec.users[0].options") |
| 125 | + }) |
| 126 | + |
| 127 | + t.Run("Valid", func(t *testing.T) { |
| 128 | + cluster := base.DeepCopy() |
| 129 | + cluster.Spec.Users = []v1beta1.PostgresUserSpec{ |
| 130 | + {Name: "normal", Options: "CREATEDB valid until '2006-01-02'"}, |
| 131 | + {Name: "very-full", Options: "NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT NOLOGIN NOREPLICATION NOBYPASSRLS CONNECTION LIMIT 5"}, |
| 132 | + } |
| 133 | + |
| 134 | + assert.NilError(t, cc.Create(ctx, cluster, client.DryRunAll)) |
| 135 | + }) |
| 136 | +} |
0 commit comments