Unit test for getOrphan
@@ -10,6 +10,7 @@ import (
"syscall"
"testing"
+ "github.com/docker/docker/pkg/stringid"
"github.com/opencontainers/go-digest"
)
@@ -102,3 +103,50 @@ func TestStartTransactionFailure(t *testing.T) {
t.Fatal(err)
}
}
+
+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)
+ if err := ioutil.WriteFile(filepath.Join(layerPath, "cache-id"), []byte(stringid.GenerateRandomID()), 0644); err != nil {
+ 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")
+ }
+}
