github.com/richardmarshall/terraform@v0.9.5-0.20170429023105-15704cc6ee35/helper/resource/id_test.go (about)

     1  package resource
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  var allHex = regexp.MustCompile(`^[a-f0-9]+$`)
    10  
    11  func TestUniqueId(t *testing.T) {
    12  	iterations := 10000
    13  	ids := make(map[string]struct{})
    14  	var id, lastId string
    15  	for i := 0; i < iterations; i++ {
    16  		id = UniqueId()
    17  
    18  		if _, ok := ids[id]; ok {
    19  			t.Fatalf("Got duplicated id! %s", id)
    20  		}
    21  
    22  		if !strings.HasPrefix(id, "terraform-") {
    23  			t.Fatalf("Unique ID didn't have terraform- prefix! %s", id)
    24  		}
    25  
    26  		rest := strings.TrimPrefix(id, "terraform-")
    27  
    28  		if len(rest) != 26 {
    29  			t.Fatalf("Post-prefix part has wrong length! %s", rest)
    30  		}
    31  
    32  		if !allHex.MatchString(rest) {
    33  			t.Fatalf("Random part not all hex! %s", rest)
    34  		}
    35  
    36  		if lastId != "" && lastId >= id {
    37  			t.Fatalf("IDs not ordered! %s vs %s", lastId, id)
    38  		}
    39  
    40  		ids[id] = struct{}{}
    41  		lastId = id
    42  	}
    43  }