github.com/scorpionis/docker@v1.6.0-rc7/pkg/common/randomid_test.go (about)

     1  package common
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestShortenId(t *testing.T) {
     8  	id := GenerateRandomID()
     9  	truncID := TruncateID(id)
    10  	if len(truncID) != 12 {
    11  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    12  	}
    13  }
    14  
    15  func TestShortenIdEmpty(t *testing.T) {
    16  	id := ""
    17  	truncID := TruncateID(id)
    18  	if len(truncID) > len(id) {
    19  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    20  	}
    21  }
    22  
    23  func TestShortenIdInvalid(t *testing.T) {
    24  	id := "1234"
    25  	truncID := TruncateID(id)
    26  	if len(truncID) != len(id) {
    27  		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
    28  	}
    29  }
    30  
    31  func TestGenerateRandomID(t *testing.T) {
    32  	id := GenerateRandomID()
    33  
    34  	if len(id) != 64 {
    35  		t.Fatalf("Id returned is incorrect: %s", id)
    36  	}
    37  }
    38  
    39  func TestRandomString(t *testing.T) {
    40  	id := RandomString()
    41  	if len(id) != 64 {
    42  		t.Fatalf("Id returned is incorrect: %s", id)
    43  	}
    44  }
    45  
    46  func TestRandomStringUniqueness(t *testing.T) {
    47  	repeats := 25
    48  	set := make(map[string]struct{}, repeats)
    49  	for i := 0; i < repeats; i = i + 1 {
    50  		id := RandomString()
    51  		if len(id) != 64 {
    52  			t.Fatalf("Id returned is incorrect: %s", id)
    53  		}
    54  		if _, ok := set[id]; ok {
    55  			t.Fatalf("Random number is repeated")
    56  		}
    57  		set[id] = struct{}{}
    58  	}
    59  }