Skip to content

Commit 8e23550

Browse files
Fix flaky repos integration tests (#2834)
## Why We often see repos integration tests fail with object already exists errors. One hypothesis is that the Random string generator is not truly random and pulls from a small seed pool in CI. This PR attempts to fix that hypothetical issue by using UUIDs instead to generate the random string. Example failure: ``` === FAIL: integration/cmd/repos TestReposGet (3.83s) repos_test.go:80: CLOUD_ENV=*** repos_test.go:47: Error Trace: C:/a/eng-dev-ecosystem/eng-dev-ecosystem/ext/cli/integration/cmd/repos/repos_test.go:47 C:/a/eng-dev-ecosystem/eng-dev-ecosystem/ext/cli/integration/cmd/repos/repos_test.go:83 Error: Received unexpected error: a Workspace object at path /Repos/***/empty-repo-integration-pIdzKaCkfhBq already exists. Try a different path for your Repo or rename the existing Workspace object. Request ID: 2a601c0f-75d2-4568-a7ac-7fc8a3423932 Test: TestReposGet ```
1 parent f686d89 commit 8e23550

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

internal/testutil/helpers.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package testutil
22

33
import (
4-
"fmt"
5-
"math/rand"
64
"os"
75
"strings"
86
"time"
97

8+
"github.com/google/uuid"
109
"github.com/stretchr/testify/require"
1110
)
1211

@@ -19,19 +18,14 @@ func GetEnvOrSkipTest(t TestingT, name string) string {
1918
return value
2019
}
2120

22-
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
23-
2421
// RandomName gives random name with optional prefix. e.g. qa.RandomName("tf-")
2522
func RandomName(prefix ...string) string {
26-
randLen := 12
27-
b := make([]byte, randLen)
28-
for i := range b {
29-
b[i] = charset[rand.Intn(len(charset))]
30-
}
31-
if len(prefix) > 0 {
32-
return fmt.Sprintf("%s%s", strings.Join(prefix, ""), b)
23+
out := ""
24+
for _, p := range prefix {
25+
out += p
3326
}
34-
return string(b)
27+
out += strings.ReplaceAll(uuid.New().String(), "-", "")
28+
return out
3529
}
3630

3731
func SkipUntil(t TestingT, date string) {

0 commit comments

Comments
 (0)