Skip to content

Commit

Permalink
Merge branch 'main' into ops-class
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer authored Mar 22, 2024
2 parents 0007bfa + 9ebc3f9 commit 1156018
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 57 deletions.
11 changes: 0 additions & 11 deletions ops/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
Mapping,
MutableMapping,
Optional,
Sequence,
Set,
TextIO,
Tuple,
Expand Down Expand Up @@ -880,8 +879,6 @@ def _get_unique(self, relation_name: str, relation_id: Optional[int] = None):
self._our_unit, self._backend, self._cache, active=False)
relations = self[relation_name]
num_related = len(relations)
self._backend._validate_relation_access(
relation_name, relations)
if num_related == 0:
return None
elif num_related == 1:
Expand Down Expand Up @@ -3043,14 +3040,6 @@ def _run(self, *args: str, return_output: bool = False,
def _is_relation_not_found(model_error: Exception) -> bool:
return 'relation not found' in str(model_error)

def _validate_relation_access(self, relation_name: str, relations: Sequence['Relation']):
"""Checks for relation usage inconsistent with the framework/backend state.
This is used for catching Harness configuration errors and the production implementation
here should remain empty.
"""
pass

def relation_ids(self, relation_name: str) -> List[int]:
relation_ids = self._run('relation-ids', relation_name, return_output=True, use_json=True)
relation_ids = typing.cast(Iterable[str], relation_ids)
Expand Down
16 changes: 0 additions & 16 deletions ops/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2171,22 +2171,6 @@ def __init__(self, unit_name: str, meta: charm.CharmMeta, config: '_RawConfig'):
self._running_action: Optional[_RunningAction] = None
self._cloud_spec: Optional[model.CloudSpec] = None

def _validate_relation_access(self, relation_name: str, relations: List[model.Relation]):
"""Ensures that the named relation exists/has been added.
This is called whenever relation data is accessed via model.get_relation(...).
"""
if len(relations) > 0:
return

valid_relation_endpoints: List[str] = list(self._meta.peers.keys())
valid_relation_endpoints.extend(self._meta.requires.keys())
valid_relation_endpoints.extend(self._meta.provides.keys())
if self._hook_is_running == 'leader_elected' and relation_name in valid_relation_endpoints:
raise RuntimeError(
'cannot access relation data without first adding the relation: '
f'use Harness.add_relation({relation_name!r}, <app>) before calling set_leader')

def _can_connect(self, pebble_client: '_TestingPebbleClient') -> bool:
"""Returns whether the mock client is active and can support API calls with no errors."""
return self._pebble_clients_can_connect[pebble_client]
Expand Down
30 changes: 0 additions & 30 deletions test/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,6 @@
is_linux = platform.system() == 'Linux'


class SetLeaderErrorTester(ops.CharmBase):
"""Sets peer relation data inside leader-elected."""

def __init__(self, framework: ops.Framework):
super().__init__(framework)
self._peer_name = 'peer'
self.framework.observe(self.on.leader_elected,
self._on_leader_elected)

def _on_leader_elected(self, event: ops.EventBase):
peers = self.model.get_relation(self._peer_name)
assert peers is not None
peers.data[self.app]["foo"] = "bar"


class StorageTester(ops.CharmBase):
"""Record the relation-changed events."""

Expand Down Expand Up @@ -893,21 +878,6 @@ def test_update_peer_relation_no_local_unit_change_event(self):
self.assertEqual(rel.data[harness.charm.model.unit]['key'], 'v4')
self.assertEqual([], helper.changes)

def test_harness_leader_misconfig(self):
# language=YAML
harness = ops.testing.Harness(SetLeaderErrorTester, meta='''
name: postgresql
peers:
peer:
interface: foo
''')
self.addCleanup(harness.cleanup)
harness.begin()

with self.assertRaises(RuntimeError) as cm:
harness.set_leader(is_leader=True)
self.assertTrue(cm.exception.args[0].find('use Harness.add_relation') != -1)

def test_update_peer_relation_app_data(self):
# language=YAML
harness = ops.testing.Harness(ops.CharmBase, meta='''
Expand Down

0 comments on commit 1156018

Please # to comment.