github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/test/system/065-cp.bats (about)

     1  #!/usr/bin/env bats   -*- bats -*-
     2  #
     3  # Tests for 'podman cp'
     4  #
     5  # ASSUMPTION FOR ALL THESE TESTS: /tmp in the container starts off empty
     6  #
     7  
     8  load helpers
     9  
    10  # Create two random-name random-content files in /tmp in the container
    11  # podman-cp them into the host using '/tmp/*', i.e. asking podman to
    12  # perform wildcard expansion in the container. We should get both
    13  # files copied into the host.
    14  @test "podman cp * - wildcard copy multiple files from container to host" {
    15      skip_if_remote "podman-remote does not yet handle cp"
    16  
    17      srcdir=$PODMAN_TMPDIR/cp-test-in
    18      dstdir=$PODMAN_TMPDIR/cp-test-out
    19      mkdir -p $srcdir $dstdir
    20  
    21      rand_filename1=$(random_string 20)
    22      rand_content1=$(random_string 50)
    23      rand_filename2=$(random_string 20)
    24      rand_content2=$(random_string 50)
    25  
    26      run_podman run --name cpcontainer $IMAGE sh -c \
    27                 "echo $rand_content1 >/tmp/$rand_filename1;
    28                  echo $rand_content2 >/tmp/$rand_filename2"
    29  
    30      # cp no longer supports wildcarding
    31      run_podman 125 cp 'cpcontainer:/tmp/*' $dstdir
    32  
    33      run_podman rm cpcontainer
    34  }
    35  
    36  
    37  # Create a file on the host; make a symlink in the container pointing
    38  # into host-only space. Try to podman-cp that symlink. It should fail.
    39  @test "podman cp - will not recognize symlink pointing into host space" {
    40      skip_if_remote "podman-remote does not yet handle cp"
    41  
    42      srcdir=$PODMAN_TMPDIR/cp-test-in
    43      dstdir=$PODMAN_TMPDIR/cp-test-out
    44      mkdir -p $srcdir $dstdir
    45      echo "this file is on the host" >$srcdir/hostfile
    46  
    47      run_podman run --name cpcontainer $IMAGE \
    48                 sh -c "ln -s $srcdir/hostfile /tmp/badlink"
    49      # This should fail because, from the container's perspective, the symlink
    50      # points to a nonexistent file
    51      run_podman 125 cp 'cpcontainer:/tmp/*' $dstdir/
    52  
    53      # FIXME: this might not be the exactly correct error message
    54      is "$output" ".*error evaluating symlinks.*lstat.*no such file or dir" \
    55         "Expected error from copying invalid symlink"
    56  
    57      # make sure there are no files in dstdir
    58      is "$(/bin/ls -1 $dstdir)" "" "incorrectly copied symlink from host"
    59  
    60      run_podman rm cpcontainer
    61  }
    62  
    63  
    64  # Issue #3829 - like the above, but with a level of indirection in the
    65  # wildcard expansion: create a file on the host; create a symlink in
    66  # the container named 'file1' pointing to this file; then another symlink
    67  # in the container pointing to 'file*' (file star). Try to podman-cp
    68  # this invalid double symlink. It must fail.
    69  @test "podman cp - will not expand globs in host space (#3829)" {
    70      skip_if_remote "podman-remote does not yet handle cp"
    71  
    72      srcdir=$PODMAN_TMPDIR/cp-test-in
    73      dstdir=$PODMAN_TMPDIR/cp-test-out
    74      mkdir -p $srcdir $dstdir
    75      echo "This file is on the host" > $srcdir/hostfile
    76  
    77      run_podman run --name cpcontainer $IMAGE \
    78                 sh -c "ln -s $srcdir/hostfile file1;ln -s file\* copyme"
    79      run_podman 125 cp cpcontainer:copyme $dstdir
    80  
    81      is "$output" ".*error evaluating symlinks.*lstat.*no such file or dir" \
    82         "Expected error from copying invalid symlink"
    83  
    84      # make sure there are no files in dstdir
    85      is "$(/bin/ls -1 $dstdir)" "" "incorrectly copied symlink from host"
    86  
    87      run_podman rm cpcontainer
    88  }
    89  
    90  
    91  # Another symlink into host space, this one named '*' (star). cp should fail.
    92  @test "podman cp - will not expand wildcard" {
    93      skip_if_remote "podman-remote does not yet handle cp"
    94  
    95      srcdir=$PODMAN_TMPDIR/cp-test-in
    96      dstdir=$PODMAN_TMPDIR/cp-test-out
    97      mkdir -p $srcdir $dstdir
    98      echo "This file lives on the host" > $srcdir/hostfile
    99  
   100      run_podman run --name cpcontainer $IMAGE \
   101                 sh -c "ln -s $srcdir/hostfile /tmp/\*"
   102      run_podman 125 cp 'cpcontainer:/tmp/*' $dstdir
   103  
   104      is "$output" ".*error evaluating symlinks.*lstat.*no such file or dir" \
   105         "Expected error from copying invalid symlink"
   106  
   107      # dstdir must be empty
   108      is "$(/bin/ls -1 $dstdir)" "" "incorrectly copied symlink from host"
   109  
   110      run_podman rm cpcontainer
   111  }
   112  
   113  ###############################################################################
   114  # cp INTO container
   115  
   116  # THIS IS EXTREMELY WEIRD. Podman expands symlinks in weird ways.
   117  @test "podman cp into container: weird symlink expansion" {
   118      skip_if_remote "podman-remote does not yet handle cp"
   119  
   120      srcdir=$PODMAN_TMPDIR/cp-test-in
   121      dstdir=$PODMAN_TMPDIR/cp-test-out
   122      mkdir -p $srcdir $dstdir
   123  
   124      rand_filename1=$(random_string 20)
   125      rand_content1=$(random_string 50)
   126      echo $rand_content1 > $srcdir/$rand_filename1
   127  
   128      rand_filename2=$(random_string 20)
   129      rand_content2=$(random_string 50)
   130      echo $rand_content2 > $srcdir/$rand_filename2
   131  
   132      rand_filename3=$(random_string 20)
   133      rand_content3=$(random_string 50)
   134      echo $rand_content3 > $srcdir/$rand_filename3
   135  
   136      # Create tmp subdirectories in container, most with an invalid 'x' symlink
   137      # Keep container running so we can exec into it.
   138      run_podman run -d --name cpcontainer $IMAGE \
   139                 sh -c "mkdir /tmp/d1;ln -s /tmp/nonesuch1 /tmp/d1/x;
   140                        mkdir /tmp/d2;ln -s /tmp/nonesuch2 /tmp/d2/x;
   141                        mkdir /tmp/d3;
   142                        trap 'exit 0' 15;while :;do sleep 0.5;done"
   143  
   144      # Copy file from host into container, into a file named 'x'
   145      # Note that the second has a trailing slash, implying a directory.
   146      # Since that destination directory doesn't exist, the cp will fail
   147      run_podman cp --pause=false $srcdir/$rand_filename1 cpcontainer:/tmp/d1/x
   148      is "$output" "" "output from podman cp 1"
   149  
   150      run_podman 125 cp --pause=false $srcdir/$rand_filename2 cpcontainer:/tmp/d2/x/
   151      is "$output" "Error: failed to get stat of dest path .*stat.* no such file or directory" "cp will not create nonexistent destination directory"
   152  
   153      run_podman cp --pause=false $srcdir/$rand_filename3 cpcontainer:/tmp/d3/x
   154      is "$output" "" "output from podman cp 3"
   155  
   156      # Read back.
   157      # In the first case, podman actually creates the file nonesuch1 (i.e.
   158      # podman expands 'x -> nonesuch1' and, instead of overwriting x,
   159      # creates an actual file).
   160      run_podman exec cpcontainer cat /tmp/nonesuch1
   161      is "$output" "$rand_content1" "cp creates destination file"
   162  
   163      # cp into nonexistent directory should not mkdir nonesuch2 directory
   164      run_podman 1 exec cpcontainer test -e /tmp/nonesuch2
   165  
   166      # In the third case, podman (correctly imo) creates a file named 'x'
   167      run_podman exec cpcontainer cat /tmp/d3/x
   168      is "$output" "$rand_content3" "cp creates file named x"
   169  
   170      run_podman rm -f cpcontainer
   171  
   172  
   173  }
   174  
   175  
   176  # rhbz1741718 : file copied into container:/var/lib/foo appears as /foo
   177  # (docker only, never seems to have affected podman. Make sure it never does).
   178  @test "podman cp into a subdirectory matching GraphRoot" {
   179      skip_if_remote "podman-remote does not yet handle cp"
   180  
   181      # Create tempfile with random name and content
   182      srcdir=$PODMAN_TMPDIR/cp-test-in
   183      mkdir -p $srcdir
   184      rand_filename=$(random_string 20)
   185      rand_content=$(random_string 50)
   186      echo $rand_content > $srcdir/$rand_filename
   187      chmod 644 $srcdir/$rand_filename
   188  
   189      # Determine path to podman storage (eg /var/lib/c/s, or $HOME/.local/...)
   190      run_podman info --format '{{.Store.GraphRoot}}'
   191      graphroot=$output
   192  
   193      # Create that directory in the container, and sleep (to keep container
   194      # running, so we can exec into it). The trap/while is so podman-rm will
   195      # run quickly instead of taking 10 seconds.
   196      run_podman run -d --name cpcontainer $IMAGE sh -c \
   197                 "mkdir -p $graphroot; trap 'exit 0' 15;while :;do sleep 0.5;done"
   198  
   199      # Copy from host into container.
   200      run_podman cp --pause=false $srcdir/$rand_filename cpcontainer:$graphroot/$rand_filename
   201  
   202      # ls, and confirm it's there.
   203      run_podman exec cpcontainer ls -l $graphroot/$rand_filename
   204      is "$output" "-rw-r--r-- .* 1 .* root .* 51 .* $graphroot/$rand_filename" \
   205         "File is copied into container in the correct (full) path"
   206  
   207      # Confirm it has the expected content (this is unlikely to ever fail)
   208      run_podman exec cpcontainer cat $graphroot/$rand_filename
   209      is "$output" "$rand_content" "Contents of file copied into container"
   210  
   211      run_podman rm -f cpcontainer
   212  }
   213  
   214  
   215  function teardown() {
   216      # In case any test fails, clean up the container we left behind
   217      run_podman rm -f cpcontainer
   218      basic_teardown
   219  }
   220  
   221  # vim: filetype=sh