Best Python code snippet using localstack_python
test_docker.py
Source:test_docker.py
...224 network_name = f"test-network-{short_uid()}"225 network_id = create_network(network_name)226 safe_run(["docker", "network", "connect", network_id, dummy_container.container_id])227 docker_client.start_container(dummy_container.container_id)228 result_bridge_network = docker_client.get_container_ipv4_for_network(229 container_name_or_id=dummy_container.container_id, container_network="bridge"230 ).strip()231 assert is_ipv4_address(result_bridge_network)232 bridge_network = docker_client.inspect_network("bridge")["IPAM"]["Config"][0]["Subnet"]233 assert ipaddress.IPv4Address(result_bridge_network) in ipaddress.IPv4Network(bridge_network)234 result_custom_network = docker_client.get_container_ipv4_for_network(235 container_name_or_id=dummy_container.container_id, container_network=network_name236 ).strip()237 assert is_ipv4_address(result_custom_network)238 assert result_custom_network != result_bridge_network239 custom_network = docker_client.inspect_network(network_name)["IPAM"]["Config"][0]["Subnet"]240 assert ipaddress.IPv4Address(result_custom_network) in ipaddress.IPv4Network(custom_network)241 def test_get_container_ip_for_network_wrong_network(242 self, docker_client: ContainerClient, dummy_container, create_network243 ):244 network_name = f"test-network-{short_uid()}"245 create_network(network_name)246 docker_client.start_container(dummy_container.container_id)247 result_bridge_network = docker_client.get_container_ipv4_for_network(248 container_name_or_id=dummy_container.container_id, container_network="bridge"249 ).strip()250 assert is_ipv4_address(result_bridge_network)251 with pytest.raises(ContainerException):252 docker_client.get_container_ipv4_for_network(253 container_name_or_id=dummy_container.container_id, container_network=network_name254 )255 def test_get_container_ip_for_host_network(256 self, docker_client: ContainerClient, create_container257 ):258 container = create_container(259 "alpine", command=["sh", "-c", "while true; do sleep 1; done"], network="host"260 )261 assert "host" == docker_client.get_networks(container.container_name)[0]262 # host network containers have no dedicated IP, so it will throw an exception here263 with pytest.raises(ContainerException):264 docker_client.get_container_ipv4_for_network(265 container_name_or_id=container.container_name, container_network="host"266 )267 def test_get_container_ip_for_network_non_existent_network(268 self, docker_client: ContainerClient, dummy_container, create_network269 ):270 network_name = f"invalid-test-network-{short_uid()}"271 docker_client.start_container(dummy_container.container_id)272 with pytest.raises(NoSuchNetwork):273 docker_client.get_container_ipv4_for_network(274 container_name_or_id=dummy_container.container_id, container_network=network_name275 )276 def test_create_with_host_network(self, docker_client: ContainerClient, create_container):277 info = create_container("alpine", network="host")278 network = docker_client.get_networks(info.container_name)279 assert ["host"] == network280 def test_create_with_port_mapping(self, docker_client: ContainerClient, create_container):281 ports = PortMappings()282 ports.add(45122, 22)283 ports.add(45180, 80)284 create_container("alpine", ports=ports)285 @pytest.mark.skipif(286 condition=in_docker(), reason="cannot test volume mounts from host when in docker"287 )...
container_networking.py
Source:container_networking.py
...39 network = network or get_main_container_network()40 main_container_ip = None41 try:42 if config.is_in_docker:43 main_container_ip = DOCKER_CLIENT.get_container_ipv4_for_network(44 container_name_or_id=container_name,45 container_network=network,46 )47 else:48 # default gateway for the network should be the host49 # (only under Linux - otherwise fall back to DOCKER_HOST_FROM_CONTAINER below)50 if config.is_in_linux:51 main_container_ip = DOCKER_CLIENT.inspect_network(network)["IPAM"]["Config"][0][52 "Gateway"53 ]54 LOG.info("Determined main container target IP: %s", main_container_ip)55 except Exception as e:56 LOG.info('Unable to get IP address of main Docker container "%s": %s', container_name, e)57 # return (1) predefined endpoint host, or (2) main container IP, or (3) Docker host (e.g., bridge IP)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!