github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/tarsum/versioning_test.go (about)

     1  package tarsum // import "github.com/demonoid81/moby/pkg/tarsum"
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestVersionLabelForChecksum(t *testing.T) {
     8  	version := VersionLabelForChecksum("tarsum+sha256:deadbeef")
     9  	if version != "tarsum" {
    10  		t.Fatalf("Version should have been 'tarsum', was %v", version)
    11  	}
    12  	version = VersionLabelForChecksum("tarsum.v1+sha256:deadbeef")
    13  	if version != "tarsum.v1" {
    14  		t.Fatalf("Version should have been 'tarsum.v1', was %v", version)
    15  	}
    16  	version = VersionLabelForChecksum("something+somethingelse")
    17  	if version != "something" {
    18  		t.Fatalf("Version should have been 'something', was %v", version)
    19  	}
    20  	version = VersionLabelForChecksum("invalidChecksum")
    21  	if version != "" {
    22  		t.Fatalf("Version should have been empty, was %v", version)
    23  	}
    24  }
    25  
    26  func TestVersion(t *testing.T) {
    27  	expected := "tarsum"
    28  	var v Version
    29  	if v.String() != expected {
    30  		t.Errorf("expected %q, got %q", expected, v.String())
    31  	}
    32  
    33  	expected = "tarsum.v1"
    34  	v = 1
    35  	if v.String() != expected {
    36  		t.Errorf("expected %q, got %q", expected, v.String())
    37  	}
    38  
    39  	expected = "tarsum.dev"
    40  	v = 2
    41  	if v.String() != expected {
    42  		t.Errorf("expected %q, got %q", expected, v.String())
    43  	}
    44  }
    45  
    46  func TestGetVersion(t *testing.T) {
    47  	testSet := []struct {
    48  		Str      string
    49  		Expected Version
    50  	}{
    51  		{"tarsum+sha256:e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b", Version0},
    52  		{"tarsum+sha256", Version0},
    53  		{"tarsum", Version0},
    54  		{"tarsum.dev", VersionDev},
    55  		{"tarsum.dev+sha256:deadbeef", VersionDev},
    56  	}
    57  
    58  	for _, ts := range testSet {
    59  		v, err := GetVersionFromTarsum(ts.Str)
    60  		if err != nil {
    61  			t.Fatalf("%q : %s", err, ts.Str)
    62  		}
    63  		if v != ts.Expected {
    64  			t.Errorf("expected %d (%q), got %d (%q)", ts.Expected, ts.Expected, v, v)
    65  		}
    66  	}
    67  
    68  	// test one that does not exist, to ensure it errors
    69  	str := "weak+md5:abcdeabcde"
    70  	_, err := GetVersionFromTarsum(str)
    71  	if err != ErrNotVersion {
    72  		t.Fatalf("%q : %s", err, str)
    73  	}
    74  }
    75  
    76  func TestGetVersions(t *testing.T) {
    77  	expected := []Version{
    78  		Version0,
    79  		Version1,
    80  		VersionDev,
    81  	}
    82  	versions := GetVersions()
    83  	if len(versions) != len(expected) {
    84  		t.Fatalf("Expected %v versions, got %v", len(expected), len(versions))
    85  	}
    86  	if !containsVersion(versions, expected[0]) || !containsVersion(versions, expected[1]) || !containsVersion(versions, expected[2]) {
    87  		t.Fatalf("Expected [%v], got [%v]", expected, versions)
    88  	}
    89  }
    90  
    91  func containsVersion(versions []Version, version Version) bool {
    92  	for _, v := range versions {
    93  		if v == version {
    94  			return true
    95  		}
    96  	}
    97  	return false
    98  }