2018-02-05 21:05:59 +00:00
|
|
|
package graphtest // import "github.com/docker/docker/daemon/graphdriver/graphtest"
|
2016-04-15 01:14:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-09-23 00:59:58 +00:00
|
|
|
"io/fs"
|
2016-04-15 01:14:42 +00:00
|
|
|
"math/rand"
|
|
|
|
"os"
|
2022-09-23 00:49:47 +00:00
|
|
|
"path/filepath"
|
2016-04-15 01:14:42 +00:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
)
|
|
|
|
|
|
|
|
func randomContent(size int, seed int64) []byte {
|
|
|
|
s := rand.NewSource(seed)
|
|
|
|
content := make([]byte, size)
|
|
|
|
|
|
|
|
for i := 0; i < len(content); i += 7 {
|
|
|
|
val := s.Int63()
|
|
|
|
for j := 0; i+j < len(content) && j < 7; j++ {
|
|
|
|
content[i+j] = byte(val)
|
|
|
|
val >>= 8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
|
|
|
func addFiles(drv graphdriver.Driver, layer string, seed int64) error {
|
|
|
|
root, err := drv.Get(layer, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer drv.Put(layer)
|
|
|
|
|
2022-01-20 12:59:20 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(root, "file-a"), randomContent(64, seed), 0o755); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-20 12:59:20 +00:00
|
|
|
if err := os.MkdirAll(filepath.Join(root, "dir-b"), 0o755); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-20 12:59:20 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(root, "dir-b", "file-b"), randomContent(128, seed+1), 0o755); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-20 12:59:20 +00:00
|
|
|
return os.WriteFile(filepath.Join(root, "file-c"), randomContent(128*128, seed+2), 0o755)
|
2016-04-15 01:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkFile(drv graphdriver.Driver, layer, filename string, content []byte) error {
|
|
|
|
root, err := drv.Get(layer, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer drv.Put(layer)
|
|
|
|
|
2022-09-23 18:09:51 +00:00
|
|
|
fileContent, err := os.ReadFile(filepath.Join(root, filename))
|
2016-04-15 01:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-06 08:54:24 +00:00
|
|
|
if !bytes.Equal(fileContent, content) {
|
2016-04-15 01:14:42 +00:00
|
|
|
return fmt.Errorf("mismatched file content %v, expecting %v", fileContent, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addFile(drv graphdriver.Driver, layer, filename string, content []byte) error {
|
|
|
|
root, err := drv.Get(layer, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer drv.Put(layer)
|
|
|
|
|
2022-01-20 12:59:20 +00:00
|
|
|
return os.WriteFile(filepath.Join(root, filename), content, 0o755)
|
2016-04-15 01:14:42 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 18:56:38 +00:00
|
|
|
func addDirectory(drv graphdriver.Driver, layer, dir string) error {
|
2016-06-23 20:34:38 +00:00
|
|
|
root, err := drv.Get(layer, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer drv.Put(layer)
|
|
|
|
|
2022-01-20 12:59:20 +00:00
|
|
|
return os.MkdirAll(filepath.Join(root, dir), 0o755)
|
2016-07-26 18:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeAll(drv graphdriver.Driver, layer string, names ...string) error {
|
|
|
|
root, err := drv.Get(layer, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer drv.Put(layer)
|
|
|
|
|
|
|
|
for _, filename := range names {
|
2022-09-23 18:09:51 +00:00
|
|
|
if err := os.RemoveAll(filepath.Join(root, filename)); err != nil {
|
2016-07-26 18:56:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2016-06-23 20:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkFileRemoved(drv graphdriver.Driver, layer, filename string) error {
|
|
|
|
root, err := drv.Get(layer, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer drv.Put(layer)
|
|
|
|
|
2022-09-23 18:09:51 +00:00
|
|
|
if _, err := os.Stat(filepath.Join(root, filename)); err == nil {
|
|
|
|
return fmt.Errorf("file still exists: %s", filepath.Join(root, filename))
|
2016-06-23 20:34:38 +00:00
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-15 01:14:42 +00:00
|
|
|
func addManyFiles(drv graphdriver.Driver, layer string, count int, seed int64) error {
|
|
|
|
root, err := drv.Get(layer, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer drv.Put(layer)
|
|
|
|
|
|
|
|
for i := 0; i < count; i += 100 {
|
2022-09-23 18:09:51 +00:00
|
|
|
dir := filepath.Join(root, fmt.Sprintf("directory-%d", i))
|
2022-01-20 12:59:20 +00:00
|
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for j := 0; i+j < count && j < 100; j++ {
|
2022-09-23 00:49:47 +00:00
|
|
|
file := filepath.Join(dir, fmt.Sprintf("file-%d", i+j))
|
2022-01-20 12:59:20 +00:00
|
|
|
if err := os.WriteFile(file, randomContent(64, seed+int64(i+j)), 0o755); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func changeManyFiles(drv graphdriver.Driver, layer string, count int, seed int64) ([]archive.Change, error) {
|
|
|
|
root, err := drv.Get(layer, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer drv.Put(layer)
|
|
|
|
|
2018-05-19 11:38:54 +00:00
|
|
|
var changes []archive.Change
|
2016-04-15 01:14:42 +00:00
|
|
|
for i := 0; i < count; i += 100 {
|
|
|
|
archiveRoot := fmt.Sprintf("/directory-%d", i)
|
2022-01-20 12:59:20 +00:00
|
|
|
if err := os.MkdirAll(filepath.Join(root, archiveRoot), 0o755); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for j := 0; i+j < count && j < 100; j++ {
|
|
|
|
if j == 0 {
|
|
|
|
changes = append(changes, archive.Change{
|
|
|
|
Path: archiveRoot,
|
|
|
|
Kind: archive.ChangeModify,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
var change archive.Change
|
|
|
|
switch j % 3 {
|
|
|
|
// Update file
|
|
|
|
case 0:
|
2022-09-23 00:49:47 +00:00
|
|
|
change.Path = filepath.Join(archiveRoot, fmt.Sprintf("file-%d", i+j))
|
2016-04-15 01:14:42 +00:00
|
|
|
change.Kind = archive.ChangeModify
|
2022-01-20 12:59:20 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(root, change.Path), randomContent(64, seed+int64(i+j)), 0o755); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Add file
|
|
|
|
case 1:
|
2022-09-23 00:49:47 +00:00
|
|
|
change.Path = filepath.Join(archiveRoot, fmt.Sprintf("file-%d-%d", seed, i+j))
|
2016-04-15 01:14:42 +00:00
|
|
|
change.Kind = archive.ChangeAdd
|
2022-01-20 12:59:20 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(root, change.Path), randomContent(64, seed+int64(i+j)), 0o755); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Remove file
|
|
|
|
case 2:
|
2022-09-23 00:49:47 +00:00
|
|
|
change.Path = filepath.Join(archiveRoot, fmt.Sprintf("file-%d", i+j))
|
2016-04-15 01:14:42 +00:00
|
|
|
change.Kind = archive.ChangeDelete
|
2022-09-23 18:09:51 +00:00
|
|
|
if err := os.Remove(filepath.Join(root, change.Path)); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
changes = append(changes, change)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkManyFiles(drv graphdriver.Driver, layer string, count int, seed int64) error {
|
|
|
|
root, err := drv.Get(layer, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer drv.Put(layer)
|
|
|
|
|
|
|
|
for i := 0; i < count; i += 100 {
|
2022-09-23 18:09:51 +00:00
|
|
|
dir := filepath.Join(root, fmt.Sprintf("directory-%d", i))
|
2016-04-15 01:14:42 +00:00
|
|
|
for j := 0; i+j < count && j < 100; j++ {
|
2022-09-23 00:49:47 +00:00
|
|
|
file := filepath.Join(dir, fmt.Sprintf("file-%d", i+j))
|
2022-09-23 00:59:58 +00:00
|
|
|
fileContent, err := os.ReadFile(file)
|
2016-04-15 01:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
content := randomContent(64, seed+int64(i+j))
|
|
|
|
|
2017-09-06 08:54:24 +00:00
|
|
|
if !bytes.Equal(fileContent, content) {
|
2016-04-15 01:14:42 +00:00
|
|
|
return fmt.Errorf("mismatched file content %v, expecting %v", fileContent, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type changeList []archive.Change
|
|
|
|
|
|
|
|
func (c changeList) Less(i, j int) bool {
|
|
|
|
if c[i].Path == c[j].Path {
|
|
|
|
return c[i].Kind < c[j].Kind
|
|
|
|
}
|
|
|
|
return c[i].Path < c[j].Path
|
|
|
|
}
|
|
|
|
func (c changeList) Len() int { return len(c) }
|
|
|
|
func (c changeList) Swap(i, j int) { c[j], c[i] = c[i], c[j] }
|
|
|
|
|
|
|
|
func checkChanges(expected, actual []archive.Change) error {
|
|
|
|
if len(expected) != len(actual) {
|
|
|
|
return fmt.Errorf("unexpected number of changes, expected %d, got %d", len(expected), len(actual))
|
|
|
|
}
|
|
|
|
sort.Sort(changeList(expected))
|
|
|
|
sort.Sort(changeList(actual))
|
|
|
|
|
|
|
|
for i := range expected {
|
|
|
|
if expected[i] != actual[i] {
|
|
|
|
return fmt.Errorf("unexpected change, expecting %v, got %v", expected[i], actual[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addLayerFiles(drv graphdriver.Driver, layer, parent string, i int) error {
|
|
|
|
root, err := drv.Get(layer, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer drv.Put(layer)
|
|
|
|
|
2022-01-20 12:59:20 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(root, "top-id"), []byte(layer), 0o755); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-09-23 18:09:51 +00:00
|
|
|
layerDir := filepath.Join(root, fmt.Sprintf("layer-%d", i))
|
2022-01-20 12:59:20 +00:00
|
|
|
if err := os.MkdirAll(layerDir, 0o755); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-20 12:59:20 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(layerDir, "layer-id"), []byte(layer), 0o755); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-20 12:59:20 +00:00
|
|
|
return os.WriteFile(filepath.Join(layerDir, "parent-id"), []byte(parent), 0o755)
|
2016-04-15 01:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addManyLayers(drv graphdriver.Driver, baseLayer string, count int) (string, error) {
|
|
|
|
lastLayer := baseLayer
|
|
|
|
for i := 1; i <= count; i++ {
|
|
|
|
nextLayer := stringid.GenerateRandomID()
|
2016-11-09 20:59:58 +00:00
|
|
|
if err := drv.Create(nextLayer, lastLayer, nil); err != nil {
|
2016-04-15 01:14:42 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := addLayerFiles(drv, nextLayer, lastLayer, i); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastLayer = nextLayer
|
|
|
|
}
|
|
|
|
return lastLayer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkManyLayers(drv graphdriver.Driver, layer string, count int) error {
|
|
|
|
root, err := drv.Get(layer, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer drv.Put(layer)
|
|
|
|
|
2022-09-23 18:09:51 +00:00
|
|
|
layerIDBytes, err := os.ReadFile(filepath.Join(root, "top-id"))
|
2016-04-15 01:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-06 08:54:24 +00:00
|
|
|
if !bytes.Equal(layerIDBytes, []byte(layer)) {
|
2016-04-15 01:14:42 +00:00
|
|
|
return fmt.Errorf("mismatched file content %v, expecting %v", layerIDBytes, []byte(layer))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := count; i > 0; i-- {
|
2022-09-23 18:09:51 +00:00
|
|
|
layerDir := filepath.Join(root, fmt.Sprintf("layer-%d", i))
|
2016-04-15 01:14:42 +00:00
|
|
|
|
2022-09-23 00:59:58 +00:00
|
|
|
thisLayerIDBytes, err := os.ReadFile(filepath.Join(layerDir, "layer-id"))
|
2016-04-15 01:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-06 08:54:24 +00:00
|
|
|
if !bytes.Equal(thisLayerIDBytes, layerIDBytes) {
|
2016-04-15 01:14:42 +00:00
|
|
|
return fmt.Errorf("mismatched file content %v, expecting %v", thisLayerIDBytes, layerIDBytes)
|
|
|
|
}
|
2022-09-23 00:59:58 +00:00
|
|
|
layerIDBytes, err = os.ReadFile(filepath.Join(layerDir, "parent-id"))
|
2016-04-15 01:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-23 00:59:58 +00:00
|
|
|
// readDir reads a directory just like os.ReadDir()
|
2016-04-15 01:14:42 +00:00
|
|
|
// then hides specific files (currently "lost+found")
|
|
|
|
// so the tests don't "see" it
|
2022-09-23 00:59:58 +00:00
|
|
|
func readDir(dir string) ([]fs.DirEntry, error) {
|
|
|
|
a, err := os.ReadDir(dir)
|
2016-04-15 01:14:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := a[:0]
|
|
|
|
for _, x := range a {
|
|
|
|
if x.Name() != "lost+found" { // ext4 always have this dir
|
|
|
|
b = append(b, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|