InfluxTest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 InfluxTest
  11. {
  12. [Fact, Trait("Category", "integration")]
  13. public async void CanInsertIntoInflux()
  14. {
  15. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  16. .WithImage("influxdb:1.8-alpine")
  17. .WithEnvironment("INFLUXDB_DB", "mydb")
  18. .WithEnvironment("INFLUXDB_USER", "my_user")
  19. .WithEnvironment("INFLUXDB_USER_PASSWORD", "my_pass")
  20. .WithPortBinding(8086, assignRandomHostPort: true)
  21. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));
  22. await using var container = testContainersBuilder.Build();
  23. await container.StartAsync();
  24. var baseUrl = $"http://{container.Hostname}:{container.GetMappedPublicPort(8086)}";
  25. var config = new InfluxConfig(new Uri(baseUrl), "mydb", "my_user", "my_pass");
  26. using var writer = new InfluxWriter(config, "my-pc");
  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. $"{baseUrl}/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");
  35. Assert.True(resp.IsSuccessStatusCode);
  36. var content = await resp.Content.ReadAsStringAsync();
  37. Assert.Contains("/intelcpu/0/temperature/0", content);
  38. break;
  39. }
  40. catch (Exception)
  41. {
  42. if (attempts >= 10)
  43. {
  44. throw;
  45. }
  46. Thread.Sleep(TimeSpan.FromSeconds(1));
  47. }
  48. }
  49. }
  50. [Fact, Trait("Category", "integration")]
  51. public async void CanInsertIntoPasswordLessInfluxdb()
  52. {
  53. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  54. .WithImage("influxdb:1.8-alpine")
  55. .WithEnvironment("INFLUXDB_DB", "mydb")
  56. .WithEnvironment("INFLUXDB_USER", "my_user")
  57. .WithEnvironment("INFLUXDB_HTTP_AUTH_ENABLED", "false")
  58. .WithPortBinding(8086, assignRandomHostPort: true)
  59. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));
  60. await using var container = testContainersBuilder.Build();
  61. await container.StartAsync();
  62. var baseUrl = $"http://{container.Hostname}:{container.GetMappedPublicPort(8086)}";
  63. var config = new InfluxConfig(new Uri(baseUrl), "mydb", "my_user", null);
  64. using var writer = new InfluxWriter(config, "my-pc");
  65. using var client = new HttpClient();
  66. for (int attempts = 0; ; attempts++)
  67. {
  68. try
  69. {
  70. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  71. var resp = await client.GetAsync(
  72. $"{baseUrl}/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");
  73. Assert.True(resp.IsSuccessStatusCode);
  74. var content = await resp.Content.ReadAsStringAsync();
  75. Assert.Contains("/intelcpu/0/temperature/0", 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. //[Fact, Trait("Category", "integration")]
  89. //public async void CanInsertIntoInflux2()
  90. //{
  91. // var config = new InfluxConfig(new Uri("http://influx2:8086"), "mydb", "my_user", "my_pass");
  92. // using (var writer = new InfluxWriter(config, "my-pc"))
  93. // using (var client = new HttpClient())
  94. // {
  95. // for (int attempts = 0; ; attempts++)
  96. // {
  97. // try
  98. // {
  99. // await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  100. // var resp = await client.GetAsync(
  101. // "http://influx2:8086/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");
  102. // Assert.True(resp.IsSuccessStatusCode);
  103. // var content = await resp.Content.ReadAsStringAsync();
  104. // Assert.Contains("/intelcpu/0/temperature/0", content);
  105. // break;
  106. // }
  107. // catch (Exception)
  108. // {
  109. // if (attempts >= 10)
  110. // {
  111. // throw;
  112. // }
  113. // Thread.Sleep(TimeSpan.FromSeconds(1));
  114. // }
  115. // }
  116. // }
  117. //}
  118. }
  119. }