2015-04-27 10:53:12 -04:00
|
|
|
// +build linux
|
|
|
|
|
2013-11-07 07:33:31 -05:00
|
|
|
package aufs
|
|
|
|
|
|
|
|
import (
|
2013-11-26 13:50:53 -05:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2013-11-07 07:33:31 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
2017-08-03 20:22:00 -04:00
|
|
|
"path/filepath"
|
2016-02-12 10:20:16 -05:00
|
|
|
"sync"
|
2013-11-07 07:33:31 -05:00
|
|
|
"testing"
|
2014-11-20 12:39:08 -05:00
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2014-11-11 06:02:14 -05:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2016-02-12 10:20:16 -05:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-09-11 14:55:05 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2013-11-07 07:33:31 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-11-20 12:39:08 -05:00
|
|
|
tmpOuter = path.Join(os.TempDir(), "aufs-tests")
|
|
|
|
tmp = path.Join(tmpOuter, "aufs")
|
2013-11-07 07:33:31 -05:00
|
|
|
)
|
|
|
|
|
2014-11-11 06:02:14 -05:00
|
|
|
func init() {
|
|
|
|
reexec.Init()
|
|
|
|
}
|
|
|
|
|
2016-02-12 10:20:16 -05:00
|
|
|
func testInit(dir string, t testing.TB) graphdriver.Driver {
|
2015-10-08 11:51:41 -04:00
|
|
|
d, err := Init(dir, nil, nil, nil)
|
2014-02-18 05:58:38 -05:00
|
|
|
if err != nil {
|
2014-03-27 12:41:06 -04:00
|
|
|
if err == graphdriver.ErrNotSupported {
|
2014-02-18 05:58:38 -05:00
|
|
|
t.Skip(err)
|
|
|
|
} else {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
func driverGet(d *Driver, id string, mntLabel string) (string, error) {
|
|
|
|
mnt, err := d.Get(id, mntLabel)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return mnt.Path(), nil
|
|
|
|
}
|
|
|
|
|
2016-02-12 10:20:16 -05:00
|
|
|
func newDriver(t testing.TB) *Driver {
|
2013-11-07 07:33:31 -05:00
|
|
|
if err := os.MkdirAll(tmp, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-02-18 05:58:38 -05:00
|
|
|
d := testInit(tmp, t)
|
2013-11-19 06:27:59 -05:00
|
|
|
return d.(*Driver)
|
2013-11-07 07:33:31 -05:00
|
|
|
}
|
|
|
|
|
2013-11-19 06:27:59 -05:00
|
|
|
func TestNewDriver(t *testing.T) {
|
2013-11-07 07:33:31 -05:00
|
|
|
if err := os.MkdirAll(tmp, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-02-18 05:58:38 -05:00
|
|
|
d := testInit(tmp, t)
|
2013-11-07 07:33:31 -05:00
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
if d == nil {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("Driver should not be nil")
|
2013-11-07 07:33:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAufsString(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
if d.String() != "aufs" {
|
|
|
|
t.Fatalf("Expected aufs got %s", d.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateDirStructure(t *testing.T) {
|
|
|
|
newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
paths := []string{
|
|
|
|
"mnt",
|
|
|
|
"layers",
|
|
|
|
"diff",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range paths {
|
2013-11-08 14:36:58 -05:00
|
|
|
if _, err := os.Stat(path.Join(tmp, p)); err != nil {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should be able to create two drivers with the same dir structure
|
|
|
|
func TestNewDriverFromExistingDir(t *testing.T) {
|
|
|
|
if err := os.MkdirAll(tmp, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-02-18 05:58:38 -05:00
|
|
|
testInit(tmp, t)
|
|
|
|
testInit(tmp, t)
|
2013-11-07 07:33:31 -05:00
|
|
|
os.RemoveAll(tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateNewDir(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("1", "", nil); err != nil {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateNewDirStructure(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("1", "", nil); err != nil {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
paths := []string{
|
|
|
|
"mnt",
|
|
|
|
"diff",
|
|
|
|
"layers",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range paths {
|
2013-11-08 14:36:58 -05:00
|
|
|
if _, err := os.Stat(path.Join(tmp, p, "1")); err != nil {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveImage(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("1", "", nil); err != nil {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.Remove("1"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
paths := []string{
|
|
|
|
"mnt",
|
|
|
|
"diff",
|
|
|
|
"layers",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range paths {
|
2013-11-08 14:36:58 -05:00
|
|
|
if _, err := os.Stat(path.Join(tmp, p, "1")); err == nil {
|
2017-08-21 17:38:13 -04:00
|
|
|
t.Fatalf("Error should not be nil because dirs with id 1 should be deleted: %s", p)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(path.Join(tmp, p, "1-removing")); err == nil {
|
|
|
|
t.Fatalf("Error should not be nil because dirs with id 1-removing should be deleted: %s", p)
|
2013-11-07 07:33:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetWithoutParent(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("1", "", nil); err != nil {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-04-17 19:47:27 -04:00
|
|
|
diffPath, err := d.Get("1", "")
|
2013-11-07 07:33:31 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-08 14:36:58 -05:00
|
|
|
expected := path.Join(tmp, "diff", "1")
|
2017-08-03 20:22:00 -04:00
|
|
|
if diffPath.Path() != expected {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatalf("Expected path %s got %s", expected, diffPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanupWithNoDirs(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2017-09-11 14:55:05 -04:00
|
|
|
err := d.Cleanup()
|
|
|
|
assert.NoError(t, err)
|
2013-11-07 07:33:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanupWithDir(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("1", "", nil); err != nil {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.Cleanup(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountedFalseResponse(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2017-09-11 14:55:05 -04:00
|
|
|
err := d.Create("1", "", nil)
|
|
|
|
require.NoError(t, err)
|
2013-11-07 07:33:31 -05:00
|
|
|
|
2016-03-09 16:23:04 -05:00
|
|
|
response, err := d.mounted(d.getDiffPath("1"))
|
2017-09-11 14:55:05 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, response)
|
2013-11-07 07:33:31 -05:00
|
|
|
}
|
|
|
|
|
2017-01-16 23:45:27 -05:00
|
|
|
func TestMountedTrueResponse(t *testing.T) {
|
2013-11-07 07:33:31 -05:00
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
defer d.Cleanup()
|
|
|
|
|
2017-09-11 14:55:05 -04:00
|
|
|
err := d.Create("1", "", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = d.Create("2", "1", nil)
|
|
|
|
require.NoError(t, err)
|
2013-11-07 07:33:31 -05:00
|
|
|
|
2017-09-11 14:55:05 -04:00
|
|
|
_, err = d.Get("2", "")
|
|
|
|
require.NoError(t, err)
|
2013-11-07 07:33:31 -05:00
|
|
|
|
2016-03-09 16:23:04 -05:00
|
|
|
response, err := d.mounted(d.pathCache["2"])
|
2017-09-11 14:55:05 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, response)
|
2013-11-07 07:33:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountWithParent(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("1", "", nil); err != nil {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("2", "1", nil); err != nil {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-11-08 13:17:51 -05:00
|
|
|
defer func() {
|
|
|
|
if err := d.Cleanup(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-04-17 19:47:27 -04:00
|
|
|
mntPath, err := d.Get("2", "")
|
2013-11-07 07:33:31 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-08-03 20:22:00 -04:00
|
|
|
if mntPath == nil {
|
|
|
|
t.Fatal("mntPath should not be nil")
|
2013-11-07 07:33:31 -05:00
|
|
|
}
|
|
|
|
|
2013-11-08 14:36:58 -05:00
|
|
|
expected := path.Join(tmp, "mnt", "2")
|
2017-08-03 20:22:00 -04:00
|
|
|
if mntPath.Path() != expected {
|
|
|
|
t.Fatalf("Expected %s got %s", expected, mntPath.Path())
|
2013-11-07 07:33:31 -05:00
|
|
|
}
|
2013-11-08 13:17:51 -05:00
|
|
|
}
|
2013-11-07 07:33:31 -05:00
|
|
|
|
2013-11-08 13:17:51 -05:00
|
|
|
func TestRemoveMountedDir(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("1", "", nil); err != nil {
|
2013-11-08 13:17:51 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("2", "1", nil); err != nil {
|
2013-11-08 13:17:51 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := d.Cleanup(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-04-17 19:47:27 -04:00
|
|
|
mntPath, err := d.Get("2", "")
|
2013-11-08 13:17:51 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-08-03 20:22:00 -04:00
|
|
|
if mntPath == nil {
|
|
|
|
t.Fatal("mntPath should not be nil")
|
2013-11-08 13:17:51 -05:00
|
|
|
}
|
|
|
|
|
2016-03-09 16:23:04 -05:00
|
|
|
mounted, err := d.mounted(d.pathCache["2"])
|
2013-11-08 13:17:51 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mounted {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("Dir id 2 should be mounted")
|
2013-11-08 13:17:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.Remove("2"); err != nil {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateWithInvalidParent(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("1", "docker", nil); err == nil {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("Error should not be nil with parent does not exist")
|
2013-11-07 07:33:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDiff(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.CreateReadWrite("1", "", nil); err != nil {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
diffPath, err := driverGet(d, "1", "")
|
2013-11-07 07:33:31 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a file to the diff path with a fixed size
|
|
|
|
size := int64(1024)
|
|
|
|
|
|
|
|
f, err := os.Create(path.Join(diffPath, "test_file"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := f.Truncate(size); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
2014-09-10 23:30:52 -04:00
|
|
|
a, err := d.Diff("1", "")
|
2013-11-07 07:33:31 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if a == nil {
|
2017-02-21 03:53:29 -05:00
|
|
|
t.Fatal("Archive should not be nil")
|
2013-11-07 07:33:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-08 14:10:33 -05:00
|
|
|
func TestChanges(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("1", "", nil); err != nil {
|
2013-11-08 14:10:33 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-11-09 15:59:58 -05:00
|
|
|
|
|
|
|
if err := d.CreateReadWrite("2", "1", nil); err != nil {
|
2013-11-08 14:10:33 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := d.Cleanup(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
mntPoint, err := driverGet(d, "2", "")
|
2013-11-08 14:10:33 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a file to save in the mountpoint
|
|
|
|
f, err := os.Create(path.Join(mntPoint, "test.txt"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := f.WriteString("testline"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-10 23:30:52 -04:00
|
|
|
changes, err := d.Changes("2", "")
|
2013-11-08 14:10:33 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(changes) != 1 {
|
|
|
|
t.Fatalf("Dir 2 should have one change from parent got %d", len(changes))
|
|
|
|
}
|
|
|
|
change := changes[0]
|
|
|
|
|
|
|
|
expectedPath := "/test.txt"
|
|
|
|
if change.Path != expectedPath {
|
|
|
|
t.Fatalf("Expected path %s got %s", expectedPath, change.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if change.Kind != archive.ChangeAdd {
|
|
|
|
t.Fatalf("Change kind should be ChangeAdd got %s", change.Kind)
|
|
|
|
}
|
2013-11-08 15:25:17 -05:00
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.CreateReadWrite("3", "2", nil); err != nil {
|
2013-11-08 15:25:17 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-08-03 20:22:00 -04:00
|
|
|
mntPoint, err = driverGet(d, "3", "")
|
2013-11-08 15:25:17 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a file to save in the mountpoint
|
|
|
|
f, err = os.Create(path.Join(mntPoint, "test2.txt"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := f.WriteString("testline"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-04-21 12:08:37 -04:00
|
|
|
changes, err = d.Changes("3", "2")
|
2013-11-08 15:25:17 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(changes) != 1 {
|
|
|
|
t.Fatalf("Dir 2 should have one change from parent got %d", len(changes))
|
|
|
|
}
|
|
|
|
change = changes[0]
|
|
|
|
|
|
|
|
expectedPath = "/test2.txt"
|
|
|
|
if change.Path != expectedPath {
|
|
|
|
t.Fatalf("Expected path %s got %s", expectedPath, change.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if change.Kind != archive.ChangeAdd {
|
|
|
|
t.Fatalf("Change kind should be ChangeAdd got %s", change.Kind)
|
|
|
|
}
|
2013-11-08 14:10:33 -05:00
|
|
|
}
|
|
|
|
|
2013-11-07 07:33:31 -05:00
|
|
|
func TestDiffSize(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.CreateReadWrite("1", "", nil); err != nil {
|
2013-11-07 07:33:31 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
diffPath, err := driverGet(d, "1", "")
|
2013-11-07 07:33:31 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a file to the diff path with a fixed size
|
|
|
|
size := int64(1024)
|
|
|
|
|
|
|
|
f, err := os.Create(path.Join(diffPath, "test_file"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-20 16:53:54 -05:00
|
|
|
if err := f.Truncate(size); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
size = s.Size()
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-10 23:30:52 -04:00
|
|
|
diffSize, err := d.DiffSize("1", "")
|
2013-11-20 16:53:54 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if diffSize != size {
|
|
|
|
t.Fatalf("Expected size to be %d got %d", size, diffSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChildDiffSize(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
defer d.Cleanup()
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.CreateReadWrite("1", "", nil); err != nil {
|
2013-11-20 16:53:54 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
diffPath, err := driverGet(d, "1", "")
|
2013-11-20 16:53:54 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a file to the diff path with a fixed size
|
|
|
|
size := int64(1024)
|
|
|
|
|
|
|
|
f, err := os.Create(path.Join(diffPath, "test_file"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := f.Truncate(size); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-07 07:33:31 -05:00
|
|
|
s, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
size = s.Size()
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-10 23:30:52 -04:00
|
|
|
diffSize, err := d.DiffSize("1", "")
|
2013-11-07 07:33:31 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if diffSize != size {
|
|
|
|
t.Fatalf("Expected size to be %d got %d", size, diffSize)
|
|
|
|
}
|
2013-11-20 16:53:54 -05:00
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("2", "1", nil); err != nil {
|
2013-11-20 16:53:54 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-04-21 12:08:37 -04:00
|
|
|
diffSize, err = d.DiffSize("2", "1")
|
2013-11-20 16:53:54 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// The diff size for the child should be zero
|
|
|
|
if diffSize != 0 {
|
|
|
|
t.Fatalf("Expected size to be %d got %d", 0, diffSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExists(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
defer d.Cleanup()
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("1", "", nil); err != nil {
|
2013-11-20 16:53:54 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Exists("none") {
|
2016-08-16 04:28:32 -04:00
|
|
|
t.Fatal("id none should not exist in the driver")
|
2013-11-20 16:53:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !d.Exists("1") {
|
|
|
|
t.Fatal("id 1 should exist in the driver")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatus(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
defer d.Cleanup()
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("1", "", nil); err != nil {
|
2013-11-20 16:53:54 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
status := d.Status()
|
2017-09-11 14:55:05 -04:00
|
|
|
assert.Len(t, status, 4)
|
|
|
|
|
2013-11-20 16:53:54 -05:00
|
|
|
rootDir := status[0]
|
2015-01-15 16:40:39 -05:00
|
|
|
dirs := status[2]
|
2013-11-20 16:53:54 -05:00
|
|
|
if rootDir[0] != "Root Dir" {
|
|
|
|
t.Fatalf("Expected Root Dir got %s", rootDir[0])
|
|
|
|
}
|
|
|
|
if rootDir[1] != d.rootPath() {
|
|
|
|
t.Fatalf("Expected %s got %s", d.rootPath(), rootDir[1])
|
|
|
|
}
|
|
|
|
if dirs[0] != "Dirs" {
|
|
|
|
t.Fatalf("Expected Dirs got %s", dirs[0])
|
|
|
|
}
|
|
|
|
if dirs[1] != "1" {
|
|
|
|
t.Fatalf("Expected 1 got %s", dirs[1])
|
|
|
|
}
|
2013-11-07 07:33:31 -05:00
|
|
|
}
|
2013-11-13 14:03:56 -05:00
|
|
|
|
|
|
|
func TestApplyDiff(t *testing.T) {
|
|
|
|
d := newDriver(t)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
defer d.Cleanup()
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.CreateReadWrite("1", "", nil); err != nil {
|
2013-11-13 14:03:56 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
diffPath, err := driverGet(d, "1", "")
|
2013-11-13 14:03:56 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a file to the diff path with a fixed size
|
|
|
|
size := int64(1024)
|
|
|
|
|
|
|
|
f, err := os.Create(path.Join(diffPath, "test_file"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-20 16:53:54 -05:00
|
|
|
if err := f.Truncate(size); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-13 14:03:56 -05:00
|
|
|
f.Close()
|
|
|
|
|
2014-09-10 23:30:52 -04:00
|
|
|
diff, err := d.Diff("1", "")
|
2013-11-13 14:03:56 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("2", "", nil); err != nil {
|
2013-11-13 14:03:56 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create("3", "2", nil); err != nil {
|
2013-11-13 14:03:56 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-10 23:30:52 -04:00
|
|
|
if err := d.applyDiff("3", diff); err != nil {
|
2013-11-13 14:03:56 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the file is in the mount point for id 3
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
mountPoint, err := driverGet(d, "3", "")
|
2013-11-13 14:03:56 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(path.Join(mountPoint, "test_file")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2013-11-26 13:50:53 -05:00
|
|
|
|
|
|
|
func hash(c string) string {
|
|
|
|
h := sha256.New()
|
|
|
|
fmt.Fprint(h, c)
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2014-11-13 12:57:28 -05:00
|
|
|
func testMountMoreThan42Layers(t *testing.T, mountPath string) {
|
|
|
|
if err := os.MkdirAll(mountPath, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll(mountPath)
|
2014-11-20 12:39:08 -05:00
|
|
|
d := testInit(mountPath, t).(*Driver)
|
2013-11-26 13:50:53 -05:00
|
|
|
defer d.Cleanup()
|
|
|
|
var last string
|
|
|
|
var expected int
|
|
|
|
|
|
|
|
for i := 1; i < 127; i++ {
|
|
|
|
expected++
|
|
|
|
var (
|
|
|
|
parent = fmt.Sprintf("%d", i-1)
|
|
|
|
current = fmt.Sprintf("%d", i)
|
|
|
|
)
|
|
|
|
|
|
|
|
if parent == "0" {
|
|
|
|
parent = ""
|
|
|
|
} else {
|
|
|
|
parent = hash(parent)
|
|
|
|
}
|
|
|
|
current = hash(current)
|
|
|
|
|
2017-09-11 14:55:05 -04:00
|
|
|
err := d.CreateReadWrite(current, parent, nil)
|
|
|
|
require.NoError(t, err, "current layer %d", i)
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
point, err := driverGet(d, current, "")
|
2017-09-11 14:55:05 -04:00
|
|
|
require.NoError(t, err, "current layer %d", i)
|
|
|
|
|
2013-11-26 13:50:53 -05:00
|
|
|
f, err := os.Create(path.Join(point, current))
|
2017-09-11 14:55:05 -04:00
|
|
|
require.NoError(t, err, "current layer %d", i)
|
2013-11-26 13:50:53 -05:00
|
|
|
f.Close()
|
|
|
|
|
|
|
|
if i%10 == 0 {
|
2017-09-11 14:55:05 -04:00
|
|
|
err := os.Remove(path.Join(point, parent))
|
|
|
|
require.NoError(t, err, "current layer %d", i)
|
2013-11-26 13:50:53 -05:00
|
|
|
expected--
|
|
|
|
}
|
|
|
|
last = current
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the actual mount for the top most image
|
2017-08-03 20:22:00 -04:00
|
|
|
point, err := driverGet(d, last, "")
|
2017-09-11 14:55:05 -04:00
|
|
|
require.NoError(t, err)
|
2013-11-26 13:50:53 -05:00
|
|
|
files, err := ioutil.ReadDir(point)
|
2017-09-11 14:55:05 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, files, expected)
|
2013-11-26 13:50:53 -05:00
|
|
|
}
|
2014-11-13 12:57:28 -05:00
|
|
|
|
|
|
|
func TestMountMoreThan42Layers(t *testing.T) {
|
2017-09-11 14:55:05 -04:00
|
|
|
defer os.RemoveAll(tmpOuter)
|
2014-11-13 12:57:28 -05:00
|
|
|
testMountMoreThan42Layers(t, tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMountMoreThan42LayersMatchingPathLength(t *testing.T) {
|
2014-11-20 12:39:08 -05:00
|
|
|
defer os.RemoveAll(tmpOuter)
|
|
|
|
zeroes := "0"
|
2014-11-13 12:57:28 -05:00
|
|
|
for {
|
|
|
|
// This finds a mount path so that when combined into aufs mount options
|
|
|
|
// 4096 byte boundary would be in between the paths or in permission
|
2014-11-20 12:39:08 -05:00
|
|
|
// section. For '/tmp' it will use '/tmp/aufs-tests/00000000/aufs'
|
|
|
|
mountPath := path.Join(tmpOuter, zeroes, "aufs")
|
2014-11-13 12:57:28 -05:00
|
|
|
pathLength := 77 + len(mountPath)
|
|
|
|
|
|
|
|
if mod := 4095 % pathLength; mod == 0 || mod > pathLength-2 {
|
|
|
|
t.Logf("Using path: %s", mountPath)
|
|
|
|
testMountMoreThan42Layers(t, mountPath)
|
|
|
|
return
|
|
|
|
}
|
2014-11-20 12:39:08 -05:00
|
|
|
zeroes += "0"
|
2014-11-13 12:57:28 -05:00
|
|
|
}
|
|
|
|
}
|
2016-02-12 10:20:16 -05:00
|
|
|
|
|
|
|
func BenchmarkConcurrentAccess(b *testing.B) {
|
|
|
|
b.StopTimer()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
d := newDriver(b)
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
defer d.Cleanup()
|
|
|
|
|
2017-01-16 23:45:27 -05:00
|
|
|
numConcurrent := 256
|
2016-02-12 10:20:16 -05:00
|
|
|
// create a bunch of ids
|
|
|
|
var ids []string
|
2017-01-16 23:45:27 -05:00
|
|
|
for i := 0; i < numConcurrent; i++ {
|
2016-02-12 10:20:16 -05:00
|
|
|
ids = append(ids, stringid.GenerateNonCryptoID())
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create(ids[0], "", nil); err != nil {
|
2016-02-12 10:20:16 -05:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create(ids[1], ids[0], nil); err != nil {
|
2016-02-12 10:20:16 -05:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parent := ids[1]
|
|
|
|
ids = append(ids[2:])
|
|
|
|
|
2017-01-16 23:45:27 -05:00
|
|
|
chErr := make(chan error, numConcurrent)
|
2016-02-12 10:20:16 -05:00
|
|
|
var outerGroup sync.WaitGroup
|
|
|
|
outerGroup.Add(len(ids))
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
// here's the actual bench
|
|
|
|
for _, id := range ids {
|
|
|
|
go func(id string) {
|
|
|
|
defer outerGroup.Done()
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := d.Create(id, parent, nil); err != nil {
|
2016-02-12 10:20:16 -05:00
|
|
|
b.Logf("Create %s failed", id)
|
|
|
|
chErr <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var innerGroup sync.WaitGroup
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
innerGroup.Add(1)
|
|
|
|
go func() {
|
|
|
|
d.Get(id, "")
|
|
|
|
d.Put(id)
|
|
|
|
innerGroup.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
innerGroup.Wait()
|
|
|
|
d.Remove(id)
|
|
|
|
}(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
outerGroup.Wait()
|
|
|
|
b.StopTimer()
|
|
|
|
close(chErr)
|
|
|
|
for err := range chErr {
|
|
|
|
if err != nil {
|
|
|
|
b.Log(err)
|
|
|
|
b.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-21 17:38:13 -04:00
|
|
|
|
|
|
|
func TestInitStaleCleanup(t *testing.T) {
|
|
|
|
if err := os.MkdirAll(tmp, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
for _, d := range []string{"diff", "mnt"} {
|
|
|
|
if err := os.MkdirAll(filepath.Join(tmp, d, "123-removing"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testInit(tmp, t)
|
|
|
|
for _, d := range []string{"diff", "mnt"} {
|
|
|
|
if _, err := os.Stat(filepath.Join(tmp, d, "123-removing")); err == nil {
|
|
|
|
t.Fatal("cleanup failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|