Skip to content

Commit 7ef4f36

Browse files
committed
Add/adjust tests/code after rebasing on newer retention API commits.
1 parent c230438 commit 7ef4f36

File tree

3 files changed

+78
-18
lines changed

3 files changed

+78
-18
lines changed

internal/collector/config.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,10 @@ func generateLogrotateConfig(logFilePath string, retentionPeriod *v1beta1.Durati
149149
)
150150
}
151151

152-
// FIXME: If the rotate count is 0, logrotate will not keep any archives, it will
153-
// essentially just empty out the working file during rotation, but it will still
154-
// rotate on the interval specified (hourly, daily, weekly, etc). If the user
155-
// sets the retentionPeriod to 0, we currently default to hourly regardless of
156-
// what unit the user provides. E.g. If the user sets the retentionPeriod to "0w"
157-
// they might expect that it will rotate once a week, but not keep any archives,
158-
// but we will actually rotate every hour... Is there a way to get the unit
159-
// provided in the Duration??
152+
// parseDurationForLogrotate takes a retention period and returns the rotate
153+
// number and interval string that should be used in the logrotate config
160154
func parseDurationForLogrotate(retentionPeriod *v1beta1.Duration) (int, string) {
161-
hours := math.Round(retentionPeriod.Duration.Hours())
155+
hours := math.Round(retentionPeriod.AsDuration().Hours())
162156
if hours < 24 {
163157
return int(hours), "hourly"
164158
}

internal/collector/config_test.go

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
74136
func 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)

internal/collector/logrotate.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
nocompress
77
%s
88
postrotate
9-
%s
9+
%s
1010
endscript
1111
}

0 commit comments

Comments
 (0)