Skip to content

Commit

Permalink
minor changes to address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bhautikpip committed Feb 10, 2021
1 parent 21db8fa commit 5204d27
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
22 changes: 11 additions & 11 deletions detectors/aws/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@ var (
)

// Create interface for methods needing to be mocked
type detectorUtilsResources interface {
type detectorUtils interface {
getContainerName() (string, error)
getContainerID() (string, error)
}

// struct implements detectorUtilsResources interface
type DetectorUtils struct{}
// struct implements detectorUtils interface
type ecsDetectorUtils struct{}

// resource detector collects resource information from Elastic Container Service environment
type resourceDetector struct {
utils detectorUtilsResources
utils detectorUtils
}

// compile time assertion that ecsDetectorUtils implements detectorUtilsResources interface
var _ detectorUtilsResources = (*DetectorUtils)(nil)
// compile time assertion that ecsDetectorUtils implements detectorUtils interface
var _ detectorUtils = (*ecsDetectorUtils)(nil)

// compile time assertion that resource detector implements the resource.Detector interface.
var _ resource.Detector = (*resourceDetector)(nil)

// returns resource detector struct
func NewResourceDetector(detectorUtils detectorUtilsResources) resource.Detector {
return &resourceDetector{utils: detectorUtils}
// NewResourceDetector returns a resource detector that will detect AWS ECS resources.
func NewResourceDetector() resource.Detector {
return &resourceDetector{utils: ecsDetectorUtils{}}
}

// Detect finds associated resources when running on ECS environment.
Expand Down Expand Up @@ -92,7 +92,7 @@ func (detector *resourceDetector) Detect(ctx context.Context) (*resource.Resourc
}

// returns docker container ID from default c group path
func (ecsUtils DetectorUtils) getContainerID() (string, error) {
func (ecsUtils ecsDetectorUtils) getContainerID() (string, error) {
fileData, err := ioutil.ReadFile(defaultCgroupPath)
if err != nil {
return "", errCannotReadCGroupFile
Expand All @@ -107,7 +107,7 @@ func (ecsUtils DetectorUtils) getContainerID() (string, error) {
}

// returns host name reported by the kernel
func (ecsUtils DetectorUtils) getContainerName() (string, error) {
func (ecsUtils ecsDetectorUtils) getContainerName() (string, error) {
hostName, err := os.Hostname()
if err != nil {
return "", errCannotReadContainerName
Expand Down
8 changes: 4 additions & 4 deletions detectors/aws/ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestDetect(t *testing.T) {
semconv.ContainerIDKey.String("0123456789A"),
}
expectedResource := resource.NewWithAttributes(labels...)
detector := NewResourceDetector(detectorUtils)
detector := &resourceDetector{utils: detectorUtils}
res, _ := detector.Detect(context.Background())

assert.Equal(t, res, expectedResource, "Resource returned is incorrect")
Expand All @@ -74,7 +74,7 @@ func TestDetectCannotReadContainerID(t *testing.T) {
detectorUtils.On("getContainerName").Return("container-Name", nil)
detectorUtils.On("getContainerID").Return("", errCannotReadContainerID)

detector := NewResourceDetector(detectorUtils)
detector := &resourceDetector{utils: detectorUtils}
res, err := detector.Detect(context.Background())

assert.Equal(t, errCannotReadContainerID, err)
Expand All @@ -91,7 +91,7 @@ func TestDetectCannotReadContainerName(t *testing.T) {
detectorUtils.On("getContainerName").Return("", errCannotReadContainerName)
detectorUtils.On("getContainerID").Return("0123456789A", nil)

detector := NewResourceDetector(detectorUtils)
detector := &resourceDetector{utils: detectorUtils}
res, err := detector.Detect(context.Background())

assert.Equal(t, errCannotReadContainerName, err)
Expand All @@ -101,7 +101,7 @@ func TestDetectCannotReadContainerName(t *testing.T) {
//returns empty resource when process is not running ECS
func TestReturnsIfNoEnvVars(t *testing.T) {
os.Clearenv()
detector := NewResourceDetector(nil)
detector := &resourceDetector{utils: nil}
res, err := detector.Detect(context.Background())

assert.Equal(t, errNotOnECS, err)
Expand Down

0 comments on commit 5204d27

Please sign in to comment.