journald_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. //go:build linux
  4. // +build linux
  5. package journaldreceiver
  6. import (
  7. "context"
  8. "path/filepath"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/component"
  13. "go.opentelemetry.io/collector/confmap/confmaptest"
  14. "go.opentelemetry.io/collector/consumer/consumertest"
  15. "go.opentelemetry.io/collector/receiver/receivertest"
  16. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/consumerretry"
  17. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
  18. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/journald"
  20. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/journaldreceiver/internal/metadata"
  21. )
  22. func TestLoadConfig(t *testing.T) {
  23. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  24. require.NoError(t, err)
  25. factory := newFactoryAdapter()
  26. cfg := factory.CreateDefaultConfig()
  27. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "").String())
  28. require.NoError(t, err)
  29. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  30. assert.Equal(t, testdataConfigYaml(), cfg)
  31. }
  32. func TestInputConfigFailure(t *testing.T) {
  33. sink := new(consumertest.LogsSink)
  34. factory := newFactoryAdapter()
  35. badCfg := &JournaldConfig{
  36. BaseConfig: adapter.BaseConfig{
  37. Operators: []operator.Config{},
  38. },
  39. InputConfig: func() journald.Config {
  40. c := journald.NewConfig()
  41. c.StartAt = "middle"
  42. return *c
  43. }(),
  44. }
  45. receiver, err := factory.CreateLogsReceiver(context.Background(), receivertest.NewNopCreateSettings(), badCfg, sink)
  46. require.Error(t, err, "receiver creation should fail if input config isn't valid")
  47. require.Nil(t, receiver, "receiver creation should fail if input config isn't valid")
  48. }
  49. func testdataConfigYaml() *JournaldConfig {
  50. return &JournaldConfig{
  51. BaseConfig: adapter.BaseConfig{
  52. Operators: []operator.Config{},
  53. RetryOnFailure: consumerretry.NewDefaultConfig(),
  54. },
  55. InputConfig: func() journald.Config {
  56. c := journald.NewConfig()
  57. c.Units = []string{"ssh"}
  58. c.Priority = "info"
  59. dir := "/run/log/journal"
  60. c.Directory = &dir
  61. return *c
  62. }(),
  63. }
  64. }