|
4 | 4 | package plogutil_test
|
5 | 5 |
|
6 | 6 | import (
|
7 |
| - "path/filepath" |
8 | 7 | "testing"
|
9 | 8 |
|
10 | 9 | "github.com/stretchr/testify/assert"
|
11 |
| - "github.com/stretchr/testify/require" |
12 | 10 | "go.opentelemetry.io/collector/pdata/plog"
|
13 | 11 |
|
14 | 12 | "github.com/open-telemetry/opentelemetry-collector-contrib/connector/routingconnector/internal/plogutil"
|
15 |
| - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden" |
| 13 | + "github.com/open-telemetry/opentelemetry-collector-contrib/connector/routingconnector/internal/plogutiltest" |
16 | 14 | "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/plogtest"
|
17 | 15 | )
|
18 | 16 |
|
19 | 17 | func TestMoveResourcesIf(t *testing.T) {
|
20 | 18 | testCases := []struct {
|
21 |
| - name string |
22 |
| - condition func(plog.ResourceLogs) bool |
| 19 | + name string |
| 20 | + moveIf func(plog.ResourceLogs) bool |
| 21 | + from plog.Logs |
| 22 | + to plog.Logs |
| 23 | + expectFrom plog.Logs |
| 24 | + expectTo plog.Logs |
23 | 25 | }{
|
24 | 26 | {
|
25 | 27 | name: "move_none",
|
26 |
| - condition: func(plog.ResourceLogs) bool { |
| 28 | + moveIf: func(plog.ResourceLogs) bool { |
27 | 29 | return false
|
28 | 30 | },
|
| 31 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 32 | + to: plog.NewLogs(), |
| 33 | + expectFrom: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 34 | + expectTo: plog.NewLogs(), |
29 | 35 | },
|
30 | 36 | {
|
31 | 37 | name: "move_all",
|
32 |
| - condition: func(plog.ResourceLogs) bool { |
| 38 | + moveIf: func(plog.ResourceLogs) bool { |
33 | 39 | return true
|
34 | 40 | },
|
| 41 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 42 | + to: plog.NewLogs(), |
| 43 | + expectFrom: plog.NewLogs(), |
| 44 | + expectTo: plogutiltest.NewLogs("AB", "CD", "EF"), |
35 | 45 | },
|
36 | 46 | {
|
37 | 47 | name: "move_one",
|
38 |
| - condition: func(rl plog.ResourceLogs) bool { |
| 48 | + moveIf: func(rl plog.ResourceLogs) bool { |
39 | 49 | rname, ok := rl.Resource().Attributes().Get("resourceName")
|
40 | 50 | return ok && rname.AsString() == "resourceA"
|
41 | 51 | },
|
| 52 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 53 | + to: plog.NewLogs(), |
| 54 | + expectFrom: plogutiltest.NewLogs("B", "CD", "EF"), |
| 55 | + expectTo: plogutiltest.NewLogs("A", "CD", "EF"), |
42 | 56 | },
|
43 | 57 | {
|
44 | 58 | name: "move_to_preexisting",
|
45 |
| - condition: func(rl plog.ResourceLogs) bool { |
| 59 | + moveIf: func(rl plog.ResourceLogs) bool { |
46 | 60 | rname, ok := rl.Resource().Attributes().Get("resourceName")
|
47 | 61 | return ok && rname.AsString() == "resourceB"
|
48 | 62 | },
|
| 63 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 64 | + to: plogutiltest.NewLogs("1", "2", "3"), |
| 65 | + expectFrom: plogutiltest.NewLogs("A", "CD", "EF"), |
| 66 | + expectTo: plogutiltest.NewLogsFromOpts( |
| 67 | + plogutiltest.WithResource('1', plogutiltest.WithScope('2', "3")), |
| 68 | + plogutiltest.WithResource('B', plogutiltest.WithScope('C', "EF"), plogutiltest.WithScope('D', "EF")), |
| 69 | + ), |
49 | 70 | },
|
50 | 71 | }
|
51 | 72 |
|
52 | 73 | for _, tt := range testCases {
|
53 | 74 | t.Run(tt.name, func(t *testing.T) {
|
54 |
| - // Load up a fresh copy of the input for each test, since it may be modified in place. |
55 |
| - from, err := golden.ReadLogs(filepath.Join("testdata", "resource", tt.name, "from.yaml")) |
56 |
| - require.NoError(t, err) |
57 |
| - |
58 |
| - to, err := golden.ReadLogs(filepath.Join("testdata", "resource", tt.name, "to.yaml")) |
59 |
| - require.NoError(t, err) |
60 |
| - |
61 |
| - fromModifed, err := golden.ReadLogs(filepath.Join("testdata", "resource", tt.name, "from_modified.yaml")) |
62 |
| - require.NoError(t, err) |
63 |
| - |
64 |
| - toModified, err := golden.ReadLogs(filepath.Join("testdata", "resource", tt.name, "to_modified.yaml")) |
65 |
| - require.NoError(t, err) |
66 |
| - |
67 |
| - plogutil.MoveResourcesIf(from, to, tt.condition) |
68 |
| - |
69 |
| - assert.NoError(t, plogtest.CompareLogs(fromModifed, from), "from not modified as expected") |
70 |
| - assert.NoError(t, plogtest.CompareLogs(toModified, to), "to not as expected") |
| 75 | + plogutil.MoveResourcesIf(tt.from, tt.to, tt.moveIf) |
| 76 | + assert.NoError(t, plogtest.CompareLogs(tt.expectFrom, tt.from), "from not modified as expected") |
| 77 | + assert.NoError(t, plogtest.CompareLogs(tt.expectTo, tt.to), "to not as expected") |
71 | 78 | })
|
72 | 79 | }
|
73 | 80 | }
|
74 | 81 |
|
75 | 82 | func TestMoveRecordsWithContextIf(t *testing.T) {
|
76 | 83 | testCases := []struct {
|
77 |
| - name string |
78 |
| - condition func(plog.ResourceLogs, plog.ScopeLogs, plog.LogRecord) bool |
| 84 | + name string |
| 85 | + moveIf func(plog.ResourceLogs, plog.ScopeLogs, plog.LogRecord) bool |
| 86 | + from plog.Logs |
| 87 | + to plog.Logs |
| 88 | + expectFrom plog.Logs |
| 89 | + expectTo plog.Logs |
79 | 90 | }{
|
80 | 91 | {
|
81 | 92 | name: "move_none",
|
82 |
| - condition: func(plog.ResourceLogs, plog.ScopeLogs, plog.LogRecord) bool { |
| 93 | + moveIf: func(plog.ResourceLogs, plog.ScopeLogs, plog.LogRecord) bool { |
83 | 94 | return false
|
84 | 95 | },
|
| 96 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 97 | + to: plog.NewLogs(), |
| 98 | + expectFrom: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 99 | + expectTo: plog.NewLogs(), |
85 | 100 | },
|
86 | 101 | {
|
87 | 102 | name: "move_all",
|
88 |
| - condition: func(plog.ResourceLogs, plog.ScopeLogs, plog.LogRecord) bool { |
| 103 | + moveIf: func(plog.ResourceLogs, plog.ScopeLogs, plog.LogRecord) bool { |
89 | 104 | return true
|
90 | 105 | },
|
| 106 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 107 | + to: plog.NewLogs(), |
| 108 | + expectFrom: plog.NewLogs(), |
| 109 | + expectTo: plogutiltest.NewLogs("AB", "CD", "EF"), |
91 | 110 | },
|
92 | 111 | {
|
93 | 112 | name: "move_all_from_one_resource",
|
94 |
| - condition: func(rl plog.ResourceLogs, _ plog.ScopeLogs, _ plog.LogRecord) bool { |
| 113 | + moveIf: func(rl plog.ResourceLogs, _ plog.ScopeLogs, _ plog.LogRecord) bool { |
95 | 114 | rname, ok := rl.Resource().Attributes().Get("resourceName")
|
96 | 115 | return ok && rname.AsString() == "resourceB"
|
97 | 116 | },
|
| 117 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 118 | + to: plog.NewLogs(), |
| 119 | + expectFrom: plogutiltest.NewLogs("A", "CD", "EF"), |
| 120 | + expectTo: plogutiltest.NewLogs("B", "CD", "EF"), |
98 | 121 | },
|
99 | 122 | {
|
100 | 123 | name: "move_all_from_one_scope",
|
101 |
| - condition: func(rl plog.ResourceLogs, sl plog.ScopeLogs, _ plog.LogRecord) bool { |
| 124 | + moveIf: func(rl plog.ResourceLogs, sl plog.ScopeLogs, _ plog.LogRecord) bool { |
102 | 125 | rname, ok := rl.Resource().Attributes().Get("resourceName")
|
103 |
| - return ok && rname.AsString() == "resourceB" && sl.Scope().Name() == "scopeA" |
| 126 | + return ok && rname.AsString() == "resourceB" && sl.Scope().Name() == "scopeC" |
104 | 127 | },
|
| 128 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 129 | + to: plog.NewLogs(), |
| 130 | + expectFrom: plogutiltest.NewLogsFromOpts( |
| 131 | + plogutiltest.WithResource('A', plogutiltest.WithScope('C', "EF"), plogutiltest.WithScope('D', "EF")), |
| 132 | + plogutiltest.WithResource('B', plogutiltest.WithScope('D', "EF")), |
| 133 | + ), |
| 134 | + expectTo: plogutiltest.NewLogs("B", "C", "EF"), |
105 | 135 | },
|
106 | 136 | {
|
107 | 137 | name: "move_all_from_one_scope_in_each_resource",
|
108 |
| - condition: func(_ plog.ResourceLogs, sl plog.ScopeLogs, _ plog.LogRecord) bool { |
109 |
| - return sl.Scope().Name() == "scopeB" |
| 138 | + moveIf: func(_ plog.ResourceLogs, sl plog.ScopeLogs, _ plog.LogRecord) bool { |
| 139 | + return sl.Scope().Name() == "scopeD" |
110 | 140 | },
|
| 141 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 142 | + to: plog.NewLogs(), |
| 143 | + expectFrom: plogutiltest.NewLogs("AB", "C", "EF"), |
| 144 | + expectTo: plogutiltest.NewLogs("AB", "D", "EF"), |
111 | 145 | },
|
112 | 146 | {
|
113 | 147 | name: "move_one",
|
114 |
| - condition: func(rl plog.ResourceLogs, sl plog.ScopeLogs, lr plog.LogRecord) bool { |
| 148 | + moveIf: func(rl plog.ResourceLogs, sl plog.ScopeLogs, lr plog.LogRecord) bool { |
115 | 149 | rname, ok := rl.Resource().Attributes().Get("resourceName")
|
116 |
| - return ok && rname.AsString() == "resourceA" && sl.Scope().Name() == "scopeB" && lr.Body().AsString() == "logB" |
| 150 | + return ok && rname.AsString() == "resourceA" && sl.Scope().Name() == "scopeD" && lr.Body().AsString() == "logF" |
117 | 151 | },
|
| 152 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 153 | + to: plog.NewLogs(), |
| 154 | + expectFrom: plogutiltest.NewLogsFromOpts( |
| 155 | + plogutiltest.WithResource('A', plogutiltest.WithScope('C', "EF"), plogutiltest.WithScope('D', "E")), |
| 156 | + plogutiltest.WithResource('B', plogutiltest.WithScope('C', "EF"), plogutiltest.WithScope('D', "EF")), |
| 157 | + ), |
| 158 | + expectTo: plogutiltest.NewLogs("A", "D", "F"), |
118 | 159 | },
|
119 | 160 | {
|
120 | 161 | name: "move_one_from_each_scope",
|
121 |
| - condition: func(_ plog.ResourceLogs, _ plog.ScopeLogs, lr plog.LogRecord) bool { |
122 |
| - return lr.Body().AsString() == "logA" |
| 162 | + moveIf: func(_ plog.ResourceLogs, _ plog.ScopeLogs, lr plog.LogRecord) bool { |
| 163 | + return lr.Body().AsString() == "logE" |
123 | 164 | },
|
| 165 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 166 | + to: plog.NewLogs(), |
| 167 | + expectFrom: plogutiltest.NewLogs("AB", "CD", "F"), |
| 168 | + expectTo: plogutiltest.NewLogs("AB", "CD", "E"), |
124 | 169 | },
|
125 | 170 | {
|
126 | 171 | name: "move_one_from_each_scope_in_one_resource",
|
127 |
| - condition: func(rl plog.ResourceLogs, _ plog.ScopeLogs, lr plog.LogRecord) bool { |
| 172 | + moveIf: func(rl plog.ResourceLogs, _ plog.ScopeLogs, lr plog.LogRecord) bool { |
128 | 173 | rname, ok := rl.Resource().Attributes().Get("resourceName")
|
129 |
| - return ok && rname.AsString() == "resourceB" && lr.Body().AsString() == "logA" |
| 174 | + return ok && rname.AsString() == "resourceB" && lr.Body().AsString() == "logE" |
130 | 175 | },
|
| 176 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 177 | + to: plog.NewLogs(), |
| 178 | + expectFrom: plogutiltest.NewLogsFromOpts( |
| 179 | + plogutiltest.WithResource('A', plogutiltest.WithScope('C', "EF"), plogutiltest.WithScope('D', "EF")), |
| 180 | + plogutiltest.WithResource('B', plogutiltest.WithScope('C', "F"), plogutiltest.WithScope('D', "F")), |
| 181 | + ), |
| 182 | + expectTo: plogutiltest.NewLogs("B", "CD", "E"), |
131 | 183 | },
|
132 | 184 | {
|
133 | 185 | name: "move_some_to_preexisting",
|
134 |
| - condition: func(_ plog.ResourceLogs, sl plog.ScopeLogs, _ plog.LogRecord) bool { |
135 |
| - return sl.Scope().Name() == "scopeB" |
| 186 | + moveIf: func(_ plog.ResourceLogs, sl plog.ScopeLogs, _ plog.LogRecord) bool { |
| 187 | + return sl.Scope().Name() == "scopeD" |
136 | 188 | },
|
| 189 | + from: plogutiltest.NewLogs("AB", "CD", "EF"), |
| 190 | + to: plogutiltest.NewLogs("1", "2", "3"), |
| 191 | + expectFrom: plogutiltest.NewLogs("AB", "C", "EF"), |
| 192 | + expectTo: plogutiltest.NewLogsFromOpts( |
| 193 | + plogutiltest.WithResource('1', plogutiltest.WithScope('2', "3")), |
| 194 | + plogutiltest.WithResource('A', plogutiltest.WithScope('D', "EF")), |
| 195 | + plogutiltest.WithResource('B', plogutiltest.WithScope('D', "EF")), |
| 196 | + ), |
137 | 197 | },
|
138 | 198 | }
|
139 | 199 |
|
140 | 200 | for _, tt := range testCases {
|
141 | 201 | t.Run(tt.name, func(t *testing.T) {
|
142 |
| - // Load up a fresh copy of the input for each test, since it may be modified in place. |
143 |
| - from, err := golden.ReadLogs(filepath.Join("testdata", "record", tt.name, "from.yaml")) |
144 |
| - require.NoError(t, err) |
145 |
| - |
146 |
| - to, err := golden.ReadLogs(filepath.Join("testdata", "record", tt.name, "to.yaml")) |
147 |
| - require.NoError(t, err) |
148 |
| - |
149 |
| - fromModifed, err := golden.ReadLogs(filepath.Join("testdata", "record", tt.name, "from_modified.yaml")) |
150 |
| - require.NoError(t, err) |
151 |
| - |
152 |
| - toModified, err := golden.ReadLogs(filepath.Join("testdata", "record", tt.name, "to_modified.yaml")) |
153 |
| - require.NoError(t, err) |
154 |
| - |
155 |
| - plogutil.MoveRecordsWithContextIf(from, to, tt.condition) |
156 |
| - |
157 |
| - assert.NoError(t, plogtest.CompareLogs(fromModifed, from), "from not modified as expected") |
158 |
| - assert.NoError(t, plogtest.CompareLogs(toModified, to), "to not as expected") |
| 202 | + plogutil.MoveRecordsWithContextIf(tt.from, tt.to, tt.moveIf) |
| 203 | + assert.NoError(t, plogtest.CompareLogs(tt.expectFrom, tt.from), "from not modified as expected") |
| 204 | + assert.NoError(t, plogtest.CompareLogs(tt.expectTo, tt.to), "to not as expected") |
159 | 205 | })
|
160 | 206 | }
|
161 | 207 | }
|
0 commit comments