Merge ~mertkirpici/charm-logrotated:lp/1969155 into charm-logrotated:master

Proposed by Mert Kirpici
Status: Merged
Approved by: Erhan Sunar
Approved revision: fecd39f8e9feac48e708e8141816a8e4acef3c6a
Merged at revision: e0b3d4381624e3e48f5a56cefd51fd24af4b9f1b
Proposed branch: ~mertkirpici/charm-logrotated:lp/1969155
Merge into: charm-logrotated:master
Diff against target: 50 lines (+20/-8)
2 files modified
src/lib/lib_logrotate.py (+4/-8)
src/tests/unit/test_logrotate.py (+16/-0)
Reviewer Review Type Date Requested Status
🤖 prod-jenkaas-bootstack (community) continuous-integration Approve
Robert Gildein Approve
JamesLin Approve
BootStack Reviewers Pending
Eric Chen Pending
Review via email: mp+441836@code.launchpad.net

This proposal supersedes a proposal from 2022-08-29.

Commit message

Removed duplicated header in logrotate config file

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : Posted in a previous version of this proposal

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Eric Chen (eric-chen) wrote : Posted in a previous version of this proposal

Could we add some unit test for this function? because the behavior is changed.

And please attach the log of unit/func test by pastebin. Currently , CI bot is not integrated, we need to run it manually and attach the result.

I also leave some comment below. Thanks!

review: Needs Information
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
Robert Gildein (rgildein) wrote :

Suggestion to make it simpler.

Revision history for this message
Mert Kirpici (mertkirpici) wrote :

> Suggestion to make it simpler.

Hey, thanks for the suggestion. I do agree it does make it simpler. I will apply it. Marking this in progress since I want to test it thoroughly.

Revision history for this message
JamesLin (jneo8) wrote :

+1 for Eric's comment.

review: Needs Fixing
Revision history for this message
JamesLin (jneo8) :
review: Needs Information
Revision history for this message
JamesLin (jneo8) wrote :

LGTM

review: Approve
Revision history for this message
Robert Gildein (rgildein) wrote :

LGTM, Thanks

review: Approve
Revision history for this message
Mert Kirpici (mertkirpici) wrote :

Applied the suggestion, also changed the os.linesep into "\n" since we are writing to the file in text mode, upon reading this[0] in the python docs.

[0] https://docs.python.org/3/library/os.html#os.linesep
...
Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms
...

Thanks for the reviews. If CI comes clean, I think we can land this.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision e0b3d4381624e3e48f5a56cefd51fd24af4b9f1b

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/lib/lib_logrotate.py b/src/lib/lib_logrotate.py
2index 7b60c25..c413b8b 100644
3--- a/src/lib/lib_logrotate.py
4+++ b/src/lib/lib_logrotate.py
5@@ -114,14 +114,10 @@ class LogrotateHelper:
6 header = (
7 "# Configuration file maintained by Juju. Local changes may be overwritten"
8 )
9-
10- split = content.split("\n")
11- if split[0].startswith(header):
12- result = content
13- else:
14- result = header + "\n" + content
15-
16- return result
17+ content = [
18+ row for row in content.splitlines() if row and not row.startswith(header)
19+ ]
20+ return "\n".join([header, *content]) + "\n"
21
22 @classmethod
23 def calculate_count(cls, item, retention):
24diff --git a/src/tests/unit/test_logrotate.py b/src/tests/unit/test_logrotate.py
25index c0c5cdb..08797ce 100644
26--- a/src/tests/unit/test_logrotate.py
27+++ b/src/tests/unit/test_logrotate.py
28@@ -138,6 +138,22 @@ class TestLogrotateHelper:
29 )
30 assert mod_contents == expected_contents
31
32+ @pytest.mark.parametrize("header_count", [0, 1, 2, 10])
33+ def test_modify_header(self, logrotate, header_count):
34+ """Test the modify_header method works and is idempotent."""
35+ header = "# Configuration file maintained by Juju. Local changes may be overwritten\n" # noqa
36+ content_body = (
37+ "\n/log/some.log {\n rotate 42\n daily\n}\n\n"
38+ "/log/other.log {\n rotate 6\n weekly\n}\n"
39+ )
40+ content = (header * header_count) + content_body
41+ expected_content = (
42+ header + "/log/some.log {\n rotate 42\n daily\n}\n"
43+ "/log/other.log {\n rotate 6\n weekly\n}\n"
44+ )
45+ modified_content = logrotate.modify_header(logrotate, content)
46+ assert modified_content == expected_content
47+
48
49 class TestCronHelper:
50 """Main cron test class."""

Subscribers

People subscribed via source and target branches

to all changes: