opentofu/helper/resource/id_test.go
David Glasser 4ad825fe08 core: name_prefix names now start with a timestamp
This means that two resources created by the same rule will get names
 which sort in the order they are created.

The rest of the ID is still random base32 characters; we no longer set
the bit values that denote UUIDv4.

The length of the ID returned by PrefixedUniqueId is not changed by this
commit; that way we don't break any resources where the underlying
resource has a name length limit.

Fixes #8143.
2016-08-17 11:06:28 -07:00

52 lines
1.0 KiB
Go

package resource
import (
"regexp"
"strings"
"testing"
)
var allDigits = regexp.MustCompile(`^\d+$`)
var allBase32 = regexp.MustCompile(`^[a-z234567]+$`)
func TestUniqueId(t *testing.T) {
iterations := 10000
ids := make(map[string]struct{})
var id, lastId string
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)
}
rest := strings.TrimPrefix(id, "terraform-")
if len(rest) != 26 {
t.Fatalf("Post-prefix part has wrong length! %s", rest)
}
timestamp := rest[:23]
random := rest[23:]
if !allDigits.MatchString(timestamp) {
t.Fatalf("Timestamp not all digits! %s", timestamp)
}
if !allBase32.MatchString(random) {
t.Fatalf("Random part not all base32! %s", random)
}
if lastId != "" && lastId >= id {
t.Fatalf("IDs not ordered! %s vs %s", lastId, id)
}
ids[id] = struct{}{}
lastId = id
}
}