gitee.com/bomy/docker.git@v1.13.1/pkg/tarsum/fileinfosums_test.go (about)

     1  package tarsum
     2  
     3  import "testing"
     4  
     5  func newFileInfoSums() FileInfoSums {
     6  	return FileInfoSums{
     7  		fileInfoSum{name: "file3", sum: "2abcdef1234567890", pos: 2},
     8  		fileInfoSum{name: "dup1", sum: "deadbeef1", pos: 5},
     9  		fileInfoSum{name: "file1", sum: "0abcdef1234567890", pos: 0},
    10  		fileInfoSum{name: "file4", sum: "3abcdef1234567890", pos: 3},
    11  		fileInfoSum{name: "dup1", sum: "deadbeef0", pos: 4},
    12  		fileInfoSum{name: "file2", sum: "1abcdef1234567890", pos: 1},
    13  	}
    14  }
    15  
    16  func TestSortFileInfoSums(t *testing.T) {
    17  	dups := newFileInfoSums().GetAllFile("dup1")
    18  	if len(dups) != 2 {
    19  		t.Errorf("expected length 2, got %d", len(dups))
    20  	}
    21  	dups.SortByNames()
    22  	if dups[0].Pos() != 4 {
    23  		t.Errorf("sorted dups should be ordered by position. Expected 4, got %d", dups[0].Pos())
    24  	}
    25  
    26  	fis := newFileInfoSums()
    27  	expected := "0abcdef1234567890"
    28  	fis.SortBySums()
    29  	got := fis[0].Sum()
    30  	if got != expected {
    31  		t.Errorf("Expected %q, got %q", expected, got)
    32  	}
    33  
    34  	fis = newFileInfoSums()
    35  	expected = "dup1"
    36  	fis.SortByNames()
    37  	gotFis := fis[0]
    38  	if gotFis.Name() != expected {
    39  		t.Errorf("Expected %q, got %q", expected, gotFis.Name())
    40  	}
    41  	// since a duplicate is first, ensure it is ordered first by position too
    42  	if gotFis.Pos() != 4 {
    43  		t.Errorf("Expected %d, got %d", 4, gotFis.Pos())
    44  	}
    45  
    46  	fis = newFileInfoSums()
    47  	fis.SortByPos()
    48  	if fis[0].Pos() != 0 {
    49  		t.Errorf("sorted fileInfoSums by Pos should order them by position.")
    50  	}
    51  
    52  	fis = newFileInfoSums()
    53  	expected = "deadbeef1"
    54  	gotFileInfoSum := fis.GetFile("dup1")
    55  	if gotFileInfoSum.Sum() != expected {
    56  		t.Errorf("Expected %q, got %q", expected, gotFileInfoSum)
    57  	}
    58  	if fis.GetFile("noPresent") != nil {
    59  		t.Errorf("Should have return nil if name not found.")
    60  	}
    61  
    62  }