-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathddlambda_test.go
More file actions
205 lines (190 loc) · 4.53 KB
/
ddlambda_test.go
File metadata and controls
205 lines (190 loc) · 4.53 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Unless explicitly stated otherwise all files in this repository are licensed
* under the Apache License Version 2.0.
*
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2021 Datadog, Inc.
*/
package ddlambda
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInvokeDryRun(t *testing.T) {
t.Setenv(UniversalInstrumentation, "false")
t.Setenv(DatadogTraceEnabledEnvVar, "false")
called := false
_, err := InvokeDryRun(func(ctx context.Context) {
called = true
globalCtx := GetContext()
assert.Equal(t, globalCtx, ctx)
}, nil)
assert.NoError(t, err)
assert.True(t, called)
}
func TestMetricsSilentFailWithoutWrapper(t *testing.T) {
Metric("my-metric", 100, "my:tag")
}
func TestMetricsSubmitWithWrapper(t *testing.T) {
called := false
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
w.WriteHeader(http.StatusCreated)
}))
defer server.Close()
_, err := InvokeDryRun(func(ctx context.Context) {
Metric("my-metric", 100, "my:tag")
}, &Config{
APIKey: "abc-123",
Site: server.URL,
})
assert.NoError(t, err)
assert.True(t, called)
}
func TestToMetricConfigLocalTest(t *testing.T) {
testcases := []struct {
envs map[string]string
cval bool
}{
{
envs: map[string]string{"DD_LOCAL_TEST": "True"},
cval: true,
},
{
envs: map[string]string{"DD_LOCAL_TEST": "true"},
cval: true,
},
{
envs: map[string]string{"DD_LOCAL_TEST": "1"},
cval: true,
},
{
envs: map[string]string{"DD_LOCAL_TEST": "False"},
cval: false,
},
{
envs: map[string]string{"DD_LOCAL_TEST": "false"},
cval: false,
},
{
envs: map[string]string{"DD_LOCAL_TEST": "0"},
cval: false,
},
{
envs: map[string]string{"DD_LOCAL_TEST": ""},
cval: false,
},
{
envs: map[string]string{},
cval: false,
},
}
cfg := Config{}
for _, tc := range testcases {
t.Run(fmt.Sprintf("%#v", tc.envs), func(t *testing.T) {
for k, v := range tc.envs {
_ = os.Setenv(k, v)
}
mc := cfg.toMetricsConfig(true)
assert.Equal(t, tc.cval, mc.LocalTest)
})
}
}
func TestCalculateFipsMode(t *testing.T) {
// Save original environment to restore later
originalRegion := os.Getenv("AWS_REGION")
originalFipsMode := os.Getenv(FIPSModeEnvVar)
defer func() {
_ = os.Setenv("AWS_REGION", originalRegion)
_ = os.Setenv(FIPSModeEnvVar, originalFipsMode)
}()
testCases := []struct {
name string
configFIPSMode *bool
region string
fipsModeEnv string
expected bool
}{
{
name: "Config explicit true",
configFIPSMode: boolPtr(true),
region: "us-east-1",
fipsModeEnv: "",
expected: true,
},
{
name: "Config explicit false",
configFIPSMode: boolPtr(false),
region: "us-gov-west-1",
fipsModeEnv: "",
expected: false,
},
{
name: "GovCloud default true",
configFIPSMode: nil,
region: "us-gov-east-1",
fipsModeEnv: "",
expected: true,
},
{
name: "Non-GovCloud default false",
configFIPSMode: nil,
region: "us-east-1",
fipsModeEnv: "",
expected: false,
},
{
name: "Env var override to true",
configFIPSMode: nil,
region: "us-east-1",
fipsModeEnv: "true",
expected: true,
},
{
name: "Env var override to false",
configFIPSMode: nil,
region: "us-gov-west-1",
fipsModeEnv: "false",
expected: false,
},
{
name: "Invalid env var in GovCloud",
configFIPSMode: nil,
region: "us-gov-west-1",
fipsModeEnv: "invalid",
expected: true,
},
{
name: "Invalid env var in non-GovCloud",
configFIPSMode: nil,
region: "us-east-1",
fipsModeEnv: "invalid",
expected: false,
},
{
name: "Config takes precedence over env and region",
configFIPSMode: boolPtr(true),
region: "us-east-1",
fipsModeEnv: "false",
expected: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_ = os.Setenv("AWS_REGION", tc.region)
_ = os.Setenv(FIPSModeEnvVar, tc.fipsModeEnv)
cfg := &Config{FIPSMode: tc.configFIPSMode}
result := cfg.calculateFipsMode()
assert.Equal(t, tc.expected, result, "calculateFipsMode returned incorrect value")
})
}
}
// Helper function to create bool pointers
func boolPtr(b bool) *bool {
return &b
}