github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/layer/layer_unix_test.go (about)

     1  // +build !windows
     2  
     3  package layer // import "github.com/demonoid81/moby/layer"
     4  
     5  import (
     6  	"testing"
     7  )
     8  
     9  func graphDiffSize(ls Store, l Layer) (int64, error) {
    10  	cl := getCachedLayer(l)
    11  	var parent string
    12  	if cl.parent != nil {
    13  		parent = cl.parent.cacheID
    14  	}
    15  	return ls.(*layerStore).driver.DiffSize(cl.cacheID, parent)
    16  }
    17  
    18  // Unix as Windows graph driver does not support Changes which is indirectly
    19  // invoked by calling DiffSize on the driver
    20  func TestLayerSize(t *testing.T) {
    21  	ls, _, cleanup := newTestStore(t)
    22  	defer cleanup()
    23  
    24  	content1 := []byte("Base contents")
    25  	content2 := []byte("Added contents")
    26  
    27  	layer1, err := createLayer(ls, "", initWithFiles(newTestFile("file1", content1, 0644)))
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	layer2, err := createLayer(ls, layer1.ChainID(), initWithFiles(newTestFile("file2", content2, 0644)))
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	layer1DiffSize, err := graphDiffSize(ls, layer1)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	if int(layer1DiffSize) != len(content1) {
    43  		t.Fatalf("Unexpected diff size %d, expected %d", layer1DiffSize, len(content1))
    44  	}
    45  
    46  	layer1Size, err := layer1.Size()
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	if expected := len(content1); int(layer1Size) != expected {
    52  		t.Fatalf("Unexpected size %d, expected %d", layer1Size, expected)
    53  	}
    54  
    55  	layer2DiffSize, err := graphDiffSize(ls, layer2)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	if int(layer2DiffSize) != len(content2) {
    61  		t.Fatalf("Unexpected diff size %d, expected %d", layer2DiffSize, len(content2))
    62  	}
    63  
    64  	layer2Size, err := layer2.Size()
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	if expected := len(content1) + len(content2); int(layer2Size) != expected {
    70  		t.Fatalf("Unexpected size %d, expected %d", layer2Size, expected)
    71  	}
    72  
    73  }