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
|
package tracing
import (
"context"
"os"
"reflect"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/labkit/correlation"
)
func TestNewEnvInjector(t *testing.T) {
tests := []struct {
name string
tracingEnv string
correlationID string
env []string
wantEnv []string
}{
{
name: "trivial",
},
{
name: "gitlab_tracing_config",
tracingEnv: "opentracing://null",
wantEnv: []string{
"GITLAB_TRACING=opentracing://null",
},
},
{
name: "merge",
tracingEnv: "opentracing://null",
env: []string{
"A=1",
},
wantEnv: []string{
"A=1",
"GITLAB_TRACING=opentracing://null",
},
},
{
name: "correlation",
correlationID: "abc123",
env: []string{},
wantEnv: []string{
"CORRELATION_ID=abc123",
},
},
{
name: "correlation_merge",
tracingEnv: "opentracing://null",
correlationID: "abc123",
env: []string{"A=1"},
wantEnv: []string{
"A=1",
"CORRELATION_ID=abc123",
"GITLAB_TRACING=opentracing://null",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.tracingEnv != "" {
resetEnvironment := modifyEnvironment(tracingEnvKey, tt.tracingEnv)
defer resetEnvironment()
} else {
resetEnvironment := clearEnvironment(tracingEnvKey)
defer resetEnvironment()
}
envInjector := NewEnvInjector()
ctx := context.Background()
if tt.correlationID != "" {
ctx = correlation.ContextWithCorrelation(ctx, tt.correlationID)
}
got := envInjector(ctx, tt.env)
require.Equal(t, tt.wantEnv, got)
})
}
}
// modifyEnvironment will change an environment variable and return a func suitable
// for `defer` to change the value back.
func modifyEnvironment(key string, value string) func() {
oldValue, hasOldValue := os.LookupEnv(key)
os.Setenv(key, value)
return func() {
if hasOldValue {
os.Setenv(key, oldValue)
} else {
os.Unsetenv(key)
}
}
}
// clearEnvironment will clear an environment variable and return a func suitable
// for `defer` to change the value back.
func clearEnvironment(key string) func() {
oldValue, hasOldValue := os.LookupEnv(key)
if !hasOldValue {
return func() {
// Nothing to do
}
}
os.Unsetenv(key)
return func() {
if hasOldValue {
os.Setenv(key, oldValue)
}
}
}
func Test_appendMapToEnv(t *testing.T) {
tests := []struct {
name string
env []string
envMap map[string]string
want []string
}{
{
name: "empty_case",
env: nil,
envMap: nil,
want: nil,
},
{
name: "no_new_values",
env: []string{"A=1"},
envMap: nil,
want: []string{"A=1"},
},
{
name: "no_original_values",
env: nil,
envMap: map[string]string{"A": "1"},
want: []string{"A=1"},
},
{
name: "merge_values",
env: []string{"A=1"},
envMap: map[string]string{"B": "2"},
want: []string{"A=1", "B=2"},
},
{
name: "merge_conflicts",
env: []string{"A=1", "C=3"},
envMap: map[string]string{"B": "2", "C": "4"},
want: []string{"A=1", "C=3", "B=2", "C=4"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := appendMapToEnv(tt.env, tt.envMap); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendMapToEnv() = %v, want %v", got, tt.want)
}
})
}
}
|