logs_exporter_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package logicmonitorexporter
  4. import (
  5. "context"
  6. "encoding/json"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. lmsdklogs "github.com/logicmonitor/lm-data-sdk-go/api/logs"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. "go.opentelemetry.io/collector/component/componenttest"
  14. "go.opentelemetry.io/collector/config/confighttp"
  15. "go.opentelemetry.io/collector/exporter/exportertest"
  16. "go.opentelemetry.io/collector/pdata/plog"
  17. "go.uber.org/zap"
  18. "go.uber.org/zap/zaptest"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/logicmonitorexporter/internal/testutil"
  20. )
  21. func Test_NewLogsExporter(t *testing.T) {
  22. tests := []struct {
  23. name string
  24. args struct {
  25. config *Config
  26. logger *zap.Logger
  27. }
  28. }{
  29. {
  30. name: "should create LogExporter",
  31. args: struct {
  32. config *Config
  33. logger *zap.Logger
  34. }{
  35. config: &Config{
  36. HTTPClientSettings: confighttp.HTTPClientSettings{
  37. Endpoint: "http://example.logicmonitor.com/rest",
  38. },
  39. APIToken: APIToken{AccessID: "testid", AccessKey: "testkey"},
  40. },
  41. logger: zaptest.NewLogger(t),
  42. },
  43. },
  44. }
  45. for _, tt := range tests {
  46. t.Run(tt.name, func(t *testing.T) {
  47. set := exportertest.NewNopCreateSettings()
  48. exp := newLogsExporter(context.Background(), tt.args.config, set)
  49. assert.NotNil(t, exp)
  50. })
  51. }
  52. }
  53. func TestPushLogData(t *testing.T) {
  54. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  55. response := lmsdklogs.LMLogIngestResponse{
  56. Success: true,
  57. Message: "Accepted",
  58. }
  59. w.WriteHeader(http.StatusAccepted)
  60. assert.NoError(t, json.NewEncoder(w).Encode(&response))
  61. }))
  62. defer ts.Close()
  63. cfg := &Config{
  64. HTTPClientSettings: confighttp.HTTPClientSettings{
  65. Endpoint: ts.URL,
  66. },
  67. APIToken: APIToken{AccessID: "testid", AccessKey: "testkey"},
  68. }
  69. tests := []struct {
  70. name string
  71. fields struct {
  72. config *Config
  73. logger *zap.Logger
  74. }
  75. args struct {
  76. ctx context.Context
  77. lg plog.Logs
  78. }
  79. }{
  80. {
  81. name: "Send Log data",
  82. fields: struct {
  83. config *Config
  84. logger *zap.Logger
  85. }{
  86. logger: zaptest.NewLogger(t),
  87. config: cfg,
  88. },
  89. args: struct {
  90. ctx context.Context
  91. lg plog.Logs
  92. }{
  93. ctx: context.Background(),
  94. lg: testutil.CreateLogData(1),
  95. },
  96. },
  97. }
  98. for _, test := range tests {
  99. t.Run(test.name, func(t *testing.T) {
  100. set := exportertest.NewNopCreateSettings()
  101. exp := newLogsExporter(test.args.ctx, test.fields.config, set)
  102. require.NoError(t, exp.start(test.args.ctx, componenttest.NewNopHost()))
  103. err := exp.PushLogData(test.args.ctx, test.args.lg)
  104. assert.NoError(t, err)
  105. })
  106. }
  107. }