2015-04-22 19:53:05 +02:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
2016-08-17 04:40:05 +02:00
|
|
|
"regexp"
|
2015-04-22 19:53:05 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-11-16 03:09:32 +01:00
|
|
|
var allHex = regexp.MustCompile(`^[a-f0-9]+$`)
|
2016-08-17 04:40:05 +02:00
|
|
|
|
2015-04-22 19:53:05 +02:00
|
|
|
func TestUniqueId(t *testing.T) {
|
|
|
|
iterations := 10000
|
|
|
|
ids := make(map[string]struct{})
|
2016-08-17 04:40:05 +02:00
|
|
|
var id, lastId string
|
2015-04-22 19:53:05 +02:00
|
|
|
for i := 0; i < iterations; i++ {
|
|
|
|
id = UniqueId()
|
|
|
|
|
|
|
|
if _, ok := ids[id]; ok {
|
|
|
|
t.Fatalf("Got duplicated id! %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(id, "terraform-") {
|
|
|
|
t.Fatalf("Unique ID didn't have terraform- prefix! %s", id)
|
|
|
|
}
|
|
|
|
|
2016-08-17 04:40:05 +02:00
|
|
|
rest := strings.TrimPrefix(id, "terraform-")
|
|
|
|
|
|
|
|
if len(rest) != 26 {
|
|
|
|
t.Fatalf("Post-prefix part has wrong length! %s", rest)
|
|
|
|
}
|
|
|
|
|
2016-11-16 03:09:32 +01:00
|
|
|
if !allHex.MatchString(rest) {
|
|
|
|
t.Fatalf("Random part not all hex! %s", rest)
|
2016-08-17 04:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if lastId != "" && lastId >= id {
|
|
|
|
t.Fatalf("IDs not ordered! %s vs %s", lastId, id)
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:53:05 +02:00
|
|
|
ids[id] = struct{}{}
|
2016-08-17 04:40:05 +02:00
|
|
|
lastId = id
|
2015-04-22 19:53:05 +02:00
|
|
|
}
|
|
|
|
}
|