2018-02-05 16:05:59 -05:00
|
|
|
package tarsum // import "github.com/docker/docker/pkg/tarsum"
|
2014-08-21 16:12:52 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-06-02 03:08:01 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 16:12:52 -04:00
|
|
|
func TestVersion(t *testing.T) {
|
|
|
|
expected := "tarsum"
|
|
|
|
var v Version
|
|
|
|
if v.String() != expected {
|
|
|
|
t.Errorf("expected %q, got %q", expected, v.String())
|
|
|
|
}
|
|
|
|
|
2014-10-30 16:47:31 -04:00
|
|
|
expected = "tarsum.v1"
|
2014-08-21 16:12:52 -04:00
|
|
|
v = 1
|
|
|
|
if v.String() != expected {
|
|
|
|
t.Errorf("expected %q, got %q", expected, v.String())
|
|
|
|
}
|
2014-10-30 16:47:31 -04:00
|
|
|
|
|
|
|
expected = "tarsum.dev"
|
|
|
|
v = 2
|
|
|
|
if v.String() != expected {
|
|
|
|
t.Errorf("expected %q, got %q", expected, v.String())
|
|
|
|
}
|
2014-08-21 16:12:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetVersion(t *testing.T) {
|
|
|
|
testSet := []struct {
|
|
|
|
Str string
|
|
|
|
Expected Version
|
|
|
|
}{
|
|
|
|
{"tarsum+sha256:e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b", Version0},
|
|
|
|
{"tarsum+sha256", Version0},
|
|
|
|
{"tarsum", Version0},
|
|
|
|
{"tarsum.dev", VersionDev},
|
|
|
|
{"tarsum.dev+sha256:deadbeef", VersionDev},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ts := range testSet {
|
|
|
|
v, err := GetVersionFromTarsum(ts.Str)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%q : %s", err, ts.Str)
|
|
|
|
}
|
|
|
|
if v != ts.Expected {
|
|
|
|
t.Errorf("expected %d (%q), got %d (%q)", ts.Expected, ts.Expected, v, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test one that does not exist, to ensure it errors
|
|
|
|
str := "weak+md5:abcdeabcde"
|
|
|
|
_, err := GetVersionFromTarsum(str)
|
|
|
|
if err != ErrNotVersion {
|
|
|
|
t.Fatalf("%q : %s", err, str)
|
|
|
|
}
|
|
|
|
}
|
2015-06-02 03:08:01 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|