GraphiteTest.cs 4.0 KB

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