Skip to content

Commit

Permalink
Add tests for V4 on Fargate launch type
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Mancioppi committed Aug 16, 2022
1 parent f32f201 commit 2ef2026
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 6 deletions.
59 changes: 55 additions & 4 deletions detectors/aws/ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ func TestDetectV3(t *testing.T) {
}

// succesfully returns resource when process is running on Amazon ECS environment
// with Metadata v4.
func TestDetectV4(t *testing.T) {
// with Metadata v4 with the EC2 Launch type.
func TestDetectV4LaunchTypeEc2(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if strings.HasSuffix(req.URL.String(), "/task") {
content, err := os.ReadFile("testdata/metadatav4-response-task.json")
content, err := os.ReadFile("testdata/metadatav4-response-task-ec2.json")
if err == nil {
res.Write(content)
}
} else {
content, err := os.ReadFile("testdata/metadatav4-response-container.json")
content, err := os.ReadFile("testdata/metadatav4-response-container-ec2.json")
if err == nil {
res.Write(content)
}
Expand Down Expand Up @@ -122,6 +122,57 @@ func TestDetectV4(t *testing.T) {
assert.Equal(t, expectedResource, res, "Resource returned is incorrect")
}

// succesfully returns resource when process is running on Amazon ECS environment
// with Metadata v4 with the Fargate Launch type.
func TestDetectV4LaunchTypeFargate(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if strings.HasSuffix(req.URL.String(), "/task") {
content, err := os.ReadFile("testdata/metadatav4-response-task-fargate.json")
if err == nil {
res.Write(content)
}
} else {
content, err := os.ReadFile("testdata/metadatav4-response-container-fargate.json")
if err == nil {
res.Write(content)
}
}
}))
defer func() { testServer.Close() }()

os.Clearenv()
_ = os.Setenv(metadataV3EnvVar, "3")
_ = os.Setenv(metadataV4EnvVar, testServer.URL)

detectorUtils := new(MockDetectorUtils)

detectorUtils.On("getContainerName").Return("container-Name", nil)
detectorUtils.On("getContainerID").Return("0123456789A", nil)

attributes := []attribute.KeyValue{
semconv.CloudProviderAWS,
semconv.CloudPlatformAWSECS,
semconv.ContainerNameKey.String("container-Name"),
semconv.ContainerIDKey.String("0123456789A"),
semconv.AWSECSContainerARNKey.String("arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1"),
semconv.AWSECSClusterARNKey.String("arn:aws:ecs:us-west-2:111122223333:cluster/default"),
semconv.AWSECSLaunchtypeKey.String("fargate"),
semconv.AWSECSTaskARNKey.String("arn:aws:ecs:us-west-2:111122223333:task/default/e9028f8d5d8e4f258373e7b93ce9a3c3"),
semconv.AWSECSTaskFamilyKey.String("curltest"),
semconv.AWSECSTaskRevisionKey.String("3"),
semconv.AWSLogGroupNamesKey.String("/ecs/containerlogs"),
semconv.AWSLogGroupARNsKey.String("arn:aws:logs:us-west-2:111122223333:log-group:/ecs/containerlogs:*"),
semconv.AWSLogStreamNamesKey.String("ecs/curl/cd189a933e5849daa93386466019ab50"),
semconv.AWSLogStreamARNsKey.String("arn:aws:logs:us-west-2:111122223333:log-group:/ecs/containerlogs:log-stream:ecs/curl/cd189a933e5849daa93386466019ab50"),
}
expectedResource := resource.NewWithAttributes(semconv.SchemaURL, attributes...)
detector := &resourceDetector{utils: detectorUtils}
res, err := detector.Detect(context.Background())

assert.Equal(t, err, nil, "Detector should not file")
assert.Equal(t, expectedResource, res, "Resource returned is incorrect")
}

