|
@@ -14,6 +14,7 @@ import (
|
|
|
"testing"
|
|
|
"time"
|
|
|
|
|
|
+ "github.com/docker/docker/pkg/idtools"
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
"github.com/stretchr/testify/require"
|
|
|
)
|
|
@@ -724,6 +725,57 @@ func TestTarUntar(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func TestTarWithOptionsChownOptsAlwaysOverridesIdPair(t *testing.T) {
|
|
|
+ origin, err := ioutil.TempDir("", "docker-test-tar-chown-opt")
|
|
|
+ require.NoError(t, err)
|
|
|
+
|
|
|
+ defer os.RemoveAll(origin)
|
|
|
+ filePath := filepath.Join(origin, "1")
|
|
|
+ err = ioutil.WriteFile(filePath, []byte("hello world"), 0700)
|
|
|
+ require.NoError(t, err)
|
|
|
+
|
|
|
+ idMaps := []idtools.IDMap{
|
|
|
+ 0: {
|
|
|
+ ContainerID: 0,
|
|
|
+ HostID: 0,
|
|
|
+ Size: 65536,
|
|
|
+ },
|
|
|
+ 1: {
|
|
|
+ ContainerID: 0,
|
|
|
+ HostID: 100000,
|
|
|
+ Size: 65536,
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ cases := []struct {
|
|
|
+ opts *TarOptions
|
|
|
+ expectedUID int
|
|
|
+ expectedGID int
|
|
|
+ }{
|
|
|
+ {&TarOptions{ChownOpts: &idtools.IDPair{UID: 1337, GID: 42}}, 1337, 42},
|
|
|
+ {&TarOptions{ChownOpts: &idtools.IDPair{UID: 100001, GID: 100001}, UIDMaps: idMaps, GIDMaps: idMaps}, 100001, 100001},
|
|
|
+ {&TarOptions{ChownOpts: &idtools.IDPair{UID: 0, GID: 0}, NoLchown: false}, 0, 0},
|
|
|
+ {&TarOptions{ChownOpts: &idtools.IDPair{UID: 1, GID: 1}, NoLchown: true}, 1, 1},
|
|
|
+ {&TarOptions{ChownOpts: &idtools.IDPair{UID: 1000, GID: 1000}, NoLchown: true}, 1000, 1000},
|
|
|
+ }
|
|
|
+ for _, testCase := range cases {
|
|
|
+ reader, err := TarWithOptions(filePath, testCase.opts)
|
|
|
+ require.NoError(t, err)
|
|
|
+ tr := tar.NewReader(reader)
|
|
|
+ defer reader.Close()
|
|
|
+ for {
|
|
|
+ hdr, err := tr.Next()
|
|
|
+ if err == io.EOF {
|
|
|
+ // end of tar archive
|
|
|
+ break
|
|
|
+ }
|
|
|
+ require.NoError(t, err)
|
|
|
+ assert.Equal(t, hdr.Uid, testCase.expectedUID, "Uid equals expected value")
|
|
|
+ assert.Equal(t, hdr.Gid, testCase.expectedGID, "Gid equals expected value")
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func TestTarWithOptions(t *testing.T) {
|
|
|
// TODO Windows: Figure out how to fix this test.
|
|
|
if runtime.GOOS == "windows" {
|