@@ -65,12 +65,74 @@ service:
6565}
6666
6767// TODO: write this test after rebasing on new retention API changes.
68- // func TestGenerateLogrotateConfig(t *testing.T) {
69-
70- // }
68+ func TestGenerateLogrotateConfig (t * testing.T ) {
69+ for _ , tt := range []struct {
70+ logFilePath string
71+ retentionPeriod string
72+ postrotateScript string
73+ result string
74+ }{
75+ {
76+ logFilePath : "/this/is/a/file.path" ,
77+ retentionPeriod : "12h" ,
78+ postrotateScript : "echo 'Hello, World'" ,
79+ result : `/this/is/a/file.path {
80+ rotate 12
81+ missingok
82+ sharedscripts
83+ notifempty
84+ nocompress
85+ hourly
86+ postrotate
87+ echo 'Hello, World'
88+ endscript
89+ }
90+ ` ,
91+ },
92+ {
93+ logFilePath : "/tmp/test.log" ,
94+ retentionPeriod : "5 days" ,
95+ postrotateScript : "" ,
96+ result : `/tmp/test.log {
97+ rotate 5
98+ missingok
99+ sharedscripts
100+ notifempty
101+ nocompress
102+ daily
103+ postrotate
104+
105+ endscript
106+ }
107+ ` ,
108+ },
109+ {
110+ logFilePath : "/tmp/test.log" ,
111+ retentionPeriod : "5wk" ,
112+ postrotateScript : "pkill -HUP --exact pgbouncer" ,
113+ result : `/tmp/test.log {
114+ rotate 35
115+ missingok
116+ sharedscripts
117+ notifempty
118+ nocompress
119+ daily
120+ postrotate
121+ pkill -HUP --exact pgbouncer
122+ endscript
123+ }
124+ ` ,
125+ },
126+ } {
127+ t .Run (tt .retentionPeriod , func (t * testing.T ) {
128+ duration , err := v1beta1 .NewDuration (tt .retentionPeriod )
129+ assert .NilError (t , err )
130+ result := generateLogrotateConfig (tt .logFilePath , duration , tt .postrotateScript )
131+ assert .Equal (t , tt .result , result )
132+ })
133+ }
134+ }
71135
72- // FIXME: This test is currently broken. Fix after rebasing on new
73- // retention API changes.
74136func TestParseDurationForLogrotate (t * testing.T ) {
75137 for _ , tt := range []struct {
76138 retentionPeriod string
@@ -88,7 +150,12 @@ func TestParseDurationForLogrotate(t *testing.T) {
88150 interval : "daily" ,
89151 },
90152 {
91- retentionPeriod : "36hour" ,
153+ retentionPeriod : "35hour" ,
154+ number : 1 ,
155+ interval : "daily" ,
156+ },
157+ {
158+ retentionPeriod : "36 hours" ,
92159 number : 2 ,
93160 interval : "daily" ,
94161 },
@@ -119,8 +186,7 @@ func TestParseDurationForLogrotate(t *testing.T) {
119186 },
120187 } {
121188 t .Run (tt .retentionPeriod , func (t * testing.T ) {
122- var duration * v1beta1.Duration
123- err := duration .UnmarshalJSON ([]byte (tt .retentionPeriod ))
189+ duration , err := v1beta1 .NewDuration (tt .retentionPeriod )
124190 assert .NilError (t , err )
125191 number , interval := parseDurationForLogrotate (duration )
126192 assert .Equal (t , tt .number , number )
0 commit comments