-
Notifications
You must be signed in to change notification settings - Fork 496
/
detector_test.go
44 lines (36 loc) · 1.28 KB
/
detector_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package lambda
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)
// successfully return resource when process is running on Amazon Lambda environment
func TestDetectSuccess(t *testing.T) {
os.Clearenv()
_ = os.Setenv(lambdaFunctionNameEnvVar, "testFunction")
_ = os.Setenv(awsRegionEnvVar, "us-texas-1")
_ = os.Setenv(lambdaFunctionVersionEnvVar, "$LATEST")
attributes := []attribute.KeyValue{
semconv.CloudProviderAWS,
semconv.CloudRegionKey.String("us-texas-1"),
semconv.FaaSNameKey.String("testFunction"),
semconv.FaaSVersionKey.String("$LATEST"),
}
expectedResource := resource.NewWithAttributes(semconv.SchemaURL, attributes...)
detector := resourceDetector{}
res, err := detector.Detect(context.Background())
assert.Nil(t, err, "Detector unexpectedly returned error")
assert.Equal(t, expectedResource, res, "Resource returned is incorrect")
}
// return empty resource when not running on lambda
func TestReturnsIfNoEnvVars(t *testing.T) {
os.Clearenv()
detector := resourceDetector{}
res, err := detector.Detect(context.Background())
assert.Equal(t, errNotOnLambda, err)
assert.Equal(t, 0, len(res.Attributes()))
}