factory_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sshcheckreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sshcheckreceiver"
  4. import (
  5. "context"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/component"
  10. "go.opentelemetry.io/collector/consumer/consumertest"
  11. "go.opentelemetry.io/collector/receiver/receivertest"
  12. "go.opentelemetry.io/collector/receiver/scraperhelper"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sshcheckreceiver/internal/configssh"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sshcheckreceiver/internal/metadata"
  15. )
  16. func TestNewFactory(t *testing.T) {
  17. if !supportedOS() {
  18. t.Skip("Skip tests if not running on one of: [linux, darwin, freebsd, openbsd]")
  19. }
  20. t.Parallel()
  21. testCases := []struct {
  22. desc string
  23. testFunc func(*testing.T)
  24. }{
  25. {
  26. desc: "creates a new factory with correct type",
  27. testFunc: func(t *testing.T) {
  28. factory := NewFactory()
  29. require.EqualValues(t, metadata.Type, factory.Type())
  30. },
  31. },
  32. {
  33. desc: "creates a new factory with default config",
  34. testFunc: func(t *testing.T) {
  35. factory := NewFactory()
  36. var expectedCfg component.Config = &Config{
  37. ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
  38. CollectionInterval: 10 * time.Second,
  39. InitialDelay: time.Second,
  40. },
  41. SSHClientSettings: configssh.SSHClientSettings{
  42. Timeout: 10 * time.Second,
  43. },
  44. MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  45. }
  46. require.Equal(t, expectedCfg, factory.CreateDefaultConfig())
  47. },
  48. },
  49. {
  50. desc: "creates a new factory and CreateMetricsReceiver returns no error",
  51. testFunc: func(t *testing.T) {
  52. factory := NewFactory()
  53. cfg := factory.CreateDefaultConfig()
  54. _, err := factory.CreateMetricsReceiver(
  55. context.Background(),
  56. receivertest.NewNopCreateSettings(),
  57. cfg,
  58. consumertest.NewNop(),
  59. )
  60. require.NoError(t, err)
  61. },
  62. },
  63. {
  64. desc: "creates a new factory and CreateMetricsReceiver returns error with incorrect config",
  65. testFunc: func(t *testing.T) {
  66. factory := NewFactory()
  67. _, err := factory.CreateMetricsReceiver(
  68. context.Background(),
  69. receivertest.NewNopCreateSettings(),
  70. nil,
  71. consumertest.NewNop(),
  72. )
  73. require.ErrorIs(t, err, errConfigNotSSHCheck)
  74. },
  75. },
  76. }
  77. for _, tc := range testCases {
  78. t.Run(tc.desc, tc.testFunc)
  79. }
  80. }
  81. func TestWindowsReceiverUnsupported(t *testing.T) {
  82. if supportedOS() {
  83. t.Skip("Skip test if not running windows.")
  84. }
  85. factory := NewFactory()
  86. _, err := factory.CreateMetricsReceiver(
  87. context.Background(),
  88. receivertest.NewNopCreateSettings(),
  89. nil,
  90. consumertest.NewNop(),
  91. )
  92. require.ErrorIs(t, err, errWindowsUnsupported)
  93. }