Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update centrally managed files #43

Merged
merged 17 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.10"]
python-version: ['3.8', '3.10']
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
Expand All @@ -52,7 +52,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.10"]
python-version: ['3.8', '3.10']
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
Expand Down
96 changes: 79 additions & 17 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,35 +1,97 @@
# This is a template `pyproject.toml` file for ops charms
# This file is managed by bootstack-charms-spec and should not be modified
# within individual charm repos. https://launchpad.net/bootstack-charms-spec
# This file is centrally managed as a template file in https://github.com/canonical/solutions-engineering-automation
# To update the file:
# - Edit it in the canonical/solutions-engineering-automation repository.
# - Open a PR with the changes.
# - When the PR merges, the soleng-terraform bot will open a PR to the target repositories with the changes.

[tool.flake8]
ignore = ["D100", "D101", "D102", "D103"]
exclude = ['.eggs', '.git', '.tox', '.venv', '.build', 'report']
max-line-length = 88
max-line-length = 99
max-doc-length = 99
max-complexity = 10
exclude = [
".git",
"__pycache__",
".tox",
".build",
"build",
"dist",
".eggs",
"*.egg_info",
"venv",
".venv",
"report",
"docs",
]
select = ["E", "W", "F", "C", "N", "R", "D", "H"]
# Ignore W503, E501 because using black creates errors with this
# Ignore D107 Missing docstring in __init__
# Ignore D415 Docstring first line punctuation (doesn't make sense for properties)
# Ignore N818 Exceptions end with "Error" (not all exceptions are errors)
# D100, D101, D102, D103: Ignore missing docstrings in tests
ignore = ["C901", "W503", "E501", "D107", "D415", "N818", "D100", "D101", "D102", "D103", "W504"]
per-file-ignores = ["tests/*:D100,D101,D102,D103,D104"]
# Check for properly formatted copyright header in each file
copyright-check = "True"
copyright-author = "Canonical Ltd."
copyright-regexp = "Copyright\\s\\d{4}([-,]\\d{4})*\\s+%(author)s"

[tool.black]
line-length = 88
line-length = 99
exclude = '''
/(
| .eggs
| .git
| .tox
| .venv
| .build
| build
| lib
| report
| docs
)/
'''

[tool.isort]
profile = "black"
skip_glob = '''
/(
| .eggs
| .git
| .tox
| .venv
| .build
| report
)/
'''
line_length = 99
skip_glob = [".eggs", ".git", ".tox", ".venv", ".build", "build", "lib", "report"]

[tool.pylint]
max-line-length = 99
disable = ["E1102"]
ignore = ['.eggs', '.git', '.tox', '.venv', '.build', 'lib', 'report', 'tests', 'docs']

[tool.mypy]
warn_unused_ignores = true
warn_unused_configs = true
warn_unreachable = true
disallow_untyped_defs = true
ignore_missing_imports = true
no_namespace_packages = true
exclude = ['.eggs', '.git', '.tox', '.venv', '.build', 'lib', 'report', 'tests', 'docs']

[tool.codespell]
skip = ".eggs,.tox,.git,.venv,venv,build,.build,lib,report,docs,poetry.lock,htmlcov"
quiet-level = 3
check-filenames = true
ignore-words-list = "assertIn"

## Ignore unsupported imports
[[tool.mypy.overrides]]
module = ["charmhelpers.*", "setuptools"]
ignore_missing_imports = true

[tool.coverage.run]
relative_files = true
source = ["."]
omit = ["tests/**", "docs/**", "lib/**", "snap/**", "build/**", "setup.py"]

[tool.coverage.report]
fail_under = 100
show_missing = true

[tool.coverage.html]
directory = "tests/report/html"

