Skip to content

Commit

Permalink
Remove duplicated validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer committed Sep 22, 2023
1 parent 743a8fb commit 685daf3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 22 deletions.
14 changes: 2 additions & 12 deletions ops/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,12 +618,7 @@ def open_port(self, protocol: typing.Literal['tcp', 'udp', 'icmp'],
or ``port`` is not provided when ``protocol`` is 'tcp' or
'udp'.
"""
normalised_protocol : str = protocol.lower()
if normalised_protocol == 'icmp' and port is not None:
raise ModelError("icmp cannot have a port number specified")
elif normalised_protocol in ('tcp', 'udp') and port is None:
raise ModelError(f"{normalised_protocol} must have a port number specified")
self._backend.open_port(normalised_protocol, port)
self._backend.open_port(protocol.lower(), port)

def close_port(self, protocol: typing.Literal['tcp', 'udp', 'icmp'],
port: Optional[int] = None) -> None:
Expand Down Expand Up @@ -651,12 +646,7 @@ def close_port(self, protocol: typing.Literal['tcp', 'udp', 'icmp'],
or ``port`` is not provided when ``protocol`` is 'tcp' or
'udp'.
"""
normalised_protocol : str = protocol.lower()
if normalised_protocol == 'icmp' and port is not None:
raise ModelError("icmp cannot have a port number specified")
elif normalised_protocol in ('tcp', 'udp') and port is None:
raise ModelError(f"{normalised_protocol} must have a port number specified")
self._backend.close_port(normalised_protocol, port)
self._backend.close_port(protocol.lower(), port)

def opened_ports(self) -> Set['Port']:
"""Return a list of opened ports for this unit."""
Expand Down
10 changes: 0 additions & 10 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3337,11 +3337,6 @@ def test_open_port_error(self):
['open-port', '8080/ftp'],
])

with self.assertRaises(ops.ModelError) as cm:
self.unit.open_port('icmp', 8000)
with self.assertRaises(ops.ModelError) as cm:
self.unit.open_port('udp')

def test_close_port(self):
fake_script(self, 'close-port', 'exit 0')

Expand All @@ -3366,11 +3361,6 @@ def test_close_port_error(self):
['close-port', '8080/ftp'],
])

with self.assertRaises(ops.ModelError) as cm:
self.unit.close_port('icmp', 8000)
with self.assertRaises(ops.ModelError) as cm:
self.unit.close_port('udp')

def test_opened_ports(self):
fake_script(self, 'opened-ports', """echo 8080/tcp; echo icmp""")

Expand Down

0 comments on commit 685daf3

Please sign in to comment.