Skip to content

Commit e5e5940

Browse files
authored
Generate infra service test id from timestamp (#885)
The ultimate goal is to have a way to look for stale environments generated by the infra service tests. The way we plan on doing that is to encode timestamp information in the id itself, but because we want the id to be short we are base62 encoding the id.
1 parent e95981c commit e5e5940

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

infra/test/infra_test.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ import (
1616
"time"
1717

1818
http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
19-
"github.com/gruntwork-io/terratest/modules/random"
2019
"github.com/gruntwork-io/terratest/modules/shell"
2120
"github.com/gruntwork-io/terratest/modules/terraform"
2221
)
2322

24-
var uniqueId = strings.ToLower(random.UniqueId())
23+
var uniqueId = strings.ToLower(generateTestId())
2524
var workspaceName = fmt.Sprintf("t-%s", uniqueId)
2625
var testAppName = os.Getenv("APP_NAME")
2726

@@ -94,3 +93,26 @@ func DestroyService(t *testing.T, terraformOptions *terraform.Options) {
9493
terraform.Destroy(t, terraformOptions)
9594
fmt.Println("::endgroup::")
9695
}
96+
97+
func generateTestId() string {
98+
now := time.Now()
99+
timeNum := now.Unix()
100+
return toBase62(timeNum)
101+
}
102+
103+
func toBase62(n int64) string {
104+
const charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
105+
if n == 0 {
106+
return "000000"
107+
}
108+
result := ""
109+
for n > 0 {
110+
result = string(charset[n%62]) + result
111+
n /= 62
112+
}
113+
// Pad with leading zeros to ensure at least 6 characters
114+
for len(result) < 6 {
115+
result = "0" + result
116+
}
117+
return result
118+
}

0 commit comments

Comments
 (0)