Skip to content

Commit

Permalink
chore: remove compatibility code with older ops (canonical#1418)
Browse files Browse the repository at this point in the history
As part of the merge of the `ops-scenario` repo into this one,
`ops-scenario` now requires ops 2.17+. This has the added benefit that
we can get rid of the compatibility code that supported ops 2.15 and ops
2.15.

With the pyright "detect ignores that aren't needed" check enabled
again, some additional ignores can also be removed.

Closes canonical/ops-scenario#192 (rather than
copying it into this repo).
  • Loading branch information
tonyandrewmeyer authored Oct 11, 2024
1 parent a4b1d2e commit 86d71fd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 70 deletions.
9 changes: 1 addition & 8 deletions testing/src/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,8 @@
_Event,
)

# TODO: Remove the line below after the ops compatibility code is removed.
# pyright: reportUnnecessaryTypeIgnoreComment=false

if TYPE_CHECKING: # pragma: no cover
try:
from ops._private.harness import ExecArgs # type: ignore
except ImportError:
from ops.testing import ExecArgs # type: ignore

from ops._private.harness import ExecArgs
from scenario.ops_main_mock import Ops
from scenario.state import (
AnyJson,
Expand Down
47 changes: 16 additions & 31 deletions testing/src/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@
)

from ops import JujuVersion, pebble

# TODO: Remove the line below after the ops compatibility code is removed.
# pyright: reportUnnecessaryTypeIgnoreComment=false
try:
from ops._private.harness import ExecArgs, _TestingPebbleClient # type: ignore
except ImportError:
from ops.testing import ExecArgs, _TestingPebbleClient # type: ignore

from ops._private.harness import ExecArgs, _TestingPebbleClient
from ops.model import CloudSpec as CloudSpec_Ops
from ops.model import ModelError
from ops.model import Port as Port_Ops
Expand Down Expand Up @@ -148,7 +141,7 @@ def opened_ports(self) -> Set[Port_Ops]:

