Skip to content

Commit

Permalink
Unit test for getOrphan
Browse files Browse the repository at this point in the history
Signed-off-by: Olli Janatuinen <[email protected]>
  • Loading branch information
olljanat committed Aug 10, 2019
1 parent c5c11f9 commit 8660330
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 8660330

Please sign in to comment.