def test_open_close_port(self): # expose calls open-port with port/protocol parameters ports_set = [] def mock_open_port(*args): for arg in args: ports_set.append(arg) def mock_close_port(*args): if args[0] in ports_set: ports_set.remove(args[0]) # Monkey patch in the open-port mock self._patch_command(mock_open_port) charmhelpers.open_port(80, "TCP") charmhelpers.open_port(90, "UDP") charmhelpers.open_port(100) self.assertTrue("80/TCP" in ports_set) self.assertTrue("90/UDP" in ports_set) self.assertTrue("100/TCP" in ports_set) # Monkey patch in the close-port mock function self._patch_command(mock_close_port) charmhelpers.close_port(80, "TCP") charmhelpers.close_port(90, "UDP") charmhelpers.close_port(100) # ports_set should now be empty self.assertEquals(len(ports_set), 0)
def setup_ports(previous_port, current_port): """Open or close ports based on the supplied ports. The given ports specify the previously provided and the current value. They can be int numbers if the ports are specified, None otherwise, in which case the default ones (80 and 443) are used. """ # If a custom port was previously defined we want to make sure we close it. if previous_port is not None and port_in_range(previous_port): log('Closing user provided port {}.'.format(previous_port)) close_port(previous_port) if current_port is not None: if port_in_range(current_port): # Ensure the default ports are closed when setting the custom one. log('Closing default ports 80 and 443.') close_port(80) close_port(443) # Open the custom defined port. log('Opening user provided port {}.'.format(current_port)) open_port(current_port) return log('Ignoring provided port {}: not in range.'.format(current_port)) log('Opening default ports 80 and 443.') open_port(80) open_port(443)