github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/env_config_internal_test.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2020 3 4 package instana 5 6 import ( 7 "errors" 8 "regexp" 9 "testing" 10 "time" 11 12 "github.com/instana/go-sensor/secrets" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestParseInstanaTags(t *testing.T) { 18 examples := map[string]struct { 19 Value string 20 Expected map[string]interface{} 21 }{ 22 "empty": {"", nil}, 23 "single tag, empty key": {"=value", nil}, 24 "single tag, no value": {"key", map[string]interface{}{"key": nil}}, 25 "single tag, empty value": {"key=", map[string]interface{}{"key": ""}}, 26 "single tag, with value": {"key=value", map[string]interface{}{"key": "value"}}, 27 "multiple tags, mixed": { 28 `key1, key2= , key3 ="",key4=42`, 29 map[string]interface{}{ 30 "key1": nil, 31 "key2": " ", 32 "key3": `""`, 33 "key4": "42", 34 }, 35 }, 36 } 37 38 for name, example := range examples { 39 t.Run(name, func(t *testing.T) { 40 assert.Equal(t, example.Expected, parseInstanaTags(example.Value)) 41 }) 42 } 43 } 44 func TestParseInstanaEmptySecrets(t *testing.T) { 45 examples := map[string]struct { 46 Value string 47 Expected error 48 }{ 49 "empty": {"", errors.New("empty value for secret matcher configuration")}, 50 } 51 52 for name, example := range examples { 53 t.Run(name, func(t *testing.T) { 54 _, err := parseInstanaSecrets(example.Value) 55 require.Error(t, err) 56 assert.Equal(t, example.Expected, err) 57 }) 58 } 59 } 60 61 func TestParseInstanaSecrets(t *testing.T) { 62 regexMatcher, err := secrets.NewRegexpMatcher(regexp.MustCompile("a|b|c"), regexp.MustCompile("d")) 63 require.NoError(t, err) 64 65 examples := map[string]struct { 66 Value string 67 Expected Matcher 68 }{ 69 "equals": {"equals:a,b,c", secrets.NewEqualsMatcher("a", "b", "c")}, 70 "equals-ignore-case": {"equals-ignore-case:a,b,c", secrets.NewEqualsIgnoreCaseMatcher("a", "b", "c")}, 71 "contains": {"contains:a,b,c", secrets.NewContainsMatcher("a", "b", "c")}, 72 "contains-ignore-case": {"contains-ignore-case:a,b,c", secrets.NewContainsIgnoreCaseMatcher("a", "b", "c")}, 73 "regexp": {"regex:a|b|c,d", regexMatcher}, 74 } 75 76 for name, example := range examples { 77 t.Run(name, func(t *testing.T) { 78 m, err := parseInstanaSecrets(example.Value) 79 require.NoError(t, err) 80 assert.Equal(t, example.Expected, m) 81 }) 82 } 83 } 84 85 func TestParseInstanaSecrets_Error(t *testing.T) { 86 examples := map[string]string{ 87 "unknown matcher": "magic:pew,pew", 88 "malformed": "equals;a,b,c", 89 } 90 91 for name, example := range examples { 92 t.Run(name, func(t *testing.T) { 93 _, err := parseInstanaSecrets(example) 94 assert.Error(t, err) 95 }) 96 } 97 } 98 99 func TestParseInstanaExtraHTTPHeaders(t *testing.T) { 100 examples := map[string]struct { 101 Value string 102 Expected []string 103 }{ 104 "empty": {"", nil}, 105 "one": {"a", []string{"a"}}, 106 "multiple": {"a; ; b ;c;", []string{"a", "b", "c"}}, 107 } 108 109 for name, example := range examples { 110 t.Run(name, func(t *testing.T) { 111 assert.Equal(t, example.Expected, parseInstanaExtraHTTPHeaders(example.Value)) 112 }) 113 } 114 } 115 116 func TestParseInstanaTimeout(t *testing.T) { 117 examples := map[string]struct { 118 Value string 119 Expected time.Duration 120 }{ 121 "empty": {"", defaultServerlessTimeout}, 122 "positive integer": {"123", 123 * time.Millisecond}, 123 } 124 125 for name, example := range examples { 126 t.Run(name, func(t *testing.T) { 127 d, err := parseInstanaTimeout(example.Value) 128 require.NoError(t, err) 129 assert.Equal(t, example.Expected, d) 130 }) 131 } 132 } 133 134 func TestParseInstanaTimeout_Error(t *testing.T) { 135 examples := map[string]string{ 136 "non-number": "twenty", 137 "non-integer": "12.5:", 138 "zero": "0", 139 "negative integer": "-100", 140 } 141 142 for name, example := range examples { 143 t.Run(name, func(t *testing.T) { 144 _, err := parseInstanaTimeout(example) 145 assert.Error(t, err) 146 }) 147 } 148 }