Exercise Copy[File]WithTar, optionally with Chown flags
Add integration tests to exercise CopyFileWithTar and CopyWithTar, both with and without Chown flags. Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
parent
9d1f351db5
commit
b22d9f20d3
|
|
@ -0,0 +1,109 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/containers/storage"
|
||||
"github.com/containers/storage/pkg/chrootarchive"
|
||||
"github.com/containers/storage/pkg/idtools"
|
||||
"github.com/containers/storage/pkg/mflag"
|
||||
)
|
||||
|
||||
var (
|
||||
chownOptions = ""
|
||||
)
|
||||
|
||||
func copyContent(flags *mflag.FlagSet, action string, m storage.Store, args []string) int {
|
||||
var untarIDMappings *idtools.IDMappings
|
||||
var chownOpts *idtools.IDPair
|
||||
if len(args) < 1 {
|
||||
return 1
|
||||
}
|
||||
if len(chownOptions) > 0 {
|
||||
chownParts := strings.SplitN(chownOptions, ":", 2)
|
||||
if len(chownParts) == 1 {
|
||||
chownParts = append(chownParts, chownParts[0])
|
||||
}
|
||||
uid, err := strconv.ParseUint(chownParts[0], 10, 32)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error %q as a numeric UID: %v", chownParts[0], err)
|
||||
return 1
|
||||
}
|
||||
gid, err := strconv.ParseUint(chownParts[1], 10, 32)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error %q as a numeric GID: %v", chownParts[1], err)
|
||||
return 1
|
||||
}
|
||||
chownOpts = &idtools.IDPair{UID: int(uid), GID: int(gid)}
|
||||
}
|
||||
target := args[len(args)-1]
|
||||
if strings.Contains(target, ":") {
|
||||
targetParts := strings.SplitN(target, ":", 2)
|
||||
if len(targetParts) != 2 {
|
||||
fmt.Fprintf(os.Stderr, "error parsing target location %q: only one part\n", target)
|
||||
return 1
|
||||
}
|
||||
targetLayer, err := m.Layer(targetParts[0])
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error finding layer %q: %v\n", targetParts[0], err)
|
||||
return 1
|
||||
}
|
||||
untarIDMappings = idtools.NewIDMappingsFromMaps(targetLayer.UIDMap, targetLayer.GIDMap)
|
||||
targetMount, err := m.Mount(targetLayer.ID, targetLayer.MountLabel)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error mounting layer %q: %v\n", targetLayer.ID, err)
|
||||
return 1
|
||||
}
|
||||
target = filepath.Join(targetMount, targetParts[1])
|
||||
defer m.Unmount(targetLayer.ID)
|
||||
}
|
||||
args = args[:len(args)-1]
|
||||
for _, srcSpec := range args {
|
||||
var tarIDMappings *idtools.IDMappings
|
||||
source := srcSpec
|
||||
if strings.Contains(source, ":") {
|
||||
sourceParts := strings.SplitN(source, ":", 2)
|
||||
if len(sourceParts) != 2 {
|
||||
fmt.Fprintf(os.Stderr, "error parsing source location %q: only one part\n", source)
|
||||
return 1
|
||||
}
|
||||
sourceLayer, err := m.Layer(sourceParts[0])
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error finding layer %q: %v\n", sourceParts[0], err)
|
||||
return 1
|
||||
}
|
||||
tarIDMappings = idtools.NewIDMappingsFromMaps(sourceLayer.UIDMap, sourceLayer.GIDMap)
|
||||
sourceMount, err := m.Mount(sourceLayer.ID, sourceLayer.MountLabel)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error mounting layer %q: %v\n", sourceLayer.ID, err)
|
||||
return 1
|
||||
}
|
||||
source = filepath.Join(sourceMount, sourceParts[1])
|
||||
defer m.Unmount(sourceLayer.ID)
|
||||
}
|
||||
archiver := chrootarchive.NewArchiverWithChown(tarIDMappings, chownOpts, untarIDMappings)
|
||||
if err := archiver.CopyWithTar(source, target); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error copying %q to %q: %v\n", source, target, err)
|
||||
return 1
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func init() {
|
||||
commands = append(commands, command{
|
||||
names: []string{"copy"},
|
||||
usage: "Copy files or directories into a layer, possibly from another layer",
|
||||
optionsHelp: "[options [...]] [sourceLayerNameOrID:]/path [...] targetLayerNameOrID:/path",
|
||||
minArgs: 2,
|
||||
action: copyContent,
|
||||
addFlags: func(flags *mflag.FlagSet, cmd *command) {
|
||||
flags.StringVar(&chownOptions, []string{"-chown", ""}, chownOptions, "Set owner on new copies")
|
||||
flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
## containers-storage-copy 1 "April 2018"
|
||||
|
||||
## NAME
|
||||
containers-storage copy - Copy content into a layer
|
||||
|
||||
## SYNOPSIS
|
||||
**containers-storage** **copy** [--chown UID[:GID]] [*sourceLayerNameOrID*:]/path [...] *targetLayerNameOrID*:/path
|
||||
|
||||
## DESCRIPTION
|
||||
Copies contents from a layer, or outside of layers, into a layer, performing ID mapping as appropriate.
|
||||
|
||||
## OPTIONS
|
||||
**--chown** *UID[:GID]*
|
||||
|
||||
Set owners for the new copies to the specified UID/GID pair. If the GID is not
|
||||
specified, the UID is used. The owner IDs are mapped as appropriate for the
|
||||
target layer.
|
||||
|
||||
## EXAMPLE
|
||||
**containers-storage copy layer-with-mapping:/root/config.txt layer-with-different-mapping:/root/config.txt**
|
||||
|
||||
## SEE ALSO
|
||||
containers-storage(1)
|
||||
|
|
@ -333,3 +333,157 @@ load helpers
|
|||
fgrep -q 'GIDs: [0, '$(id -g)']' <<< "$output"
|
||||
fi
|
||||
}
|
||||
|
||||
@test "idmaps-copy" {
|
||||
n=5
|
||||
host=2
|
||||
# Create some temporary files.
|
||||
mkdir -p "$TESTDIR"/subdir/subdir2
|
||||
for i in 0 $(seq $n) ; do
|
||||
createrandom "$TESTDIR"/file$i
|
||||
chown "$i":"$i" "$TESTDIR"/file$i
|
||||
createrandom "$TESTDIR"/subdir/subdir2/file$i
|
||||
chown "$i":"$i" "$TESTDIR"/subdir/subdir2/file$i
|
||||
done
|
||||
chown "$(($n+1))":"$(($n+1))" "$TESTDIR"/subdir/subdir2
|
||||
# Select some ID ranges for ID mappings.
|
||||
for i in 0 $(seq $(($n+1))) ; do
|
||||
uidrange[$i]=$((($RANDOM+32767)*65536))
|
||||
gidrange[$i]=$((($RANDOM+32767)*65536))
|
||||
# Create a layer using some those mappings.
|
||||
run storage --debug=false create-layer --uidmap 0:${uidrange[$i]}:101 --gidmap 0:${gidrange[$i]}:101
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$output" != "" ]
|
||||
layer[$i]="$output"
|
||||
done
|
||||
# Copy the file in, and check that its ownerships get mapped correctly.
|
||||
for i in 0 $(seq $n) ; do
|
||||
run storage copy "$TESTDIR"/file$i ${layer[$i]}:/file$i
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run storage --debug=false mount ${layer[$i]}
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
mnt[$i]="$output"
|
||||
run stat -c "%u:%g" ${mnt[$i]}/file$i
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
uid=$((${uidrange[$i]}+$i))
|
||||
gid=$((${gidrange[$i]}+$i))
|
||||
echo comparing "$output" and "$uid:$gid"
|
||||
[ "$output" == "$uid:$gid" ]
|
||||
# Try copying with --chown.
|
||||
run storage copy --chown 100:100 "$TESTDIR"/file$i ${layer[$i]}:/file$i
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run storage --debug=false mount ${layer[$i]}
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
mnt[$i]="$output"
|
||||
run stat -c "%u:%g" ${mnt[$i]}/file$i
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
uid=$((${uidrange[$i]}+100))
|
||||
gid=$((${gidrange[$i]}+100))
|
||||
echo comparing "$output" and "$uid:$gid"
|
||||
[ "$output" == "$uid:$gid" ]
|
||||
done
|
||||
# Copy the subdirectory, and check that its ownerships and that of its contents get mapped correctly.
|
||||
for i in 0 $(seq $n) ; do
|
||||
run storage copy "$TESTDIR"/file$i ${layer[$i]}:/file$i
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run stat -c "%u:%g" ${mnt[$i]}/file$i
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
uid=$((${uidrange[$i]}+$i))
|
||||
gid=$((${gidrange[$i]}+$i))
|
||||
echo comparing "$output" and "$uid:$gid"
|
||||
[ "$output" == "$uid:$gid" ]
|
||||
|
||||
# Try copying with --chown.
|
||||
run storage copy --chown 100:100 "$TESTDIR"/file$i ${layer[$i]}:/file$i
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run stat -c "%u:%g" ${mnt[$i]}/file$i
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
uid=$((${uidrange[$i]}+100))
|
||||
gid=$((${gidrange[$i]}+100))
|
||||
echo comparing "$output" and "$uid:$gid"
|
||||
[ "$output" == "$uid:$gid" ]
|
||||
|
||||
# Try copying a directory tree.
|
||||
run storage copy "$TESTDIR"/subdir ${layer[$i]}:/subdir
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run stat -c "%u:%g" ${mnt[$i]}/subdir/subdir2/file$i
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
uid=$((${uidrange[$i]}+$i))
|
||||
gid=$((${gidrange[$i]}+$i))
|
||||
echo comparing "$output" and "$uid:$gid"
|
||||
[ "$output" == "$uid:$gid" ]
|
||||
run stat -c "%u:%g" ${mnt[$i]}/subdir/subdir2
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
uid=$((${uidrange[$i]}+$n+1))
|
||||
gid=$((${gidrange[$i]}+$n+1))
|
||||
echo comparing "$output" and "$uid:$gid"
|
||||
[ "$output" == "$uid:$gid" ]
|
||||
|
||||
# Try copying a directory tree with --chown.
|
||||
run storage copy --chown 100:100 "$TESTDIR"/subdir ${layer[$i]}:/subdir2
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run stat -c "%u:%g" ${mnt[$i]}/subdir2/subdir2/file$i
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
uid=$((${uidrange[$i]}+100))
|
||||
gid=$((${gidrange[$i]}+100))
|
||||
echo comparing "$output" and "$uid:$gid"
|
||||
[ "$output" == "$uid:$gid" ]
|
||||
run stat -c "%u:%g" ${mnt[$i]}/subdir2/subdir2
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
uid=$((${uidrange[$i]}+100))
|
||||
gid=$((${gidrange[$i]}+100))
|
||||
echo comparing "$output" and "$uid:$gid"
|
||||
[ "$output" == "$uid:$gid" ]
|
||||
done
|
||||
|
||||
# Copy a file out of a layer, into another one.
|
||||
run storage copy "$TESTDIR"/file$n ${layer[0]}:/file$n
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run storage copy ${layer[0]}:/file$n ${layer[$n]}:/file$n
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run stat -c "%u:%g" ${mnt[$n]}/file$n
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
uid=$((${uidrange[$n]}+$n))
|
||||
gid=$((${gidrange[$n]}+$n))
|
||||
echo comparing "$output" and "$uid:$gid"
|
||||
[ "$output" == "$uid:$gid" ]
|
||||
|
||||
# Try copying a directory tree.
|
||||
run storage copy ${layer[0]}:/subdir ${layer[$n]}:/subdir
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
run stat -c "%u:%g" ${mnt[$n]}/subdir/subdir2/file$n
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
uid=$((${uidrange[$n]}+$n))
|
||||
gid=$((${gidrange[$n]}+$n))
|
||||
echo comparing "$output" and "$uid:$gid"
|
||||
[ "$output" == "$uid:$gid" ]
|
||||
run stat -c "%u:%g" ${mnt[$n]}/subdir/subdir2
|
||||
echo "$output"
|
||||
[ "$status" -eq 0 ]
|
||||
uid=$((${uidrange[$n]}+$n+1))
|
||||
gid=$((${gidrange[$n]}+$n+1))
|
||||
echo comparing "$output" and "$uid:$gid"
|
||||
[ "$output" == "$uid:$gid" ]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue