diff --git a/src/charm.py b/src/charm.py index c3ce599b..8b107e16 100755 --- a/src/charm.py +++ b/src/charm.py @@ -5,7 +5,6 @@ import os import json import pwd -import subprocess from charms.mongodb.v1.helpers import copy_licenses_to_unit, KEY_FILE from charms.operator_libs_linux.v1 import snap from pathlib import Path @@ -364,12 +363,7 @@ def _generate_relation_departed_key(rel_id: int) -> str: def open_mongos_port(self) -> None: """Opens the mongos port for TCP connections.""" - try: - logger.debug("opening tcp port") - subprocess.check_call(["open-port", "{}/TCP".format(Config.MONGOS_PORT)]) - except subprocess.CalledProcessError as e: - logger.exception("failed opening port: %s", str(e)) - raise + self.unit.open_port("tcp", Config.MONGOS_PORT) # END: helper functions diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index e0996059..04061e4b 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -16,7 +16,7 @@ from ops.model import BlockedStatus, WaitingStatus from ops.testing import Harness -from charm import MongosOperatorCharm, subprocess +from charm import MongosOperatorCharm from .helpers import patch_network_get @@ -158,21 +158,3 @@ def test_mongos_host(self): self.harness.charm.app_peer_data["external-connectivity"] = json.dumps(True) mongos_host = self.harness.charm.get_mongos_host() self.assertEqual(mongos_host, "1.1.1.1") - - @patch("subprocess.check_call") - def test_set_port(self, _call): - """Test verifies operation of set port.""" - self.harness.charm.open_mongos_port() - - # Make sure the port is opened and the service is started - self.assertEqual(_call.call_args_list, [mock.call(["open-port", "27018/TCP"])]) - - @patch("subprocess.check_call") - def test_set_port_failure(self, _call): - """Test verifies that we raise the correct errors when we fail to open a port.""" - _call.side_effect = subprocess.CalledProcessError( - cmd="open-port 27018/TCP", returncode=1 - ) - - with self.assertRaises(subprocess.CalledProcessError): - self.harness.charm.open_mongos_port()