|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package ecs |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "net/http" |
| 20 | + "net/http/httptest" |
| 21 | + "os" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + ecs "go.opentelemetry.io/contrib/detectors/aws/ecs" |
| 26 | + "go.opentelemetry.io/otel/attribute" |
| 27 | + "go.opentelemetry.io/otel/sdk/resource" |
| 28 | + semconv "go.opentelemetry.io/otel/semconv/v1.12.0" |
| 29 | + |
| 30 | + "github.com/stretchr/testify/assert" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + metadataV4EnvVar = "ECS_CONTAINER_METADATA_URI_V4" |
| 35 | +) |
| 36 | + |
| 37 | +// successfully returns resource when process is running on Amazon ECS environment |
| 38 | +// with Metadata v4 with the EC2 Launch type. |
| 39 | +func TestDetectV4LaunchTypeEc2(t *testing.T) { |
| 40 | + testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { |
| 41 | + if strings.HasSuffix(req.URL.String(), "/task") { |
| 42 | + content, err := os.ReadFile("metadatav4-response-task-ec2.json") |
| 43 | + if err == nil { |
| 44 | + _, err = res.Write(content) |
| 45 | + if err != nil { |
| 46 | + t.Fatal(err) |
| 47 | + } |
| 48 | + } |
| 49 | + } else { |
| 50 | + content, err := os.ReadFile("metadatav4-response-container-ec2.json") |
| 51 | + if err == nil { |
| 52 | + _, err = res.Write(content) |
| 53 | + if err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + })) |
| 59 | + defer testServer.Close() |
| 60 | + |
| 61 | + os.Clearenv() |
| 62 | + _ = os.Setenv(metadataV4EnvVar, testServer.URL) |
| 63 | + |
| 64 | + hostname, err := os.Hostname() |
| 65 | + assert.NoError(t, err, "Error") |
| 66 | + |
| 67 | + attributes := []attribute.KeyValue{ |
| 68 | + semconv.CloudProviderAWS, |
| 69 | + semconv.CloudPlatformAWSECS, |
| 70 | + semconv.ContainerNameKey.String(hostname), |
| 71 | + // We are not running the test in an actual container, |
| 72 | + // the container id is tested with mocks of the cgroup |
| 73 | + // file in the unit tests |
| 74 | + semconv.ContainerIDKey.String(""), |
| 75 | + semconv.AWSECSContainerARNKey.String("arn:aws:ecs:us-west-2:111122223333:container/0206b271-b33f-47ab-86c6-a0ba208a70a9"), |
| 76 | + semconv.AWSECSClusterARNKey.String("arn:aws:ecs:us-west-2:111122223333:cluster/default"), |
| 77 | + semconv.AWSECSLaunchtypeKey.String("ec2"), |
| 78 | + semconv.AWSECSTaskARNKey.String("arn:aws:ecs:us-west-2:111122223333:task/default/158d1c8083dd49d6b527399fd6414f5c"), |
| 79 | + semconv.AWSECSTaskFamilyKey.String("curltest"), |
| 80 | + semconv.AWSECSTaskRevisionKey.String("26"), |
| 81 | + semconv.AWSLogGroupNamesKey.String("/ecs/metadata"), |
| 82 | + semconv.AWSLogGroupARNsKey.String("arn:aws:logs:us-west-2:111122223333:log-group:/ecs/metadata:*"), |
| 83 | + semconv.AWSLogStreamNamesKey.String("ecs/curl/8f03e41243824aea923aca126495f665"), |
| 84 | + semconv.AWSLogStreamARNsKey.String("arn:aws:logs:us-west-2:111122223333:log-group:/ecs/metadata:log-stream:ecs/curl/8f03e41243824aea923aca126495f665"), |
| 85 | + } |
| 86 | + expectedResource := resource.NewWithAttributes(semconv.SchemaURL, attributes...) |
| 87 | + detector := ecs.NewResourceDetector() |
| 88 | + res, err := detector.Detect(context.Background()) |
| 89 | + |
| 90 | + assert.Equal(t, nil, err, "Detector should not fail") |
| 91 | + assert.Equal(t, expectedResource, res, "Resource returned is incorrect") |
| 92 | +} |
| 93 | + |
| 94 | +// successfully returns resource when process is running on Amazon ECS environment |
| 95 | +// with Metadata v4 with the Fargate Launch type. |
| 96 | +func TestDetectV4LaunchTypeFargate(t *testing.T) { |
| 97 | + testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { |
| 98 | + if strings.HasSuffix(req.URL.String(), "/task") { |
| 99 | + content, err := os.ReadFile("metadatav4-response-task-fargate.json") |
| 100 | + if err == nil { |
| 101 | + _, err = res.Write(content) |
| 102 | + if err != nil { |
| 103 | + panic(err) |
| 104 | + } |
| 105 | + } |
| 106 | + } else { |
| 107 | + content, err := os.ReadFile("metadatav4-response-container-fargate.json") |
| 108 | + if err == nil { |
| 109 | + _, err = res.Write(content) |
| 110 | + if err != nil { |
| 111 | + panic(err) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + })) |
| 116 | + defer testServer.Close() |
| 117 | + |
| 118 | + os.Clearenv() |
| 119 | + _ = os.Setenv(metadataV4EnvVar, testServer.URL) |
| 120 | + |
| 121 | + hostname, err := os.Hostname() |
| 122 | + assert.NoError(t, err, "Error") |
| 123 | + |
| 124 | + attributes := []attribute.KeyValue{ |
| 125 | + semconv.CloudProviderAWS, |
| 126 | + semconv.CloudPlatformAWSECS, |
| 127 | + semconv.ContainerNameKey.String(hostname), |
| 128 | + // We are not running the test in an actual container, |
| 129 | + // the container id is tested with mocks of the cgroup |
| 130 | + // file in the unit tests |
| 131 | + semconv.ContainerIDKey.String(""), |
| 132 | + semconv.AWSECSContainerARNKey.String("arn:aws:ecs:us-west-2:111122223333:container/05966557-f16c-49cb-9352-24b3a0dcd0e1"), |
| 133 | + semconv.AWSECSClusterARNKey.String("arn:aws:ecs:us-west-2:111122223333:cluster/default"), |
| 134 | + semconv.AWSECSLaunchtypeKey.String("fargate"), |
| 135 | + semconv.AWSECSTaskARNKey.String("arn:aws:ecs:us-west-2:111122223333:task/default/e9028f8d5d8e4f258373e7b93ce9a3c3"), |
| 136 | + semconv.AWSECSTaskFamilyKey.String("curltest"), |
| 137 | + semconv.AWSECSTaskRevisionKey.String("3"), |
| 138 | + semconv.AWSLogGroupNamesKey.String("/ecs/containerlogs"), |
| 139 | + semconv.AWSLogGroupARNsKey.String("arn:aws:logs:us-west-2:111122223333:log-group:/ecs/containerlogs:*"), |
| 140 | + semconv.AWSLogStreamNamesKey.String("ecs/curl/cd189a933e5849daa93386466019ab50"), |
| 141 | + semconv.AWSLogStreamARNsKey.String("arn:aws:logs:us-west-2:111122223333:log-group:/ecs/containerlogs:log-stream:ecs/curl/cd189a933e5849daa93386466019ab50"), |
| 142 | + } |
| 143 | + expectedResource := resource.NewWithAttributes(semconv.SchemaURL, attributes...) |
| 144 | + detector := ecs.NewResourceDetector() |
| 145 | + res, err := detector.Detect(context.Background()) |
| 146 | + |
| 147 | + assert.Equal(t, nil, err, "Detector should not fail") |
| 148 | + assert.Equal(t, expectedResource, res, "Resource returned is incorrect") |
| 149 | +} |
0 commit comments