Skip to content

Commit

Permalink
Add juju 3 compatibility for cronjob (#42)
Browse files Browse the repository at this point in the history
Add juju 3 compatibility for cronjob

Fixes: #41
  • Loading branch information
samuelallan72 authored Jun 5, 2024
1 parent 8e6c09f commit 8c3b4c4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/lib/lib_cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,20 @@ def install_cronjob(self):

logrotate_unit = hookenv.local_unit()
python_venv_path = os.getcwd().replace("charm", "") + ".venv/bin/python3"
# juju run was changed to juju exec in juju 3.0.
# This will return True if juju is at least version 3.0.
if hookenv.has_juju_version("3.0"):
juju_exec = "juju-exec"
else:
juju_exec = "juju-run"
# upgrade to template if logic increases
cron_job = """#!/bin/bash
/usr/bin/sudo /usr/bin/juju-run {} "{} {}"
/usr/bin/sudo /usr/bin/{juju_exec} {logrotate_unit} "{python_venv_path} {cronjob_path}"
""".format(
logrotate_unit, python_venv_path, cronjob_path
juju_exec=juju_exec,
logrotate_unit=logrotate_unit,
python_venv_path=python_venv_path,
cronjob_path=cronjob_path,
)
with open(cron_file_path, "w") as cron_file:
cron_file.write(cron_job)
Expand Down
58 changes: 58 additions & 0 deletions src/tests/unit/test_logrotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,12 @@ def test_install_cronjob(self, cron, mock_local_unit, mocker):
"lib_cron.os.path.realpath",
return_value=os.path.join(mock_charm_dir, "lib/lib_cron.py"),
)
# has_juju_version is used to test for juju3,
# so keep it false here to verify the original juju2 behaviour.
mocker.patch(
"lib_cron.hookenv.has_juju_version",
return_value=False,
)
mocker.patch("lib_cron.os.getcwd", return_value=mock_charm_dir)
mock_open = mocker.patch("lib_cron.open")
mock_handle = mock_open.return_value.__enter__.return_value
Expand Down Expand Up @@ -509,6 +515,58 @@ def test_install_cronjob(self, cron, mock_local_unit, mocker):
)
mock_chmod.assert_called_once_with("/etc/cron.weekly/charm-logrotate", 0o755)

def test_install_cronjob_juju3(self, cron, mock_local_unit, mocker):
"""Test install cronjob method under juju3."""
mock_charm_dir = "/mock/unit-logrotated-0/charm"
mock_exists = mocker.patch("lib_cron.os.path.exists", return_value=True)
mock_remove = mocker.patch("lib_cron.os.remove")
mock_chmod = mocker.patch("lib_cron.os.chmod")
mocker.patch(
"lib_cron.os.path.realpath",
return_value=os.path.join(mock_charm_dir, "lib/lib_cron.py"),
)
# has_juju_version is used to test for juju3.
# Set it True here so it thinks it's running under juju3.
mocker.patch(
"lib_cron.hookenv.has_juju_version",
return_value=True,
)
mocker.patch("lib_cron.os.getcwd", return_value=mock_charm_dir)
mock_open = mocker.patch("lib_cron.open")
mock_handle = mock_open.return_value.__enter__.return_value
mock_write_to_crontab = mocker.Mock()
mocker.patch.object(cron, "write_to_crontab", new=mock_write_to_crontab)
expected_files_to_be_removed = [
"/etc/cron.hourly/charm-logrotate",
"/etc/cron.daily/charm-logrotate",
"/etc/cron.weekly/charm-logrotate",
"/etc/cron.monthly/charm-logrotate",
]

cron_config = cron()
cron_config.cronjob_enabled = True
cron_config.cronjob_frequency = 2
cron_config.cron_daily_schedule = "unset"
cron_config.install_cronjob()

mock_exists.assert_has_calls(
[mock.call(file) for file in expected_files_to_be_removed], any_order=True
)
mock_remove.assert_has_calls(
[mock.call(file) for file in expected_files_to_be_removed], any_order=True
)
mock_open.assert_called_once_with("/etc/cron.weekly/charm-logrotate", "w")
# should be juju-exec under juju3
mock_handle.write.assert_called_once_with(
dedent(
"""\
#!/bin/bash
/usr/bin/sudo /usr/bin/juju-exec unit-logrotated/0 "/mock/unit-logrotated-0/.venv/bin/python3 /mock/unit-logrotated-0/charm/lib/lib_cron.py"
""" # noqa
)
)
mock_chmod.assert_called_once_with("/etc/cron.weekly/charm-logrotate", 0o755)

def test_install_cronjob_removes_etc_config_when_cronjob_disabled(
self, cron, mocker
):
Expand Down

0 comments on commit 8c3b4c4

Please sign in to comment.