Merge pull request #39715 from olljanat/getorplan-unit-test

Unit test for getOrphan
This commit is contained in:
Sebastiaan van Stijn 2019-09-16 13:53:27 +02:00 committed by GitHub
commit e894aae2a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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