correctness_test_case.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package metrics // import "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/correctnesstests/metrics"
  4. import (
  5. "log"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/otelcol"
  10. "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/correctnesstests"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/testbed/testbed"
  12. )
  13. type correctnessTestCase struct {
  14. t *testing.T
  15. sender testbed.DataSender
  16. receiver testbed.DataReceiver
  17. harness *testHarness
  18. collector testbed.OtelcolRunner
  19. }
  20. func newCorrectnessTestCase(
  21. t *testing.T,
  22. sender testbed.DataSender,
  23. receiver testbed.DataReceiver,
  24. harness *testHarness,
  25. ) *correctnessTestCase {
  26. return &correctnessTestCase{t: t, sender: sender, receiver: receiver, harness: harness}
  27. }
  28. func (tc *correctnessTestCase) startCollector() {
  29. tc.collector = testbed.NewInProcessCollector(componentFactories(tc.t))
  30. _, err := tc.collector.PrepareConfig(correctnesstests.CreateConfigYaml(tc.sender, tc.receiver, nil, "metrics"))
  31. require.NoError(tc.t, err)
  32. rd, err := newResultsDir(tc.t.Name())
  33. require.NoError(tc.t, err)
  34. err = rd.mkDir()
  35. require.NoError(tc.t, err)
  36. fname, err := rd.fullPath("agent.log")
  37. require.NoError(tc.t, err)
  38. log.Println("starting collector")
  39. err = tc.collector.Start(testbed.StartParams{
  40. Name: "Agent",
  41. LogFilePath: fname,
  42. })
  43. require.NoError(tc.t, err)
  44. }
  45. func (tc *correctnessTestCase) stopCollector() {
  46. _, err := tc.collector.Stop()
  47. require.NoError(tc.t, err)
  48. }
  49. func (tc *correctnessTestCase) startTestbedSender() {
  50. log.Println("starting testbed sender")
  51. err := tc.sender.Start()
  52. require.NoError(tc.t, err)
  53. }
  54. func (tc *correctnessTestCase) startTestbedReceiver() {
  55. log.Println("starting testbed receiver")
  56. err := tc.receiver.Start(&testbed.MockTraceConsumer{}, tc.harness, &testbed.MockLogConsumer{})
  57. require.NoError(tc.t, err)
  58. }
  59. func (tc *correctnessTestCase) stopTestbedReceiver() {
  60. log.Println("stopping testbed receiver")
  61. err := tc.receiver.Stop()
  62. require.NoError(tc.t, err)
  63. }
  64. func (tc *correctnessTestCase) sendFirstMetric() {
  65. tc.harness.sendNextMetric()
  66. }
  67. func (tc *correctnessTestCase) waitForAllMetrics() {
  68. log.Println("waiting for allMetricsReceived")
  69. for {
  70. select {
  71. case <-time.After(10 * time.Second):
  72. tc.t.Fatal("Deadline exceeded while waiting to receive metrics")
  73. return
  74. case <-tc.harness.allMetricsReceived:
  75. log.Println("all metrics received")
  76. return
  77. }
  78. }
  79. }
  80. func componentFactories(t *testing.T) otelcol.Factories {
  81. factories, err := testbed.Components()
  82. require.NoError(t, err)
  83. return factories
  84. }