Skip to content

Commit 0a95d7c

Browse files
committed
FIXUP: test ISO 8601, max length on rules
1 parent 55704fd commit 0a95d7c

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

config/crd/bases/postgres-operator.crunchydata.com_pgadmins.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1954,8 +1954,9 @@ spec:
19541954
How long to retain log files locally. An RFC 3339 duration or a number
19551955
and unit: `3d`, `4 weeks`, `12 hr`, etc.
19561956
format: duration
1957+
maxLength: 20
19571958
minLength: 1
1958-
pattern: ^(PT)?[0-9]+ *(?i:(h|hr|d|w|wk)|(hour|day|week)s?)+$
1959+
pattern: ^(PT)?( *[0-9]+ *(?i:(h|hr|d|w|wk)|(hour|day|week)s?))+$
19591960
type: string
19601961
x-kubernetes-validations:
19611962
- message: must be at least one hour

config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11487,8 +11487,9 @@ spec:
1148711487
How long to retain log files locally. An RFC 3339 duration or a number
1148811488
and unit: `3d`, `4 weeks`, `12 hr`, etc.
1148911489
format: duration
11490+
maxLength: 20
1149011491
minLength: 1
11491-
pattern: ^(PT)?[0-9]+ *(?i:(h|hr|d|w|wk)|(hour|day|week)s?)+$
11492+
pattern: ^(PT)?( *[0-9]+ *(?i:(h|hr|d|w|wk)|(hour|day|week)s?))+$
1149211493
type: string
1149311494
x-kubernetes-validations:
1149411495
- message: must be at least one hour

internal/testing/validation/pgadmin_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ func TestPGAdminInstrumentation(t *testing.T) {
6060
"28 weeks",
6161
"90 DAY",
6262
"1 hr",
63-
"PT1D",
63+
"PT1D2H",
64+
"1 week 2 days",
6465
} {
6566
u, err := runtime.ToUnstructuredObject(pgadmin)
6667
assert.NilError(t, err)
@@ -73,8 +74,12 @@ func TestPGAdminInstrumentation(t *testing.T) {
7374

7475
t.Run("Invalid", func(t *testing.T) {
7576
for _, tt := range []string{
77+
// Amount too small
7678
"0 days",
7779
"0",
80+
81+
// Text too long
82+
"2 weeks 3 days 4 hours",
7883
} {
7984
u, err := runtime.ToUnstructuredObject(pgadmin)
8085
assert.NilError(t, err)

pkg/apis/postgres-operator.crunchydata.com/v1beta1/instrumentation_types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@ type InstrumentationLogsSpec struct {
5858
// ---
5959
// Kubernetes ensures the value is in the "duration" format, but go ahead
6060
// and loosely validate the format to show some acceptable units.
61-
// +kubebuilder:validation:Pattern=`^(PT)?[0-9]+ *(?i:(h|hr|d|w|wk)|(hour|day|week)s?)+$`
61+
// +kubebuilder:validation:Pattern=`^(PT)?( *[0-9]+ *(?i:(h|hr|d|w|wk)|(hour|day|week)s?))+$`
6262
//
6363
// `controller-gen` needs to know "Type=string" to allow a "Pattern".
6464
// +kubebuilder:validation:Type=string
6565
//
66+
// Set a max length to keep rule costs low.
67+
// +kubebuilder:validation:MaxLength=20
6668
// +kubebuilder:validation:XValidation:rule=`duration("1h") <= self && self <= duration("8760h")`,message="must be at least one hour"
69+
//
6770
// +optional
6871
RetentionPeriod *Duration `json:"retentionPeriod,omitempty"`
6972
}

pkg/apis/postgres-operator.crunchydata.com/v1beta1/shared_types_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ func TestDurationYAML(t *testing.T) {
4848
})
4949

5050
t.Run("UnitsIn", func(t *testing.T) {
51+
const Day = 24 * time.Hour
52+
const Week = 7 * Day
53+
5154
for _, tt := range []struct {
5255
input string
5356
result time.Duration
@@ -91,16 +94,19 @@ func TestDurationYAML(t *testing.T) {
9194
{"4 hours", 4 * time.Hour},
9295
{"5 hourglass", 5 * time.Hour},
9396

94-
{"1d", 24 * time.Hour},
95-
{"2 day", 2 * 24 * time.Hour},
96-
{"3 days", 3 * 24 * time.Hour},
97-
{"4 dayrock", 4 * 24 * time.Hour},
97+
{"1d", Day},
98+
{"2 day", 2 * Day},
99+
{"3 days", 3 * Day},
100+
{"4 dayrock", 4 * Day},
101+
102+
{"1w", Week},
103+
{"2 wk", 2 * Week},
104+
{"3 week", 3 * Week},
105+
{"4 weeks", 4 * Week},
106+
{"5 weekpasta", 5 * Week},
98107

99-
{"1w", 7 * 24 * time.Hour},
100-
{"2 wk", 2 * 7 * 24 * time.Hour},
101-
{"3 week", 3 * 7 * 24 * time.Hour},
102-
{"4 weeks", 4 * 7 * 24 * time.Hour},
103-
{"5 weekpasta", 5 * 7 * 24 * time.Hour},
108+
// ISO 8601 / RFC 33339
109+
{"PT2D9H", (2 * Day) + 9*time.Hour},
104110
} {
105111
var parsed Duration
106112
assert.NilError(t, yaml.Unmarshal([]byte(tt.input), &parsed))

0 commit comments

Comments
 (0)