config_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "path/filepath"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/component/componenttest"
  10. "go.opentelemetry.io/collector/confmap/confmaptest"
  11. "go.opentelemetry.io/collector/receiver/scraperhelper"
  12. "go.uber.org/multierr"
  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. // check that OTel Collector patterns are implemented
  17. func TestCheckConfig(t *testing.T) {
  18. t.Parallel()
  19. if err := componenttest.CheckConfigStruct(&Config{}); err != nil {
  20. t.Fatal(err)
  21. }
  22. }
  23. // test the validate function for config
  24. func TestValidate(t *testing.T) {
  25. t.Parallel()
  26. testCases := []struct {
  27. desc string
  28. cfg *Config
  29. expectedErr error
  30. }{
  31. {
  32. desc: "missing password and key_file",
  33. cfg: &Config{
  34. SSHClientSettings: configssh.SSHClientSettings{
  35. Username: "otelu",
  36. Endpoint: "goodhost:2222",
  37. },
  38. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  39. },
  40. expectedErr: multierr.Combine(errMissingPasswordAndKeyFile),
  41. },
  42. {
  43. desc: "missing username",
  44. cfg: &Config{
  45. SSHClientSettings: configssh.SSHClientSettings{
  46. Endpoint: "goodhost:2222",
  47. KeyFile: "/home/.ssh/id_rsa",
  48. },
  49. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  50. },
  51. expectedErr: multierr.Combine(
  52. errMissingUsername,
  53. ),
  54. },
  55. {
  56. desc: "invalid endpoint",
  57. cfg: &Config{
  58. SSHClientSettings: configssh.SSHClientSettings{
  59. Endpoint: "badendpoint . cuz spaces:2222",
  60. Username: "otelu",
  61. Password: "otelp",
  62. },
  63. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  64. },
  65. expectedErr: multierr.Combine(
  66. errInvalidEndpoint,
  67. ),
  68. },
  69. {
  70. desc: "no error with password",
  71. cfg: &Config{
  72. SSHClientSettings: configssh.SSHClientSettings{
  73. Endpoint: "localhost:2222",
  74. Username: "otelu",
  75. Password: "otelp",
  76. },
  77. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  78. },
  79. expectedErr: error(nil),
  80. },
  81. {
  82. desc: "no error with key_file",
  83. cfg: &Config{
  84. SSHClientSettings: configssh.SSHClientSettings{
  85. Endpoint: "localhost:2222",
  86. Username: "otelu",
  87. KeyFile: "/possibly/a_path",
  88. },
  89. ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
  90. },
  91. expectedErr: error(nil),
  92. },
  93. }
  94. for _, tc := range testCases {
  95. t.Run(tc.desc, func(t *testing.T) {
  96. actualErr := tc.cfg.Validate()
  97. if tc.expectedErr != nil {
  98. require.EqualError(t, actualErr, tc.expectedErr.Error())
  99. } else {
  100. require.NoError(t, actualErr)
  101. }
  102. })
  103. }
  104. }
  105. func TestLoadConfig(t *testing.T) {
  106. // load test config
  107. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  108. require.NoError(t, err)
  109. rcvrs, err := cm.Sub("receivers")
  110. require.NoError(t, err)
  111. sshconf, err := rcvrs.Sub("sshcheck")
  112. require.NoError(t, err)
  113. // unmarshal to receiver config
  114. actualConfig, ok := NewFactory().CreateDefaultConfig().(*Config)
  115. require.True(t, ok)
  116. require.NoError(t, sshconf.Unmarshal(actualConfig))
  117. // set expected config
  118. expectedConfig, ok := NewFactory().CreateDefaultConfig().(*Config)
  119. require.True(t, ok)
  120. expectedConfig.ScraperControllerSettings = scraperhelper.ScraperControllerSettings{
  121. InitialDelay: time.Second,
  122. CollectionInterval: 10 * time.Second,
  123. }
  124. expectedConfig.SSHClientSettings = configssh.SSHClientSettings{
  125. Endpoint: "notdefault:1313",
  126. Username: "notdefault_username",
  127. Password: "notdefault_password",
  128. KeyFile: "notdefault/path/keyfile",
  129. KnownHosts: "path/to/collector_known_hosts",
  130. IgnoreHostKey: false,
  131. Timeout: 10 * time.Second,
  132. }
  133. require.Equal(t, expectedConfig, actualConfig)
  134. }