github.com/magnusbaeck/logstash-filter-verifier/v2@v2.0.0-pre.1/testcase/helper_test.go (about) 1 package testcase 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestExtractBracketFields(t *testing.T) { 10 cases := []struct { 11 key string 12 expected []string 13 }{ 14 // Correct nested field notation. 15 { 16 "[log][file][path]", 17 []string{"log", "file", "path"}, 18 }, 19 // Plain non-nested field notation. 20 { 21 "message", 22 []string{"message"}, 23 }, 24 // Bad syntax for nested fields. 25 { 26 "[log]badformat", 27 []string{"[log]badformat"}, 28 }, 29 } 30 for _, c := range cases { 31 assert.Equal(t, c.expected, extractBracketFields(c.key)) 32 } 33 } 34 35 // TestParseBracketProperty test keys that contain bracket notation are converted to sub structure. 36 func TestParseBracketProperty(t *testing.T) { 37 cases := []struct { 38 key []string 39 value string 40 expected map[string]interface{} 41 }{ 42 // Created nested fields when input key is nested. 43 { 44 key: []string{"log", "file", "path"}, 45 value: "/tmp/test.log", 46 expected: map[string]interface{}{ 47 "log": map[string]interface{}{ 48 "file": map[string]interface{}{ 49 "path": "/tmp/test.log", 50 }, 51 }, 52 }, 53 }, 54 // Do nothing when the input key isn't nested. 55 { 56 key: []string{"message"}, 57 value: "/tmp/test.log", 58 expected: map[string]interface{}{ 59 "message": "/tmp/test.log", 60 }, 61 }, 62 } 63 for _, c := range cases { 64 result := make(map[string]interface{}) 65 parseBracketProperty(c.key, c.value, result) 66 assert.Equal(t, c.expected, result) 67 } 68 } 69 70 // TestParseAllDotProperties tests that all keys on map that contain dot 71 // or bracket notation are converted on sub structure. 72 func TestParseAllDotProperties(t *testing.T) { 73 data := map[string]interface{}{ 74 "message": "my message", 75 "[log][file][path]": "/tmp/test.log", 76 "[log][origin][line]": 2, 77 } 78 expected := map[string]interface{}{ 79 "message": "my message", 80 "log": map[string]interface{}{ 81 "file": map[string]interface{}{ 82 "path": "/tmp/test.log", 83 }, 84 "origin": map[string]interface{}{ 85 "line": 2, 86 }, 87 }, 88 } 89 90 result := parseAllBracketProperties(data) 91 assert.Equal(t, expected, result) 92 } 93 94 // TestRemoveField tests that ignored fields are removed from actual events. 95 // It supports bracket notation. 96 func TestRemoveField(t *testing.T) { 97 cases := []struct { 98 keys []string 99 data map[string]interface{} 100 expected map[string]interface{} 101 }{ 102 // Non-nested fields removed. 103 { 104 keys: []string{"message"}, 105 data: map[string]interface{}{ 106 "message": "my message", 107 "log": map[string]interface{}{ 108 "file": map[string]interface{}{ 109 "path": "/tmp/file.log", 110 }, 111 }, 112 "source": "test", 113 }, 114 expected: map[string]interface{}{ 115 "log": map[string]interface{}{ 116 "file": map[string]interface{}{ 117 "path": "/tmp/file.log", 118 }, 119 }, 120 "source": "test", 121 }, 122 }, 123 // Nested fields removed and the removed field is the last remaining field. 124 { 125 keys: []string{"log", "file", "path"}, 126 data: map[string]interface{}{ 127 "message": "my message", 128 "log": map[string]interface{}{ 129 "file": map[string]interface{}{ 130 "path": "/tmp/file.log", 131 }, 132 }, 133 "source": "test", 134 }, 135 expected: map[string]interface{}{ 136 "message": "my message", 137 "source": "test", 138 }, 139 }, 140 // Nested fields and the removed field has siblings. 141 { 142 keys: []string{"log", "file", "path"}, 143 data: map[string]interface{}{ 144 "message": "my message", 145 "log": map[string]interface{}{ 146 "file": map[string]interface{}{ 147 "path": "/tmp/file.log", 148 "size": "test", 149 }, 150 }, 151 "source": "test", 152 }, 153 expected: map[string]interface{}{ 154 "message": "my message", 155 "log": map[string]interface{}{ 156 "file": map[string]interface{}{ 157 "size": "test", 158 }, 159 }, 160 "source": "test", 161 }, 162 }, 163 } 164 for _, c := range cases { 165 removeField(c.keys, c.data) 166 assert.Equal(t, c.expected, c.data) 167 } 168 } 169 170 // TestRemoveFields tests that ignored field are removed from actual events 171 // It supports bracket notation. 172 func TestRemoveFields(t *testing.T) { 173 cases := []struct { 174 key string 175 data map[string]interface{} 176 expected map[string]interface{} 177 }{ 178 // Non-nested fields removed. 179 { 180 key: "message", 181 data: map[string]interface{}{ 182 "message": "my message", 183 "log": map[string]interface{}{ 184 "file": map[string]interface{}{ 185 "path": "/tmp/file.log", 186 }, 187 }, 188 "source": "test", 189 }, 190 expected: map[string]interface{}{ 191 "log": map[string]interface{}{ 192 "file": map[string]interface{}{ 193 "path": "/tmp/file.log", 194 }, 195 }, 196 "source": "test", 197 }, 198 }, 199 // Nested fields removed. 200 { 201 key: "[log][file][path]", 202 data: map[string]interface{}{ 203 "message": "my message", 204 "log": map[string]interface{}{ 205 "file": map[string]interface{}{ 206 "path": "/tmp/file.log", 207 }, 208 }, 209 "source": "test", 210 }, 211 expected: map[string]interface{}{ 212 "message": "my message", 213 "source": "test", 214 }, 215 }, 216 } 217 for _, c := range cases { 218 removeFields(c.key, c.data) 219 assert.Equal(t, c.expected, c.data) 220 } 221 }