From d6a029a416da03330da5a1898c191ce42f9f656d Mon Sep 17 00:00:00 2001 From: Martin Majlis Date: Wed, 17 May 2023 15:44:59 +0200 Subject: [PATCH 01/11] [exporter/datasetexporter]: Fix failing test on windows --- exporter/datasetexporter/logs_exporter_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/exporter/datasetexporter/logs_exporter_test.go b/exporter/datasetexporter/logs_exporter_test.go index ca8e69c124be..c29edc8b5d30 100644 --- a/exporter/datasetexporter/logs_exporter_test.go +++ b/exporter/datasetexporter/logs_exporter_test.go @@ -273,6 +273,7 @@ func (s *SuiteLogsExporter) TestConsumeLogsShouldSucceed() { wasSuccessful := atomic.Bool{} addRequest := add_events.AddEventsRequest{} + // dummy change server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { attempt.Add(1) cer, err := extract(req) From 2cab463a1764be92a73455b1b2cd042747f96cb2 Mon Sep 17 00:00:00 2001 From: Martin Majlis Date: Wed, 17 May 2023 18:00:10 +0200 Subject: [PATCH 02/11] Remove suite - it's the only difference --- .../datasetexporter/logs_exporter_test.go | 77 ++++++++----------- 1 file changed, 32 insertions(+), 45 deletions(-) diff --git a/exporter/datasetexporter/logs_exporter_test.go b/exporter/datasetexporter/logs_exporter_test.go index c29edc8b5d30..5636a89272ee 100644 --- a/exporter/datasetexporter/logs_exporter_test.go +++ b/exporter/datasetexporter/logs_exporter_test.go @@ -19,17 +19,16 @@ import ( "compress/gzip" "context" "encoding/json" + "github.com/stretchr/testify/assert" "io" "net/http" "net/http/httptest" - "os" "sync/atomic" "testing" "time" "github.com/scalyr/dataset-go/pkg/api/add_events" "github.com/scalyr/dataset-go/pkg/api/request" - "github.com/stretchr/testify/suite" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/exporter/exporterhelper" "go.opentelemetry.io/collector/exporter/exportertest" @@ -39,50 +38,38 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata" ) -type SuiteLogsExporter struct { - suite.Suite -} - -func (s *SuiteLogsExporter) SetupTest() { - os.Clearenv() -} - -func TestSuiteLogsExporter(t *testing.T) { - suite.Run(t, new(SuiteLogsExporter)) -} - -func (s *SuiteLogsExporter) TestCreateLogsExporter() { +func TestCreateLogsExporter(t *testing.T) { ctx := context.Background() createSettings := exportertest.NewNopCreateSettings() tests := createExporterTests() for _, tt := range tests { - s.T().Run(tt.name, func(*testing.T) { + t.Run(tt.name, func(*testing.T) { logs, err := createLogsExporter(ctx, createSettings, tt.config) if err == nil { - s.Nil(tt.expectedError, tt.name) - s.NotNil(logs, tt.name) + assert.Nil(t, tt.expectedError, tt.name) + assert.NotNil(t, logs, tt.name) } else { if tt.expectedError == nil { - s.Nil(err, tt.name) + assert.Nil(t, err, tt.name) } else { - s.Equal(tt.expectedError.Error(), err.Error(), tt.name) - s.Nil(logs, tt.name) + assert.Equal(t, tt.expectedError.Error(), err.Error(), tt.name) + assert.Nil(t, logs, tt.name) } } }) } } -func (s *SuiteLogsExporter) TestBuildBody() { +func TestBuildBody(t *testing.T) { slice := pcommon.NewValueSlice() err := slice.FromRaw([]any{1, 2, 3}) - s.NoError(err) + assert.NoError(t, err) bytes := pcommon.NewValueBytes() err = bytes.FromRaw([]byte{byte(65), byte(66), byte(67)}) - s.NoError(err) + assert.NoError(t, err) tests := []struct { body pcommon.Value key string @@ -134,20 +121,20 @@ func (s *SuiteLogsExporter) TestBuildBody() { } for _, tt := range tests { - s.T().Run(tt.key, func(*testing.T) { + t.Run(tt.key, func(*testing.T) { attrs := make(map[string]interface{}) msg := buildBody(attrs, tt.body) expectedAttrs := make(map[string]interface{}) expectedAttrs["body.type"] = tt.body.Type().String() expectedAttrs[tt.key] = tt.value - s.Equal(tt.message, msg, tt.key) - s.Equal(expectedAttrs, attrs, tt.key) + assert.Equal(t, tt.message, msg, tt.key) + assert.Equal(t, expectedAttrs, attrs, tt.key) }) } } -func (s *SuiteLogsExporter) TestBuildBodyMap() { +func TestBuildBodyMap(t *testing.T) { m := pcommon.NewValueMap() err := m.FromRaw(map[string]any{ "scalar": "scalar-value", @@ -157,7 +144,7 @@ func (s *SuiteLogsExporter) TestBuildBodyMap() { }, "array": []any{1, 2, 3}, }) - if s.NoError(err) { + if assert.NoError(t, err) { attrs := make(map[string]interface{}) msg := buildBody(attrs, m) expectedAttrs := make(map[string]interface{}) @@ -171,8 +158,8 @@ func (s *SuiteLogsExporter) TestBuildBodyMap() { expectedMsg := "{\"array\":[1,2,3],\"map\":{\"m1\":\"v1\",\"m2\":\"v2\"},\"scalar\":\"scalar-value\"}" - s.Equal(expectedMsg, msg) - s.Equal(expectedAttrs, attrs) + assert.Equal(t, expectedMsg, msg) + assert.Equal(t, expectedAttrs, attrs) } } @@ -235,7 +222,7 @@ var testLLog = &add_events.Log{ Attrs: map[string]interface{}{}, } -func (s *SuiteLogsExporter) TestBuildEventFromLog() { +func TestBuildEventFromLog(t *testing.T) { lr := testdata.GenerateLogsOneLogRecord() ld := lr.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0) @@ -250,7 +237,7 @@ func (s *SuiteLogsExporter) TestBuildEventFromLog() { lr.ResourceLogs().At(0).ScopeLogs().At(0).Scope(), ) - s.Equal(expected, was) + assert.Equal(t, expected, was) } func extract(req *http.Request) (add_events.AddEventsRequest, error) { @@ -266,7 +253,7 @@ func extract(req *http.Request) (add_events.AddEventsRequest, error) { return *cer, err } -func (s *SuiteLogsExporter) TestConsumeLogsShouldSucceed() { +func TestConsumeLogsShouldSucceed(t *testing.T) { createSettings := exportertest.NewNopCreateSettings() attempt := atomic.Uint64{} @@ -279,17 +266,17 @@ func (s *SuiteLogsExporter) TestConsumeLogsShouldSucceed() { cer, err := extract(req) addRequest = cer - s.NoError(err, "Error reading request: %v", err) + assert.NoError(t, err, "Error reading request: %v", err) wasSuccessful.Store(true) payload, err := json.Marshal(map[string]interface{}{ "status": "success", "bytesCharged": 42, }) - s.NoError(err) + assert.NoError(t, err) l, err := w.Write(payload) - s.Greater(l, 1) - s.NoError(err) + assert.Greater(t, l, 1) + assert.NoError(t, err) })) defer server.Close() @@ -308,20 +295,20 @@ func (s *SuiteLogsExporter) TestConsumeLogsShouldSucceed() { lr := testdata.GenerateLogsOneLogRecord() logs, err := createLogsExporter(context.Background(), createSettings, config) - if s.NoError(err) { + if assert.NoError(t, err) { err = logs.Start(context.Background(), componenttest.NewNopHost()) - s.NoError(err) + assert.NoError(t, err) - s.NotNil(logs) + assert.NotNil(t, logs) err = logs.ConsumeLogs(context.Background(), lr) - s.Nil(err) + assert.Nil(t, err) time.Sleep(time.Second) err = logs.Shutdown(context.Background()) - s.Nil(err) + assert.Nil(t, err) } - s.True(wasSuccessful.Load()) - s.Equal( + assert.True(wasSuccessful.Load()) + assert.Equal(t, add_events.AddEventsRequest{ AuthParams: request.AuthParams{ Token: "key-lib", From c3e1df7c9b0f577b179d2ec06d953f850c7d0ba5 Mon Sep 17 00:00:00 2001 From: Martin Majlis Date: Wed, 17 May 2023 18:47:47 +0200 Subject: [PATCH 03/11] Reformat the code --- exporter/datasetexporter/logs_exporter_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporter/datasetexporter/logs_exporter_test.go b/exporter/datasetexporter/logs_exporter_test.go index 5636a89272ee..9356a90300d9 100644 --- a/exporter/datasetexporter/logs_exporter_test.go +++ b/exporter/datasetexporter/logs_exporter_test.go @@ -19,7 +19,6 @@ import ( "compress/gzip" "context" "encoding/json" - "github.com/stretchr/testify/assert" "io" "net/http" "net/http/httptest" @@ -29,6 +28,7 @@ import ( "github.com/scalyr/dataset-go/pkg/api/add_events" "github.com/scalyr/dataset-go/pkg/api/request" + "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/exporter/exporterhelper" "go.opentelemetry.io/collector/exporter/exportertest" @@ -307,7 +307,7 @@ func TestConsumeLogsShouldSucceed(t *testing.T) { assert.Nil(t, err) } - assert.True(wasSuccessful.Load()) + assert.True(t, wasSuccessful.Load()) assert.Equal(t, add_events.AddEventsRequest{ AuthParams: request.AuthParams{ From 52923ead340f92dcdde0ddfa872969121fd069e2 Mon Sep 17 00:00:00 2001 From: Martin Majlis Date: Wed, 17 May 2023 19:21:20 +0200 Subject: [PATCH 04/11] Skip problematic test --- .../datasetexporter/logs_exporter_test.go | 191 +++++++++--------- 1 file changed, 93 insertions(+), 98 deletions(-) diff --git a/exporter/datasetexporter/logs_exporter_test.go b/exporter/datasetexporter/logs_exporter_test.go index 9356a90300d9..888c9cd23286 100644 --- a/exporter/datasetexporter/logs_exporter_test.go +++ b/exporter/datasetexporter/logs_exporter_test.go @@ -21,16 +21,10 @@ import ( "encoding/json" "io" "net/http" - "net/http/httptest" - "sync/atomic" "testing" - "time" "github.com/scalyr/dataset-go/pkg/api/add_events" - "github.com/scalyr/dataset-go/pkg/api/request" "github.com/stretchr/testify/assert" - "go.opentelemetry.io/collector/component/componenttest" - "go.opentelemetry.io/collector/exporter/exporterhelper" "go.opentelemetry.io/collector/exporter/exportertest" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/plog" @@ -187,30 +181,30 @@ var testLEventRaw = &add_events.Event{ }, } -var testLEventReq = &add_events.Event{ - Thread: testLEventRaw.Thread, - Log: testLEventRaw.Log, - Sev: testLEventRaw.Sev, - Ts: testLEventRaw.Ts, - Attrs: map[string]interface{}{ - "attributes.app": "server", - "attributes.instance_num": float64(1), - "body.str": "This is a log message", - "body.type": "Str", - "dropped_attributes_count": float64(1), - "flag.is_sampled": false, - "flags": float64(plog.LogRecordFlags(0)), - "message": "OtelExporter - Log - This is a log message", - "resource.attributes.resource-attr": "resource-attr-val-1", - "scope.name": "", - "severity.number": float64(plog.SeverityNumberInfo), - "severity.text": "Info", - "span_id": "0102040800000000", - "timestamp": "2020-02-11 20:26:13.000000789 +0000 UTC", - "trace_id": "08040201000000000000000000000000", - "bundle_key": "d41d8cd98f00b204e9800998ecf8427e", - }, -} +// var testLEventReq = &add_events.Event{ +// Thread: testLEventRaw.Thread, +// Log: testLEventRaw.Log, +// Sev: testLEventRaw.Sev, +// Ts: testLEventRaw.Ts, +// Attrs: map[string]interface{}{ +// "attributes.app": "server", +// "attributes.instance_num": float64(1), +// "body.str": "This is a log message", +// "body.type": "Str", +// "dropped_attributes_count": float64(1), +// "flag.is_sampled": false, +// "flags": float64(plog.LogRecordFlags(0)), +// "message": "OtelExporter - Log - This is a log message", +// "resource.attributes.resource-attr": "resource-attr-val-1", +// "scope.name": "", +// "severity.number": float64(plog.SeverityNumberInfo), +// "severity.text": "Info", +// "span_id": "0102040800000000", +// "timestamp": "2020-02-11 20:26:13.000000789 +0000 UTC", +// "trace_id": "08040201000000000000000000000000", +// "bundle_key": "d41d8cd98f00b204e9800998ecf8427e", +// }, +// } var testLThread = &add_events.Thread{ Id: "TL", @@ -254,72 +248,73 @@ func extract(req *http.Request) (add_events.AddEventsRequest, error) { } func TestConsumeLogsShouldSucceed(t *testing.T) { - createSettings := exportertest.NewNopCreateSettings() - - attempt := atomic.Uint64{} - wasSuccessful := atomic.Bool{} - addRequest := add_events.AddEventsRequest{} - - // dummy change - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - attempt.Add(1) - cer, err := extract(req) - addRequest = cer - - assert.NoError(t, err, "Error reading request: %v", err) - - wasSuccessful.Store(true) - payload, err := json.Marshal(map[string]interface{}{ - "status": "success", - "bytesCharged": 42, - }) - assert.NoError(t, err) - l, err := w.Write(payload) - assert.Greater(t, l, 1) - assert.NoError(t, err) - })) - defer server.Close() - - config := &Config{ - DatasetURL: server.URL, - APIKey: "key-lib", - BufferSettings: BufferSettings{ - MaxLifetime: time.Millisecond, - GroupBy: []string{"attributes.container_id"}, - }, - RetrySettings: exporterhelper.NewDefaultRetrySettings(), - QueueSettings: exporterhelper.NewDefaultQueueSettings(), - TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(), - } - - lr := testdata.GenerateLogsOneLogRecord() - - logs, err := createLogsExporter(context.Background(), createSettings, config) - if assert.NoError(t, err) { - err = logs.Start(context.Background(), componenttest.NewNopHost()) - assert.NoError(t, err) - - assert.NotNil(t, logs) - err = logs.ConsumeLogs(context.Background(), lr) - assert.Nil(t, err) - time.Sleep(time.Second) - err = logs.Shutdown(context.Background()) - assert.Nil(t, err) - } - - assert.True(t, wasSuccessful.Load()) - assert.Equal(t, - add_events.AddEventsRequest{ - AuthParams: request.AuthParams{ - Token: "key-lib", - }, - AddEventsRequestParams: add_events.AddEventsRequestParams{ - Session: addRequest.Session, - Events: []*add_events.Event{testLEventReq}, - Threads: []*add_events.Thread{testLThread}, - Logs: []*add_events.Log{testLLog}, - }, - }, - addRequest, - ) + assert.True(t, true) + // createSettings := exportertest.NewNopCreateSettings() + // + // attempt := atomic.Uint64{} + // wasSuccessful := atomic.Bool{} + // addRequest := add_events.AddEventsRequest{} + // + // // dummy change + // server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + // attempt.Add(1) + // cer, err := extract(req) + // addRequest = cer + // + // assert.NoError(t, err, "Error reading request: %v", err) + // + // wasSuccessful.Store(true) + // payload, err := json.Marshal(map[string]interface{}{ + // "status": "success", + // "bytesCharged": 42, + // }) + // assert.NoError(t, err) + // l, err := w.Write(payload) + // assert.Greater(t, l, 1) + // assert.NoError(t, err) + // })) + // defer server.Close() + // + // config := &Config{ + // DatasetURL: server.URL, + // APIKey: "key-lib", + // BufferSettings: BufferSettings{ + // MaxLifetime: time.Millisecond, + // GroupBy: []string{"attributes.container_id"}, + // }, + // RetrySettings: exporterhelper.NewDefaultRetrySettings(), + // QueueSettings: exporterhelper.NewDefaultQueueSettings(), + // TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(), + // } + // + // lr := testdata.GenerateLogsOneLogRecord() + // + // logs, err := createLogsExporter(context.Background(), createSettings, config) + // if assert.NoError(t, err) { + // err = logs.Start(context.Background(), componenttest.NewNopHost()) + // assert.NoError(t, err) + // + // assert.NotNil(t, logs) + // err = logs.ConsumeLogs(context.Background(), lr) + // assert.Nil(t, err) + // time.Sleep(time.Second) + // err = logs.Shutdown(context.Background()) + // assert.Nil(t, err) + // } + // + // assert.True(t, wasSuccessful.Load()) + // assert.Equal(t, + // add_events.AddEventsRequest{ + // AuthParams: request.AuthParams{ + // Token: "key-lib", + // }, + // AddEventsRequestParams: add_events.AddEventsRequestParams{ + // Session: addRequest.Session, + // Events: []*add_events.Event{testLEventReq}, + // Threads: []*add_events.Thread{testLThread}, + // Logs: []*add_events.Log{testLLog}, + // }, + // }, + // addRequest, + // ) } From 8c5a04d55202191da5c51baf7447f2528caeb8dc Mon Sep 17 00:00:00 2001 From: Martin Majlis Date: Thu, 18 May 2023 12:30:55 +0200 Subject: [PATCH 05/11] Enable those tests again --- .../datasetexporter/logs_exporter_test.go | 191 +++++++++--------- 1 file changed, 98 insertions(+), 93 deletions(-) diff --git a/exporter/datasetexporter/logs_exporter_test.go b/exporter/datasetexporter/logs_exporter_test.go index 888c9cd23286..4d9abaa4a560 100644 --- a/exporter/datasetexporter/logs_exporter_test.go +++ b/exporter/datasetexporter/logs_exporter_test.go @@ -21,10 +21,16 @@ import ( "encoding/json" "io" "net/http" + "net/http/httptest" + "sync/atomic" "testing" + "time" "github.com/scalyr/dataset-go/pkg/api/add_events" + "github.com/scalyr/dataset-go/pkg/api/request" "github.com/stretchr/testify/assert" + "go.opentelemetry.io/collector/component/componenttest" + "go.opentelemetry.io/collector/exporter/exporterhelper" "go.opentelemetry.io/collector/exporter/exportertest" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/plog" @@ -181,30 +187,30 @@ var testLEventRaw = &add_events.Event{ }, } -// var testLEventReq = &add_events.Event{ -// Thread: testLEventRaw.Thread, -// Log: testLEventRaw.Log, -// Sev: testLEventRaw.Sev, -// Ts: testLEventRaw.Ts, -// Attrs: map[string]interface{}{ -// "attributes.app": "server", -// "attributes.instance_num": float64(1), -// "body.str": "This is a log message", -// "body.type": "Str", -// "dropped_attributes_count": float64(1), -// "flag.is_sampled": false, -// "flags": float64(plog.LogRecordFlags(0)), -// "message": "OtelExporter - Log - This is a log message", -// "resource.attributes.resource-attr": "resource-attr-val-1", -// "scope.name": "", -// "severity.number": float64(plog.SeverityNumberInfo), -// "severity.text": "Info", -// "span_id": "0102040800000000", -// "timestamp": "2020-02-11 20:26:13.000000789 +0000 UTC", -// "trace_id": "08040201000000000000000000000000", -// "bundle_key": "d41d8cd98f00b204e9800998ecf8427e", -// }, -// } +var testLEventReq = &add_events.Event{ + Thread: testLEventRaw.Thread, + Log: testLEventRaw.Log, + Sev: testLEventRaw.Sev, + Ts: testLEventRaw.Ts, + Attrs: map[string]interface{}{ + "attributes.app": "server", + "attributes.instance_num": float64(1), + "body.str": "This is a log message", + "body.type": "Str", + "dropped_attributes_count": float64(1), + "flag.is_sampled": false, + "flags": float64(plog.LogRecordFlags(0)), + "message": "OtelExporter - Log - This is a log message", + "resource.attributes.resource-attr": "resource-attr-val-1", + "scope.name": "", + "severity.number": float64(plog.SeverityNumberInfo), + "severity.text": "Info", + "span_id": "0102040800000000", + "timestamp": "2020-02-11 20:26:13.000000789 +0000 UTC", + "trace_id": "08040201000000000000000000000000", + "bundle_key": "d41d8cd98f00b204e9800998ecf8427e", + }, +} var testLThread = &add_events.Thread{ Id: "TL", @@ -248,73 +254,72 @@ func extract(req *http.Request) (add_events.AddEventsRequest, error) { } func TestConsumeLogsShouldSucceed(t *testing.T) { - assert.True(t, true) - // createSettings := exportertest.NewNopCreateSettings() - // - // attempt := atomic.Uint64{} - // wasSuccessful := atomic.Bool{} - // addRequest := add_events.AddEventsRequest{} - // - // // dummy change - // server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - // attempt.Add(1) - // cer, err := extract(req) - // addRequest = cer - // - // assert.NoError(t, err, "Error reading request: %v", err) - // - // wasSuccessful.Store(true) - // payload, err := json.Marshal(map[string]interface{}{ - // "status": "success", - // "bytesCharged": 42, - // }) - // assert.NoError(t, err) - // l, err := w.Write(payload) - // assert.Greater(t, l, 1) - // assert.NoError(t, err) - // })) - // defer server.Close() - // - // config := &Config{ - // DatasetURL: server.URL, - // APIKey: "key-lib", - // BufferSettings: BufferSettings{ - // MaxLifetime: time.Millisecond, - // GroupBy: []string{"attributes.container_id"}, - // }, - // RetrySettings: exporterhelper.NewDefaultRetrySettings(), - // QueueSettings: exporterhelper.NewDefaultQueueSettings(), - // TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(), - // } - // - // lr := testdata.GenerateLogsOneLogRecord() - // - // logs, err := createLogsExporter(context.Background(), createSettings, config) - // if assert.NoError(t, err) { - // err = logs.Start(context.Background(), componenttest.NewNopHost()) - // assert.NoError(t, err) - // - // assert.NotNil(t, logs) - // err = logs.ConsumeLogs(context.Background(), lr) - // assert.Nil(t, err) - // time.Sleep(time.Second) - // err = logs.Shutdown(context.Background()) - // assert.Nil(t, err) - // } - // - // assert.True(t, wasSuccessful.Load()) - // assert.Equal(t, - // add_events.AddEventsRequest{ - // AuthParams: request.AuthParams{ - // Token: "key-lib", - // }, - // AddEventsRequestParams: add_events.AddEventsRequestParams{ - // Session: addRequest.Session, - // Events: []*add_events.Event{testLEventReq}, - // Threads: []*add_events.Thread{testLThread}, - // Logs: []*add_events.Log{testLLog}, - // }, - // }, - // addRequest, - // ) + createSettings := exportertest.NewNopCreateSettings() + + attempt := atomic.Uint64{} + wasSuccessful := atomic.Bool{} + addRequest := add_events.AddEventsRequest{} + + // dummy change + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + attempt.Add(1) + cer, err := extract(req) + addRequest = cer + + assert.NoError(t, err, "Error reading request: %v", err) + + wasSuccessful.Store(true) + payload, err := json.Marshal(map[string]interface{}{ + "status": "success", + "bytesCharged": 42, + }) + assert.NoError(t, err) + l, err := w.Write(payload) + assert.Greater(t, l, 1) + assert.NoError(t, err) + })) + defer server.Close() + + config := &Config{ + DatasetURL: server.URL, + APIKey: "key-lib", + BufferSettings: BufferSettings{ + MaxLifetime: time.Millisecond, + GroupBy: []string{"attributes.container_id"}, + }, + RetrySettings: exporterhelper.NewDefaultRetrySettings(), + QueueSettings: exporterhelper.NewDefaultQueueSettings(), + TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(), + } + + lr := testdata.GenerateLogsOneLogRecord() + + logs, err := createLogsExporter(context.Background(), createSettings, config) + if assert.NoError(t, err) { + err = logs.Start(context.Background(), componenttest.NewNopHost()) + assert.NoError(t, err) + + assert.NotNil(t, logs) + err = logs.ConsumeLogs(context.Background(), lr) + assert.Nil(t, err) + time.Sleep(time.Second) + err = logs.Shutdown(context.Background()) + assert.Nil(t, err) + } + + assert.True(t, wasSuccessful.Load()) + assert.Equal(t, + add_events.AddEventsRequest{ + AuthParams: request.AuthParams{ + Token: "key-lib", + }, + AddEventsRequestParams: add_events.AddEventsRequestParams{ + Session: addRequest.Session, + Events: []*add_events.Event{testLEventReq}, + Threads: []*add_events.Thread{testLThread}, + Logs: []*add_events.Log{testLLog}, + }, + }, + addRequest, + ) } From 9819b4591c318eb296895b84af3e107cb8ce096b Mon Sep 17 00:00:00 2001 From: Martin Majlis Date: Thu, 18 May 2023 16:27:16 +0200 Subject: [PATCH 06/11] Rename files so they are first --- ...logs_exporter_stress_test.go => alogs_exporter_stress_test.go} | 0 .../{logs_exporter_test.go => alogs_exporter_test.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename exporter/datasetexporter/{logs_exporter_stress_test.go => alogs_exporter_stress_test.go} (100%) rename exporter/datasetexporter/{logs_exporter_test.go => alogs_exporter_test.go} (100%) diff --git a/exporter/datasetexporter/logs_exporter_stress_test.go b/exporter/datasetexporter/alogs_exporter_stress_test.go similarity index 100% rename from exporter/datasetexporter/logs_exporter_stress_test.go rename to exporter/datasetexporter/alogs_exporter_stress_test.go diff --git a/exporter/datasetexporter/logs_exporter_test.go b/exporter/datasetexporter/alogs_exporter_test.go similarity index 100% rename from exporter/datasetexporter/logs_exporter_test.go rename to exporter/datasetexporter/alogs_exporter_test.go From 6c86ce7a38ea0143096adc07179eb11fbc9aeb55 Mon Sep 17 00:00:00 2001 From: Martin Majlis Date: Thu, 18 May 2023 16:37:11 +0200 Subject: [PATCH 07/11] Revert back to the original code --- .../datasetexporter/alogs_exporter_test.go | 78 +++++++++++-------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/exporter/datasetexporter/alogs_exporter_test.go b/exporter/datasetexporter/alogs_exporter_test.go index 585542ce4c07..3890df04be33 100644 --- a/exporter/datasetexporter/alogs_exporter_test.go +++ b/exporter/datasetexporter/alogs_exporter_test.go @@ -11,13 +11,14 @@ import ( "io" "net/http" "net/http/httptest" + "os" "sync/atomic" "testing" "time" "github.com/scalyr/dataset-go/pkg/api/add_events" "github.com/scalyr/dataset-go/pkg/api/request" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/exporter/exporterhelper" "go.opentelemetry.io/collector/exporter/exportertest" @@ -27,38 +28,50 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata" ) -func TestCreateLogsExporter(t *testing.T) { +type SuiteLogsExporter struct { + suite.Suite +} + +func (s *SuiteLogsExporter) SetupTest() { + os.Clearenv() +} + +func TestSuiteLogsExporter(t *testing.T) { + suite.Run(t, new(SuiteLogsExporter)) +} + +func (s *SuiteLogsExporter) TestCreateLogsExporter() { ctx := context.Background() createSettings := exportertest.NewNopCreateSettings() tests := createExporterTests() for _, tt := range tests { - t.Run(tt.name, func(*testing.T) { + s.T().Run(tt.name, func(*testing.T) { logs, err := createLogsExporter(ctx, createSettings, tt.config) if err == nil { - assert.Nil(t, tt.expectedError, tt.name) - assert.NotNil(t, logs, tt.name) + s.Nil(tt.expectedError, tt.name) + s.NotNil(logs, tt.name) } else { if tt.expectedError == nil { - assert.Nil(t, err, tt.name) + s.Nil(err, tt.name) } else { - assert.Equal(t, tt.expectedError.Error(), err.Error(), tt.name) - assert.Nil(t, logs, tt.name) + s.Equal(tt.expectedError.Error(), err.Error(), tt.name) + s.Nil(logs, tt.name) } } }) } } -func TestBuildBody(t *testing.T) { +func (s *SuiteLogsExporter) TestBuildBody() { slice := pcommon.NewValueSlice() err := slice.FromRaw([]any{1, 2, 3}) - assert.NoError(t, err) + s.NoError(err) bytes := pcommon.NewValueBytes() err = bytes.FromRaw([]byte{byte(65), byte(66), byte(67)}) - assert.NoError(t, err) + s.NoError(err) tests := []struct { body pcommon.Value key string @@ -110,20 +123,20 @@ func TestBuildBody(t *testing.T) { } for _, tt := range tests { - t.Run(tt.key, func(*testing.T) { + s.T().Run(tt.key, func(*testing.T) { attrs := make(map[string]interface{}) msg := buildBody(attrs, tt.body) expectedAttrs := make(map[string]interface{}) expectedAttrs["body.type"] = tt.body.Type().String() expectedAttrs[tt.key] = tt.value - assert.Equal(t, tt.message, msg, tt.key) - assert.Equal(t, expectedAttrs, attrs, tt.key) + s.Equal(tt.message, msg, tt.key) + s.Equal(expectedAttrs, attrs, tt.key) }) } } -func TestBuildBodyMap(t *testing.T) { +func (s *SuiteLogsExporter) TestBuildBodyMap() { m := pcommon.NewValueMap() err := m.FromRaw(map[string]any{ "scalar": "scalar-value", @@ -133,7 +146,7 @@ func TestBuildBodyMap(t *testing.T) { }, "array": []any{1, 2, 3}, }) - if assert.NoError(t, err) { + if s.NoError(err) { attrs := make(map[string]interface{}) msg := buildBody(attrs, m) expectedAttrs := make(map[string]interface{}) @@ -147,8 +160,8 @@ func TestBuildBodyMap(t *testing.T) { expectedMsg := "{\"array\":[1,2,3],\"map\":{\"m1\":\"v1\",\"m2\":\"v2\"},\"scalar\":\"scalar-value\"}" - assert.Equal(t, expectedMsg, msg) - assert.Equal(t, expectedAttrs, attrs) + s.Equal(expectedMsg, msg) + s.Equal(expectedAttrs, attrs) } } @@ -211,7 +224,7 @@ var testLLog = &add_events.Log{ Attrs: map[string]interface{}{}, } -func TestBuildEventFromLog(t *testing.T) { +func (s *SuiteLogsExporter) TestBuildEventFromLog() { lr := testdata.GenerateLogsOneLogRecord() ld := lr.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0) @@ -226,7 +239,7 @@ func TestBuildEventFromLog(t *testing.T) { lr.ResourceLogs().At(0).ScopeLogs().At(0).Scope(), ) - assert.Equal(t, expected, was) + s.Equal(expected, was) } func extract(req *http.Request) (add_events.AddEventsRequest, error) { @@ -242,30 +255,29 @@ func extract(req *http.Request) (add_events.AddEventsRequest, error) { return *cer, err } -func TestConsumeLogsShouldSucceed(t *testing.T) { +func (s *SuiteLogsExporter) TestConsumeLogsShouldSucceed() { createSettings := exportertest.NewNopCreateSettings() attempt := atomic.Uint64{} wasSuccessful := atomic.Bool{} addRequest := add_events.AddEventsRequest{} - // dummy change server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { attempt.Add(1) cer, err := extract(req) addRequest = cer - assert.NoError(t, err, "Error reading request: %v", err) + s.NoError(err, "Error reading request: %v", err) wasSuccessful.Store(true) payload, err := json.Marshal(map[string]interface{}{ "status": "success", "bytesCharged": 42, }) - assert.NoError(t, err) + s.NoError(err) l, err := w.Write(payload) - assert.Greater(t, l, 1) - assert.NoError(t, err) + s.Greater(l, 1) + s.NoError(err) })) defer server.Close() @@ -284,20 +296,20 @@ func TestConsumeLogsShouldSucceed(t *testing.T) { lr := testdata.GenerateLogsOneLogRecord() logs, err := createLogsExporter(context.Background(), createSettings, config) - if assert.NoError(t, err) { + if s.NoError(err) { err = logs.Start(context.Background(), componenttest.NewNopHost()) - assert.NoError(t, err) + s.NoError(err) - assert.NotNil(t, logs) + s.NotNil(logs) err = logs.ConsumeLogs(context.Background(), lr) - assert.Nil(t, err) + s.Nil(err) time.Sleep(time.Second) err = logs.Shutdown(context.Background()) - assert.Nil(t, err) + s.Nil(err) } - assert.True(t, wasSuccessful.Load()) - assert.Equal(t, + s.True(wasSuccessful.Load()) + s.Equal( add_events.AddEventsRequest{ AuthParams: request.AuthParams{ Token: "key-lib", From d9bf4429e7d05d440e3d62a7c06329f21ca6c86e Mon Sep 17 00:00:00 2001 From: Martin Majlis Date: Thu, 18 May 2023 16:55:14 +0200 Subject: [PATCH 08/11] Update configuration option in README.md --- exporter/datasetexporter/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporter/datasetexporter/README.md b/exporter/datasetexporter/README.md index 221bfd1f9dce..2aa362d9210e 100644 --- a/exporter/datasetexporter/README.md +++ b/exporter/datasetexporter/README.md @@ -29,7 +29,7 @@ If you do not want to specify `api_key` in the file, you can use the [builtin fu - `group_by` (default = []): The list of attributes based on which events should be grouped. - `retry_initial_interval` (default = 5s): Time to wait after the first failure before retrying. - `retry_max_interval` (default = 30s): Is the upper bound on backoff. - - `max_elapsed_time` (default = 300s): Is the maximum amount of time spent trying to send a buffer. + - `retry_max_elapsed_time` (default = 300s): Is the maximum amount of time spent trying to send a buffer. - `traces`: - `aggregate` (default = false): Count the number of spans and errors belonging to a trace. - `max_wait` (default = 5s): The maximum waiting for all spans from single trace to arrive; ignored if `aggregate` is false. @@ -67,4 +67,4 @@ service: processors: [batch] # add dataset among your exporters exporters: [dataset] -``` \ No newline at end of file +``` From 4599d609e54b6b2d137a6e77ee40c31813de059d Mon Sep 17 00:00:00 2001 From: Martin Majlis Date: Thu, 18 May 2023 17:45:53 +0200 Subject: [PATCH 09/11] Remove suite from tests --- .../datasetexporter/alogs_exporter_test.go | 77 ++++++++----------- 1 file changed, 32 insertions(+), 45 deletions(-) diff --git a/exporter/datasetexporter/alogs_exporter_test.go b/exporter/datasetexporter/alogs_exporter_test.go index 3890df04be33..819c1ffca7ae 100644 --- a/exporter/datasetexporter/alogs_exporter_test.go +++ b/exporter/datasetexporter/alogs_exporter_test.go @@ -11,14 +11,13 @@ import ( "io" "net/http" "net/http/httptest" - "os" "sync/atomic" "testing" "time" "github.com/scalyr/dataset-go/pkg/api/add_events" "github.com/scalyr/dataset-go/pkg/api/request" - "github.com/stretchr/testify/suite" + "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/exporter/exporterhelper" "go.opentelemetry.io/collector/exporter/exportertest" @@ -28,50 +27,38 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata" ) -type SuiteLogsExporter struct { - suite.Suite -} - -func (s *SuiteLogsExporter) SetupTest() { - os.Clearenv() -} - -func TestSuiteLogsExporter(t *testing.T) { - suite.Run(t, new(SuiteLogsExporter)) -} - -func (s *SuiteLogsExporter) TestCreateLogsExporter() { +func TestCreateLogsExporter(t *testing.T) { ctx := context.Background() createSettings := exportertest.NewNopCreateSettings() tests := createExporterTests() for _, tt := range tests { - s.T().Run(tt.name, func(*testing.T) { + t.Run(tt.name, func(*testing.T) { logs, err := createLogsExporter(ctx, createSettings, tt.config) if err == nil { - s.Nil(tt.expectedError, tt.name) - s.NotNil(logs, tt.name) + assert.Nil(t, tt.expectedError, tt.name) + assert.NotNil(t, logs, tt.name) } else { if tt.expectedError == nil { - s.Nil(err, tt.name) + assert.Nil(t, err, tt.name) } else { - s.Equal(tt.expectedError.Error(), err.Error(), tt.name) - s.Nil(logs, tt.name) + assert.Equal(t, tt.expectedError.Error(), err.Error(), tt.name) + assert.Nil(t, logs, tt.name) } } }) } } -func (s *SuiteLogsExporter) TestBuildBody() { +func TestBuildBody(t *testing.T) { slice := pcommon.NewValueSlice() err := slice.FromRaw([]any{1, 2, 3}) - s.NoError(err) + assert.NoError(t, err) bytes := pcommon.NewValueBytes() err = bytes.FromRaw([]byte{byte(65), byte(66), byte(67)}) - s.NoError(err) + assert.NoError(t, err) tests := []struct { body pcommon.Value key string @@ -123,20 +110,20 @@ func (s *SuiteLogsExporter) TestBuildBody() { } for _, tt := range tests { - s.T().Run(tt.key, func(*testing.T) { + t.Run(tt.key, func(*testing.T) { attrs := make(map[string]interface{}) msg := buildBody(attrs, tt.body) expectedAttrs := make(map[string]interface{}) expectedAttrs["body.type"] = tt.body.Type().String() expectedAttrs[tt.key] = tt.value - s.Equal(tt.message, msg, tt.key) - s.Equal(expectedAttrs, attrs, tt.key) + assert.Equal(t, tt.message, msg, tt.key) + assert.Equal(t, expectedAttrs, attrs, tt.key) }) } } -func (s *SuiteLogsExporter) TestBuildBodyMap() { +func TestBuildBodyMap(t *testing.T) { m := pcommon.NewValueMap() err := m.FromRaw(map[string]any{ "scalar": "scalar-value", @@ -146,7 +133,7 @@ func (s *SuiteLogsExporter) TestBuildBodyMap() { }, "array": []any{1, 2, 3}, }) - if s.NoError(err) { + if assert.NoError(t, err) { attrs := make(map[string]interface{}) msg := buildBody(attrs, m) expectedAttrs := make(map[string]interface{}) @@ -160,8 +147,8 @@ func (s *SuiteLogsExporter) TestBuildBodyMap() { expectedMsg := "{\"array\":[1,2,3],\"map\":{\"m1\":\"v1\",\"m2\":\"v2\"},\"scalar\":\"scalar-value\"}" - s.Equal(expectedMsg, msg) - s.Equal(expectedAttrs, attrs) + assert.Equal(t, expectedMsg, msg) + assert.Equal(t, expectedAttrs, attrs) } } @@ -224,7 +211,7 @@ var testLLog = &add_events.Log{ Attrs: map[string]interface{}{}, } -func (s *SuiteLogsExporter) TestBuildEventFromLog() { +func TestBuildEventFromLog(t *testing.T) { lr := testdata.GenerateLogsOneLogRecord() ld := lr.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0) @@ -239,7 +226,7 @@ func (s *SuiteLogsExporter) TestBuildEventFromLog() { lr.ResourceLogs().At(0).ScopeLogs().At(0).Scope(), ) - s.Equal(expected, was) + assert.Equal(t, expected, was) } func extract(req *http.Request) (add_events.AddEventsRequest, error) { @@ -255,7 +242,7 @@ func extract(req *http.Request) (add_events.AddEventsRequest, error) { return *cer, err } -func (s *SuiteLogsExporter) TestConsumeLogsShouldSucceed() { +func TestConsumeLogsShouldSucceed(t *testing.T) { createSettings := exportertest.NewNopCreateSettings() attempt := atomic.Uint64{} @@ -267,17 +254,17 @@ func (s *SuiteLogsExporter) TestConsumeLogsShouldSucceed() { cer, err := extract(req) addRequest = cer - s.NoError(err, "Error reading request: %v", err) + assert.NoError(t, err, "Error reading request: %v", err) wasSuccessful.Store(true) payload, err := json.Marshal(map[string]interface{}{ "status": "success", "bytesCharged": 42, }) - s.NoError(err) + assert.NoError(t, err) l, err := w.Write(payload) - s.Greater(l, 1) - s.NoError(err) + assert.Greater(t, l, 1) + assert.NoError(t, err) })) defer server.Close() @@ -296,20 +283,20 @@ func (s *SuiteLogsExporter) TestConsumeLogsShouldSucceed() { lr := testdata.GenerateLogsOneLogRecord() logs, err := createLogsExporter(context.Background(), createSettings, config) - if s.NoError(err) { + if assert.NoError(t, err) { err = logs.Start(context.Background(), componenttest.NewNopHost()) - s.NoError(err) + assert.NoError(t, err) - s.NotNil(logs) + assert.NotNil(t, logs) err = logs.ConsumeLogs(context.Background(), lr) - s.Nil(err) + assert.Nil(t, err) time.Sleep(time.Second) err = logs.Shutdown(context.Background()) - s.Nil(err) + assert.Nil(t, err) } - s.True(wasSuccessful.Load()) - s.Equal( + assert.True(t, wasSuccessful.Load()) + assert.Equal(t, add_events.AddEventsRequest{ AuthParams: request.AuthParams{ Token: "key-lib", From 42c6a6e5dacd48bcb06968d56af7902de12d8d26 Mon Sep 17 00:00:00 2001 From: Martin Majlis Date: Thu, 18 May 2023 18:01:55 +0200 Subject: [PATCH 10/11] Remove suite from everywhere --- .../alogs_exporter_stress_test.go | 47 ++++++-------- exporter/datasetexporter/config_test.go | 43 +++++-------- .../datasetexporter/datasetexporter_test.go | 19 ------ exporter/datasetexporter/factory_test.go | 36 ++++------- .../datasetexporter/traces_exporter_test.go | 61 ++++++++----------- 5 files changed, 68 insertions(+), 138 deletions(-) diff --git a/exporter/datasetexporter/alogs_exporter_stress_test.go b/exporter/datasetexporter/alogs_exporter_stress_test.go index 830f212730de..0fb27a4afd26 100644 --- a/exporter/datasetexporter/alogs_exporter_stress_test.go +++ b/exporter/datasetexporter/alogs_exporter_stress_test.go @@ -13,14 +13,13 @@ import ( "math/rand" "net/http" "net/http/httptest" - "os" "strings" "sync" "sync/atomic" "testing" "time" - "github.com/stretchr/testify/suite" + "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/exporter/exporterhelper" "go.opentelemetry.io/collector/exporter/exportertest" @@ -28,19 +27,7 @@ import ( "go.opentelemetry.io/collector/pdata/plog" ) -type SuiteLogsE2EExporter struct { - suite.Suite -} - -func (s *SuiteLogsE2EExporter) SetupTest() { - os.Clearenv() -} - -func TestSuiteLogsE2EExporterIntegration(t *testing.T) { - suite.Run(t, new(SuiteLogsE2EExporter)) -} - -func (s *SuiteLogsE2EExporter) TestConsumeLogsManyLogsShouldSucceed() { +func TestConsumeLogsManyLogsShouldSucceed(t *testing.T) { const maxDelay = 200 * time.Millisecond createSettings := exportertest.NewNopCreateSettings() @@ -59,12 +46,12 @@ func (s *SuiteLogsE2EExporter) TestConsumeLogsManyLogsShouldSucceed() { attempt.Add(1) cer, err := extract(req) - s.NoError(err, "Error reading request: %v", err) + assert.NoError(t, err, "Error reading request: %v", err) for _, ev := range cer.Events { processedEvents.Add(1) key, found := ev.Attrs["body.str"] - s.True(found) + assert.True(t, found) mutex.Lock() sKey := key.(string) _, f := seenKeys[sKey] @@ -80,10 +67,10 @@ func (s *SuiteLogsE2EExporter) TestConsumeLogsManyLogsShouldSucceed() { "status": "success", "bytesCharged": 42, }) - s.NoError(err) + assert.NoError(t, err) l, err := w.Write(payload) - s.Greater(l, 1) - s.NoError(err) + assert.Greater(t, l, 1) + assert.NoError(t, err) })) defer server.Close() @@ -101,9 +88,9 @@ func (s *SuiteLogsE2EExporter) TestConsumeLogsManyLogsShouldSucceed() { logs, err := createLogsExporter(context.Background(), createSettings, config) waitingTime := time.Duration(0) - if s.NoError(err) { + if assert.NoError(t, err) { err = logs.Start(context.Background(), componenttest.NewNopHost()) - s.NoError(err) + assert.NoError(t, err) for bI := 0; bI < maxBatchCount; bI++ { batch := plog.NewLogs() @@ -119,19 +106,19 @@ func (s *SuiteLogsE2EExporter) TestConsumeLogsManyLogsShouldSucceed() { expectedKeys[key] = 1 } err = logs.ConsumeLogs(context.Background(), batch) - s.Nil(err) + assert.Nil(t, err) time.Sleep(time.Duration(float64(maxDelay.Nanoseconds()) * 0.7)) } - s.NotNil(logs) + assert.NotNil(t, logs) time.Sleep(time.Second) err = logs.Shutdown(context.Background()) - s.Nil(err) + assert.Nil(t, err) lastProcessed := uint64(0) sameNumber := 0 for { - s.T().Logf("Processed events: %d / %d", processedEvents.Load(), expectedLogs) + t.Logf("Processed events: %d / %d", processedEvents.Load(), expectedLogs) if lastProcessed == processedEvents.Load() { sameNumber++ } @@ -146,9 +133,9 @@ func (s *SuiteLogsE2EExporter) TestConsumeLogsManyLogsShouldSucceed() { time.Sleep(2 * time.Second) - s.True(wasSuccessful.Load()) + assert.True(t, wasSuccessful.Load()) - s.Equal(seenKeys, expectedKeys) - s.Equal(expectedLogs, processedEvents.Load(), "processed items") - s.Equal(expectedLogs, uint64(len(seenKeys)), "unique items") + assert.Equal(t, seenKeys, expectedKeys) + assert.Equal(t, expectedLogs, processedEvents.Load(), "processed items") + assert.Equal(t, expectedLogs, uint64(len(seenKeys)), "unique items") } diff --git a/exporter/datasetexporter/config_test.go b/exporter/datasetexporter/config_test.go index d25b64977709..66c38e6d09d4 100644 --- a/exporter/datasetexporter/config_test.go +++ b/exporter/datasetexporter/config_test.go @@ -5,28 +5,15 @@ package datasetexporter import ( "fmt" - "os" "testing" "time" - "github.com/stretchr/testify/suite" + "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/confmap" "go.opentelemetry.io/collector/exporter/exporterhelper" ) -type SuiteConfig struct { - suite.Suite -} - -func TestSuiteConfig(t *testing.T) { - suite.Run(t, new(SuiteConfig)) -} - -func (s *SuiteConfig) SetupTest() { - os.Clearenv() -} - -func (s *SuiteConfig) TestConfigUnmarshalUnknownAttributes() { +func TestConfigUnmarshalUnknownAttributes(t *testing.T) { f := NewFactory() config := f.CreateDefaultConfig().(*Config) configMap := confmap.NewFromStringMap(map[string]interface{}{ @@ -39,10 +26,10 @@ func (s *SuiteConfig) TestConfigUnmarshalUnknownAttributes() { unmarshalErr := fmt.Errorf("1 error(s) decoding:\n\n* '' has invalid keys: unknown_attribute") expectedError := fmt.Errorf("cannot unmarshal config: %w", unmarshalErr) - s.Equal(expectedError.Error(), err.Error()) + assert.Equal(t, expectedError.Error(), err.Error()) } -func (s *SuiteConfig) TestConfigUseDefaults() { +func TestConfigUseDefaults(t *testing.T) { f := NewFactory() config := f.CreateDefaultConfig().(*Config) configMap := confmap.NewFromStringMap(map[string]interface{}{ @@ -50,15 +37,15 @@ func (s *SuiteConfig) TestConfigUseDefaults() { "api_key": "secret", }) err := config.Unmarshal(configMap) - s.Nil(err) + assert.Nil(t, err) - s.Equal("https://example.com", config.DatasetURL) - s.Equal("secret", string(config.APIKey)) - s.Equal(bufferMaxLifetime, config.MaxLifetime) - s.Equal(tracesMaxWait, config.TracesSettings.MaxWait) + assert.Equal(t, "https://example.com", config.DatasetURL) + assert.Equal(t, "secret", string(config.APIKey)) + assert.Equal(t, bufferMaxLifetime, config.MaxLifetime) + assert.Equal(t, tracesMaxWait, config.TracesSettings.MaxWait) } -func (s *SuiteConfig) TestConfigValidate() { +func TestConfigValidate(t *testing.T) { tests := []struct { name string config Config @@ -98,18 +85,18 @@ func (s *SuiteConfig) TestConfigValidate() { } for _, tt := range tests { - s.T().Run(tt.name, func(t *testing.T) { + t.Run(tt.name, func(t *testing.T) { err := tt.config.Validate() if err == nil { - s.Nil(tt.expected, tt.name) + assert.Nil(t, tt.expected, tt.name) } else { - s.Equal(tt.expected.Error(), err.Error(), tt.name) + assert.Equal(t, tt.expected.Error(), err.Error(), tt.name) } }) } } -func (s *SuiteConfig) TestConfigString() { +func TestConfigString(t *testing.T) { config := Config{ DatasetURL: "https://example.com", APIKey: "secret", @@ -126,7 +113,7 @@ func (s *SuiteConfig) TestConfigString() { TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(), } - s.Equal( + assert.Equal(t, "DatasetURL: https://example.com; BufferSettings: {MaxLifetime:123ns GroupBy:[field1 field2] RetryInitialInterval:0s RetryMaxInterval:0s RetryMaxElapsedTime:0s}; TracesSettings: {Aggregate:true MaxWait:45s}; RetrySettings: {Enabled:true InitialInterval:5s RandomizationFactor:0.5 Multiplier:1.5 MaxInterval:30s MaxElapsedTime:5m0s}; QueueSettings: {Enabled:true NumConsumers:10 QueueSize:1000 StorageID:}; TimeoutSettings: {Timeout:5s}", config.String(), ) diff --git a/exporter/datasetexporter/datasetexporter_test.go b/exporter/datasetexporter/datasetexporter_test.go index 5be570149b58..56d06e623df1 100644 --- a/exporter/datasetexporter/datasetexporter_test.go +++ b/exporter/datasetexporter/datasetexporter_test.go @@ -2,22 +2,3 @@ // SPDX-License-Identifier: Apache-2.0 package datasetexporter - -import ( - "os" - "testing" - - "github.com/stretchr/testify/suite" -) - -type SuiteDataSetExporter struct { - suite.Suite -} - -func (s *SuiteDataSetExporter) SuiteDataSetExporter() { - os.Clearenv() -} - -func TestSuiteDataSetExporter(t *testing.T) { - suite.Run(t, new(SuiteDataSetExporter)) -} diff --git a/exporter/datasetexporter/factory_test.go b/exporter/datasetexporter/factory_test.go index 217f39a9f80d..582c1130aa67 100644 --- a/exporter/datasetexporter/factory_test.go +++ b/exporter/datasetexporter/factory_test.go @@ -5,35 +5,23 @@ package datasetexporter import ( "fmt" - "os" "path/filepath" "testing" "time" - "github.com/stretchr/testify/suite" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/confmap/confmaptest" "go.opentelemetry.io/collector/exporter/exporterhelper" ) -type SuiteFactory struct { - suite.Suite -} - -func (s *SuiteFactory) SetupTest() { - os.Clearenv() -} - -func TestSuiteFactory(t *testing.T) { - suite.Run(t, new(SuiteFactory)) -} - -func (s *SuiteFactory) TestCreateDefaultConfig() { +func TestCreateDefaultConfig(t *testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() - s.Equal(&Config{ + assert.Equal(t, &Config{ BufferSettings: newDefaultBufferSettings(), TracesSettings: newDefaultTracesSettings(), RetrySettings: exporterhelper.NewDefaultRetrySettings(), @@ -41,12 +29,12 @@ func (s *SuiteFactory) TestCreateDefaultConfig() { TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(), }, cfg, "failed to create default config") - s.Nil(componenttest.CheckConfigStruct(cfg)) + assert.Nil(t, componenttest.CheckConfigStruct(cfg)) } -func (s *SuiteFactory) TestLoadConfig() { +func TestLoadConfig(t *testing.T) { cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) - s.Nil(err) + assert.Nil(t, err) tests := []struct { id component.ID @@ -118,15 +106,15 @@ func (s *SuiteFactory) TestLoadConfig() { } for _, tt := range tests { - s.T().Run(tt.id.Name(), func(*testing.T) { + t.Run(tt.id.Name(), func(*testing.T) { factory := NewFactory() cfg := factory.CreateDefaultConfig() sub, err := cm.Sub(tt.id.String()) - s.Require().Nil(err) - s.Require().Nil(component.UnmarshalConfig(sub, cfg)) - if s.Nil(component.ValidateConfig(cfg)) { - s.Equal(tt.expected, cfg) + require.Nil(t, err) + require.Nil(t, component.UnmarshalConfig(sub, cfg)) + if assert.Nil(t, component.ValidateConfig(cfg)) { + assert.Equal(t, tt.expected, cfg) } }) } diff --git a/exporter/datasetexporter/traces_exporter_test.go b/exporter/datasetexporter/traces_exporter_test.go index 31d9ba347fbb..9d3666037ec5 100644 --- a/exporter/datasetexporter/traces_exporter_test.go +++ b/exporter/datasetexporter/traces_exporter_test.go @@ -6,14 +6,13 @@ package datasetexporter import ( "context" "fmt" - "os" "sort" "strings" "testing" "time" "github.com/scalyr/dataset-go/pkg/api/add_events" - "github.com/stretchr/testify/suite" + "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/exporter/exportertest" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/ptrace" @@ -21,33 +20,21 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata" ) -type SuiteTracesExporter struct { - suite.Suite -} - -func (s *SuiteTracesExporter) SetupTest() { - os.Clearenv() -} - -func TestSuiteTracesExporter(t *testing.T) { - suite.Run(t, new(SuiteTracesExporter)) -} - -func (s *SuiteTracesExporter) TestCreateTracesExporter() { +func TestCreateTracesExporter(t *testing.T) { ctx := context.Background() createSettings := exportertest.NewNopCreateSettings() tests := createExporterTests() for _, tt := range tests { - s.T().Run(tt.name, func(*testing.T) { + t.Run(tt.name, func(*testing.T) { logs, err := createTracesExporter(ctx, createSettings, tt.config) if err == nil { - s.Nil(tt.expectedError) - s.NotNil(logs) + assert.Nil(t, tt.expectedError) + assert.NotNil(t, logs) } else { - s.Equal(tt.expectedError.Error(), err.Error()) - s.Nil(logs) + assert.Equal(t, tt.expectedError.Error(), err.Error()) + assert.Nil(t, logs) } }) } @@ -148,7 +135,7 @@ var testTLog = &add_events.Log{ Attrs: map[string]interface{}{}, } -func (s *SuiteTracesExporter) TestBuildEventFromSpanOne() { +func TestBuildEventFromSpanOne(t *testing.T) { traces := testdata.GenerateTracesOneSpan() span := traces.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0) expected := &add_events.EventBundle{ @@ -165,10 +152,10 @@ func (s *SuiteTracesExporter) TestBuildEventFromSpanOne() { newSpanTracker(time.Hour), ) - s.Equal(expected, was) + assert.Equal(t, expected, was) } -func (s *SuiteTracesExporter) TestBuildEventsFromSpanAttributesCollision() { +func TestBuildEventsFromSpanAttributesCollision(t *testing.T) { td := ptrace.NewTraces() rs := td.ResourceSpans().AppendEmpty() rss := rs.ScopeSpans().AppendEmpty() @@ -215,10 +202,10 @@ func (s *SuiteTracesExporter) TestBuildEventsFromSpanAttributesCollision() { newSpanTracker(time.Hour), ) - s.Equal(expected, was) + assert.Equal(t, expected, was) } -func (s *SuiteTracesExporter) TestBuildEventsFromTracesFromTwoSpansSameResourceOneDifferent() { +func TestBuildEventsFromTracesFromTwoSpansSameResourceOneDifferent(t *testing.T) { traces := testdata.GenerateTracesTwoSpansSameResourceOneDifferent() was := buildEventsFromTraces(traces, newSpanTracker(time.Hour)) @@ -240,7 +227,7 @@ func (s *SuiteTracesExporter) TestBuildEventsFromTracesFromTwoSpansSameResourceO }, } - s.Equal(expected, was) + assert.Equal(t, expected, was) } var span0Id = [8]byte{1, 1, 1, 1, 1, 1, 1, 1} @@ -439,7 +426,7 @@ func generateSimpleEvent( } } -func (s *SuiteTracesExporter) TestBuildEventsFromTracesTreesAndOrphansWithTracker() { +func TestBuildEventsFromTracesTreesAndOrphansWithTracker(t *testing.T) { tracker := newSpanTracker(time.Second) traces := GenerateTracesTreesAndOrphans() was := buildEventsFromTraces(traces, tracker) @@ -533,7 +520,7 @@ func (s *SuiteTracesExporter) TestBuildEventsFromTracesTreesAndOrphansWithTracke expected[11].Event.Attrs["error_count"] = 0 expected[11].Event.Attrs["span_count"] = 1 - s.Equal(expected, was) + assert.Equal(t, expected, was) expectedKeys := []string{ newTraceAndSpan(trace2Id, span22PId).String(), @@ -551,14 +538,14 @@ func (s *SuiteTracesExporter) TestBuildEventsFromTracesTreesAndOrphansWithTracke return wasKeys[i] < wasKeys[j] }) - s.Equal(expectedKeys, wasKeys) - s.Equal(2, len(tracker.spans)) + assert.Equal(t, expectedKeys, wasKeys) + assert.Equal(t, 2, len(tracker.spans)) time.Sleep(time.Second) - s.Equal(2, tracker.purge()) - s.Equal(0, len(tracker.spans)) + assert.Equal(t, 2, tracker.purge()) + assert.Equal(t, 0, len(tracker.spans)) } -func (s *SuiteTracesExporter) TestBuildEventsFromTracesTreesAndOrphansWithoutTracker() { +func TestBuildEventsFromTracesTreesAndOrphansWithoutTracker(t *testing.T) { traces := GenerateTracesTreesAndOrphans() was := buildEventsFromTraces(traces, nil) @@ -629,10 +616,10 @@ func (s *SuiteTracesExporter) TestBuildEventsFromTracesTreesAndOrphansWithoutTra }, } - s.Equal(expected, was) + assert.Equal(t, expected, was) } -func (s *SuiteTracesExporter) TestUpdateResource() { +func TestUpdateResource(t *testing.T) { tests := []struct { name string resource map[string]any @@ -666,11 +653,11 @@ func (s *SuiteTracesExporter) TestUpdateResource() { } for _, tt := range tests { - s.T().Run(tt.name, func(*testing.T) { + t.Run(tt.name, func(*testing.T) { attrs := make(map[string]interface{}) updateResource(attrs, tt.resource) - s.Equal(tt.expected, attrs, tt.name) + assert.Equal(t, tt.expected, attrs, tt.name) }) } } From e7d1a54aff92ab296a549b65c31eaafa8feeeccf Mon Sep 17 00:00:00 2001 From: Martin Majlis Date: Thu, 18 May 2023 18:08:28 +0200 Subject: [PATCH 11/11] Remove the files back --- ...alogs_exporter_stress_test.go => logs_exporter_stress_test.go} | 0 .../{alogs_exporter_test.go => logs_exporter_test.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename exporter/datasetexporter/{alogs_exporter_stress_test.go => logs_exporter_stress_test.go} (100%) rename exporter/datasetexporter/{alogs_exporter_test.go => logs_exporter_test.go} (100%) diff --git a/exporter/datasetexporter/alogs_exporter_stress_test.go b/exporter/datasetexporter/logs_exporter_stress_test.go similarity index 100% rename from exporter/datasetexporter/alogs_exporter_stress_test.go rename to exporter/datasetexporter/logs_exporter_stress_test.go diff --git a/exporter/datasetexporter/alogs_exporter_test.go b/exporter/datasetexporter/logs_exporter_test.go similarity index 100% rename from exporter/datasetexporter/alogs_exporter_test.go rename to exporter/datasetexporter/logs_exporter_test.go