-
Notifications
You must be signed in to change notification settings - Fork 39
/
generic_serverless_agent_test.go
105 lines (77 loc) · 2.49 KB
/
generic_serverless_agent_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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// (c) Copyright IBM Corp. 2024
//go:build generic_serverless && integration
// +build generic_serverless,integration
package instana_test
import (
"context"
"encoding/json"
"log"
"os"
"testing"
"time"
instana "github.com/instana/go-sensor"
"github.com/stretchr/testify/require"
)
var agent *serverlessAgent
func TestMain(m *testing.M) {
teardownInstanaEnv := setupInstanaEnv()
defer teardownInstanaEnv()
var err error
agent, err = setupServerlessAgent()
if err != nil {
log.Fatalf("failed to initialize serverless agent: %s", err)
}
os.Exit(m.Run())
}
func TestLocalServerlessAgent_SendSpans(t *testing.T) {
defer agent.Reset()
tracer := instana.NewTracer()
sensor := instana.NewSensorWithTracer(tracer)
defer instana.ShutdownSensor()
sp := sensor.Tracer().StartSpan("generic_serverless")
sp.Finish()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, tracer.Flush(ctx))
require.Len(t, agent.Bundles, 1)
var spans []map[string]json.RawMessage
for _, bundle := range agent.Bundles {
var payload struct {
Spans []map[string]json.RawMessage `json:"spans"`
}
require.NoError(t, json.Unmarshal(bundle.Body, &payload), "%s", string(bundle.Body))
spans = append(spans, payload.Spans...)
}
require.Len(t, spans, 1)
}
func TestLocalServerlessAgent_SendSpans_Error(t *testing.T) {
defer agent.Reset()
tracer := instana.NewTracer()
sensor := instana.NewSensorWithTracer(tracer)
defer instana.ShutdownSensor()
sp := sensor.Tracer().StartSpan("http")
sp.SetTag("returnError", "true")
sp.Finish()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
require.NoError(t, tracer.Flush(ctx))
require.Len(t, agent.Bundles, 0)
}
func setupInstanaEnv() func() {
var teardownFuncs []func()
teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("INSTANA_AGENT_KEY"))
os.Setenv("INSTANA_AGENT_KEY", "testkey1")
teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("INSTANA_ZONE"))
os.Setenv("INSTANA_ZONE", "testzone")
teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("INSTANA_TAGS"))
os.Setenv("INSTANA_TAGS", "key1=value1,key2")
teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("INSTANA_SECRETS"))
os.Setenv("INSTANA_SECRETS", "contains-ignore-case:key,password,secret,classified")
teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("CLASSIFIED_DATA"))
os.Setenv("CLASSIFIED_DATA", "classified")
return func() {
for _, f := range teardownFuncs {
f()
}
}
}