github.com/containers/podman/v5@v5.1.0-rc1/test/apiv2/35-networks.at (about)

     1  # -*- sh -*-
     2  #
     3  # network-related tests
     4  #
     5  
     6  t GET networks/non-existing-network 404 \
     7    .cause='network not found'
     8  
     9  t POST libpod/networks/create name='"network1"' 200 \
    10    .name=network1 \
    11    .created~[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}.*
    12  
    13  network1_id=$(jq -r '.id' <<<"$output")
    14  
    15  t POST /v3.4.0/libpod/networks/create name='"bad_version"' 400 \
    16      .cause='given version is not supported'
    17  
    18  # --data '{"name":"network2","subnets":[{"subnet":"10.10.254.0/24"}],"Labels":{"abc":"val"}}'
    19  t POST libpod/networks/create name='"network2"' \
    20    subnets='[{"subnet":"10.10.254.0/24"}]' \
    21    labels='{"abc":"val"}' \
    22    200 \
    23    .name=network2 \
    24    .subnets[0].subnet=10.10.254.0/24 \
    25    .subnets[0].gateway=10.10.254.1 \
    26    .labels.abc=val
    27  
    28  # --data '{"name":"network3","subnets":[{"subnet":"10.10.133.0/24"}],"Labels":{"xyz":"val"}}'
    29  t POST libpod/networks/create name="network3" \
    30    subnets='[{"subnet":"10.10.133.0/24"}]' \
    31    labels='{"xyz":"val"}' \
    32    200 \
    33    .name=network3 \
    34    .subnets[0].subnet=10.10.133.0/24 \
    35    .subnets[0].gateway=10.10.133.1 \
    36    .labels.xyz=val
    37  
    38  # --data '{"name":"network4","subnets":[{"subnet":"10.10.134.0/24"}],"Labels":{"zaq":"val"}}'
    39  t POST libpod/networks/create name="network4" \
    40    subnets='[{"subnet":"10.10.134.0/24"}]' \
    41    labels='{"zaq":"val"}' \
    42    200 \
    43    .name=network4 \
    44    .subnets[0].subnet=10.10.134.0/24 \
    45    .subnets[0].gateway=10.10.134.1 \
    46    .labels.zaq=val
    47  
    48  # test for empty mask
    49  t POST libpod/networks/create subnets='[{"subnet":"10.10.134.0"}]' 500 \
    50    .cause~'.*invalid CIDR address: 10.10.134.0'
    51  # test for invalid mask
    52  t POST libpod/networks/create subnets='[{"subnet":"10.10.134.0/65"}]' 500 \
    53    .cause~'.*invalid CIDR address: 10.10.134.0/65'
    54  
    55  # network list
    56  t GET libpod/networks/json 200
    57  t GET libpod/networks/json?filters='{"name":["network1"]}' 200 \
    58    length=1 \
    59    .[0].name=network1
    60  t GET networks 200
    61  
    62  #inspect network
    63  t GET libpod/networks/network1/json 200 \
    64    .name="network1"
    65  
    66  #network list docker endpoint
    67  t GET networks?filters='{"name":["network1","network2"]}' 200 \
    68    length=2
    69  t GET networks?filters='{"name":["network"]}' 200 \
    70    length=4
    71  t GET networks?filters='{"label":["abc"]}' 200 \
    72    length=1
    73  # old docker filter type see #9526
    74  t GET networks?filters='{"label":{"abc":true}}' 200 \
    75    length=1
    76  t GET networks?filters="{\"id\":[\"$network1_id\"]}" 200 \
    77    length=1 \
    78    .[0].Name=network1 \
    79    .[0].Id=$network1_id
    80  # invalid filter
    81  t GET networks?filters='{"dangling":["true","0"]}' 500 \
    82    .cause="got more than one value for filter key \"dangling\""
    83  # (#9293 with no networks the endpoint should return empty array instead of null)
    84  t GET networks?filters='{"name":["doesnotexists"]}' 200 \
    85    "[]"
    86  
    87  # check default name in list endpoint
    88  t GET networks 200 \
    89    .[].Name~.*bridge.*
    90  
    91  # network inspect docker
    92  t GET networks/$network1_id 200 \
    93    .Name=network1 \
    94    .Id=$network1_id \
    95    .Scope=local
    96  
    97  # inspect default bridge network
    98  t GET networks/bridge 200 \
    99    .Name=bridge
   100  
   101  # inspect default bridge network with real podman name should return real name
   102  t GET networks/podman 200 \
   103    .Name=podman
   104  
   105  # network create docker
   106  t POST networks/create Name=net3\ IPAM='{"Config":[]}' 201
   107  # Since Docker API 1.44, the server always checks for duplicate networks, even
   108  # when the deprecated CheckDuplicate parameter is set
   109  t POST networks/create Name=net3 409
   110  t POST networks/create Name=net3\ CheckDuplicate=false 409
   111  # network delete docker
   112  t DELETE networks/net3 204
   113  
   114  #compat api list networks sanity checks
   115  t GET networks?filters='garb1age}' 500 \
   116      .cause="invalid character 'g' looking for beginning of value"
   117  t GET networks?filters='{"label":["testl' 500 \
   118      .cause="unexpected end of JSON input"
   119  
   120  #libpod api list networks sanity checks
   121  t GET libpod/networks/json?filters='garb1age}' 500 \
   122      .cause="invalid character 'g' looking for beginning of value"
   123  t GET libpod/networks/json?filters='{"label":["testl' 500 \
   124      .cause="unexpected end of JSON input"
   125  
   126  # Prune networks compat api
   127  t POST networks/prune?filters='garb1age}' 500 \
   128      .cause="invalid character 'g' looking for beginning of value"
   129  t POST networks/prune?filters='{"label":["tes' 500 \
   130      .cause="unexpected end of JSON input"
   131  
   132  # Prune networks libpod api
   133  t POST libpod/networks/prune?filters='garb1age}' 500 \
   134      .cause="invalid character 'g' looking for beginning of value"
   135  t POST libpod/networks/prune?filters='{"label":["tes' 500 \
   136      .cause="unexpected end of JSON input"
   137  
   138  # prune networks using filter - compat api
   139  t POST networks/prune?filters='{"label":["xyz"]}' 200
   140  t GET networks?filters='{"label":["xyz"]}' 200 length=0
   141  
   142  # prune networks using filter - libpod api
   143  t POST libpod/networks/prune?filters='{"label":["zaq=val"]}' 200
   144  t GET libpod/networks/json?filters='{"label":["zaq=val"]}' 200 length=0
   145  
   146  # clean the network
   147  t DELETE libpod/networks/network1 200 \
   148    .[0].Name~network1 \
   149    .[0].Err=null
   150  t DELETE libpod/networks/network2 200 \
   151    .[0].Name~network2 \
   152    .[0].Err=null
   153  
   154  # test until filter - libpod api
   155  # create network via cli to test that the server can use it
   156  podman network create --label xyz network5
   157  
   158  # with date way back in the past, network should not be deleted
   159  t POST libpod/networks/prune?filters='{"until":["500000"]}' 200
   160  t GET libpod/networks/json?filters='{"label":["xyz"]}' 200 length=1
   161  
   162  # with date far in the future, network should be deleted
   163  t POST libpod/networks/prune?filters='{"until":["5000000000"]}' 200
   164  t GET libpod/networks/json?filters='{"label":["xyz"]}' 200 length=0
   165  
   166  # test until filter - compat api
   167  t POST networks/create Name='"network6"' Labels='{"zaq":""}' 201 \
   168    .Id~[0-9a-f]\\{64\\}
   169  
   170  # with date way back in the past, network should not be deleted
   171  t POST networks/prune?filters='{"until":["500000"]}' 200
   172  t GET networks?filters='{"label":["zaq"]}' 200 length=1
   173  
   174  # with date far in the future, network should be deleted
   175  t POST networks/prune?filters='{"until":["5000000000"]}' 200
   176  t GET networks?filters='{"label":["zaq"]}' 200 length=0
   177  
   178  # test macvlan network response
   179  # we have to use a static subnet because of netavark does not support dhcp yet
   180  t POST libpod/networks/create name='"macvlan1"' driver="macvlan" subnets='[{"subnet":"10.10.135.0/24"}]' 200 \
   181    .name=macvlan1 \
   182    .driver=macvlan
   183  
   184  # libpod api inspect the macvlan network
   185  t GET libpod/networks/macvlan1/json 200 .name="macvlan1"
   186  
   187  # compat api inspect the macvlan network
   188  t GET networks/macvlan1 200 .Name="macvlan1"
   189  
   190  # clean the macvlan network
   191  t DELETE libpod/networks/macvlan1 200 \
   192    .[0].Name~macvlan1 \
   193    .[0].Err=null
   194  
   195  
   196  # create network with isolate option and make sure it is not shown in docker compat endpoint
   197  podman network create --opt isolate=true isolate-test
   198  # Note the order of both list calls is important to test for https://github.com/containers/podman/issues/22330
   199  # First call the compat endpoint, then the libpod one. Previously this would have removed
   200  # the internal option for the libpod endpoint as well.
   201  t GET networks?filters='{"name":["isolate-test"]}' 200 \
   202    .[0].Name=isolate-test \
   203    .[0].Options="{}"
   204  
   205  t GET libpod/networks/json?filters='{"name":["isolate-test"]}' 200 \
   206    .[0].name=isolate-test \
   207    .[0].options.isolate="true"
   208  
   209  t DELETE libpod/networks/isolate-test 200
   210  
   211  #
   212  # test networks with containers
   213  #
   214  podman pull $IMAGE &>/dev/null
   215  
   216  # Ensure clean slate
   217  podman rm -a -f &>/dev/null
   218  
   219  # create a network
   220  podman network create --subnet 10.10.253.0/24 --gateway 10.10.253.1 network5
   221  t GET libpod/networks/json?filters='{"name":["network5"]}' 200 \
   222    .[0].id~[0-9a-f]\\{64\\}
   223  nid=$(jq -r '.[0].id' <<<"$output")
   224  # create a pod on a network
   225  CNAME=mynettest
   226  podman run --network network5 --name $CNAME --ip 10.10.253.2 --mac-address 0a:01:73:78:43:18 -td $IMAGE top
   227  t GET libpod/containers/json?all=true 200 \
   228    .[0].Id~[0-9a-f]\\{64\\}
   229  cid=$(jq -r '.[0].Id' <<<"$output")
   230  # compat api inspect network
   231  t GET networks/$nid 200 .Name="network5" \
   232    .Containers[\"$cid\"].Name=$CNAME \
   233    .Containers[\"$cid\"].MacAddress=0a:01:73:78:43:18 \
   234    .Containers[\"$cid\"].IPv4Address=10.10.253.2/24
   235  # clean the network
   236  podman network rm -f network5
   237  
   238  #
   239  # Test connecting container to network when connection already exists
   240  #
   241  podman network create netcon
   242  podman create --network bridge --name c1 $IMAGE top
   243  
   244  # connect c1 to the network for the first time.
   245  t POST networks/netcon/connect Container=c1 200 OK
   246  # connect c1 to netcon again should not error
   247  t POST networks/netcon/connect Container=c1 200 OK
   248  
   249  # connect c1 to netcon here should error as the container is running and it is already connected
   250  podman start c1
   251  t POST networks/netcon/connect Container=c1 403 .cause="network is already connected"
   252  
   253  # connect c1 to netcon here should not error as container was stopped
   254  podman stop c1
   255  t POST networks/netcon/connect Container=c1 200 OK
   256  
   257  # cleanup
   258  podman network rm -f netcon
   259  
   260  # vim: filetype=sh