|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2016-present Datadog, Inc. |
| 5 | + |
| 6 | +//go:build unix && !noexecwrapper |
| 7 | + |
| 8 | +package lambda |
| 9 | + |
| 10 | +import ( |
| 11 | + "os" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestExecAwsLambdaExecWrapperNotSet(t *testing.T) { |
| 18 | + exec, execCalled := mockExec(t, "<nope>") |
| 19 | + execAwsLambdaExecWrapper( |
| 20 | + mockedGetenv(t, ""), |
| 21 | + exec, |
| 22 | + ) |
| 23 | + require.False(t, *execCalled) |
| 24 | +} |
| 25 | + |
| 26 | +func TestExecAwsLambdaExecWrapperSet(t *testing.T) { |
| 27 | + wrapper := "/path/to/wrapper/entry/point" |
| 28 | + exec, execCalled := mockExec(t, wrapper) |
| 29 | + execAwsLambdaExecWrapper( |
| 30 | + mockedGetenv(t, wrapper), |
| 31 | + exec, |
| 32 | + ) |
| 33 | + require.True(t, *execCalled) |
| 34 | +} |
| 35 | + |
| 36 | +func mockExec(t *testing.T, value string) (mock func(string, []string, []string) error, called *bool) { |
| 37 | + mock = func(argv0 string, argv []string, envv []string) error { |
| 38 | + *called = true |
| 39 | + require.Equal(t, value, argv0) |
| 40 | + require.Equal(t, append([]string{value}, os.Args...), argv) |
| 41 | + require.Equal(t, awsLambdaExecWrapper+"=", envv[len(envv)-1]) |
| 42 | + return nil |
| 43 | + } |
| 44 | + called = ptrTo(false) |
| 45 | + return |
| 46 | +} |
| 47 | + |
| 48 | +func mockedGetenv(t *testing.T, value string) func(string) string { |
| 49 | + return func(key string) string { |
| 50 | + require.Equal(t, awsLambdaExecWrapper, key) |
| 51 | + return value |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func ptrTo[T any](val T) *T { |
| 56 | + return &val |
| 57 | +} |
0 commit comments