Skip to content

Commit

Permalink
Merge branch 'main' into juju-reboot-929
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer authored Oct 10, 2023
2 parents a8576c3 + 529488d commit 74b2e1f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/db-charm-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ jobs:

- name: Run the charm's unit tests
run: tox -vve unit
# TODO: remove this once https://github.com/canonical/mysql-k8s-operator/pull/316 is fixed
env:
# This env var is only to indicate Juju version to "simulate" in the mysqk-k8s-operator
# unit tests. No libjuju is being actually used in unit testing.
LIBJUJU_VERSION_SPECIFIER: 3.1
11 changes: 11 additions & 0 deletions ops/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
'_RelationMetaDict', {
'interface': Required[str],
'limit': int,
'optional': bool,
'scope': _Scopes},
total=False)

Expand Down Expand Up @@ -1300,6 +1301,14 @@ class RelationMeta:
Will be either ``"global"`` or ``"container"``.
"""

optional: bool
"""If True, the relation is considered optional.
This value is informational only and is not used by Juju itself (all
relations are optional from Juju's perspective), but it may be set in
``metadata.yaml`` and used by the charm code if appropriate.
"""

VALID_SCOPES = ['global', 'container']

def __init__(self, role: RelationRole, relation_name: str, raw: '_RelationMetaDict'):
Expand All @@ -1319,6 +1328,8 @@ def __init__(self, role: RelationRole, relation_name: str, raw: '_RelationMetaDi
raise TypeError("scope should be one of {}; not '{}'".format(
', '.join(f"'{s}'" for s in self.VALID_SCOPES), self.scope))

self.optional = raw.get('optional', False)


class StorageMeta:
"""Object containing metadata about a storage definition."""
Expand Down
10 changes: 9 additions & 1 deletion ops/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2546,7 +2546,15 @@ def remove_path(self, path: Union[str, PurePath], *, recursive: bool = False):
Args:
path: Path of the file or directory to delete from the remote system.
recursive: If True, recursively delete path and everything under it.
recursive: If True, and path is a directory, recursively delete it and
everything under it. If path is a file, delete the file. In
either case, do nothing if the file or directory does not
exist. Behaviourally similar to ``rm -rf <file|dir>``.
Raises:
pebble.PathError: If a relative path is provided, or if `recursive` is False
and the file or directory cannot be removed (it does not exist or is not empty).
"""
self._pebble.remove_path(str(path), recursive=recursive)

Expand Down
12 changes: 8 additions & 4 deletions ops/pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -2164,10 +2164,14 @@ def remove_path(self, path: str, *, recursive: bool = False):
Args:
path: Path of the file or directory to delete from the remote system.
recursive: If True, and path is a directory recursively deletes it and
everything under it. If the path is a file, delete the file and
do nothing if the file is non-existent. Behaviourally similar
to ``rm -rf <file|dir>``.
recursive: If True, and path is a directory, recursively delete it and
everything under it. If path is a file, delete the file. In
either case, do nothing if the file or directory does not
exist. Behaviourally similar to ``rm -rf <file|dir>``.
Raises:
pebble.PathError: If a relative path is provided, or if `recursive` is False
and the file or directory cannot be removed (it does not exist or is not empty).
"""
info: Dict[str, Any] = {'path': path}
Expand Down
4 changes: 3 additions & 1 deletion test/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ def on_any_pebble_ready(self, event: ops.PebbleReadyEvent):
self.assertEqual(charm.count, 2)

def test_relations_meta(self):

# language=YAML
self.meta = ops.CharmMeta.from_yaml('''
name: my-charm
Expand All @@ -373,15 +372,18 @@ def test_relations_meta(self):
scope: container
metrics:
interface: prometheus-scraping
optional: true
''')

self.assertEqual(self.meta.requires['database'].interface_name, 'mongodb')
self.assertEqual(self.meta.requires['database'].limit, 1)
self.assertEqual(self.meta.requires['database'].scope, 'container')
self.assertFalse(self.meta.requires['database'].optional)

self.assertEqual(self.meta.requires['metrics'].interface_name, 'prometheus-scraping')
self.assertIsNone(self.meta.requires['metrics'].limit)
self.assertEqual(self.meta.requires['metrics'].scope, 'global') # Default value
self.assertTrue(self.meta.requires['metrics'].optional)

def test_relations_meta_limit_type_validation(self):
with self.assertRaisesRegex(TypeError, "limit should be an int, not <class 'str'>"):
Expand Down

0 comments on commit 74b2e1f

Please sign in to comment.