integration_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. //go:build integration
  4. // +build integration
  5. package mysqlreceiver
  6. import (
  7. "net"
  8. "path/filepath"
  9. "testing"
  10. "time"
  11. "github.com/testcontainers/testcontainers-go"
  12. "github.com/testcontainers/testcontainers-go/wait"
  13. "go.opentelemetry.io/collector/component"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/scraperinttest"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
  16. )
  17. const mysqlPort = "3306"
  18. func TestIntegrationWithoutTLS(t *testing.T) {
  19. scraperinttest.NewIntegrationTest(
  20. NewFactory(),
  21. scraperinttest.WithContainerRequest(
  22. testcontainers.ContainerRequest{
  23. Image: "mysql:8.0.33",
  24. ExposedPorts: []string{mysqlPort},
  25. WaitingFor: wait.ForListeningPort(mysqlPort).
  26. WithStartupTimeout(2 * time.Minute),
  27. Env: map[string]string{
  28. "MYSQL_ROOT_PASSWORD": "otel",
  29. "MYSQL_DATABASE": "otel",
  30. "MYSQL_USER": "otel",
  31. "MYSQL_PASSWORD": "otel",
  32. },
  33. Files: []testcontainers.ContainerFile{
  34. {
  35. HostFilePath: filepath.Join("testdata", "integration", "init.sh"),
  36. ContainerFilePath: "/docker-entrypoint-initdb.d/init.sh",
  37. FileMode: 700,
  38. },
  39. },
  40. }),
  41. scraperinttest.WithCustomConfig(
  42. func(t *testing.T, cfg component.Config, ci *scraperinttest.ContainerInfo) {
  43. rCfg := cfg.(*Config)
  44. rCfg.CollectionInterval = time.Second
  45. rCfg.Endpoint = net.JoinHostPort(ci.Host(t), ci.MappedPort(t, mysqlPort))
  46. rCfg.Username = "otel"
  47. rCfg.Password = "otel"
  48. // disable TLS connection
  49. rCfg.TLS.Insecure = true
  50. }),
  51. scraperinttest.WithCompareOptions(
  52. pmetrictest.IgnoreResourceAttributeValue("mysql.instance.endpoint"),
  53. pmetrictest.IgnoreMetricValues(),
  54. pmetrictest.IgnoreMetricDataPointsOrder(),
  55. pmetrictest.IgnoreStartTimestamp(),
  56. pmetrictest.IgnoreTimestamp(),
  57. ),
  58. ).Run(t)
  59. }
  60. func TestIntegrationWithTLS(t *testing.T) {
  61. scraperinttest.NewIntegrationTest(
  62. NewFactory(),
  63. scraperinttest.WithContainerRequest(
  64. testcontainers.ContainerRequest{
  65. Image: "mysql:8.0.33",
  66. // enable auto TLS certs AND require TLS connections only !
  67. Cmd: []string{"--auto_generate_certs=ON", "--require_secure_transport=ON"},
  68. ExposedPorts: []string{mysqlPort},
  69. WaitingFor: wait.ForListeningPort(mysqlPort).
  70. WithStartupTimeout(2 * time.Minute),
  71. Env: map[string]string{
  72. "MYSQL_ROOT_PASSWORD": "otel",
  73. "MYSQL_DATABASE": "otel",
  74. "MYSQL_USER": "otel",
  75. "MYSQL_PASSWORD": "otel",
  76. },
  77. Files: []testcontainers.ContainerFile{
  78. {
  79. HostFilePath: filepath.Join("testdata", "integration", "init.sh"),
  80. ContainerFilePath: "/docker-entrypoint-initdb.d/init.sh",
  81. FileMode: 700,
  82. },
  83. },
  84. }),
  85. scraperinttest.WithCustomConfig(
  86. func(t *testing.T, cfg component.Config, ci *scraperinttest.ContainerInfo) {
  87. rCfg := cfg.(*Config)
  88. rCfg.CollectionInterval = time.Second
  89. rCfg.Endpoint = net.JoinHostPort(ci.Host(t), ci.MappedPort(t, mysqlPort))
  90. rCfg.Username = "otel"
  91. rCfg.Password = "otel"
  92. // mysql container is using self-signed certs
  93. // InsecureSkipVerify will enable TLS but not verify the certificate.
  94. rCfg.TLS.InsecureSkipVerify = true
  95. }),
  96. scraperinttest.WithCompareOptions(
  97. pmetrictest.IgnoreResourceAttributeValue("mysql.instance.endpoint"),
  98. pmetrictest.IgnoreMetricValues(),
  99. pmetrictest.IgnoreMetricDataPointsOrder(),
  100. pmetrictest.IgnoreStartTimestamp(),
  101. pmetrictest.IgnoreTimestamp(),
  102. ),
  103. ).Run(t)
  104. }