integration_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. //go:build integration
  4. // +build integration
  5. package snmpreceiver
  6. import (
  7. "context"
  8. "path/filepath"
  9. "testing"
  10. "time"
  11. "github.com/stretchr/testify/require"
  12. "github.com/testcontainers/testcontainers-go"
  13. "go.opentelemetry.io/collector/component"
  14. "go.opentelemetry.io/collector/component/componenttest"
  15. "go.opentelemetry.io/collector/consumer/consumertest"
  16. "go.opentelemetry.io/collector/otelcol/otelcoltest"
  17. "go.opentelemetry.io/collector/receiver/receivertest"
  18. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
  20. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snmpreceiver/internal/metadata"
  21. )
  22. func TestIntegration(t *testing.T) {
  23. // remove nolint when https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/24240 is resolved
  24. // nolint:staticcheck
  25. testCases := []struct {
  26. desc string
  27. configFilename string
  28. expectedResultsFilename string
  29. }{
  30. {
  31. desc: "Integration test with v2c configuration",
  32. configFilename: "integration_test_v2c_config.yaml",
  33. expectedResultsFilename: "v2c_config_expected_metrics.yaml",
  34. },
  35. {
  36. desc: "Integration test with v3 configuration",
  37. configFilename: "integration_test_v3_config.yaml",
  38. expectedResultsFilename: "v3_config_expected_metrics.yaml",
  39. },
  40. }
  41. container := getContainer(t, snmpAgentContainerRequest)
  42. defer func() {
  43. require.NoError(t, container.Terminate(context.Background()))
  44. }()
  45. _, err := container.Host(context.Background())
  46. require.NoError(t, err)
  47. factories, err := otelcoltest.NopFactories()
  48. require.NoError(t, err)
  49. for _, testCase := range testCases {
  50. t.Run(testCase.desc, func(t *testing.T) {
  51. t.Skip("Flaky test, see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/21086")
  52. factory := NewFactory()
  53. factories.Receivers[metadata.Type] = factory
  54. configFile := filepath.Join("testdata", "integration", testCase.configFilename)
  55. cfg, err := otelcoltest.LoadConfigAndValidate(configFile, factories)
  56. require.NoError(t, err)
  57. snmpConfig := cfg.Receivers[component.NewID(metadata.Type)].(*Config)
  58. consumer := new(consumertest.MetricsSink)
  59. settings := receivertest.NewNopCreateSettings()
  60. rcvr, err := factory.CreateMetricsReceiver(context.Background(), settings, snmpConfig, consumer)
  61. require.NoError(t, err, "failed creating metrics receiver")
  62. require.NoError(t, rcvr.Start(context.Background(), componenttest.NewNopHost()))
  63. require.Eventuallyf(t, func() bool {
  64. return len(consumer.AllMetrics()) > 0
  65. }, 2*time.Minute, 1*time.Second, "failed to receive more than 0 metrics")
  66. require.NoError(t, rcvr.Shutdown(context.Background()))
  67. actualMetrics := consumer.AllMetrics()[0]
  68. expectedFile := filepath.Join("testdata", "integration", testCase.expectedResultsFilename)
  69. expectedMetrics, err := golden.ReadMetrics(expectedFile)
  70. require.NoError(t, err)
  71. err = pmetrictest.CompareMetrics(expectedMetrics, actualMetrics, pmetrictest.IgnoreMetricsOrder(),
  72. pmetrictest.IgnoreTimestamp(), pmetrictest.IgnoreStartTimestamp())
  73. require.NoError(t, err)
  74. })
  75. }
  76. }
  77. var (
  78. snmpAgentContainerRequest = testcontainers.ContainerRequest{
  79. FromDockerfile: testcontainers.FromDockerfile{
  80. Context: filepath.Join("testdata", "integration", "docker"),
  81. Dockerfile: "snmp_agent.Dockerfile",
  82. },
  83. ExposedPorts: []string{"1024:1024/udp"},
  84. }
  85. )
  86. func getContainer(t *testing.T, req testcontainers.ContainerRequest) testcontainers.Container {
  87. require.NoError(t, req.Validate())
  88. container, err := testcontainers.GenericContainer(
  89. context.Background(),
  90. testcontainers.GenericContainerRequest{
  91. ContainerRequest: req,
  92. Started: true,
  93. })
  94. require.NoError(t, err)
  95. return container
  96. }