[tool.coverage.xml]
output = "tests/report/coverage.xml"
8 changes: 2 additions & 6 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def __init__(self, *args): # noqa: D107
self.framework.observe(self.on.config_changed, self._on_config_changed)
self.framework.observe(self.on.install, self._on_install)
self.framework.observe(self.on.update_status, self._on_update_status)
self.framework.observe(
self.on.publish_relation_changed, self._on_publish_relation_changed
)
self.framework.observe(self.on.publish_relation_changed, self._on_publish_relation_changed)
self.framework.observe(
self.on.publish_relation_departed, self._on_publish_relation_departed
)
Expand Down Expand Up @@ -87,9 +85,7 @@ def _on_config_changed(self, _):
atomic_write_root_file(SSL_CERT_PATH, b64decode(config["ssl_cert"]), 0o640)
atomic_write_root_file(SSL_KEY_PATH, b64decode(config["ssl_key"]), 0o640)
elif config.get("ssl_cert") or config.get("ssl_key"):
self.model.unit.status = BlockedStatus(
"Both ssl_cert and ssl_key must be configured"
)
self.model.unit.status = BlockedStatus("Both ssl_cert and ssl_key must be configured")
return
else:
ssl_cert_path.unlink(missing_ok=True)
Expand Down
4 changes: 1 addition & 3 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ def atomic_write_root_file(path: str, content: bytes, perms: int) -> None:
:param bytes content: The content to write to the file.
:param int perms: The file permissions to set on the created file.
"""
logger.debug(
"Writing file {} with root ownership and permissions {:o}".format(path, perms)
)
logger.debug("Writing file {} with root ownership and permissions {:o}".format(path, perms))

# Create a temporary file and replace the target file
# This ensures atomic file writing for safety
Expand Down
8 changes: 2 additions & 6 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ def test_config_changed(self, mock_subproc):
self.assertTrue(harness.charm._render_config.called)
self.assertTrue(harness.charm._reload_config.called)
assert mock_subproc.call_args == call(["update-ca-certificates", "--fresh"])
assert harness.model.unit.status == BlockedStatus(
"Failed to update CA certificates"
)
assert harness.model.unit.status == BlockedStatus("Failed to update CA certificates")

def test_publish_relation_joined(self):
harness = Harness(NginxCharm)
Expand Down Expand Up @@ -167,9 +165,7 @@ def test_reload_config(self, mock_subproc):
self.addCleanup(harness.cleanup)
harness.begin()
harness.charm._reload_config()
assert mock_subproc.call_args == call(
["systemctl", "reload-or-restart", "nginx"]
)
assert mock_subproc.call_args == call(["systemctl", "reload-or-restart", "nginx"])

@patch("charm.islink")
@patch("os.symlink")
Expand Down
12 changes: 3 additions & 9 deletions tests/unit/test_charm_TLS_enabled.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class TestCharmTLS(unittest.TestCase):
@patch("subprocess.check_call")
@patch("charm.atomic_write_root_file")
def test_config_changed(self, mock_write_file, mock_check_call):
mock_check_call.side_effect = subprocess.CalledProcessError(
1, "update-ca-certificates"
)
mock_check_call.side_effect = subprocess.CalledProcessError(1, "update-ca-certificates")
harness = Harness(NginxCharm)
self.addCleanup(harness.cleanup)
harness.begin()
Expand Down Expand Up @@ -65,9 +63,7 @@ def test_config_changed(self, mock_write_file, mock_check_call):
)
@patch("charm.Path.unlink")
@patch("charm.atomic_write_root_file")
def test_config_changed_remove_files(
self, mock_write_file, mock_unlink, mock_path_exists
):
def test_config_changed_remove_files(self, mock_write_file, mock_unlink, mock_path_exists):
harness = Harness(NginxCharm)
self.addCleanup(harness.cleanup)
harness.begin()
Expand Down Expand Up @@ -142,9 +138,7 @@ def test_atomic_write_root_file(
mock_tempfile_instance.write.assert_called_once_with(content)
mock_tempfile_instance.flush.assert_called_once()

mock_fchown.assert_called_once_with(
mock_tempfile_instance.fileno(), mock_uid, mock_gid
)
mock_fchown.assert_called_once_with(mock_tempfile_instance.fileno(), mock_uid, mock_gid)
mock_fchmod.assert_called_once_with(mock_tempfile_instance.fileno(), perms)

mock_rename.assert_called_once_with(mock_tempfile_instance.name, path)
5 changes: 2 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,20 @@ passenv =

[testenv:lint]
commands =
pflake8
flake8
black --check --diff --color .
isort --check --diff --color .
deps =
black
flake8
pyproject-flake8
flake8-pyproject
flake8-docstrings
pep8-naming
flake8-colors
colorama
isort

[testenv:reformat]
envdir = {toxworkdir}/lint
commands =
black .
isort .
Expand Down