Skip to content

Commit a8cd616

Browse files
committed
move to use golden fixture files for memory metrics asserts
1 parent fcab6fc commit a8cd616

File tree

3 files changed

+101
-20
lines changed

3 files changed

+101
-20
lines changed

tests/general/discoverymode/configd_test.go

+45-12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121
"testing"
2222
"time"
2323

24+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
25+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
26+
"github.com/stretchr/testify/assert"
2427
"github.com/stretchr/testify/require"
2528
"github.com/testcontainers/testcontainers-go"
2629

@@ -237,18 +240,48 @@ service:
237240
}
238241

239242
func TestStandaloneConfigD(t *testing.T) {
240-
testutils.AssertAllMetricsReceived(
241-
t, "memory.yaml", "empty-config.yaml",
242-
nil, []testutils.CollectorBuilder{
243-
func(c testutils.Collector) testutils.Collector {
244-
configd, err := filepath.Abs(filepath.Join(".", "testdata", "standalone-config.d"))
245-
require.NoError(t, err)
246-
if cc, ok := c.(*testutils.CollectorContainer); ok {
247-
cc.Container = cc.Container.WithMount(testcontainers.BindMount(configd, "/opt/config.d"))
248-
configd = "/opt/config.d"
249-
}
250-
return c.WithEnv(map[string]string{"SPLUNK_CONFIG_DIR": configd}).WithArgs("--configd")
251-
},
243+
tc := testutils.NewTestcase(t)
244+
defer tc.PrintLogsOnFailure()
245+
defer tc.ShutdownOTLPReceiverSink()
246+
247+
_, shutdown := tc.SplunkOtelCollectorContainer("empty-config.yaml",
248+
func(c testutils.Collector) testutils.Collector {
249+
configd, err := filepath.Abs(filepath.Join(".", "testdata", "standalone-config.d"))
250+
require.NoError(t, err)
251+
if cc, ok := c.(*testutils.CollectorContainer); ok {
252+
cc.Container = cc.Container.WithMount(testcontainers.BindMount(configd, "/opt/config.d"))
253+
configd = "/opt/config.d"
254+
}
255+
return c.WithEnv(map[string]string{"SPLUNK_CONFIG_DIR": configd}).WithArgs("--configd")
252256
},
253257
)
258+
defer shutdown()
259+
260+
expected, err := golden.ReadMetrics(filepath.Join("testdata", "memory-expected.yaml"))
261+
require.NoError(t, err)
262+
require.EventuallyWithT(t, func(tt *assert.CollectT) {
263+
if len(tc.OTLPReceiverSink.AllMetrics()) == 0 {
264+
assert.Fail(tt, "No metrics collected")
265+
return
266+
}
267+
err := pmetrictest.CompareMetrics(expected, tc.OTLPReceiverSink.AllMetrics()[len(tc.OTLPReceiverSink.AllMetrics())-1],
268+
pmetrictest.IgnoreResourceAttributeValue("service.instance.id"),
269+
pmetrictest.IgnoreResourceAttributeValue("net.host.port"),
270+
pmetrictest.IgnoreResourceAttributeValue("net.host.name"),
271+
pmetrictest.IgnoreResourceAttributeValue("server.address"),
272+
pmetrictest.IgnoreResourceAttributeValue("container.name"),
273+
pmetrictest.IgnoreResourceAttributeValue("server.port"),
274+
pmetrictest.IgnoreResourceAttributeValue("service.name"),
275+
pmetrictest.IgnoreResourceAttributeValue("service_instance_id"),
276+
pmetrictest.IgnoreResourceAttributeValue("service_version"),
277+
pmetrictest.IgnoreTimestamp(),
278+
pmetrictest.IgnoreStartTimestamp(),
279+
pmetrictest.IgnoreMetricDataPointsOrder(),
280+
pmetrictest.IgnoreScopeMetricsOrder(),
281+
pmetrictest.IgnoreScopeVersion(),
282+
pmetrictest.IgnoreResourceMetricsOrder(),
283+
pmetrictest.IgnoreMetricValues(),
284+
)
285+
assert.NoError(tt, err)
286+
}, 30*time.Second, 1*time.Second)
254287
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
resourceMetrics:
2+
- resource: {}
3+
schemaUrl: https://opentelemetry.io/schemas/1.9.0
4+
scopeMetrics:
5+
- metrics:
6+
- description: Bytes of memory in use.
7+
name: system.memory.usage
8+
sum:
9+
aggregationTemporality: 2
10+
dataPoints:
11+
- asInt: "84504576"
12+
attributes:
13+
- key: state
14+
value:
15+
stringValue: buffered
16+
startTimeUnixNano: "1000000"
17+
timeUnixNano: "2000000"
18+
- asInt: "486821888"
19+
attributes:
20+
- key: state
21+
value:
22+
stringValue: cached
23+
startTimeUnixNano: "1000000"
24+
timeUnixNano: "2000000"
25+
- asInt: "11449737216"
26+
attributes:
27+
- key: state
28+
value:
29+
stringValue: free
30+
startTimeUnixNano: "1000000"
31+
timeUnixNano: "2000000"
32+
- asInt: "115769344"
33+
attributes:
34+
- key: state
35+
value:
36+
stringValue: slab_reclaimable
37+
startTimeUnixNano: "1000000"
38+
timeUnixNano: "2000000"
39+
- asInt: "43827200"
40+
attributes:
41+
- key: state
42+
value:
43+
stringValue: slab_unreclaimable
44+
startTimeUnixNano: "1000000"
45+
timeUnixNano: "2000000"
46+
- asInt: "508424192"
47+
attributes:
48+
- key: state
49+
value:
50+
stringValue: used
51+
startTimeUnixNano: "1000000"
52+
timeUnixNano: "2000000"
53+
unit: By
54+
scope:
55+
name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/memoryscraper
56+
version: v0.116.0-1-g0f3e01e7

tests/general/discoverymode/testdata/resource_metrics/memory.yaml

-8
This file was deleted.

0 commit comments

Comments
 (0)