|
| 1 | +// +build integration |
| 2 | + |
| 3 | +package client |
| 4 | + |
| 5 | +import ( |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + |
| 12 | + "github.com/newrelic/newrelic-client-go/pkg/region" |
| 13 | + |
| 14 | + "github.com/newrelic/newrelic-cli/internal/credentials" |
| 15 | +) |
| 16 | + |
| 17 | +var overrideEnvVars = []string{ |
| 18 | + "NEW_RELIC_API_KEY", |
| 19 | + "NEW_RELIC_REGION", |
| 20 | +} |
| 21 | + |
| 22 | +func TestApplyOverrides(t *testing.T) { |
| 23 | + // Do not run this in parallel, we are messing with the environment |
| 24 | + |
| 25 | + f, err := ioutil.TempDir("/tmp", "newrelic") |
| 26 | + assert.NoError(t, err) |
| 27 | + defer os.RemoveAll(f) |
| 28 | + |
| 29 | + // Initialize the new configuration directory |
| 30 | + c, err := credentials.LoadCredentials(f) |
| 31 | + assert.NoError(t, err) |
| 32 | + |
| 33 | + // Create an initial profile to work with |
| 34 | + err = c.AddProfile("testCase1", "us", "apiKeyGoesHere") |
| 35 | + assert.NoError(t, err) |
| 36 | + p := c.Profiles["testCase1"] |
| 37 | + assert.NotNil(t, p) |
| 38 | + |
| 39 | + // Clear env vars we are going to mess with, and reset on exit |
| 40 | + for _, v := range overrideEnvVars { |
| 41 | + if val, ok := os.LookupEnv(v); ok { |
| 42 | + defer os.Setenv(v, val) |
| 43 | + os.Unsetenv(v) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Default case (no overrides) |
| 48 | + p2 := applyOverrides(&p) |
| 49 | + assert.NotNil(t, p2) |
| 50 | + assert.Equal(t, p.APIKey, p2.APIKey) |
| 51 | + assert.Equal(t, p.Region, p2.Region) |
| 52 | + |
| 53 | + // Override just the API Key |
| 54 | + os.Setenv("NEW_RELIC_API_KEY", "anotherAPIKey") |
| 55 | + p2 = applyOverrides(&p) |
| 56 | + assert.NotNil(t, p2) |
| 57 | + assert.Equal(t, "anotherAPIKey", p2.APIKey) |
| 58 | + assert.Equal(t, p.Region, p2.Region) |
| 59 | + |
| 60 | + // Both |
| 61 | + os.Setenv("NEW_RELIC_REGION", "US") |
| 62 | + p2 = applyOverrides(&p) |
| 63 | + assert.NotNil(t, p2) |
| 64 | + assert.Equal(t, "anotherAPIKey", p2.APIKey) |
| 65 | + assert.Equal(t, region.US, p2.Region) |
| 66 | + |
| 67 | + // Override just the REGION (valid) |
| 68 | + os.Unsetenv("NEW_RELIC_API_KEY") |
| 69 | + p2 = applyOverrides(&p) |
| 70 | + assert.NotNil(t, p2) |
| 71 | + assert.Equal(t, p.APIKey, p2.APIKey) |
| 72 | + assert.Equal(t, region.US, p2.Region) |
| 73 | + |
| 74 | + // Region lowercase |
| 75 | + os.Setenv("NEW_RELIC_REGION", "eu") |
| 76 | + p2 = applyOverrides(&p) |
| 77 | + assert.NotNil(t, p2) |
| 78 | + assert.Equal(t, p.APIKey, p2.APIKey) |
| 79 | + assert.Equal(t, region.EU, p2.Region) |
| 80 | + |
| 81 | + // Invalid REGION (Should be region.Default) |
| 82 | + os.Setenv("NEW_RELIC_REGION", "test") |
| 83 | + p2 = applyOverrides(&p) |
| 84 | + assert.NotNil(t, p2) |
| 85 | + assert.Equal(t, p.APIKey, p2.APIKey) |
| 86 | + assert.Equal(t, region.Default, p2.Region) |
| 87 | +} |
0 commit comments