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

chore: remove compatibility code with older ops #1418

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
9 changes: 1 addition & 8 deletions testing/src/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,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
Loading