2018-02-05 21:05:59 +00:00
|
|
|
package layer // import "github.com/docker/docker/layer"
|
2015-11-18 22:15:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-10 11:48:47 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2022-03-04 13:49:42 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2015-11-18 22:15:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func randomLayerID(seed int64) ChainID {
|
|
|
|
r := rand.New(rand.NewSource(seed))
|
|
|
|
|
2015-12-08 19:14:02 +00:00
|
|
|
return ChainID(digest.FromBytes([]byte(fmt.Sprintf("%d", r.Int63()))))
|
2015-11-18 22:15:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newFileMetadataStore(t *testing.T) (*fileMetadataStore, string, func()) {
|
2021-08-24 10:10:50 +00:00
|
|
|
td, err := os.MkdirTemp("", "layers-")
|
2015-11-18 22:15:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-05 23:46:21 +00:00
|
|
|
fms, err := newFSMetadataStore(td)
|
2015-11-18 22:15:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-05 23:46:21 +00:00
|
|
|
return fms, td, func() {
|
2015-11-18 22:15:00 +00:00
|
|
|
if err := os.RemoveAll(td); err != nil {
|
|
|
|
t.Logf("Failed to cleanup %q: %s", td, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNotDirectoryError(t *testing.T, err error) {
|
|
|
|
perr, ok := err.(*os.PathError)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unexpected error %#v, expected path error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if perr.Err != syscall.ENOTDIR {
|
|
|
|
t.Fatalf("Unexpected error %s, expected %s", perr.Err, syscall.ENOTDIR)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommitFailure(t *testing.T) {
|
|
|
|
fms, td, cleanup := newFileMetadataStore(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(td, "sha256"), []byte("was here first!"), 0644); err != nil {
|
2015-11-18 22:15:00 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := fms.StartTransaction()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.SetSize(0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit(randomLayerID(5))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error committing with invalid layer parent directory")
|
|
|
|
}
|
|
|
|
assertNotDirectoryError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStartTransactionFailure(t *testing.T) {
|
|
|
|
fms, td, cleanup := newFileMetadataStore(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(td, "tmp"), []byte("was here first!"), 0644); err != nil {
|
2015-11-18 22:15:00 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := fms.StartTransaction()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error starting transaction with invalid layer parent directory")
|
|
|
|
}
|
|
|
|
assertNotDirectoryError(t, err)
|
|
|
|
|
|
|
|
if err := os.Remove(filepath.Join(td, "tmp")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := fms.StartTransaction()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected := filepath.Join(td, "tmp"); strings.HasPrefix(expected, tx.String()) {
|
|
|
|
t.Fatalf("Unexpected transaction string %q, expected prefix %q", tx.String(), expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Cancel(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2019-08-10 11:48:47 +00:00
|
|
|
|
|
|
|
func TestGetOrphan(t *testing.T) {
|
|
|
|
fms, td, cleanup := newFileMetadataStore(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
layerRoot := filepath.Join(td, "sha256")
|
|
|
|
if err := os.MkdirAll(layerRoot, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := fms.StartTransaction()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
layerid := randomLayerID(5)
|
|
|
|
err = tx.Commit(layerid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
layerPath := fms.getLayerDirectory(layerid)
|
2021-08-24 10:10:50 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(layerPath, "cache-id"), []byte(stringid.GenerateRandomID()), 0644); err != nil {
|
2019-08-10 11:48:47 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
orphanLayers, err := fms.getOrphan()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(orphanLayers) != 0 {
|
|
|
|
t.Fatalf("Expected to have zero orphan layers")
|
|
|
|
}
|
|
|
|
|
|
|
|
layeridSplit := strings.Split(layerid.String(), ":")
|
|
|
|
newPath := filepath.Join(layerRoot, fmt.Sprintf("%s-%s-removing", layeridSplit[1], stringid.GenerateRandomID()))
|
|
|
|
err = os.Rename(layerPath, newPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
orphanLayers, err = fms.getOrphan()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(orphanLayers) != 1 {
|
|
|
|
t.Fatalf("Expected to have one orphan layer")
|
|
|
|
}
|
|
|
|
}
|