|
| 1 | +import pytest |
| 2 | +import docker |
| 3 | +from docker.types import IPAMConfig, IPAMPool |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture(scope="module") |
| 7 | +def docker_client() -> None: |
| 8 | + """ |
| 9 | + Initialize and Provide a Docker client instance for the test |
| 10 | +
|
| 11 | + :return: None |
| 12 | + """ |
| 13 | + return docker.from_env() |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(scope="module") |
| 17 | +def network(docker_client) -> None: |
| 18 | + """ |
| 19 | + Create a custom network with a specific subnet |
| 20 | +
|
| 21 | + :param docker_client: docker_client |
| 22 | + :yield: network |
| 23 | +
|
| 24 | + :return: None |
| 25 | + """ |
| 26 | + # Create a custom network |
| 27 | + ipam_pool = IPAMPool(subnet="15.15.15.0/24") |
| 28 | + ipam_config = IPAMConfig(pool_configs=[ipam_pool]) |
| 29 | + network = docker_client.networks.create("test_network", driver="bridge", ipam=ipam_config) |
| 30 | + yield network |
| 31 | + network.remove() |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(scope="module") |
| 35 | +def blank1(docker_client, network) -> None: |
| 36 | + """ |
| 37 | + Create and start the first container with a specific IP |
| 38 | +
|
| 39 | + :param docker_client: docker_client |
| 40 | + :param network: network |
| 41 | + :yield: container |
| 42 | +
|
| 43 | + :return: None |
| 44 | + """ |
| 45 | + # Create and start the first container with a specific IP |
| 46 | + container = docker_client.containers.create( |
| 47 | + "kimham/csle_blank_1:0.6.0", |
| 48 | + command="sh -c 'apt-get update && apt-get install -y iputils-ping && while true; do sleep 3600; done'", |
| 49 | + detach=True, |
| 50 | + ) |
| 51 | + network.connect(container, ipv4_address="15.15.15.10") |
| 52 | + container.start() |
| 53 | + yield container |
| 54 | + container.stop() |
| 55 | + container.remove() |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture(scope="module") |
| 59 | +def other_containers(docker_client, network): |
| 60 | + """ |
| 61 | + Create and start the second container with a specific IP |
| 62 | +
|
| 63 | + :param docker_client: docker_client |
| 64 | + :param network: network |
| 65 | + :yield: container |
| 66 | +
|
| 67 | + :return: None |
| 68 | + """ |
| 69 | + # Create and start the second container with a specific IP |
| 70 | + match_tag = "0.6.0" |
| 71 | + all_images = docker_client.images.list() |
| 72 | + images = [ |
| 73 | + image |
| 74 | + for image in all_images |
| 75 | + if any(match_tag in tag for tag in image.tags) |
| 76 | + and all("base" not in tag for tag in image.tags) |
| 77 | + and all("kimham/csle_blank_ubuntu_22:0.6.0" not in tag for tag in image.tags) |
| 78 | + ] |
| 79 | + containers = [] |
| 80 | + start_ip = 11 |
| 81 | + for image in images: |
| 82 | + container = docker_client.containers.create( |
| 83 | + image.tags[0], |
| 84 | + command="sh -c 'apt-get update && apt-get install -y iputils-ping && while true; do sleep 3600; done'", |
| 85 | + detach=True, |
| 86 | + ) |
| 87 | + network.connect(container, ipv4_address=f"15.15.15.{start_ip}") |
| 88 | + container.start() |
| 89 | + containers.append((container, f"15.15.15.{start_ip}")) |
| 90 | + start_ip += 1 |
| 91 | + yield containers |
| 92 | + for container, _ in containers: |
| 93 | + container.stop() |
| 94 | + container.remove() |
| 95 | + |
| 96 | + |
| 97 | +def test_ping_containers(blank1, other_containers) -> None: |
| 98 | + """ |
| 99 | + Ping container1 from container2 |
| 100 | +
|
| 101 | + :return: None |
| 102 | + """ |
| 103 | + failed_tests = [] |
| 104 | + blank1.start() |
| 105 | + blank1.reload() |
| 106 | + # Check if containers are running |
| 107 | + assert blank1.status == "running", "Container1 is not running" |
| 108 | + # Ping container2 from blank1 |
| 109 | + for container, ip in other_containers: |
| 110 | + container.start() |
| 111 | + container.reload() |
| 112 | + assert container.status == "running", "Container2 is not running" |
| 113 | + try: |
| 114 | + exec_result = blank1.exec_run(f"ping -c 3 {ip}") |
| 115 | + output = exec_result.output.decode("utf-8") |
| 116 | + # Check if the ping was successful |
| 117 | + assert "3 packets transmitted, 3 received" in output, f"Ping failed. Logs: {output}" |
| 118 | + if "3 packets transmitted, 3 received" not in output: |
| 119 | + failed_tests.append(f"Ping to {container.image.tags} from blank1 failed. Logs: {output}") |
| 120 | + except Exception as e: |
| 121 | + failed_tests.append( |
| 122 | + f"Ping to {container.image.tags} from blank1 failed. Container: {container.image.tags}, \ |
| 123 | + IP: {ip}, Error: {str(e)}" |
| 124 | + ) |
| 125 | + if failed_tests: |
| 126 | + for fail in failed_tests: |
| 127 | + print(fail) |
| 128 | + assert False, "Some ping tests failed, see the output above for details." |
0 commit comments