def open_port(
self,
protocol: "_RawPortProtocolLiteral", # pyright: ignore[reportIncompatibleMethodOverride]
protocol: "_RawPortProtocolLiteral",
port: Optional[int] = None,
):
# fixme: the charm will get hit with a StateValidationError
Expand Down Expand Up @@ -218,20 +211,12 @@ def _get_secret(self, id: Optional[str] = None, label: Optional[str] = None):
# in scenario, you can create Secret(id="foo"),
# but ops.Secret will prepend a "secret:" prefix to that ID.
# we allow getting secret by either version.
try:
secrets = [
s
for s in self._state.secrets
if canonicalize_id(s.id, model_uuid=self._state.model.uuid) # type: ignore
== canonicalize_id(id, model_uuid=self._state.model.uuid) # type: ignore
]
except TypeError:
# ops 2.16
secrets = [
s
for s in self._state.secrets
if canonicalize_id(s.id) == canonicalize_id(id) # type: ignore
]
secrets = [
s
for s in self._state.secrets
if canonicalize_id(s.id, model_uuid=self._state.model.uuid)
== canonicalize_id(id, model_uuid=self._state.model.uuid)
]
if not secrets:
raise SecretNotFoundError(id)
return secrets[0]
Expand Down Expand Up @@ -445,7 +430,7 @@ def secret_get(
secret = self._get_secret(id, label)
# If both the id and label are provided, then update the label.
if id is not None and label is not None:
secret._set_label(label) # type: ignore
secret._set_label(label)
juju_version = self._context.juju_version
if not (juju_version == "3.1.7" or juju_version >= "3.3.1"):
# In this medieval Juju chapter,
Expand All @@ -471,7 +456,7 @@ def secret_info_get(
secret = self._get_secret(id, label)
# If both the id and label are provided, then update the label.
if id is not None and label is not None:
secret._set_label(label) # type: ignore
secret._set_label(label)

# only "manage"=write access level can read secret info
self._check_can_manage_secret(secret)
Expand Down Expand Up @@ -860,17 +845,17 @@ def exec(
)

if stdin is None:
proc_stdin = self._transform_exec_handler_output("", encoding) # type: ignore
proc_stdin = self._transform_exec_handler_output("", encoding)
else:
proc_stdin = None
stdin = stdin.read() if hasattr(stdin, "read") else stdin # type: ignore
if stdout is None:
proc_stdout = self._transform_exec_handler_output(handler.stdout, encoding) # type: ignore
proc_stdout = self._transform_exec_handler_output(handler.stdout, encoding)
else:
proc_stdout = None
stdout.write(handler.stdout)
if stderr is None:
proc_stderr = self._transform_exec_handler_output(handler.stderr, encoding) # type: ignore
proc_stderr = self._transform_exec_handler_output(handler.stderr, encoding)
else:
proc_stderr = None
stderr.write(handler.stderr)
Expand Down Expand Up @@ -900,9 +885,9 @@ def exec(
change_id=change_id,
args=args,
return_code=handler.return_code,
stdin=proc_stdin, # type: ignore
stdout=proc_stdout, # type: ignore
stderr=proc_stderr, # type: ignore
stdin=proc_stdin,
stdout=proc_stdout,
stderr=proc_stderr,
),
)

Expand Down
44 changes: 13 additions & 31 deletions testing/src/ops_main_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@

import ops.charm
import ops.framework
import ops.jujucontext
import ops.model
import ops.storage
from ops import CharmBase

# use logger from ops._main so that juju_log will be triggered
try:
from ops._main import CHARM_STATE_FILE, _Dispatcher, _get_event_args # type: ignore
from ops._main import logger as ops_logger # type: ignore
except ImportError:
# Ops 2.16
from ops.main import CHARM_STATE_FILE, _Dispatcher, _get_event_args # type: ignore
from ops.main import logger as ops_logger # type: ignore
from ops._main import CHARM_STATE_FILE, _Dispatcher, _get_event_args
from ops._main import logger as ops_logger
from ops.charm import CharmMeta
from ops.log import setup_root_logging

Expand All @@ -32,8 +28,6 @@
from scenario.state import CharmType, State, _CharmSpec, _Event

# pyright: reportPrivateUsage=false
# TODO: Remove the line below after the ops compatibility code is removed.
# pyright: reportUnnecessaryTypeIgnoreComment=false


# TODO: Use ops.jujucontext's _JujuContext.charm_dir.
Expand Down Expand Up @@ -88,17 +82,11 @@ def _emit_charm_event(
f"invalid event (not on charm.on).",
)

try:
args, kwargs = _get_event_args(charm, event_to_emit) # type: ignore
except TypeError:
# ops 2.16+
import ops.jujucontext # type: ignore

args, kwargs = _get_event_args(
charm,
event_to_emit,
ops.jujucontext._JujuContext.from_dict(os.environ), # type: ignore
)
args, kwargs = _get_event_args(
charm,
event_to_emit,
ops.jujucontext._JujuContext.from_dict(os.environ),
)
ops_logger.debug("Emitting Juju event %s.", event_name)
event_to_emit.emit(*args, **kwargs)

Expand Down Expand Up @@ -126,7 +114,7 @@ def setup_framework(
ops_logger.debug(
"Operator Framework %s up and running.",
ops.__version__,
) # type:ignore
)

metadata = (charm_dir / "metadata.yaml").read_text()
actions_meta = charm_dir / "actions.yaml"
Expand Down Expand Up @@ -189,16 +177,10 @@ def setup(
charm_class = charm_spec.charm_type
charm_dir = _get_charm_dir()

try:
dispatcher = _Dispatcher(charm_dir) # type: ignore
except TypeError:
# ops 2.16+
import ops.jujucontext # type: ignore

dispatcher = _Dispatcher(
charm_dir,
ops.jujucontext._JujuContext.from_dict(os.environ), # type: ignore
)
dispatcher = _Dispatcher(
charm_dir,
ops.jujucontext._JujuContext.from_dict(os.environ),
)
dispatcher.run_any_legacy_hook()

framework = setup_framework(charm_dir, state, event, context, charm_spec)
Expand Down

0 comments on commit 86d71fd

Please sign in to comment.