Skip to content

Commit e296b0f

Browse files
authored
[pkg/ottl] Adapt e2e tests to also test statements with path context names (#37741)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description This PR adaps the `e2e` tests to run both the old syntax as it's, and the new one by leveraging the parser collection context prepend feature, giving us some extra coverage without having to duplicate every single test. <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue #29017 <!--Please delete paragraphs that you did not use before submitting.-->
1 parent d36ce1b commit e296b0f

File tree

1 file changed

+66
-32
lines changed

1 file changed

+66
-32
lines changed

pkg/ottl/e2e/e2e_test.go

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"go.opentelemetry.io/collector/pdata/plog"
1616
"go.opentelemetry.io/collector/pdata/ptrace"
1717

18+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
1819
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllog"
1920
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlspan"
2021
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
@@ -302,19 +303,18 @@ func Test_e2e_editors(t *testing.T) {
302303

303304
for _, tt := range tests {
304305
t.Run(tt.statement, func(t *testing.T) {
305-
settings := componenttest.NewNopTelemetrySettings()
306-
logParser, err := ottllog.NewParser(ottlfuncs.StandardFuncs[ottllog.TransformContext](), settings)
307-
assert.NoError(t, err)
308-
logStatements, err := logParser.ParseStatement(tt.statement)
306+
logStatements, err := parseStatementWithAndWithoutPathContext(tt.statement)
309307
assert.NoError(t, err)
310308

311-
tCtx := constructLogTransformContextEditors()
312-
_, _, _ = logStatements.Execute(context.Background(), tCtx)
309+
for _, statement := range logStatements {
310+
tCtx := constructLogTransformContextEditors()
311+
_, _, _ = statement.Execute(context.Background(), tCtx)
313312

314-
exTCtx := constructLogTransformContextEditors()
315-
tt.want(exTCtx)
313+
exTCtx := constructLogTransformContextEditors()
314+
tt.want(exTCtx)
316315

317-
assert.NoError(t, plogtest.CompareResourceLogs(newResourceLogs(exTCtx), newResourceLogs(tCtx)))
316+
assert.NoError(t, plogtest.CompareResourceLogs(newResourceLogs(exTCtx), newResourceLogs(tCtx)))
317+
}
318318
})
319319
}
320320
}
@@ -1045,24 +1045,23 @@ func Test_e2e_converters(t *testing.T) {
10451045

10461046
for _, tt := range tests {
10471047
t.Run(tt.statement, func(t *testing.T) {
1048-
settings := componenttest.NewNopTelemetrySettings()
1049-
logParser, err := ottllog.NewParser(ottlfuncs.StandardFuncs[ottllog.TransformContext](), settings)
1050-
assert.NoError(t, err)
1051-
logStatements, err := logParser.ParseStatement(tt.statement)
1048+
logStatements, err := parseStatementWithAndWithoutPathContext(tt.statement)
10521049
assert.NoError(t, err)
10531050

1054-
tCtx := constructLogTransformContext()
1055-
_, _, err = logStatements.Execute(context.Background(), tCtx)
1056-
if tt.errMsg == "" {
1057-
assert.NoError(t, err)
1058-
} else {
1059-
assert.Contains(t, err.Error(), tt.errMsg)
1060-
}
1051+
for _, statement := range logStatements {
1052+
tCtx := constructLogTransformContext()
1053+
_, _, err = statement.Execute(context.Background(), tCtx)
1054+
if tt.errMsg == "" {
1055+
assert.NoError(t, err)
1056+
} else {
1057+
assert.Contains(t, err.Error(), tt.errMsg)
1058+
}
10611059

1062-
exTCtx := constructLogTransformContext()
1063-
tt.want(exTCtx)
1060+
exTCtx := constructLogTransformContext()
1061+
tt.want(exTCtx)
10641062

1065-
assert.NoError(t, plogtest.CompareResourceLogs(newResourceLogs(exTCtx), newResourceLogs(tCtx)))
1063+
assert.NoError(t, plogtest.CompareResourceLogs(newResourceLogs(exTCtx), newResourceLogs(tCtx)))
1064+
}
10661065
})
10671066
}
10681067
}
@@ -1169,19 +1168,18 @@ func Test_e2e_ottl_features(t *testing.T) {
11691168

11701169
for _, tt := range tests {
11711170
t.Run(tt.statement, func(t *testing.T) {
1172-
settings := componenttest.NewNopTelemetrySettings()
1173-
logParser, err := ottllog.NewParser(ottlfuncs.StandardFuncs[ottllog.TransformContext](), settings)
1174-
assert.NoError(t, err)
1175-
logStatements, err := logParser.ParseStatement(tt.statement)
1171+
logStatements, err := parseStatementWithAndWithoutPathContext(tt.statement)
11761172
assert.NoError(t, err)
11771173

1178-
tCtx := constructLogTransformContext()
1179-
_, _, _ = logStatements.Execute(context.Background(), tCtx)
1174+
for _, statement := range logStatements {
1175+
tCtx := constructLogTransformContext()
1176+
_, _, _ = statement.Execute(context.Background(), tCtx)
11801177

1181-
exTCtx := constructLogTransformContext()
1182-
tt.want(exTCtx)
1178+
exTCtx := constructLogTransformContext()
1179+
tt.want(exTCtx)
11831180

1184-
assert.NoError(t, plogtest.CompareResourceLogs(newResourceLogs(exTCtx), newResourceLogs(tCtx)))
1181+
assert.NoError(t, plogtest.CompareResourceLogs(newResourceLogs(exTCtx), newResourceLogs(tCtx)))
1182+
}
11851183
})
11861184
}
11871185
}
@@ -1276,6 +1274,42 @@ func Test_ProcessTraces_TraceContext(t *testing.T) {
12761274
}
12771275
}
12781276

