github.com/newrelic/newrelic-client-go@v1.1.0/pkg/testhelpers/helpers.go (about)

     1  package testhelpers
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"os"
     7  	"strconv"
     8  	"time"
     9  )
    10  
    11  var (
    12  	letters = []rune("abcdefghijklmnopqrstuvwxyz")
    13  )
    14  
    15  // RandSeq is used to get a string made up of n random lowercase letters.
    16  func RandSeq(n int) string {
    17  	rand.Seed(time.Now().UnixNano())
    18  	b := make([]rune, n)
    19  	for i := range b {
    20  		b[i] = letters[rand.Intn(len(letters))]
    21  	}
    22  	return string(b)
    23  }
    24  
    25  // GetTestUserID returns the integer value for a New Relic user ID from the environment
    26  func GetTestUserID() (int, error) {
    27  	return getEnvInt("NEW_RELIC_TEST_USER_ID")
    28  }
    29  
    30  // GetTestAccountID returns the integer value for a New Relic Account ID from the environment
    31  func GetTestAccountID() (int, error) {
    32  	return getEnvInt("NEW_RELIC_ACCOUNT_ID")
    33  }
    34  
    35  // getEnvInt helper to DRY up the other env get calls for integers
    36  func getEnvInt(name string) (int, error) {
    37  	if name == "" {
    38  		return 0, fmt.Errorf("failed to get environment value, no name specified")
    39  	}
    40  
    41  	id := os.Getenv(name)
    42  
    43  	if id == "" {
    44  		return 0, fmt.Errorf("failed to get environment value due to undefined environment variable %s", name)
    45  	}
    46  
    47  	n, err := strconv.Atoi(id)
    48  	if err != nil {
    49  		return 0, err
    50  	}
    51  
    52  	return n, nil
    53  }