// Package test contains generic test utilities that could conceivably be used // by any type of test (unit, integration, e2e) for any test in our codebase. // e.g., if the utility is specific to a subject area (like K8s or GCP), it should not // go in this package. package test import ( "os" "strconv" "time" ) const ( // TEST_TIMEOUT is provided by the Bazel test runner timeout = "TEST_TIMEOUT" ) // Timeout returns the value of TEST_TIMEOUT if present, otherwise // it returns a default timeout of 60 seconds. Useful for writing tests // which use internal frameworks such as Ginkgo/envtest that will try to // try to set timeouts that may conflict with the --test_timeout value // provided to Bazel via command line func Timeout() (time.Duration, error) { t := os.Getenv(timeout) if t == "" { // TODO(aw185176): one size doesn't really fit all but i dont // want to think about this rn t = "60" } parsed, err := strconv.ParseInt(t, 10, 64) if err != nil { return 0, err } // convert parsed integration to duration // longwinded explanation: https://go.dev/blog/constants return time.Second * time.Duration(parsed), nil } // NoError panics if an error is passed, can be used to reduce test setup // boilerplate where assertion libraries are not initialized func NoError(e error) { if e != nil { panic(e) } }