Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chaudharysaket committed Jul 11, 2024
1 parent 1d413fe commit 2f5f71e
Showing 1 changed file with 110 additions and 97 deletions.
207 changes: 110 additions & 97 deletions telemetry/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,103 +465,116 @@ func TestGetLogEndpointURL(t *testing.T) {
}

func TestGetNewRelicTags(t *testing.T) {
os.Setenv("NR_TAGS", "env:prod;team:myTeam")
os.Setenv("NR_ENV_DELIMITER", ";")

tests := []struct {
name string
common map[string]interface{}
expected map[string]interface{}
envTags string
envDelimiter string
}{
{
name: "Add New Relic tags to common",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
"env": "prod",
"team": "myTeam",
},
envTags: "env:prod;team:myTeam",
envDelimiter: ";",
},

tests := []struct {
name string
common map[string]interface{}
expected map[string]interface{}
envTags string
envDelimiter string
}{
{
name: "Add New Relic tags to common",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
"env": "prod",
"team": "myTeam",
},
envTags: "env:prod;team:myTeam",
envDelimiter: ";",
},
{
name: "Add New Relic tags to common if no delimiter is set",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
"env": "prod",
"team": "myTeam",
},
envTags: "env:prod;team:myTeam",
},
{
name: "No New Relic tags to common if delimiter is incorrect",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
envTags: "env:prod;team:myTeam",
envDelimiter: ",",
},
{
name: "Add New Relic tags to common if no delimiter is set",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
"env": "prod",
"team": "myTeam",
},
envTags: "env:prod;team:myTeam",
},
name: "No New Relic tags to add",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
envTags: "",
envDelimiter: "",
},
{
name: "No New Relic tags to common if delimiter is incorrect",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
envTags: "env:prod;team:myTeam",
envDelimiter: ",",
},
{
name: "No New Relic tags to add",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
envTags: "",
envDelimiter: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Setenv("NR_TAGS", tt.envTags)
os.Setenv("NR_ENV_DELIMITER", tt.envDelimiter)

common := make(map[string]interface{}, len(tt.common))
for k, v := range tt.common {
common[k] = v
}

getNewRelicTags(common)

for k, v := range tt.expected {
if common[k] != v {
t.Errorf("expected common[%q] to be %v, but got %v", k, v, common[k])
}
}
for k := range common {
if _, ok := tt.expected[k]; !ok {
t.Errorf("unexpected key %q in common map", k)
}
}
})
}
name: "No New Relic tags to add when enTags and envDelimiter are undeclared",
common: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
expected: map[string]interface{}{
"plugin": "testPlugin",
"faas.arn": "arn:aws:lambda:us-east-1:123456789012:function:testFunction",
"faas.name": "testFunction",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = os.Setenv("NR_TAGS", tt.envTags)
defer os.Unsetenv("NR_TAGS")
_ = os.Setenv("NR_ENV_DELIMITER", tt.envDelimiter)
defer os.Unsetenv("NR_ENV_DELIMITER")

common := make(map[string]interface{}, len(tt.common))
for k, v := range tt.common {
common[k] = v
}

getNewRelicTags(common)

for k, v := range tt.expected {
if common[k] != v {
t.Errorf("expected common[%q] to be %v, but got %v", k, v, common[k])
}
}
for k := range common {
if _, ok := tt.expected[k]; !ok {
t.Errorf("unexpected key %q in common map", k)
}
}
})
}
}

0 comments on commit 2f5f71e

Please sign in to comment.