Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Unit test for getOrphan
Browse files Browse the repository at this point in the history
Signed-off-by: Olli Janatuinen <olli.janatuinen@gmail.com>
(cherry picked from commit 8660330)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
olljanat authored and thaJeztah committed Jan 25, 2020
1 parent 59c7e59 commit 43abde3
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions layer/filestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"syscall"
"testing"

"github.com/docker/docker/pkg/stringid"
"github.com/opencontainers/go-digest"
)

Expand Down Expand Up @@ -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")
}
}

0 comments on commit 43abde3

Please sign in to comment.