github.com/jingleWang/moby@v1.13.1/pkg/tarsum/builder_context_test.go (about)

     1  package tarsum
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  // Try to remove tarsum (in the BuilderContext) that do not exists, won't change a thing
    11  func TestTarSumRemoveNonExistent(t *testing.T) {
    12  	filename := "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar"
    13  	reader, err := os.Open(filename)
    14  	if err != nil {
    15  		t.Fatal(err)
    16  	}
    17  	defer reader.Close()
    18  
    19  	ts, err := NewTarSum(reader, false, Version0)
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  
    24  	// Read and discard bytes so that it populates sums
    25  	_, err = io.Copy(ioutil.Discard, ts)
    26  	if err != nil {
    27  		t.Errorf("failed to read from %s: %s", filename, err)
    28  	}
    29  
    30  	expected := len(ts.GetSums())
    31  
    32  	ts.(BuilderContext).Remove("")
    33  	ts.(BuilderContext).Remove("Anything")
    34  
    35  	if len(ts.GetSums()) != expected {
    36  		t.Fatalf("Expected %v sums, go %v.", expected, ts.GetSums())
    37  	}
    38  }
    39  
    40  // Remove a tarsum (in the BuilderContext)
    41  func TestTarSumRemove(t *testing.T) {
    42  	filename := "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar"
    43  	reader, err := os.Open(filename)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	defer reader.Close()
    48  
    49  	ts, err := NewTarSum(reader, false, Version0)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	// Read and discard bytes so that it populates sums
    55  	_, err = io.Copy(ioutil.Discard, ts)
    56  	if err != nil {
    57  		t.Errorf("failed to read from %s: %s", filename, err)
    58  	}
    59  
    60  	expected := len(ts.GetSums()) - 1
    61  
    62  	ts.(BuilderContext).Remove("etc/sudoers")
    63  
    64  	if len(ts.GetSums()) != expected {
    65  		t.Fatalf("Expected %v sums, go %v.", expected, len(ts.GetSums()))
    66  	}
    67  }