Add test coverage for pkg/tarsum
And removing unused code. - tarsum.go : NewTarSumHash could be non exported (for now) NewTarSumForLabel is never used, except for the tests - fileinfosums.go: SortByPos is never used, except for the tests - versionning.go: GetVersions is never used, expect for the tests Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
parent
b538852b61
commit
e4059984de
5 changed files with 199 additions and 9 deletions
63
tarsum/builder_context_test.go
Normal file
63
tarsum/builder_context_test.go
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
package tarsum
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Try to remove tarsum (in the BuilderContext) that do not exists, won't change a thing
|
||||||
|
func TestTarSumRemoveNonExistent(t *testing.T) {
|
||||||
|
filename := "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar"
|
||||||
|
reader, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ts, err := NewTarSum(reader, false, Version0)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read and discard bytes so that it populates sums
|
||||||
|
_, err = io.Copy(ioutil.Discard, ts)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to read from %s: %s", filename, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := len(ts.GetSums())
|
||||||
|
|
||||||
|
ts.(BuilderContext).Remove("")
|
||||||
|
ts.(BuilderContext).Remove("Anything")
|
||||||
|
|
||||||
|
if len(ts.GetSums()) != expected {
|
||||||
|
t.Fatalf("Expected %v sums, go %v.", expected, ts.GetSums())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove a tarsum (in the BuilderContext)
|
||||||
|
func TestTarSumRemove(t *testing.T) {
|
||||||
|
filename := "testdata/46af0962ab5afeb5ce6740d4d91652e69206fc991fd5328c1a94d364ad00e457/layer.tar"
|
||||||
|
reader, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ts, err := NewTarSum(reader, false, Version0)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read and discard bytes so that it populates sums
|
||||||
|
_, err = io.Copy(ioutil.Discard, ts)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to read from %s: %s", filename, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := len(ts.GetSums()) - 1
|
||||||
|
|
||||||
|
ts.(BuilderContext).Remove("etc/sudoers")
|
||||||
|
|
||||||
|
if len(ts.GetSums()) != expected {
|
||||||
|
t.Fatalf("Expected %v sums, go %v.", expected, len(ts.GetSums()))
|
||||||
|
}
|
||||||
|
}
|
|
@ -51,15 +51,6 @@ func (fis FileInfoSums) GetAllFile(name string) FileInfoSums {
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func contains(s []string, e string) bool {
|
|
||||||
for _, a := range s {
|
|
||||||
if a == e {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fis FileInfoSums) GetDuplicatePaths() (dups FileInfoSums) {
|
func (fis FileInfoSums) GetDuplicatePaths() (dups FileInfoSums) {
|
||||||
seen := make(map[string]int, len(fis)) // allocate earl. no need to grow this map.
|
seen := make(map[string]int, len(fis)) // allocate earl. no need to grow this map.
|
||||||
for i := range fis {
|
for i := range fis {
|
||||||
|
|
|
@ -42,4 +42,21 @@ func TestSortFileInfoSums(t *testing.T) {
|
||||||
if gotFis.Pos() != 4 {
|
if gotFis.Pos() != 4 {
|
||||||
t.Errorf("Expected %d, got %d", 4, gotFis.Pos())
|
t.Errorf("Expected %d, got %d", 4, gotFis.Pos())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fis = newFileInfoSums()
|
||||||
|
fis.SortByPos()
|
||||||
|
if fis[0].Pos() != 0 {
|
||||||
|
t.Errorf("sorted fileInfoSums by Pos should order them by position.")
|
||||||
|
}
|
||||||
|
|
||||||
|
fis = newFileInfoSums()
|
||||||
|
expected = "deadbeef1"
|
||||||
|
gotFileInfoSum := fis.GetFile("dup1")
|
||||||
|
if gotFileInfoSum.Sum() != expected {
|
||||||
|
t.Errorf("Expected %q, got %q", expected, gotFileInfoSum)
|
||||||
|
}
|
||||||
|
if fis.GetFile("noPresent") != nil {
|
||||||
|
t.Errorf("Should have return nil if name not found.")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -175,6 +176,45 @@ func emptyTarSum(gzip bool) (TarSum, error) {
|
||||||
return NewTarSum(reader, !gzip, Version0)
|
return NewTarSum(reader, !gzip, Version0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test errors on NewTarsumForLabel
|
||||||
|
func TestNewTarSumForLabelInvalid(t *testing.T) {
|
||||||
|
reader := strings.NewReader("")
|
||||||
|
|
||||||
|
if _, err := NewTarSumForLabel(reader, true, "invalidlabel"); err == nil {
|
||||||
|
t.Fatalf("Expected an error, got nothing.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := NewTarSumForLabel(reader, true, "invalid+sha256"); err == nil {
|
||||||
|
t.Fatalf("Expected an error, got nothing.")
|
||||||
|
}
|
||||||
|
if _, err := NewTarSumForLabel(reader, true, "tarsum.v1+invalid"); err == nil {
|
||||||
|
t.Fatalf("Expected an error, got nothing.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewTarSumForLabel(t *testing.T) {
|
||||||
|
|
||||||
|
layer := testLayers[0]
|
||||||
|
|
||||||
|
reader, err := os.Open(layer.filename)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
label := strings.Split(layer.tarsum, ":")[0]
|
||||||
|
ts, err := NewTarSumForLabel(reader, false, label)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure it actually worked by reading a little bit of it
|
||||||
|
nbByteToRead := 8 * 1024
|
||||||
|
dBuf := make([]byte, nbByteToRead)
|
||||||
|
_, err = ts.Read(dBuf)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to read %vKB from %s: %s", nbByteToRead, layer.filename, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestEmptyTar tests that tarsum does not fail to read an empty tar
|
// TestEmptyTar tests that tarsum does not fail to read an empty tar
|
||||||
// and correctly returns the hex digest of an empty hash.
|
// and correctly returns the hex digest of an empty hash.
|
||||||
func TestEmptyTar(t *testing.T) {
|
func TestEmptyTar(t *testing.T) {
|
||||||
|
@ -251,6 +291,33 @@ var (
|
||||||
sha512Hash = NewTHash("sha512", sha512.New)
|
sha512Hash = NewTHash("sha512", sha512.New)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Test all the build-in read size : buf8K, buf16K, buf32K and more
|
||||||
|
func TestTarSumsReadSize(t *testing.T) {
|
||||||
|
// Test always on the same layer (that is big enough)
|
||||||
|
layer := testLayers[0]
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
|
||||||
|
reader, err := os.Open(layer.filename)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ts, err := NewTarSum(reader, false, layer.version)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read and discard bytes so that it populates sums
|
||||||
|
nbByteToRead := (i + 1) * 8 * 1024
|
||||||
|
dBuf := make([]byte, nbByteToRead)
|
||||||
|
_, err = ts.Read(dBuf)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to read %vKB from %s: %s", nbByteToRead, layer.filename, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTarSums(t *testing.T) {
|
func TestTarSums(t *testing.T) {
|
||||||
for _, layer := range testLayers {
|
for _, layer := range testLayers {
|
||||||
var (
|
var (
|
||||||
|
@ -326,6 +393,15 @@ func TestTarSums(t *testing.T) {
|
||||||
if layer.tarsum != gotSum {
|
if layer.tarsum != gotSum {
|
||||||
t.Errorf("expecting [%s], but got [%s]", layer.tarsum, gotSum)
|
t.Errorf("expecting [%s], but got [%s]", layer.tarsum, gotSum)
|
||||||
}
|
}
|
||||||
|
var expectedHashName string
|
||||||
|
if layer.hash != nil {
|
||||||
|
expectedHashName = layer.hash.Name()
|
||||||
|
} else {
|
||||||
|
expectedHashName = DefaultTHash.Name()
|
||||||
|
}
|
||||||
|
if expectedHashName != ts.Hash().Name() {
|
||||||
|
t.Errorf("expecting hash [%v], but got [%s]", expectedHashName, ts.Hash().Name())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,25 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestVersionLabelForChecksum(t *testing.T) {
|
||||||
|
version := VersionLabelForChecksum("tarsum+sha256:deadbeef")
|
||||||
|
if version != "tarsum" {
|
||||||
|
t.Fatalf("Version should have been 'tarsum', was %v", version)
|
||||||
|
}
|
||||||
|
version = VersionLabelForChecksum("tarsum.v1+sha256:deadbeef")
|
||||||
|
if version != "tarsum.v1" {
|
||||||
|
t.Fatalf("Version should have been 'tarsum.v1', was %v", version)
|
||||||
|
}
|
||||||
|
version = VersionLabelForChecksum("something+somethingelse")
|
||||||
|
if version != "something" {
|
||||||
|
t.Fatalf("Version should have been 'something', was %v", version)
|
||||||
|
}
|
||||||
|
version = VersionLabelForChecksum("invalidChecksum")
|
||||||
|
if version != "" {
|
||||||
|
t.Fatalf("Version should have been empty, was %v", version)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestVersion(t *testing.T) {
|
func TestVersion(t *testing.T) {
|
||||||
expected := "tarsum"
|
expected := "tarsum"
|
||||||
var v Version
|
var v Version
|
||||||
|
@ -53,3 +72,27 @@ func TestGetVersion(t *testing.T) {
|
||||||
t.Fatalf("%q : %s", err, str)
|
t.Fatalf("%q : %s", err, str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetVersions(t *testing.T) {
|
||||||
|
expected := []Version{
|
||||||
|
Version0,
|
||||||
|
Version1,
|
||||||
|
VersionDev,
|
||||||
|
}
|
||||||
|
versions := GetVersions()
|
||||||
|
if len(versions) != len(expected) {
|
||||||
|
t.Fatalf("Expected %v versions, got %v", len(expected), len(versions))
|
||||||
|
}
|
||||||
|
if !containsVersion(versions, expected[0]) || !containsVersion(versions, expected[1]) || !containsVersion(versions, expected[2]) {
|
||||||
|
t.Fatalf("Expected [%v], got [%v]", expected, versions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsVersion(versions []Version, version Version) bool {
|
||||||
|
for _, v := range versions {
|
||||||
|
if v == version {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue