GraphiteTest.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Net.Http;
  3. using System.Threading;
  4. using DotNet.Testcontainers.Containers.Builders;
  5. using DotNet.Testcontainers.Containers.Modules;
  6. using DotNet.Testcontainers.Containers.WaitStrategies;
  7. using Xunit;
  8. namespace OhmGraphite.Test
  9. {
  10. public class GraphiteTest
  11. {
  12. [Fact, Trait("Category", "integration")]
  13. public async void InsertGraphiteTest()
  14. {
  15. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  16. .WithDockerEndpoint(DockerUtils.DockerEndpoint())
  17. .WithImage("graphiteapp/graphite-statsd")
  18. .WithEnvironment("REDIS_TAGDB", "y")
  19. .WithPortBinding(2003, assignRandomHostPort: true)
  20. .WithPortBinding(80, assignRandomHostPort: true)
  21. .WithPortBinding(8080, assignRandomHostPort: true)
  22. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8080));
  23. await using var container = testContainersBuilder.Build();
  24. await container.StartAsync();
  25. var port = container.GetMappedPublicPort(2003);
  26. using var writer = new GraphiteWriter(container.Hostname, port, "my-pc", tags: false);
  27. using var client = new HttpClient();
  28. for (int attempts = 0; ; attempts++)
  29. {
  30. try
  31. {
  32. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  33. var resp = await client.GetAsync(
  34. $"http://{container.Hostname}:{container.GetMappedPublicPort(80)}/render?format=csv&target=ohm.my-pc.intelcpu.0.temperature.cpucore.1");
  35. var content = await resp.Content.ReadAsStringAsync();
  36. Assert.Contains("ohm.my-pc.intelcpu.0.temperature.cpucore.1", content);
  37. break;
  38. }
  39. catch (Exception)
  40. {
  41. if (attempts >= 10)
  42. {
  43. throw;
  44. }
  45. Thread.Sleep(TimeSpan.FromSeconds(1));
  46. }
  47. }
  48. }
  49. [Fact, Trait("Category", "integration")]
  50. public async void InsertTagGraphiteTest()
  51. {
  52. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  53. .WithDockerEndpoint(DockerUtils.DockerEndpoint())
  54. .WithImage("graphiteapp/graphite-statsd")
  55. .WithEnvironment("REDIS_TAGDB", "y")
  56. .WithPortBinding(2003, assignRandomHostPort: true)
  57. .WithPortBinding(80, assignRandomHostPort: true)
  58. .WithPortBinding(8080, assignRandomHostPort: true)
  59. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8080));
  60. await using var container = testContainersBuilder.Build();
  61. await container.StartAsync();
  62. var port = container.GetMappedPublicPort(2003);
  63. using var writer = new GraphiteWriter(container.Hostname, port, "my-pc", tags: true);
  64. using var client = new HttpClient();
  65. for (int attempts = 0;; attempts++)
  66. {
  67. try
  68. {
  69. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  70. var resp = await client.GetAsync(
  71. $"http://{container.Hostname}:{container.GetMappedPublicPort(80)}/render?format=csv&target=seriesByTag('sensor_type=Temperature','hardware_type=CPU')");
  72. var content = await resp.Content.ReadAsStringAsync();
  73. Assert.Contains("host=my-pc", content);
  74. Assert.Contains("app=ohm", content);
  75. Assert.Contains("sensor_type=Temperature", content);
  76. break;
  77. }
  78. catch (Exception)
  79. {
  80. if (attempts >= 10)
  81. {
  82. throw;
  83. }
  84. Thread.Sleep(TimeSpan.FromSeconds(1));
  85. }
  86. }
  87. }
  88. }
  89. }