From 4b213aa1329588cf7b04d8c099d0a874d578fc55 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 16 Feb 2024 14:18:37 -0800 Subject: [PATCH] more coverage --- .../receivertest/contract_checker_test.go | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/receiver/receivertest/contract_checker_test.go b/receiver/receivertest/contract_checker_test.go index dcdd40b1482..8a058b3e19b 100644 --- a/receiver/receivertest/contract_checker_test.go +++ b/receiver/receivertest/contract_checker_test.go @@ -9,9 +9,12 @@ import ( "sync/atomic" "testing" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/consumer/consumererror" + "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/plog" "go.opentelemetry.io/collector/pdata/pmetric" "go.opentelemetry.io/collector/pdata/ptrace" @@ -291,3 +294,69 @@ func TestConsumeTracesContract(t *testing.T) { // Run the contract checker. This will trigger test failures if any problems are found. CheckConsumeContract(params) } + +func TestIDSetFromDataPoint(t *testing.T) { + require.Error(t, idSetFromDataPoint(map[UniqueIDAttrVal]bool{}, pcommon.NewMap())) + m := pcommon.NewMap() + m.PutStr("foo", "bar") + require.Error(t, idSetFromDataPoint(map[UniqueIDAttrVal]bool{}, m)) + m.PutInt(UniqueIDAttrName, 64) + require.Error(t, idSetFromDataPoint(map[UniqueIDAttrVal]bool{}, m)) + m.PutStr(UniqueIDAttrName, "myid") + result := map[UniqueIDAttrVal]bool{} + require.NoError(t, idSetFromDataPoint(result, m)) + require.True(t, result["myid"]) +} + +func TestBadMetricPoint(t *testing.T) { + for _, test := range []struct { + name string + metrics pmetric.Metrics + }{ + { + name: "gauge", + metrics: func() pmetric.Metrics { + m := pmetric.NewMetrics() + m.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptyGauge().DataPoints().AppendEmpty() + return m + }(), + }, + { + name: "sum", + metrics: func() pmetric.Metrics { + m := pmetric.NewMetrics() + m.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySum().DataPoints().AppendEmpty() + return m + }(), + }, + { + name: "summary", + metrics: func() pmetric.Metrics { + m := pmetric.NewMetrics() + m.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySummary().DataPoints().AppendEmpty() + return m + }(), + }, + { + name: "histogram", + metrics: func() pmetric.Metrics { + m := pmetric.NewMetrics() + m.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptyHistogram().DataPoints().AppendEmpty() + return m + }(), + }, + { + name: "exponential histogram", + metrics: func() pmetric.Metrics { + m := pmetric.NewMetrics() + m.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptyExponentialHistogram().DataPoints().AppendEmpty() + return m + }(), + }, + } { + t.Run(test.name, func(t *testing.T) { + _, err := idSetFromMetrics(test.metrics) + require.Error(t, err) + }) + } +}