github.com/containers/podman/v5@v5.1.0-rc1/test/system/320-system-df.bats (about)

     1  #!/usr/bin/env bats   -*- bats -*-
     2  #
     3  # tests for podman system df
     4  #
     5  
     6  load helpers
     7  
     8  function setup() {
     9      # Depending on which tests have been run prior to getting here, there
    10      # may be one or two images loaded. We want only '$IMAGE', not the
    11      # systemd one.
    12      run_podman rmi -f $SYSTEMD_IMAGE
    13  
    14      basic_setup
    15  }
    16  
    17  function teardown() {
    18      basic_teardown
    19  
    20      # In case the active-volumes test failed: clean up stray volumes
    21      run_podman volume rm -a
    22  }
    23  
    24  @test "podman system df - basic functionality" {
    25      run_podman system df
    26      is "$output" ".*Images  *1 *0 "       "Exactly one image"
    27      is "$output" ".*Containers *0 *0 "    "No containers"
    28      is "$output" ".*Local Volumes *0 *0 " "No volumes"
    29  }
    30  
    31  @test "podman system df --format {{ json . }} functionality" {
    32      run_podman system df --format '{{json .}}'
    33      is "$output" '.*"TotalCount":1'       "Exactly one image"
    34      is "$output" '.*"RawSize".*"Size"' "RawSize and Size reported"
    35      is "$output" '.*"RawReclaimable".*"Reclaimable"' "RawReclaimable and Reclaimable reported"
    36      is "$output" '.*"Containers".*"Total":0' "Total containers reported"
    37      is "$output" '.*"Local Volumes".*"Size":"0B"' "Total containers reported"
    38      is "$output" '.*"Local Volumes".*"Size":"0B"' "Total containers reported"
    39  }
    40  
    41  @test "podman system df --format json functionality" {
    42      # Run two dummy containers, one which exits, one which stays running
    43      run_podman run    --name stoppedcontainer $IMAGE true
    44      run_podman run -d --name runningcontainer $IMAGE top
    45      run_podman system df --format json
    46      local results="$output"
    47  
    48      # FIXME! This needs to be fiddled with every time we bump testimage.
    49      local size=11
    50      if [[ "$(uname -m)" = "aarch64" ]]; then
    51          size=13
    52      fi
    53  
    54      # FIXME: we can't check exact RawSize or Size because every CI system
    55      # computes a different value: 12701526, 12702113, 12706209... and
    56      # those are all amd64. aarch64 gets 12020148, 12019561.
    57      #
    58      # WARNING: RawSize and Size tests may fail if $IMAGE is updated. Since
    59      # that tends to be done yearly or less, and only by Ed, that's OK.
    60      local tests="
    61  Type           | Images         | Containers | Local Volumes
    62  Total          |              1 |          2 |             0
    63  Active         |              1 |          1 |             0
    64  RawSize        | ~${size}...... |         !0 |             0
    65  RawReclaimable |              0 |         !0 |             0
    66  Reclaimable    |        ~\(0%\) |   ~\(50%\) |       ~\(0%\)
    67  TotalCount     |              1 |          2 |             0
    68  Size           |   ~${size}.*MB |        !0B |            0B
    69  "
    70      while read -a fields; do
    71          for i in 0 1 2;do
    72              expect="${fields[$((i+1))]}"
    73              actual=$(jq -r ".[$i].${fields[0]}" <<<"$results")
    74  
    75              # Do exact-match check, unless the expect term starts with ~ or !
    76              op='='
    77              if [[ "$expect" =~ ^\! ]]; then
    78                  op='!='
    79                  expect=${expect##\!}
    80              fi
    81              if [[ "$expect" =~ ^~ ]]; then
    82                  op='=~'
    83                  expect=${expect##\~}
    84              fi
    85  
    86              assert "$actual" "$op" "$expect" "system df[$i].${fields[0]}"
    87          done
    88      done < <(parse_table "$tests")
    89  
    90      # Clean up
    91      run_podman rm -f -t 0 stoppedcontainer runningcontainer
    92  }
    93  
    94  @test "podman system df - with active containers and volumes" {
    95      run_podman run    -v /myvol1 --name c1 $IMAGE true
    96      run_podman run -d -v /myvol2 --name c2 $IMAGE top
    97  
    98      run_podman system df --format '{{ .Type }}:{{ .Total }}:{{ .Active }}'
    99      is "${lines[0]}" "Images:1:1"        "system df : Images line"
   100      is "${lines[1]}" "Containers:2:1"    "system df : Containers line"
   101      is "${lines[2]}" "Local Volumes:2:2" "system df : Volumes line"
   102  
   103      # Try -v. (Grrr. No way to specify individual formats)
   104      #
   105      # Yes, I know this would be more elegant as a separate @test, but
   106      # container/volume setup/teardown costs ~3 seconds and that matters.
   107      run_podman system df -v
   108      is "${lines[2]}" \
   109         "${PODMAN_TEST_IMAGE_REGISTRY}/${PODMAN_TEST_IMAGE_USER}/${PODMAN_TEST_IMAGE_NAME} * ${PODMAN_TEST_IMAGE_TAG} [0-9a-f]* .* 2" \
   110         "system df -v: the 'Images' line"
   111  
   112      # Containers are listed in random order. Just check that each has 1 volume
   113      is "${lines[5]}" \
   114         "[0-9a-f]\{12\} *[0-9a-f]\{12\} .* 1 .* c[12]" \
   115         "system df -v, 'Containers', first line"
   116      is "${lines[6]}" \
   117         "[0-9a-f]\{12\} *[0-9a-f]\{12\} .* 1 .* c[12]" \
   118         "system df -v, 'Containers', second line"
   119  
   120      # Volumes, likewise: random order.
   121      is "${lines[9]}" "[0-9a-f]\{64\} *[01] * 0B" \
   122         "system df -v, 'Volumes', first line"
   123      is "${lines[10]}" "[0-9a-f]\{64\} *[01] * 0B" \
   124         "system df -v, 'Volumes', second line"
   125  
   126      # Make sure that the return image "raw" size is correct
   127      run_podman image inspect $IMAGE --format "{{.Size}}"
   128      expectedSize="$output"
   129  
   130      run_podman system df --format "{{.RawSize}}"
   131      is "${lines[0]}" "$expectedSize" "raw image size is correct"
   132  
   133      # Clean up and check reclaimable image data
   134      run_podman system df --format '{{.Reclaimable}}'
   135      is "${lines[0]}" "0B (0%)" "cannot reclaim image data as it's still used by the containers"
   136  
   137      run_podman stop c2
   138  
   139      # Create a second image by committing a container.
   140      run_podman container commit -q c1
   141      image="$output"
   142  
   143      run_podman system df --format '{{.Reclaimable}}'
   144      is "${lines[0]}" ".* (100%)" "100 percent of image data is reclaimable because $IMAGE has unique size of 0"
   145  
   146      # Make sure the unique size is now really 0.  We cannot use --format for
   147      # that unfortunately but we can exploit the fact that $IMAGE is used by
   148      # two containers.
   149      run_podman system df -v
   150      is "$output" ".*0B\\s\\+2.*"
   151  
   152      run_podman rm c1 c2
   153  
   154      run_podman system df --format '{{.Reclaimable}}'
   155      is "${lines[0]}" ".* (100%)" "100 percent of image data is reclaimable because all containers are gone"
   156  
   157      run_podman rmi $image
   158      run_podman volume rm -a
   159  }
   160  
   161  # vim: filetype=sh