scraper_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package vcenterreceiver // import github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver
  4. import (
  5. "context"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/receiver/receivertest"
  10. "go.uber.org/zap"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver/internal/metadata"
  14. mock "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver/internal/mockserver"
  15. )
  16. func TestScrape(t *testing.T) {
  17. ctx := context.Background()
  18. mockServer := mock.MockServer(t, false)
  19. defer mockServer.Close()
  20. cfg := &Config{
  21. MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  22. Endpoint: mockServer.URL,
  23. Username: mock.MockUsername,
  24. Password: mock.MockPassword,
  25. }
  26. testScrape(ctx, t, cfg)
  27. }
  28. func TestScrapeWithPerfObjects(t *testing.T) {
  29. ctx := context.Background()
  30. mockServer := mock.MockServer(t, false)
  31. defer mockServer.Close()
  32. cfg := &Config{
  33. MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  34. Endpoint: mockServer.URL,
  35. Username: mock.MockUsername,
  36. Password: mock.MockPassword,
  37. }
  38. scraper := newVmwareVcenterScraper(zap.NewNop(), cfg, receivertest.NewNopCreateSettings())
  39. scraper.emitPerfWithObject = true
  40. metrics, err := scraper.scrape(ctx)
  41. require.NoError(t, err)
  42. require.NotEqual(t, metrics.MetricCount(), 0)
  43. goldenPath := filepath.Join("testdata", "metrics", "expected_with_object.yaml")
  44. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  45. require.NoError(t, err)
  46. err = pmetrictest.CompareMetrics(expectedMetrics, metrics,
  47. pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp(),
  48. pmetrictest.IgnoreResourceMetricsOrder(),
  49. pmetrictest.IgnoreMetricDataPointsOrder(),
  50. )
  51. require.NoError(t, err)
  52. require.NoError(t, scraper.Shutdown(ctx))
  53. }
  54. func TestScrape_TLS(t *testing.T) {
  55. ctx := context.Background()
  56. mockServer := mock.MockServer(t, true)
  57. defer mockServer.Close()
  58. cfg := &Config{
  59. MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
  60. Endpoint: mockServer.URL,
  61. Username: mock.MockUsername,
  62. Password: mock.MockPassword,
  63. }
  64. cfg.Insecure = true
  65. cfg.InsecureSkipVerify = true
  66. testScrape(ctx, t, cfg)
  67. }
  68. func testScrape(ctx context.Context, t *testing.T, cfg *Config) {
  69. scraper := newVmwareVcenterScraper(zap.NewNop(), cfg, receivertest.NewNopCreateSettings())
  70. metrics, err := scraper.scrape(ctx)
  71. require.NoError(t, err)
  72. require.NotEqual(t, metrics.MetricCount(), 0)
  73. goldenPath := filepath.Join("testdata", "metrics", "expected.yaml")
  74. expectedMetrics, err := golden.ReadMetrics(goldenPath)
  75. require.NoError(t, err)
  76. err = pmetrictest.CompareMetrics(expectedMetrics, metrics,
  77. pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp(),
  78. pmetrictest.IgnoreResourceMetricsOrder(),
  79. pmetrictest.IgnoreMetricDataPointsOrder(),
  80. )
  81. require.NoError(t, err)
  82. require.NoError(t, scraper.Shutdown(ctx))
  83. }
  84. func TestScrape_NoClient(t *testing.T) {
  85. ctx := context.Background()
  86. scraper := &vcenterMetricScraper{
  87. client: nil,
  88. config: &Config{
  89. Endpoint: "http://vcsa.localnet",
  90. },
  91. mb: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopCreateSettings()),
  92. logger: zap.NewNop(),
  93. }
  94. metrics, err := scraper.scrape(ctx)
  95. require.ErrorContains(t, err, "unable to connect to vSphere SDK")
  96. require.Equal(t, metrics.MetricCount(), 0)
  97. require.NoError(t, scraper.Shutdown(ctx))
  98. }
  99. func TestStartFailures_Metrics(t *testing.T) {
  100. cases := []struct {
  101. desc string
  102. cfg *Config
  103. err error
  104. }{
  105. {
  106. desc: "bad client connect",
  107. cfg: &Config{
  108. Endpoint: "http://no-host",
  109. },
  110. },
  111. {
  112. desc: "unparsable endpoint",
  113. cfg: &Config{
  114. Endpoint: "<protocol>://some-host",
  115. },
  116. },
  117. }
  118. ctx := context.Background()
  119. for _, tc := range cases {
  120. scraper := newVmwareVcenterScraper(zap.NewNop(), tc.cfg, receivertest.NewNopCreateSettings())
  121. err := scraper.Start(ctx, nil)
  122. if tc.err != nil {
  123. require.ErrorContains(t, err, tc.err.Error())
  124. } else {
  125. require.NoError(t, err)
  126. }
  127. }
  128. }