Skip to content

Commit b0697a2

Browse files
committed
Ensure Duration.AsDuration returns a copy
1 parent f26aa57 commit b0697a2

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func NewDuration(s string) (*Duration, error) {
8181
return &Duration{metav1.Duration(umd), s}, err
8282
}
8383

84-
// AsDuration returns d as a [metav1.Duration].
84+
// AsDuration returns a copy of d as a [metav1.Duration].
8585
func (d *Duration) AsDuration() metav1.Duration {
8686
return d.parsed
8787
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ import (
1616
"sigs.k8s.io/yaml"
1717
)
1818

19+
func TestDurationAsDuration(t *testing.T) {
20+
t.Parallel()
21+
22+
v, err := NewDuration("2s")
23+
assert.NilError(t, err)
24+
25+
// get the value
26+
other := v.AsDuration()
27+
assert.Equal(t, other.Duration, 2*time.Second,
28+
"expected the same value as the original")
29+
30+
// change the copy
31+
other.Duration = time.Hour
32+
assert.Equal(t, v.AsDuration().Duration, 2*time.Second,
33+
"expected no effect on the original value")
34+
}
35+
1936
func TestDurationYAML(t *testing.T) {
2037
t.Parallel()
2138

0 commit comments

Comments
 (0)