github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/helper/resource/id_test.go (about)

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