Skip to content

Commit

Permalink
test,helpers: refactor create_config to accept named args
Browse files Browse the repository at this point in the history
Signed-off-by: Aditya R <[email protected]>
  • Loading branch information
flouthoc committed Oct 20, 2022
1 parent 2a084ab commit f6df229
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 57 deletions.
17 changes: 9 additions & 8 deletions test/100-basic-name-resolution.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ load helpers
setup_slirp4netns

subnet_a=$(random_subnet 5)
create_config "podman1" $(random_string 64) "aone" "$subnet_a" "10.10.10.10" "a1" "1a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a" custom_dns_server="10.10.10.10" aliases='"a1", "1a"'
config_a1=$config
ip_a1=$(echo "$config_a1" | jq -r .networks.podman1.static_ips[0])
gw=$(echo "$config_a1" | jq -r .network_info.podman1.subnets[0].gateway)
Expand All @@ -34,7 +34,8 @@ load helpers
setup_slirp4netns

subnet_a=$(random_subnet 5)
create_config "podman1" $(random_string 64) "aone" "$subnet_a" "8.8.8.8" "a1" "1a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a" custom_dns_server="8.8.8.8" aliases='"a1", "1a"'

config_a1=$config
ip_a1=$(echo "$config_a1" | jq -r .networks.podman1.static_ips[0])
gw=$(echo "$config_a1" | jq -r .network_info.podman1.subnets[0].gateway)
Expand All @@ -58,7 +59,7 @@ load helpers
setup_slirp4netns

subnet_a=$(random_subnet 5)
create_config "podman1" $(random_string 64) "aone" "$subnet_a" "" "a1" "1a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a" aliases='"a1", "1a"'
config_a1=$config
ip_a1=$(echo "$config_a1" | jq -r .networks.podman1.static_ips[0])
gw=$(echo "$config_a1" | jq -r .network_info.podman1.subnets[0].gateway)
Expand All @@ -82,7 +83,7 @@ load helpers
setup_slirp4netns

subnet_a=$(random_subnet 5)
create_config "podman1" $(random_string 64) "aone" "$subnet_a" "" "a1" "1a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a" aliases='"a1", "1a"'
config_a1=$config
ip_a1=$(echo "$config_a1" | jq -r .networks.podman1.static_ips[0])
gw=$(echo "$config_a1" | jq -r .network_info.podman1.subnets[0].gateway)
Expand All @@ -96,7 +97,7 @@ load helpers
setup_slirp4netns

