github.com/newrelic/newrelic-client-go@v1.1.0/pkg/testhelpers/config.go (about) 1 package testhelpers 2 3 import ( 4 "net/http/httptest" 5 "os" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/assert" 10 11 "github.com/newrelic/newrelic-client-go/pkg/config" 12 "github.com/newrelic/newrelic-client-go/pkg/region" 13 ) 14 15 const ( 16 HTTPTimeout = 60 * time.Second // HTTPTimeout increases the timeout for integration tests 17 LicenseKey = "APMLicenseKey" // LicenseKey used in mock configs 18 LogLevel = "debug" // LogLevel used in mock configs 19 PersonalAPIKey = "personalAPIKey" // PersonalAPIKey used in mock configs (from Environment for Integration tests) 20 UserAgent = "newrelic/newrelic-client-go (automated testing)" // UserAgent used in mock configs 21 ) 22 23 // NewTestConfig returns a fully saturated configration with modified BaseURLs 24 // for all endpoints based on the test server passed in 25 func NewTestConfig(t *testing.T, testServer *httptest.Server) config.Config { 26 cfg := config.New() 27 28 // Set some defaults from Testing constants 29 cfg.LogLevel = LogLevel 30 cfg.PersonalAPIKey = PersonalAPIKey 31 cfg.UserAgent = UserAgent 32 cfg.LicenseKey = LicenseKey 33 34 if testServer != nil { 35 cfg.Region().SetInfrastructureBaseURL(testServer.URL) 36 cfg.Region().SetNerdGraphBaseURL(testServer.URL) 37 cfg.Region().SetRestBaseURL(testServer.URL) 38 cfg.Region().SetSyntheticsBaseURL(testServer.URL) 39 cfg.Region().SetLogsBaseURL(testServer.URL) 40 } 41 42 return cfg 43 } 44 45 // NewIntegrationTestConfig grabs environment vars for required fields or skips the test. 46 // returns a fully saturated configuration 47 func NewIntegrationTestConfig(t *testing.T) config.Config { 48 envPersonalAPIKey := os.Getenv("NEW_RELIC_API_KEY") 49 envInsightsInsertKey := os.Getenv("NEW_RELIC_INSIGHTS_INSERT_KEY") 50 envLicenseKey := os.Getenv("NEW_RELIC_LICENSE_KEY") 51 envRegion := os.Getenv("NEW_RELIC_REGION") 52 envLogLevel := os.Getenv("NEW_RELIC_LOG_LEVEL") 53 54 if envPersonalAPIKey == "" { 55 t.Skipf("acceptance testing requires NEW_RELIC_API_KEY") 56 } 57 58 cfg := config.New() 59 60 // Set some defaults 61 if envLogLevel != "" { 62 cfg.LogLevel = envLogLevel 63 } else { 64 cfg.LogLevel = LogLevel 65 } 66 cfg.Logger = cfg.GetLogger() 67 68 // HTTP Settings 69 timeout := HTTPTimeout 70 cfg.Timeout = &timeout 71 cfg.UserAgent = UserAgent 72 73 // Auth 74 cfg.PersonalAPIKey = envPersonalAPIKey 75 cfg.InsightsInsertKey = envInsightsInsertKey 76 cfg.LicenseKey = envLicenseKey 77 78 if envRegion != "" { 79 regName, err := region.Parse(envRegion) 80 assert.NoError(t, err) 81 82 reg, err := region.Get(regName) 83 assert.NoError(t, err) 84 85 err = cfg.SetRegion(reg) 86 assert.NoError(t, err) 87 } 88 89 return cfg 90 }