-
Notifications
You must be signed in to change notification settings - Fork 39
/
azure_container_apps_test.go
132 lines (97 loc) · 3.49 KB
/
azure_container_apps_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// (c) Copyright IBM Corp. 2024
//go:build azureContainerApps && integration
// +build azureContainerApps,integration
package instana_test
import (
"context"
"encoding/json"
"log"
"os"
"testing"
"time"
instana "github.com/instana/go-sensor"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var agent *serverlessAgent
func TestMain(m *testing.M) {
teardownAzureEnv := setupAzureContainerAppsEnv()
defer teardownAzureEnv()
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 TestIntegration_AzureContainerApps_SendSpans(t *testing.T) {
defer agent.Reset()
tracer := instana.NewTracer()
sensor := instana.NewSensorWithTracer(tracer)
defer instana.ShutdownSensor()
sp := sensor.Tracer().StartSpan("aca")
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)
assert.JSONEq(t, `{"hl": true, "cp": "azure", "e": "/subscriptions/testgh05-3f0d-4bf9-8f53-209408003632/resourceGroups/testresourcegroup/providers/Microsoft.App/containerapps/azureapp"}`, string(spans[0]["f"]))
}
func TestIntegration_AzureAgent_SendSpans_Error(t *testing.T) {
defer agent.Reset()
tracer := instana.NewTracer()
sensor := instana.NewSensorWithTracer(tracer)
defer instana.ShutdownSensor()
sp := sensor.Tracer().StartSpan("azf")
sp.SetTag("returnError", "true")
sp.Finish()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, tracer.Flush(ctx))
require.Len(t, agent.Bundles, 0)
}
func setupAzureContainerAppsEnv() func() {
var teardownFuncs []func()
teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("AZURE_SUBSCRIPTION_ID"))
os.Setenv("AZURE_SUBSCRIPTION_ID", "testgh05-3f0d-4bf9-8f53-209408003632")
teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("AZURE_RESOURCE_GROUP"))
os.Setenv("AZURE_RESOURCE_GROUP", "testresourcegroup")
teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("CONTAINER_APP_NAME"))
os.Setenv("CONTAINER_APP_NAME", "azureapp")
teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("CONTAINER_APP_HOSTNAME"))
os.Setenv("CONTAINER_APP_HOSTNAME", "azure_app_host")
return func() {
for _, f := range teardownFuncs {
f()
}
}
}
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()
}
}
}