Skip to content

Commit 6014094

Browse files
committed
add e2e tests
Signed-off-by: odubajDT <[email protected]>
1 parent 8c05129 commit 6014094

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

pkg/ottl/e2e/e2e_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func Test_e2e_editors(t *testing.T) {
6161
tCtx.GetLogRecord().Attributes().Remove("total.string")
6262
tCtx.GetLogRecord().Attributes().Remove("foo")
6363
tCtx.GetLogRecord().Attributes().Remove("things")
64+
tCtx.GetLogRecord().Attributes().Remove("map_slice")
6465
},
6566
},
6667
{
@@ -77,6 +78,12 @@ func Test_e2e_editors(t *testing.T) {
7778
tCtx.GetLogRecord().Attributes().PutInt("things.0.value", 2)
7879
tCtx.GetLogRecord().Attributes().PutStr("things.1.name", "bar")
7980
tCtx.GetLogRecord().Attributes().PutInt("things.1.value", 5)
81+
82+
tCtx.GetLogRecord().Attributes().Remove("map_slice")
83+
tCtx.GetLogRecord().Attributes().PutStr("map_slice.0.foo1", "bar1")
84+
tCtx.GetLogRecord().Attributes().PutStr("map_slice.0.foo2", "bar2")
85+
tCtx.GetLogRecord().Attributes().PutStr("map_slice.1.total.string", "barbar1")
86+
8087
},
8188
},
8289
{
@@ -100,6 +107,10 @@ func Test_e2e_editors(t *testing.T) {
100107
m.PutInt("test.things.0.value", 2)
101108
m.PutStr("test.things.1.name", "bar")
102109
m.PutInt("test.things.1.value", 5)
110+
111+
m.PutStr("test.map_slice.0.foo1", "bar1")
112+
m.PutStr("test.map_slice.0.foo2", "bar2")
113+
m.PutStr("test.map_slice.1.total.string", "barbar1")
103114
m.CopyTo(tCtx.GetLogRecord().Attributes())
104115
},
105116
},
@@ -128,6 +139,14 @@ func Test_e2e_editors(t *testing.T) {
128139

129140
m3 := m.PutEmptyMap("foo.nested")
130141
m3.PutStr("test", "pass")
142+
143+
m4 := m.PutEmptyMap("map_slice.0")
144+
m4.PutStr("foo1", "bar1")
145+
m4.PutStr("foo2", "bar2")
146+
147+
m5 := m.PutEmptyMap("map_slice.1")
148+
m5.PutStr("total.string", "barbar1")
149+
131150
m.CopyTo(tCtx.GetLogRecord().Attributes())
132151
},
133152
},
@@ -139,6 +158,7 @@ func Test_e2e_editors(t *testing.T) {
139158
tCtx.GetLogRecord().Attributes().Remove("http.url")
140159
tCtx.GetLogRecord().Attributes().Remove("foo")
141160
tCtx.GetLogRecord().Attributes().Remove("things")
161+
tCtx.GetLogRecord().Attributes().Remove("map_slice")
142162
},
143163
},
144164
{
@@ -154,6 +174,7 @@ func Test_e2e_editors(t *testing.T) {
154174
tCtx.GetLogRecord().Attributes().Remove("flags")
155175
tCtx.GetLogRecord().Attributes().Remove("foo")
156176
tCtx.GetLogRecord().Attributes().Remove("things")
177+
tCtx.GetLogRecord().Attributes().Remove("map_slice")
157178
},
158179
},
159180
{
@@ -185,6 +206,41 @@ func Test_e2e_editors(t *testing.T) {
185206
m2.PutStr("test", "pass")
186207
},
187208
},
209+
{
210+
statement: `merge_maps(attributes, "insert", attributes["foo"], attributes["map_slice"])`,
211+
want: func(tCtx ottllog.TransformContext) {
212+
tCtx.GetLogRecord().Attributes().PutStr("bar", "pass")
213+
tCtx.GetLogRecord().Attributes().PutStr("foo1", "bar1")
214+
tCtx.GetLogRecord().Attributes().PutStr("foo2", "bar2")
215+
s := tCtx.GetLogRecord().Attributes().PutEmptySlice("slice")
216+
v := s.AppendEmpty()
217+
v.SetStr("val")
218+
m2 := tCtx.GetLogRecord().Attributes().PutEmptyMap("nested")
219+
m2.PutStr("test", "pass")
220+
},
221+
},
222+
{
223+
statement: `merge_maps(attributes, "update", attributes["foo"], attributes["map_slice"])`,
224+
want: func(tCtx ottllog.TransformContext) {
225+
tCtx.GetLogRecord().Attributes().PutStr("flags", "pass")
226+
tCtx.GetLogRecord().Attributes().PutStr("total.string", "barbar1")
227+
},
228+
},
229+
{
230+
statement: `merge_maps(attributes, "upsert", attributes["foo"], attributes["map_slice"])`,
231+
want: func(tCtx ottllog.TransformContext) {
232+
tCtx.GetLogRecord().Attributes().PutStr("bar", "pass")
233+
tCtx.GetLogRecord().Attributes().PutStr("foo1", "bar1")
234+
tCtx.GetLogRecord().Attributes().PutStr("foo2", "bar2")
235+
tCtx.GetLogRecord().Attributes().PutStr("flags", "pass")
236+
tCtx.GetLogRecord().Attributes().PutStr("total.string", "barbar1")
237+
s := tCtx.GetLogRecord().Attributes().PutEmptySlice("slice")
238+
v := s.AppendEmpty()
239+
v.SetStr("val")
240+
m2 := tCtx.GetLogRecord().Attributes().PutEmptyMap("nested")
241+
m2.PutStr("test", "pass")
242+
},
243+
},
188244
{
189245
statement: `replace_all_matches(attributes, "*/*", "test")`,
190246
want: func(tCtx ottllog.TransformContext) {
@@ -1459,6 +1515,12 @@ func constructLogTransformContextEditors() ottllog.TransformContext {
14591515
logRecord.Attributes().PutStr("http.url", "http://localhost/health")
14601516
logRecord.Attributes().PutStr("flags", "A|B|C")
14611517
logRecord.Attributes().PutStr("total.string", "123456789")
1518+
slice := logRecord.Attributes().PutEmptySlice("map_slice")
1519+
ms := slice.AppendEmpty().SetEmptyMap()
1520+
ms.PutStr("foo1", "bar1")
1521+
ms.PutStr("foo2", "bar2")
1522+
ms2 := slice.AppendEmpty().SetEmptyMap()
1523+
ms2.PutStr("total.string", "barbar1")
14621524
m := logRecord.Attributes().PutEmptyMap("foo")
14631525
m.PutStr("bar", "pass")
14641526
m.PutStr("flags", "pass")

pkg/ottl/expression.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,26 @@ func (g StandardPMapSliceGetter[K]) Get(ctx context.Context, tCtx K) ([]pcommon.
468468
result = append(result, m)
469469
}
470470
return result, nil
471+
case pcommon.Slice:
472+
result := []pcommon.Map{}
473+
for _, mm := range v.AsRaw() {
474+
mmm, ok := mm.(pcommon.Map)
475+
if ok {
476+
result = append(result, mmm)
477+
} else {
478+
mmm, ok := mm.(map[string]any)
479+
if !ok {
480+
return []pcommon.Map{}, TypeError(fmt.Sprintf("expected pcommon.Map/map[string]any but got %T", v))
481+
}
482+
m := pcommon.NewMap()
483+
err = m.FromRaw(mmm)
484+
if err != nil {
485+
return []pcommon.Map{}, err
486+
}
487+
result = append(result, m)
488+
}
489+
}
490+
return result, nil
471491
default:
472492
return []pcommon.Map{}, TypeError(fmt.Sprintf("expected []pcommon.Map but got %T", val))
473493
}

pkg/ottl/expression_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,6 +2267,16 @@ func Test_StandardPMapSliceGetter(t *testing.T) {
22672267
want: []pcommon.Map{pcommon.NewMap()},
22682268
valid: true,
22692269
},
2270+
{
2271+
name: "pcommon.Slice type",
2272+
getter: StandardPMapSliceGetter[any]{
2273+
Getter: func(_ context.Context, _ any) (any, error) {
2274+
return pcommon.NewSlice(), nil
2275+
},
2276+
},
2277+
want: []pcommon.Map{},
2278+
valid: true,
2279+
},
22702280
{
22712281
name: "Incorrect type",
22722282
getter: StandardPMapSliceGetter[any]{

0 commit comments

Comments
 (0)