1277+
func parseStatementWithAndWithoutPathContext(statement string) ([]*ottl.Statement[ottllog.TransformContext], error) {
1278+
settings := componenttest.NewNopTelemetrySettings()
1279+
parserWithoutPathCtx, err := ottllog.NewParser(ottlfuncs.StandardFuncs[ottllog.TransformContext](), settings)
1280+
if err != nil {
1281+
return nil, err
1282+
}
1283+
1284+
withoutPathCtxResult, err := parserWithoutPathCtx.ParseStatement(statement)
1285+
if err != nil {
1286+
return nil, err
1287+
}
1288+
1289+
parserWithPathCtx, err := ottllog.NewParser(ottlfuncs.StandardFuncs[ottllog.TransformContext](), settings, ottllog.EnablePathContextNames())
1290+
if err != nil {
1291+
return nil, err
1292+
}
1293+
1294+
pc, err := ottl.NewParserCollection(settings,
1295+
ottl.WithParserCollectionContext[ottllog.TransformContext, *ottl.Statement[ottllog.TransformContext]](
1296+
ottllog.ContextName,
1297+
&parserWithPathCtx,
1298+
func(_ *ottl.ParserCollection[*ottl.Statement[ottllog.TransformContext]], _ *ottl.Parser[ottllog.TransformContext], _ string, _ ottl.StatementsGetter, parsedStatements []*ottl.Statement[ottllog.TransformContext]) (*ottl.Statement[ottllog.TransformContext], error) {
1299+
return parsedStatements[0], nil
1300+
}))
1301+
if err != nil {
1302+
return nil, err
1303+
}
1304+
1305+
withPathCtxResult, err := pc.ParseStatementsWithContext(ottllog.ContextName, ottl.NewStatementsGetter([]string{statement}), true)
1306+
if err != nil {
1307+
return nil, err
1308+
}
1309+
1310+
return []*ottl.Statement[ottllog.TransformContext]{withoutPathCtxResult, withPathCtxResult}, nil
1311+
}
1312+
12791313
func constructLogTransformContext() ottllog.TransformContext {
12801314
resource := pcommon.NewResource()
12811315
resource.Attributes().PutStr("host.name", "localhost")

0 commit comments

Comments
 (0)