|
| 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 | + "os" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/bmizerany/assert" |
| 23 | + "github.com/stretchr/testify/mock" |
| 24 | + |
| 25 | + "go.opentelemetry.io/otel/label" |
| 26 | + "go.opentelemetry.io/otel/sdk/resource" |
| 27 | + "go.opentelemetry.io/otel/semconv" |
| 28 | +) |
| 29 | + |
| 30 | +// Create interface for functions that need to be mocked |
| 31 | +type MockDetectorUtils struct { |
| 32 | + mock.Mock |
| 33 | +} |
| 34 | + |
| 35 | +func (detectorUtils *MockDetectorUtils) getContainerID() (string, error) { |
| 36 | + args := detectorUtils.Called() |
| 37 | + return args.String(0), args.Error(1) |
| 38 | +} |
| 39 | + |
| 40 | +func (detectorUtils *MockDetectorUtils) getContainerName() (string, error) { |
| 41 | + args := detectorUtils.Called() |
| 42 | + return args.String(0), args.Error(1) |
| 43 | +} |
| 44 | + |
| 45 | +//succesfully return resource when process is running on Amazon ECS environment |
| 46 | +func TestDetect(t *testing.T) { |
| 47 | + os.Clearenv() |
| 48 | + os.Setenv(metadataV3EnvVar, "3") |
| 49 | + os.Setenv(metadataV4EnvVar, "4") |
| 50 | + |
| 51 | + detectorUtils := new(MockDetectorUtils) |
| 52 | + |
| 53 | + detectorUtils.On("getContainerName").Return("container-Name", nil) |
| 54 | + detectorUtils.On("getContainerID").Return("0123456789A", nil) |
| 55 | + |
| 56 | + labels := []label.KeyValue{ |
| 57 | + semconv.ContainerNameKey.String("container-Name"), |
| 58 | + semconv.ContainerIDKey.String("0123456789A"), |
| 59 | + } |
| 60 | + expectedResource := resource.NewWithAttributes(labels...) |
| 61 | + detector := ResourceDetector{detectorUtils} |
| 62 | + resource, _ := detector.Detect(context.Background()) |
| 63 | + |
| 64 | + assert.Equal(t, resource, expectedResource, "Resource returned is incorrect") |
| 65 | +} |
| 66 | + |
| 67 | +//returns empty resource when detector cannot read container ID |
| 68 | +func TestDetectCannotReadContainerID(t *testing.T) { |
| 69 | + os.Clearenv() |
| 70 | + os.Setenv(metadataV3EnvVar, "3") |
| 71 | + os.Setenv(metadataV4EnvVar, "4") |
| 72 | + detectorUtils := new(MockDetectorUtils) |
| 73 | + |
| 74 | + detectorUtils.On("getContainerName").Return("container-Name", nil) |
| 75 | + detectorUtils.On("getContainerID").Return("", errCannotReadContainerID) |
| 76 | + |
| 77 | + detector := ResourceDetector{detectorUtils} |
| 78 | + resource, err := detector.Detect(context.Background()) |
| 79 | + |
| 80 | + assert.Equal(t, errCannotReadContainerID, err) |
| 81 | + assert.Equal(t, 0, len(resource.Attributes())) |
| 82 | +} |
| 83 | + |
| 84 | +//returns empty resource when process is not running ECS |
| 85 | +func TestReturnsIfNoEnvVars(t *testing.T) { |
| 86 | + os.Clearenv() |
| 87 | + detector := ResourceDetector{} |
| 88 | + resource, err := detector.Detect(context.Background()) |
| 89 | + |
| 90 | + assert.Equal(t, errNotOnECS, err) |
| 91 | + assert.Equal(t, 0, len(resource.Attributes())) |
| 92 | +} |
0 commit comments