1 // Package test contains generic test utilities that could conceivably be used 2 // by any type of test (unit, integration, e2e) for any test in our codebase. 3 // e.g., if the utility is specific to a subject area (like K8s or GCP), it should not 4 // go in this package. 5 package test 6 7 import ( 8 "os" 9 "strconv" 10 "time" 11 ) 12 13 const ( 14 // TEST_TIMEOUT is provided by the Bazel test runner 15 timeout = "TEST_TIMEOUT" 16 ) 17 18 // Timeout returns the value of TEST_TIMEOUT if present, otherwise 19 // it returns a default timeout of 60 seconds. Useful for writing tests 20 // which use internal frameworks such as Ginkgo/envtest that will try to 21 // try to set timeouts that may conflict with the --test_timeout value 22 // provided to Bazel via command line 23 func Timeout() (time.Duration, error) { 24 t := os.Getenv(timeout) 25 if t == "" { 26 // TODO(aw185176): one size doesn't really fit all but i dont 27 // want to think about this rn 28 t = "60" 29 } 30 31 parsed, err := strconv.ParseInt(t, 10, 64) 32 if err != nil { 33 return 0, err 34 } 35 36 // convert parsed integration to duration 37 // longwinded explanation: https://go.dev/blog/constants 38 return time.Second * time.Duration(parsed), nil 39 } 40 41 // NoError panics if an error is passed, can be used to reduce test setup 42 // boilerplate where assertion libraries are not initialized 43 func NoError(e error) { 44 if e != nil { 45 panic(e) 46 } 47 } 48