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