terraform/helper/resource/id_test.go

67 lines
1.4 KiB
Go
Raw Normal View History

package resource
import (
"regexp"
"strings"
"testing"
"time"
)
var allDigits = regexp.MustCompile(`^\d+$`)
var allHex = regexp.MustCompile(`^[a-f0-9]+$`)
func TestUniqueId(t *testing.T) {
split := func(rest string) (timestamp, increment string) {
return rest[:18], rest[18:]
}
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)
}
2018-03-11 02:56:13 +01:00
if !strings.HasPrefix(id, UniqueIdPrefix) {
t.Fatalf("Unique ID didn't have terraform- prefix! %s", id)
}
2018-03-11 02:56:13 +01:00
rest := strings.TrimPrefix(id, UniqueIdPrefix)
2018-03-11 02:56:13 +01:00
if len(rest) != UniqueIDSuffixLength {
t.Fatalf("Post-prefix part has wrong length! %s", rest)
}
timestamp, increment := split(rest)
if !allDigits.MatchString(timestamp) {
t.Fatalf("Timestamp not all digits! %s", timestamp)
}
if !allHex.MatchString(increment) {
t.Fatalf("Increment part not all hex! %s", increment)
}
if lastId != "" && lastId >= id {
t.Fatalf("IDs not ordered! %s vs %s", lastId, id)
}
ids[id] = struct{}{}
lastId = id
}
id1 := UniqueId()
time.Sleep(time.Millisecond)
id2 := UniqueId()
2018-03-11 02:56:13 +01:00
timestamp1, _ := split(strings.TrimPrefix(id1, UniqueIdPrefix))
timestamp2, _ := split(strings.TrimPrefix(id2, UniqueIdPrefix))
if timestamp1 == timestamp2 {
t.Fatalf("Timestamp part should update at least once a millisecond %s %s",
id1, id2)
}
}