Skip to content

Commit

Permalink
Add bats test for host network driver
Browse files Browse the repository at this point in the history
Bats test asserts on both scenarios:

- Binding a process port to 0.0.0.0
- Binding a process port to 127.0.0.1

Signed-off-by: Nino Kodabande <[email protected]>
  • Loading branch information
Nino-K committed Nov 18, 2024
1 parent 4e6ba48 commit 665adb2
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions bats/tests/containers/host-network-ports.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
load '../helpers/load'

LOCALHOST="127.0.0.1"

@test 'factory reset' {
factory_reset
}

@test 'start container engine' {
start_container_engine
wait_for_container_engine
}

run_container_with_host_network_driver() {
local image="python:slim"
ctrctl pull "$image"
ctrctl run -d --network=host --restart=no "$image" "$@"
}

verify_container_port() {
run try --max 9 --delay 10 curl --insecure --verbose --show-error "$@"
assert_success
assert_output --partial 'Directory listing for'
}

@test 'process is bound to 0.0.0.0 using host network driver' {
local container_port="8010"
run_container_with_host_network_driver python -m http.server "$container_port"
verify_container_port "http://$LOCALHOST:$container_port"
skip_unless_host_ip
verify_container_port "http://${HOST_IP}:$container_port"
}

@test 'process is bound to 127.0.0.1 using host network driver' {
local container_port="8016"
run_container_with_host_network_driver python -m http.server $container_port --bind "$LOCALHOST"
verify_container_port "http://$LOCALHOST:$container_port"
skip_unless_host_ip
run curl --verbose --head "http://${HOST_IP}:$container_port"
assert_output --partial "curl: (7) Failed to connect"
}

0 comments on commit 665adb2

Please sign in to comment.