//returns empty resource when detector cannot read container ID.
func TestDetectCannotReadContainerID(t *testing.T) {
os.Clearenv()
Expand Down
2 changes: 0 additions & 2 deletions detectors/aws/ecs/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/brunoscheufler/aws-ecs-metadata-go v0.0.0-20220809142905-71ffda5b3a7b h1:sDlCP3q7DzAycTPuhV2U08iCoOwSO6SV4qOBlm012Vs=
github.com/brunoscheufler/aws-ecs-metadata-go v0.0.0-20220809142905-71ffda5b3a7b/go.mod h1:5BBxNAuQFkDjY3l9UHlJ8NnnM6NEi0bocZR2xc3xGxU=
github.com/brunoscheufler/aws-ecs-metadata-go v0.0.0-20220812150832-b6b31c6eeeaf h1:WCnJxXZXx9c8gwz598wvdqmu+YTzB9wx2X1OovK3Le8=
github.com/brunoscheufler/aws-ecs-metadata-go v0.0.0-20220812150832-b6b31c6eeeaf/go.mod h1:CeKhh8xSs3WZAc50xABMxu+FlfAAd5PNumo7NfOv7EE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"DockerId": "cd189a933e5849daa93386466019ab50-2495160603",
"Name": "curl",
"DockerName": "curl",
"Image": "111122223333.dkr.ecr.us-west-2.amazonaws.com/curltest:latest",
"ImageID": "sha256:25f3695bedfb454a50f12d127839a68ad3caf91e451c1da073db34c542c4d2cb",
"Labels": {
"com.amazonaws.ecs.cluster": "arn:aws:ecs:us-west-2:111122223333:cluster/default",
"com.amazonaws.ecs.container-name": "curl",
"com.amazonaws.ecs.task-arn": "arn:aws:ecs:us-west-2:111122223333:task/default/cd189a933e5849daa93386466019ab50",
"com.amazonaws.ecs.task-definition-family": "curltest",
"com.amazonaws.ecs.task-definition-version": "2"
},
"DesiredStatus": "RUNNING",
"KnownStatus": "RUNNING",
"Limits": {
"CPU": 10,
"Memory": 128
},
"CreatedAt": "2020-10-08T20:09:11.44527186Z",
"StartedAt": "2020-10-08T20:09:11.44527186Z",
"Type": "NORMAL",
"Networks": [
{
"NetworkMode": "awsvpc",
"IPv4Addresses": [
"192.0.2.3"
],
"AttachmentIndex": 0,
"MACAddress": "0a:de:f6:10:51:e5",
"IPv4SubnetCIDRBlock": "192.0.2.0/24",
"DomainNameServers": [
"192.0.2.2"
],
"DomainNameSearchList": [
"us-west-2.compute.internal"
],
"PrivateDNSName": "ip-10-0-0-222.us-west-2.compute.internal",
"SubnetGatewayIpv4Address": "192.0.2.0/24"
}
],
"ContainerARN": "arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1",
"LogOptions": {
"awslogs-create-group": "true",
"awslogs-group": "/ecs/containerlogs",
"awslogs-region": "us-west-2",
"awslogs-stream": "ecs/curl/cd189a933e5849daa93386466019ab50"
},
"LogDriver": "awslogs"
}
77 changes: 77 additions & 0 deletions detectors/aws/ecs/testdata/metadatav4-response-task-fargate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"Cluster": "arn:aws:ecs:us-west-2:111122223333:cluster/default",
"TaskARN": "arn:aws:ecs:us-west-2:111122223333:task/default/e9028f8d5d8e4f258373e7b93ce9a3c3",
"Family": "curltest",
"Revision": "3",
"DesiredStatus": "RUNNING",
"KnownStatus": "RUNNING",
"Limits": {
"CPU": 0.25,
"Memory": 512
},
"PullStartedAt": "2020-10-08T20:47:16.053330955Z",
"PullStoppedAt": "2020-10-08T20:47:19.592684631Z",
"AvailabilityZone": "us-west-2a",
"Containers": [
{
"DockerId": "e9028f8d5d8e4f258373e7b93ce9a3c3-2495160603",
"Name": "curl",
"DockerName": "curl",
"Image": "111122223333.dkr.ecr.us-west-2.amazonaws.com/curltest:latest",
"ImageID": "sha256:25f3695bedfb454a50f12d127839a68ad3caf91e451c1da073db34c542c4d2cb",
"Labels": {
"com.amazonaws.ecs.cluster": "arn:aws:ecs:us-west-2:111122223333:cluster/default",
"com.amazonaws.ecs.container-name": "curl",
"com.amazonaws.ecs.task-arn": "arn:aws:ecs:us-west-2:111122223333:task/default/e9028f8d5d8e4f258373e7b93ce9a3c3",
"com.amazonaws.ecs.task-definition-family": "curltest",
"com.amazonaws.ecs.task-definition-version": "3"
},
"DesiredStatus": "RUNNING",
"KnownStatus": "RUNNING",
"Limits": {
"CPU": 10,
"Memory": 128
},
"CreatedAt": "2020-10-08T20:47:20.567813946Z",
"StartedAt": "2020-10-08T20:47:20.567813946Z",
"Type": "NORMAL",
"Networks": [
{
"NetworkMode": "awsvpc",
"IPv4Addresses": [
"192.0.2.3"
],
"IPv6Addresses": [
"2001:dB8:10b:1a00:32bf:a372:d80f:e958"
],
"AttachmentIndex": 0,
"MACAddress": "02:b7:20:19:72:39",
"IPv4SubnetCIDRBlock": "192.0.2.0/24",
"IPv6SubnetCIDRBlock": "2600:1f13:10b:1a00::/64",
"DomainNameServers": [
"192.0.2.2"
],
"DomainNameSearchList": [
"us-west-2.compute.internal"
],
"PrivateDNSName": "ip-172-31-30-173.us-west-2.compute.internal",
"SubnetGatewayIpv4Address": "192.0.2.0/24"
}
],
"ClockDrift": {
"ClockErrorBound": 0.5458234999999999,
"ReferenceTimestamp": "2021-09-07T16:57:44Z",
"ClockSynchronizationStatus": "SYNCHRONIZED"
},
"ContainerARN": "arn:aws:ecs:us-west-2:111122223333:container/1bdcca8b-f905-4ee6-885c-4064cb70f6e6",
"LogOptions": {
"awslogs-create-group": "true",
"awslogs-group": "/ecs/containerlogs",
"awslogs-region": "us-west-2",
"awslogs-stream": "ecs/curl/e9028f8d5d8e4f258373e7b93ce9a3c3"
},
"LogDriver": "awslogs"
}
],
"LaunchType": "FARGATE"
}

0 comments on commit 2ef2026

Please sign in to comment.