From 2ef2026553d481e14f3c5c051dc9df5afc8a2bb7 Mon Sep 17 00:00:00 2001 From: Michele Mancioppi Date: Tue, 16 Aug 2022 16:16:01 +0200 Subject: [PATCH] Add tests for V4 on Fargate launch type --- detectors/aws/ecs/ecs_test.go | 59 +++++++++++++- detectors/aws/ecs/go.sum | 2 - ...=> metadatav4-response-container-ec2.json} | 0 ...metadatav4-response-container-fargate.json | 50 ++++++++++++ ...json => metadatav4-response-task-ec2.json} | 0 .../metadatav4-response-task-fargate.json | 77 +++++++++++++++++++ 6 files changed, 182 insertions(+), 6 deletions(-) rename detectors/aws/ecs/testdata/{metadatav4-response-container.json => metadatav4-response-container-ec2.json} (100%) create mode 100644 detectors/aws/ecs/testdata/metadatav4-response-container-fargate.json rename detectors/aws/ecs/testdata/{metadatav4-response-task.json => metadatav4-response-task-ec2.json} (100%) create mode 100644 detectors/aws/ecs/testdata/metadatav4-response-task-fargate.json diff --git a/detectors/aws/ecs/ecs_test.go b/detectors/aws/ecs/ecs_test.go index 81aa37a52bf..8016c0c718b 100644 --- a/detectors/aws/ecs/ecs_test.go +++ b/detectors/aws/ecs/ecs_test.go @@ -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) } @@ -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() diff --git a/detectors/aws/ecs/go.sum b/detectors/aws/ecs/go.sum index b6e8c2d4bfe..51e9d87306a 100644 --- a/detectors/aws/ecs/go.sum +++ b/detectors/aws/ecs/go.sum @@ -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= diff --git a/detectors/aws/ecs/testdata/metadatav4-response-container.json b/detectors/aws/ecs/testdata/metadatav4-response-container-ec2.json similarity index 100% rename from detectors/aws/ecs/testdata/metadatav4-response-container.json rename to detectors/aws/ecs/testdata/metadatav4-response-container-ec2.json diff --git a/detectors/aws/ecs/testdata/metadatav4-response-container-fargate.json b/detectors/aws/ecs/testdata/metadatav4-response-container-fargate.json new file mode 100644 index 00000000000..ccbe70bc44a --- /dev/null +++ b/detectors/aws/ecs/testdata/metadatav4-response-container-fargate.json @@ -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" +} \ No newline at end of file diff --git a/detectors/aws/ecs/testdata/metadatav4-response-task.json b/detectors/aws/ecs/testdata/metadatav4-response-task-ec2.json similarity index 100% rename from detectors/aws/ecs/testdata/metadatav4-response-task.json rename to detectors/aws/ecs/testdata/metadatav4-response-task-ec2.json diff --git a/detectors/aws/ecs/testdata/metadatav4-response-task-fargate.json b/detectors/aws/ecs/testdata/metadatav4-response-task-fargate.json new file mode 100644 index 00000000000..7979db708da --- /dev/null +++ b/detectors/aws/ecs/testdata/metadatav4-response-task-fargate.json @@ -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" +} \ No newline at end of file