exporter_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package awss3exporter
  4. import (
  5. "context"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "go.opentelemetry.io/collector/pdata/plog"
  9. "go.uber.org/zap"
  10. )
  11. var testLogs = []byte(`{"resourceLogs":[{"resource":{"attributes":[{"key":"_sourceCategory","value":{"stringValue":"logfile"}},{"key":"_sourceHost","value":{"stringValue":"host"}}]},"scopeLogs":[{"scope":{},"logRecords":[{"observedTimeUnixNano":"1654257420681895000","body":{"stringValue":"2022-06-03 13:57:00.62739 +0200 CEST m=+14.018296742 log entry14"},"attributes":[{"key":"log.file.path_resolved","value":{"stringValue":"logwriter/data.log"}}],"traceId":"","spanId":""}]}],"schemaUrl":"https://opentelemetry.io/schemas/1.6.1"}]}`)
  12. type TestWriter struct {
  13. t *testing.T
  14. }
  15. func (testWriter *TestWriter) writeBuffer(_ context.Context, buf []byte, _ *Config, _ string, _ string) error {
  16. assert.Equal(testWriter.t, testLogs, buf)
  17. return nil
  18. }
  19. func getTestLogs(tb testing.TB) plog.Logs {
  20. logsMarshaler := plog.JSONUnmarshaler{}
  21. logs, err := logsMarshaler.UnmarshalLogs(testLogs)
  22. assert.NoError(tb, err, "Can't unmarshal testing logs data -> %s", err)
  23. return logs
  24. }
  25. func getLogExporter(t *testing.T) *s3Exporter {
  26. marshaler, _ := newMarshaler("otlp_json", zap.NewNop())
  27. exporter := &s3Exporter{
  28. config: createDefaultConfig().(*Config),
  29. dataWriter: &TestWriter{t},
  30. logger: zap.NewNop(),
  31. marshaler: marshaler,
  32. }
  33. return exporter
  34. }
  35. func TestLog(t *testing.T) {
  36. logs := getTestLogs(t)
  37. exporter := getLogExporter(t)
  38. assert.NoError(t, exporter.ConsumeLogs(context.Background(), logs))
  39. }