mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
integ-cli: test correct directory permissions
Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
This commit is contained in:
parent
691bbf6a29
commit
cc5fb986b0
2 changed files with 85 additions and 0 deletions
|
@ -2,8 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -191,3 +194,66 @@ func TestSaveMultipleNames(t *testing.T) {
|
||||||
|
|
||||||
logDone("save - save by multiple names")
|
logDone("save - save by multiple names")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue #6722 #5892 ensure directories are included in changes
|
||||||
|
func TestSaveDirectoryPermissions(t *testing.T) {
|
||||||
|
layerEntries := []string{"opt/", "opt/a/", "opt/a/b/", "opt/a/b/c"}
|
||||||
|
|
||||||
|
name := "save-directory-permissions"
|
||||||
|
tmpDir, err := ioutil.TempDir("", "save-layers-with-directories")
|
||||||
|
extractionDirectory := filepath.Join(tmpDir, "image-extraction-dir")
|
||||||
|
os.Mkdir(extractionDirectory, 0777)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to create temporary directory: %s", err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tmpDir)
|
||||||
|
defer deleteImages(name)
|
||||||
|
_, err = buildImage(name,
|
||||||
|
`FROM busybox
|
||||||
|
RUN adduser -D user && mkdir -p /opt/a/b && chown -R user:user /opt/a
|
||||||
|
RUN touch /opt/a/b/c && chown user:user /opt/a/b/c`,
|
||||||
|
true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
saveCmdFinal := fmt.Sprintf("%s save %s | tar -xf - -C %s", dockerBinary, name, extractionDirectory)
|
||||||
|
saveCmd := exec.Command("bash", "-c", saveCmdFinal)
|
||||||
|
out, _, err := runCommandWithOutput(saveCmd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to save and extract image: %s", out)
|
||||||
|
}
|
||||||
|
|
||||||
|
dirs, err := ioutil.ReadDir(extractionDirectory)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to get a listing of the layer directories: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
found := false
|
||||||
|
for _, entry := range dirs {
|
||||||
|
if entry.IsDir() {
|
||||||
|
layerPath := filepath.Join(extractionDirectory, entry.Name(), "layer.tar")
|
||||||
|
|
||||||
|
f, err := os.Open(layerPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to open %s: %s", layerPath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
entries, err := ListTar(f)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("encountered error while listing tar entries: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if reflect.DeepEqual(entries, layerEntries) {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
t.Fatalf("failed to find the layer with the right content listing")
|
||||||
|
}
|
||||||
|
|
||||||
|
logDone("save - ensure directories exist in exported layers")
|
||||||
|
}
|
||||||
|
|
|
@ -12,6 +12,8 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getExitCode(err error) (int, error) {
|
func getExitCode(err error) (int, error) {
|
||||||
|
@ -193,3 +195,20 @@ func compareDirectoryEntries(e1 []os.FileInfo, e2 []os.FileInfo) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ListTar(f io.Reader) ([]string, error) {
|
||||||
|
tr := tar.NewReader(f)
|
||||||
|
var entries []string
|
||||||
|
|
||||||
|
for {
|
||||||
|
th, err := tr.Next()
|
||||||
|
if err == io.EOF {
|
||||||
|
// end of tar archive
|
||||||
|
return entries, nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return entries, err
|
||||||
|
}
|
||||||
|
entries = append(entries, th.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue