1
0

GraphiteTest.cs 3.9 KB

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