| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package eks |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/mock" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
"go.opentelemetry.io/otel/attribute" |
| 14 |
"go.opentelemetry.io/otel/sdk/resource" |
| 15 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 16 |
"k8s.io/client-go/rest" |
| 17 |
) |
| 18 |
|
| 19 |
type MockDetectorUtils struct { |
| 20 |
mock.Mock |
| 21 |
} |
| 22 |
|
| 23 |
// Mock function for fileExists(). |
| 24 |
func (detectorUtils *MockDetectorUtils) fileExists(filename string) bool { |
| 25 |
args := detectorUtils.Called(filename) |
| 26 |
return args.Bool(0) |
| 27 |
} |
| 28 |
|
| 29 |
// Mock function for getConfigMap(). |
| 30 |
func (detectorUtils *MockDetectorUtils) getConfigMap(_ context.Context, namespace, name string) (map[string]string, error) { |
| 31 |
args := detectorUtils.Called(namespace, name) |
| 32 |
return args.Get(0).(map[string]string), args.Error(1) |
| 33 |
} |
| 34 |
|
| 35 |
// Mock function for getContainerID(). |
| 36 |
func (detectorUtils *MockDetectorUtils) getContainerID() (string, error) { |
| 37 |
args := detectorUtils.Called() |
| 38 |
return args.String(0), args.Error(1) |
| 39 |
} |
| 40 |
|
| 41 |
// Tests EKS resource detector running in EKS environment. |
| 42 |
func TestEks(t *testing.T) { |
| 43 |
detectorUtils := new(MockDetectorUtils) |
| 44 |
|
| 45 |
// Mock functions and set expectations |
| 46 |
detectorUtils.On("fileExists", k8sTokenPath).Return(true) |
| 47 |
detectorUtils.On("fileExists", k8sCertPath).Return(true) |
| 48 |
detectorUtils.On("getConfigMap", authConfigmapNS, authConfigmapName).Return(map[string]string{"not": "nil"}, nil) |
| 49 |
detectorUtils.On("getConfigMap", cwConfigmapNS, cwConfigmapName).Return(map[string]string{"cluster.name": "my-cluster"}, nil) |
| 50 |
detectorUtils.On("getContainerID").Return("0123456789A", nil) |
| 51 |
|
| 52 |
// Expected resource object |
| 53 |
eksResourceLabels := []attribute.KeyValue{ |
| 54 |
semconv.CloudProviderAWS, |
| 55 |
semconv.CloudPlatformAWSEKS, |
| 56 |
semconv.K8SClusterName("my-cluster"), |
| 57 |
semconv.ContainerID("0123456789A"), |
| 58 |
} |
| 59 |
expectedResource := resource.NewWithAttributes(semconv.SchemaURL, eksResourceLabels...) |
| 60 |
|
| 61 |
// Call EKS Resource detector to detect resources |
| 62 |
eksResourceDetector := resourceDetector{utils: detectorUtils} |
| 63 |
resourceObj, err := eksResourceDetector.Detect(t.Context()) |
| 64 |
require.NoError(t, err) |
| 65 |
|
| 66 |
assert.Equal(t, expectedResource, resourceObj, "Resource object returned is incorrect") |
| 67 |
detectorUtils.AssertExpectations(t) |
| 68 |
} |
| 69 |
|
| 70 |
// Tests EKS resource detector not running in EKS environment. |
| 71 |
func TestNotEKS(t *testing.T) { |
| 72 |
detectorUtils := new(MockDetectorUtils) |
| 73 |
|
| 74 |
k8sTokenPath := "/var/run/secrets/kubernetes.io/serviceaccount/token" |
| 75 |
|
| 76 |
// Mock functions and set expectations |
| 77 |
detectorUtils.On("fileExists", k8sTokenPath).Return(false) |
| 78 |
|
| 79 |
detector := resourceDetector{utils: detectorUtils} |
| 80 |
r, err := detector.Detect(t.Context()) |
| 81 |
require.NoError(t, err) |
| 82 |
assert.Equal(t, resource.Empty(), r, "Resource object should be empty") |
| 83 |
detectorUtils.AssertExpectations(t) |
| 84 |
} |
| 85 |
|
| 86 |
// Tests EKS resource detector not running K8S at all. |
| 87 |
func TestNotK8S(t *testing.T) { |
| 88 |
detectorUtils := new(MockDetectorUtils) |
| 89 |
detector := resourceDetector{utils: detectorUtils, err: rest.ErrNotInCluster} |
| 90 |
r, err := detector.Detect(t.Context()) |
| 91 |
require.NoError(t, err) |
| 92 |
assert.Equal(t, resource.Empty(), r, "Resource object should be empty") |
| 93 |
detectorUtils.AssertExpectations(t) |
| 94 |
} |