subnet_a=$(random_subnet 6)
create_config "podman1" $(random_string 64) "aone" "$subnet_a" "" "a1" "1a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a" aliases='"a1", "1a"'
config_a1=$config
ip_a1=$(echo "$config_a1" | jq -r .networks.podman1.static_ips[0])
gw=$(echo "$config_a1" | jq -r .network_info.podman1.subnets[0].gateway)
Expand All @@ -123,7 +124,7 @@ load helpers
@test "basic container - dns itself with long network name" {
subnet_a=$(random_subnet 5)
long_name="podman11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
create_config "$long_name" $(random_string 64) "aone" "$subnet_a" "" "a1" "1a"
create_config network_name="$long_name" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a" aliases='"a1", "1a"'
config_a1=$config
ip_a1=$(echo "$config_a1" | jq -r .networks.$long_name.static_ips[0])
gw=$(echo "$config_a1" | jq -r .network_info.$long_name.subnets[0].gateway)
Expand All @@ -139,15 +140,15 @@ load helpers
@test "two containers on the same network" {
# container a1
subnet_a=$(random_subnet 5)
create_config "podman1" $(random_string 64) "aone" "$subnet_a" "" "a1" "1a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a" aliases='"a1", "1a"'
config_a1="$config"
a1_ip=$(echo "$config_a1" | jq -r .networks.podman1.static_ips[0])
gw=$(echo "$config_a1" | jq -r .network_info.podman1.subnets[0].gateway)
create_container "$config_a1"
a1_pid=$CONTAINER_NS_PID

# container a2
create_config "podman1" $(random_string 64) "atwo" "$subnet_a" "" "a2" "2a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="atwo" subnet="$subnet_a" aliases='"a2", "2a"'
config_a2="$config"
a2_ip=$(echo "$config_a2" | jq -r .networks.podman1.static_ips[0])
create_container "$config_a2"
Expand Down
10 changes: 5 additions & 5 deletions test/200-two-networks.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ load helpers

# container a1 on subnet a
subnet_a=$(random_subnet 5)
create_config "podman1" $(random_string 64) "aone" "$subnet_a" ""
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a"
a1_config="$config"
a1_ip=$(echo "$a1_config" | jq -r .networks.podman1.static_ips[0])
a_gw=$(echo "$a1_config" | jq -r .network_info.podman1.subnets[0].gateway)
Expand All @@ -19,7 +19,7 @@ load helpers

# container b1 on subnet b
subnet_b=$(random_subnet 5)
create_config "podman2" $(random_string 64) "bone" "$subnet_b" ""
create_config network_name="podman2" container_id=$(random_string 64) container_name="bone" subnet="$subnet_b"
b1_config="$config"
b1_ip=$(echo "$b1_config" | jq -r .networks.podman2.static_ips[0])
b_gw=$(echo "$b1_config" | jq -r .network_info.podman2.subnets[0].gateway)
Expand Down Expand Up @@ -56,7 +56,7 @@ load helpers
subnet_b=$(random_subnet 5)

# A1
create_config "podman1" $(random_string 64) "aone" "$subnet_a" ""
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a"
a1_config=$config
a1_container_id=$(echo "$a1_config" | jq -r .container_id)
a1_ip=$(echo "$a1_config" | jq -r .networks.podman1.static_ips[0])
Expand All @@ -66,7 +66,7 @@ load helpers
a1_pid=$CONTAINER_NS_PID

# container b1 on subnet b
create_config "podman2" $(random_string 64) "bone" "$subnet_b" ""
create_config network_name="podman2" container_id=$(random_string 64) container_name="bone" subnet="$subnet_b"
b1_config=$config
b1_ip=$(echo "$b1_config" | jq -r .networks.podman2.static_ips[0])
b_gw=$(echo "$b1_config" | jq -r .network_info.podman2.subnets[0].gateway)
Expand All @@ -76,7 +76,7 @@ load helpers
b_subnets=$(echo $b1_config | jq -r .network_info.podman2.subnets[0])

# AB2
create_config "podman1" $(random_string 64) "abtwo" "$subnet_a" ""
create_config network_name="podman1" container_id=$(random_string 64) container_name="abtwo" subnet="$subnet_a"
a2_config=$config
a2_ip=$(echo "$a2_config" | jq -r .networks.podman1.static_ips[0])

Expand Down
22 changes: 11 additions & 11 deletions test/300-three-networks.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ load helpers
subnet_b=$(random_subnet 5)

# A1
create_config "podman1" $(random_string 64) "aone" "$subnet_a" ""
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a"
a1_config=$config
a1_container_id=$(echo "$a1_config" | jq -r .container_id)
a1_ip=$(echo "$a1_config" | jq -r .networks.podman1.static_ips[0])
Expand All @@ -20,7 +20,7 @@ load helpers
a1_pid=$CONTAINER_NS_PID

# container b1 on subnet b
create_config "podman2" $(random_string 64) "bone" "$subnet_b" ""
create_config network_name="podman2" container_id=$(random_string 64) container_name="bone" subnet="$subnet_b"
b1_config=$config
b1_ip=$(echo "$b1_config" | jq -r .networks.podman2.static_ips[0])
b_gw=$(echo "$b1_config" | jq -r .network_info.podman2.subnets[0].gateway)
Expand All @@ -30,7 +30,7 @@ load helpers
b_subnets=$(echo $b1_config | jq -r .network_info.podman2.subnets[0])

# AB2
create_config "podman1" $(random_string 64) "abtwo" "$subnet_a" ""
create_config network_name="podman1" container_id=$(random_string 64) container_name="abtwo" subnet="$subnet_a"
a2_config=$config
a2_ip=$(echo "$a2_config" | jq -r .networks.podman1.static_ips[0])

Expand Down Expand Up @@ -83,7 +83,7 @@ load helpers
subnet_c=$(random_subnet 5)

# A1 on subnet A
create_config "podman1" $(random_string 64) "aone" "$subnet_a" ""
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a"
a1_config=$config
a1_container_id=$(echo "$a1_config" | jq -r .container_id)
a1_ip=$(echo "$a1_config" | jq -r .networks.podman1.static_ips[0])
Expand All @@ -93,7 +93,7 @@ load helpers
a1_pid=$CONTAINER_NS_PID

# C1 on subnet C
create_config "podman3" $(random_string 64) "cone" "$subnet_c" ""
create_config network_name="podman3" container_id=$(random_string 64) container_name="cone" subnet="$subnet_c"
c1_config=$config
c1_container_id=$(echo "$c1_config" | jq -r .container_id)
c1_ip=$(echo "$c1_config" | jq -r .networks.podman3.static_ips[0])
Expand All @@ -112,7 +112,7 @@ load helpers
# the order should be OK.

# Create B1 config for network connect
create_config "podman2" $(random_string 64) "aone" "$subnet_b" "" "aone_nw"
create_config network_name="podman2" container_id=$(random_string 64) container_name="aone" subnet="$subnet_b" aliases='"aone_nw"'
b1_config=$config
# The container ID should be the same
b1_config=$(jq ".container_id |= \"$a1_container_id\"" <<<"$b1_config")
Expand All @@ -128,7 +128,7 @@ load helpers

# Create B2 config for network connect
#
create_config "podman2" $(random_string 64) "cone" "$subnet_b" "" "cone_nw"
create_config network_name="podman2" container_id=$(random_string 64) container_name="cone" subnet="$subnet_b" aliases='"cone_nw"'
b2_config=$config
# The container ID should be the same
b2_config=$(jq ".container_id |= \"$c1_container_id\"" <<<"$b2_config")
Expand Down Expand Up @@ -183,7 +183,7 @@ load helpers
subnet_c=$(random_subnet 6)

# A1 on subnet A
create_config "podman1" $(random_string 64) "aone" "$subnet_a" ""
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a"
a1_config=$config
a1_container_id=$(echo "$a1_config" | jq -r .container_id)
a1_ip=$(echo "$a1_config" | jq -r .networks.podman1.static_ips[0])
Expand All @@ -193,7 +193,7 @@ load helpers
a1_pid=$CONTAINER_NS_PID

# C1 on subnet C
create_config "podman3" $(random_string 64) "cone" "$subnet_c" ""
create_config network_name="podman3" container_id=$(random_string 64) container_name="cone" subnet="$subnet_c"
c1_config=$config
c1_container_id=$(echo "$c1_config" | jq -r .container_id)
c1_ip=$(echo "$c1_config" | jq -r .networks.podman3.static_ips[0])
Expand All @@ -207,7 +207,7 @@ load helpers
# a network connect on both to B.

# Create B1 config for network connect
create_config "podman2" $(random_string 64) "aone" "$subnet_b" "" "aone_nw"
create_config network_name="podman2" container_id=$(random_string 64) container_name="aone" subnet="$subnet_b" aliases='"aone_nw"'
b1_config=$config
# The container ID should be the same
b1_config=$(jq ".container_id |= \"$a1_container_id\"" <<<"$b1_config")
Expand All @@ -223,7 +223,7 @@ load helpers

# Create B2 config for network connect
#
create_config "podman2" $(random_string 64) "cone" "$subnet_b" "" "cone_nw"
create_config network_name="podman2" container_id=$(random_string 64) container_name="cone" subnet="$subnet_b" aliases='"cone_nw"'
b2_config=$config
# The container ID should be the same
b2_config=$(jq ".container_id |= \"$c1_container_id\"" <<<"$b2_config")
Expand Down
4 changes: 2 additions & 2 deletions test/400-aliases.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ load helpers
@test "two containers on the same network with aliases" {
# container a1
subnet_a=$(random_subnet 5)
create_config "podman1" $(random_string 64) "aone" "$subnet_a" "" "a1" "1a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a" aliases='"a1", "1a"'
config_a1="$config"
a1_ip=$(echo "$config_a1" | jq -r .networks.podman1.static_ips[0])
gw=$(echo "$config_a1" | jq -r .network_info.podman1.subnets[0].gateway)
create_container "$config_a1"
a1_pid=$CONTAINER_NS_PID

# container a2
create_config "podman1" $(random_string 64) "atwo" "$subnet_a" "" "a2" "2a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="atwo" subnet="$subnet_a" aliases='"a2", "2a"'
config_a2="$config"
a2_ip=$(echo "$config_a2" | jq -r .networks.podman1.static_ips[0])
create_container "$config_a2"
Expand Down
8 changes: 4 additions & 4 deletions test/500-reverse-lookups.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ load helpers
@test "check reverse lookups" {
# container a1
subnet_a=$(random_subnet 5)
create_config "podman1" $(random_string 64) "aone" "$subnet_a" "" "a1" "1a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a" aliases='"a1", "1a"'
a1_config="$config"
a1_ip=$(echo "$a1_config" | jq -r .networks.podman1.static_ips[0])
gw=$(echo "$a1_config" | jq -r .network_info.podman1.subnets[0].gateway)
create_container "$a1_config"
a1_pid=$CONTAINER_NS_PID

# container a2
create_config "podman1" $(random_string 64) "atwo" "$subnet_a" "" "a2" "2a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="atwo" subnet="$subnet_a" aliases='"a2", "2a"'
a2_config="$config"
a2_ip=$(echo "$a2_config" | jq -r .networks.podman1.static_ips[0])
create_container "$a2_config"
Expand All @@ -39,15 +39,15 @@ load helpers
@test "check reverse lookups on ipaddress v6" {
# container a1
subnet_a=$(random_subnet 6)
create_config "podman1" $(random_string 64) "aone" "$subnet_a" "" "a1" "1a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="aone" subnet="$subnet_a" aliases='"a1", "1a"'
a1_config="$config"
a1_ip=$(echo "$a1_config" | jq -r .networks.podman1.static_ips[0])
gw=$(echo "$a1_config" | jq -r .network_info.podman1.subnets[0].gateway)
create_container "$a1_config"
a1_pid=$CONTAINER_NS_PID

# container a2
create_config "podman1" $(random_string 64) "atwo" "$subnet_a" "" "a2" "2a"
create_config network_name="podman1" container_id=$(random_string 64) container_name="atwo" subnet="$subnet_a" aliases='"a2", "2a"'
a2_config="$config"
a2_ip=$(echo "$a2_config" | jq -r .networks.podman1.static_ips[0])
create_container "$a2_config"
Expand Down
67 changes: 40 additions & 27 deletions test/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -351,38 +351,51 @@ function run_in_host_netns() {
# create_config# Creates a config netavark can use
################
#
# first arg is the network name
# second arg is container_id
# third is container name
# fourth is subnet
# fifth is custom dns server for container (empty will not be used)
# sixth and greater are aliases
# The following arguments are supported, the order does not matter:
# network_name=$network_name
# container_id=$container_id
# container_name=$container_name
# subnet=$subnet specifies the network subnet
# custom_dns_serve=$custom_dns_server
# aliases=$aliases comma seperated container aliases for dns resolution.
function create_config() {
local network_name=$1
shift
local container_id=$1
shift
local container_name=$1
shift

local subnets=""
local subnet=$1
shift
local network_name=""
local container_id=""
local container_name=""
local subnet=""
local custom_dns_server
#local dns_server=$1
if [ -n "$1" ]; then
custom_dns_server=\"$1\"
fi
shift
local aliases=""

# parse arguments
while [[ "$#" -gt 0 ]]; do
IFS='=' read -r arg value <<<"$1"
case "$arg" in
network_name)
network_name="$value"
;;
container_id)
container_id="$value"
;;
container_name)
container_name="$value"
;;
subnet)
subnet="$value"
;;
custom_dns_server)
custom_dns_server=\"$value\"
;;
aliases)
aliases="$value"
;;
*) die "unknown argument for '$arg' create_config" ;;
esac
shift
done

container_ip=$(random_ip_in_subnet $subnet)
container_gw=$(gateway_from_subnet $subnet)
subnets="{\"subnet\":\"$subnet\",\"gateway\":\"$container_gw\"}"
aliases=""
comma=
for i; do
aliases+="$comma \"$i\""
comma=,
done

create_network "$network_name" "$container_ip" "eth0" "$aliases"
create_network_infos "$network_name" $(random_string 64) "$subnets"
Expand Down

0 comments on commit f6df229

Please sign in to comment.