Skip to content

Commit

Permalink
Refactor to use testing.T.Setenv in aws:ecs (#4618)
Browse files Browse the repository at this point in the history
  • Loading branch information
takp committed Nov 27, 2023
1 parent 932d191 commit d50aa19
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
14 changes: 4 additions & 10 deletions detectors/aws/ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package ecs

import (
"context"
"os"
"testing"

"go.opentelemetry.io/otel/attribute"
Expand Down Expand Up @@ -46,8 +45,7 @@ func (detectorUtils *MockDetectorUtils) getContainerName() (string, error) {
// successfully returns resource when process is running on Amazon ECS environment
// with no Metadata v4.
func TestDetectV3(t *testing.T) {
os.Clearenv()
_ = os.Setenv(metadataV3EnvVar, "3")
t.Setenv(metadataV3EnvVar, "3")

detectorUtils := new(MockDetectorUtils)

Expand All @@ -69,8 +67,7 @@ func TestDetectV3(t *testing.T) {

// returns empty resource when detector cannot read container ID.
func TestDetectCannotReadContainerID(t *testing.T) {
os.Clearenv()
_ = os.Setenv(metadataV3EnvVar, "3")
t.Setenv(metadataV3EnvVar, "3")
detectorUtils := new(MockDetectorUtils)

detectorUtils.On("getContainerName").Return("container-Name", nil)
Expand All @@ -92,9 +89,8 @@ func TestDetectCannotReadContainerID(t *testing.T) {

// returns empty resource when detector cannot read container Name.
func TestDetectCannotReadContainerName(t *testing.T) {
os.Clearenv()
_ = os.Setenv(metadataV3EnvVar, "3")
_ = os.Setenv(metadataV4EnvVar, "4")
t.Setenv(metadataV3EnvVar, "3")
t.Setenv(metadataV4EnvVar, "4")
detectorUtils := new(MockDetectorUtils)

detectorUtils.On("getContainerName").Return("", errCannotReadContainerName)
Expand All @@ -109,7 +105,6 @@ func TestDetectCannotReadContainerName(t *testing.T) {

// returns empty resource when process is not running ECS.
func TestReturnsIfNoEnvVars(t *testing.T) {
os.Clearenv()
detector := &resourceDetector{utils: nil}
res, err := detector.Detect(context.Background())

Expand All @@ -120,7 +115,6 @@ func TestReturnsIfNoEnvVars(t *testing.T) {

// handles alternative aws partitions (e.g. AWS GovCloud).
func TestLogsAttributesAlternatePartition(t *testing.T) {
os.Clearenv()
detector := &resourceDetector{utils: nil}

containerMetadata := &metadata.ContainerMetadataV4{
Expand Down
6 changes: 2 additions & 4 deletions detectors/aws/ecs/test/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func TestDetectV4LaunchTypeEc2(t *testing.T) {
}))
defer testServer.Close()

os.Clearenv()
_ = os.Setenv(metadataV4EnvVar, testServer.URL)
t.Setenv(metadataV4EnvVar, testServer.URL)

hostname, err := os.Hostname()
assert.NoError(t, err, "Error")
Expand Down Expand Up @@ -229,8 +228,7 @@ func TestDetectV4LaunchTypeFargate(t *testing.T) {
}))
defer testServer.Close()

os.Clearenv()
_ = os.Setenv(metadataV4EnvVar, testServer.URL)
t.Setenv(metadataV4EnvVar, testServer.URL)

hostname, err := os.Hostname()
assert.NoError(t, err, "Error")
Expand Down

0 comments on commit d50aa19

Please sign in to comment.