2024-12-12 12:35:38 +00:00
|
|
|
package testutil
|
2023-07-05 15:30:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2025-01-02 11:39:11 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-07-05 15:30:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetEnvOrSkipTest proceeds with test only with that env variable.
|
2024-12-12 14:42:15 +00:00
|
|
|
func GetEnvOrSkipTest(t TestingT, name string) string {
|
2023-07-05 15:30:54 +00:00
|
|
|
value := os.Getenv(name)
|
|
|
|
if value == "" {
|
|
|
|
t.Skipf("Environment variable %s is missing", name)
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
|
|
|
|
// RandomName gives random name with optional prefix. e.g. qa.RandomName("tf-")
|
|
|
|
func RandomName(prefix ...string) string {
|
|
|
|
randLen := 12
|
|
|
|
b := make([]byte, randLen)
|
|
|
|
for i := range b {
|
2024-12-16 16:21:20 +00:00
|
|
|
b[i] = charset[rand.Intn(len(charset))]
|
2023-07-05 15:30:54 +00:00
|
|
|
}
|
|
|
|
if len(prefix) > 0 {
|
|
|
|
return fmt.Sprintf("%s%s", strings.Join(prefix, ""), b)
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
2025-01-02 11:39:11 +00:00
|
|
|
|
|
|
|
func SkipUntil(t TestingT, date string) {
|
|
|
|
deadline, err := time.Parse(time.DateOnly, date)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if time.Now().Before(deadline) {
|
|
|
|
t.Skipf("Skipping test until %s. Time right now: %s", deadline.Format(time.DateOnly), time.Now())
|
|
|
|
}
|
|
|
|
}
|