github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/insert_part_test.go (about) 1 package processor 2 3 import ( 4 "fmt" 5 "os" 6 "reflect" 7 "testing" 8 9 "github.com/Jeffail/benthos/v3/lib/log" 10 "github.com/Jeffail/benthos/v3/lib/message" 11 "github.com/Jeffail/benthos/v3/lib/metrics" 12 ) 13 14 func TestInsertBoundaries(t *testing.T) { 15 conf := NewConfig() 16 conf.InsertPart.Content = "hello world" 17 18 testLog := log.Noop() 19 20 for i := 0; i < 10; i++ { 21 for j := -5; j <= 5; j++ { 22 conf.InsertPart.Index = j 23 proc, err := NewInsertPart(conf, nil, testLog, metrics.Noop()) 24 if err != nil { 25 t.Error(err) 26 return 27 } 28 29 var parts [][]byte 30 for k := 0; k < i; k++ { 31 parts = append(parts, []byte("foo")) 32 } 33 34 msgs, res := proc.ProcessMessage(message.New(parts)) 35 if len(msgs) != 1 { 36 t.Error("Insert Part failed") 37 } else if res != nil { 38 t.Errorf("Expected nil response: %v", res) 39 } 40 if exp, act := i+1, len(message.GetAllBytes(msgs[0])); exp != act { 41 t.Errorf("Wrong count of result parts: %v != %v", act, exp) 42 } 43 } 44 } 45 } 46 47 func TestInsertPart(t *testing.T) { 48 conf := NewConfig() 49 conf.InsertPart.Content = "hello world" 50 51 testLog := log.Noop() 52 53 type test struct { 54 index int 55 in [][]byte 56 out [][]byte 57 } 58 59 tests := []test{ 60 { 61 index: 0, 62 in: [][]byte{ 63 []byte("0"), 64 []byte("1"), 65 }, 66 out: [][]byte{ 67 []byte("hello world"), 68 []byte("0"), 69 []byte("1"), 70 }, 71 }, 72 { 73 index: 0, 74 in: [][]byte{}, 75 out: [][]byte{ 76 []byte("hello world"), 77 }, 78 }, 79 { 80 index: 1, 81 in: [][]byte{ 82 []byte("0"), 83 []byte("1"), 84 }, 85 out: [][]byte{ 86 []byte("0"), 87 []byte("hello world"), 88 []byte("1"), 89 }, 90 }, 91 { 92 index: 2, 93 in: [][]byte{ 94 []byte("0"), 95 []byte("1"), 96 }, 97 out: [][]byte{ 98 []byte("0"), 99 []byte("1"), 100 []byte("hello world"), 101 }, 102 }, 103 { 104 index: 3, 105 in: [][]byte{ 106 []byte("0"), 107 []byte("1"), 108 }, 109 out: [][]byte{ 110 []byte("0"), 111 []byte("1"), 112 []byte("hello world"), 113 }, 114 }, 115 { 116 index: -1, 117 in: [][]byte{ 118 []byte("0"), 119 []byte("1"), 120 }, 121 out: [][]byte{ 122 []byte("0"), 123 []byte("1"), 124 []byte("hello world"), 125 }, 126 }, 127 { 128 index: -2, 129 in: [][]byte{ 130 []byte("0"), 131 []byte("1"), 132 }, 133 out: [][]byte{ 134 []byte("0"), 135 []byte("hello world"), 136 []byte("1"), 137 }, 138 }, 139 { 140 index: -3, 141 in: [][]byte{ 142 []byte("0"), 143 []byte("1"), 144 }, 145 out: [][]byte{ 146 []byte("hello world"), 147 []byte("0"), 148 []byte("1"), 149 }, 150 }, 151 { 152 index: -4, 153 in: [][]byte{ 154 []byte("0"), 155 []byte("1"), 156 }, 157 out: [][]byte{ 158 []byte("hello world"), 159 []byte("0"), 160 []byte("1"), 161 }, 162 }, 163 } 164 165 for _, test := range tests { 166 conf.InsertPart.Index = test.index 167 proc, err := NewInsertPart(conf, nil, testLog, metrics.Noop()) 168 if err != nil { 169 t.Error(err) 170 return 171 } 172 173 msgs, res := proc.ProcessMessage(message.New(test.in)) 174 if len(msgs) != 1 { 175 t.Errorf("Insert Part failed on: %s", test.in) 176 } else if res != nil { 177 t.Errorf("Expected nil response: %v", res) 178 } 179 if exp, act := test.out, message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) { 180 t.Errorf("Unexpected output for %s at index %v: %s != %s", test.in, test.index, act, exp) 181 } 182 } 183 } 184 185 func TestInsertPartInterpolation(t *testing.T) { 186 conf := NewConfig() 187 conf.InsertPart.Content = "hello ${!hostname()} world" 188 189 hostname, _ := os.Hostname() 190 191 testLog := log.Noop() 192 proc, err := NewInsertPart(conf, nil, testLog, metrics.Noop()) 193 if err != nil { 194 t.Error(err) 195 return 196 } 197 198 type test struct { 199 in [][]byte 200 out [][]byte 201 } 202 203 tests := []test{ 204 { 205 in: [][]byte{ 206 []byte("0"), 207 []byte("1"), 208 }, 209 out: [][]byte{ 210 []byte("0"), 211 []byte("1"), 212 []byte(fmt.Sprintf("hello %v world", hostname)), 213 }, 214 }, 215 { 216 in: [][]byte{}, 217 out: [][]byte{ 218 []byte(fmt.Sprintf("hello %v world", hostname)), 219 }, 220 }, 221 } 222 223 for _, test := range tests { 224 msgs, res := proc.ProcessMessage(message.New(test.in)) 225 if len(msgs) != 1 { 226 t.Errorf("Insert Part failed on: %s", test.in) 227 } else if res != nil { 228 t.Errorf("Expected nil response: %v", res) 229 } 230 if exp, act := test.out, message.GetAllBytes(msgs[0]); !reflect.DeepEqual(exp, act) { 231 t.Errorf("Unexpected output for %s: %s != %s", test.in, act, exp) 232 } 233 } 234 }