InfluxTest.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Configuration;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Threading;
  6. using DotNet.Testcontainers.Containers.Builders;
  7. using DotNet.Testcontainers.Containers.Modules;
  8. using DotNet.Testcontainers.Containers.WaitStrategies;
  9. using InfluxDB.Client;
  10. using Xunit;
  11. namespace OhmGraphite.Test
  12. {
  13. public class InfluxTest
  14. {
  15. [Fact, Trait("Category", "integration")]
  16. public async void CanInsertIntoInflux()
  17. {
  18. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  19. .WithImage("influxdb:1.8-alpine")
  20. .WithEnvironment("INFLUXDB_DB", "mydb")
  21. .WithEnvironment("INFLUXDB_USER", "my_user")
  22. .WithEnvironment("INFLUXDB_USER_PASSWORD", "my_pass")
  23. .WithPortBinding(8086, assignRandomHostPort: true)
  24. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));
  25. await using var container = testContainersBuilder.Build();
  26. await container.StartAsync();
  27. var baseUrl = $"http://{container.Hostname}:{container.GetMappedPublicPort(8086)}";
  28. var config = new InfluxConfig(new Uri(baseUrl), "mydb", "my_user", "my_pass");
  29. using var writer = new InfluxWriter(config, "my-pc");
  30. using var client = new HttpClient();
  31. for (int attempts = 0; ; attempts++)
  32. {
  33. try
  34. {
  35. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  36. var resp = await client.GetAsync(
  37. $"{baseUrl}/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");
  38. Assert.True(resp.IsSuccessStatusCode);
  39. var content = await resp.Content.ReadAsStringAsync();
  40. Assert.Contains("/intelcpu/0/temperature/0", content);
  41. break;
  42. }
  43. catch (Exception)
  44. {
  45. if (attempts >= 10)
  46. {
  47. throw;
  48. }
  49. Thread.Sleep(TimeSpan.FromSeconds(1));
  50. }
  51. }
  52. }
  53. [Fact, Trait("Category", "integration")]
  54. public async void CanInsertIntoPasswordLessInfluxdb()
  55. {
  56. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  57. .WithImage("influxdb:1.8-alpine")
  58. .WithEnvironment("INFLUXDB_DB", "mydb")
  59. .WithEnvironment("INFLUXDB_USER", "my_user")
  60. .WithEnvironment("INFLUXDB_HTTP_AUTH_ENABLED", "false")
  61. .WithPortBinding(8086, assignRandomHostPort: true)
  62. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));
  63. await using var container = testContainersBuilder.Build();
  64. await container.StartAsync();
  65. var baseUrl = $"http://{container.Hostname}:{container.GetMappedPublicPort(8086)}";
  66. var config = new InfluxConfig(new Uri(baseUrl), "mydb", "my_user", null);
  67. using var writer = new InfluxWriter(config, "my-pc");
  68. using var client = new HttpClient();
  69. for (int attempts = 0; ; attempts++)
  70. {
  71. try
  72. {
  73. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  74. var resp = await client.GetAsync(
  75. $"{baseUrl}/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");
  76. Assert.True(resp.IsSuccessStatusCode);
  77. var content = await resp.Content.ReadAsStringAsync();
  78. Assert.Contains("/intelcpu/0/temperature/0", content);
  79. break;
  80. }
  81. catch (Exception)
  82. {
  83. if (attempts >= 10)
  84. {
  85. throw;
  86. }
  87. Thread.Sleep(TimeSpan.FromSeconds(1));
  88. }
  89. }
  90. }
  91. [Fact, Trait("Category", "integration")]
  92. public async void CanInsertIntoInflux2()
  93. {
  94. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  95. .WithImage("influxdb:2.0-alpine")
  96. .WithEnvironment("DOCKER_INFLUXDB_INIT_MODE", "setup")
  97. .WithEnvironment("DOCKER_INFLUXDB_INIT_USERNAME", "my-user")
  98. .WithEnvironment("DOCKER_INFLUXDB_INIT_PASSWORD", "my-password")
  99. .WithEnvironment("DOCKER_INFLUXDB_INIT_BUCKET", "mydb")
  100. .WithEnvironment("DOCKER_INFLUXDB_INIT_ORG", "myorg")
  101. .WithPortBinding(8086, assignRandomHostPort: true)
  102. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));
  103. await using var container = testContainersBuilder.Build();
  104. await container.StartAsync();
  105. var baseUrl = $"http://{container.Hostname}:{container.GetMappedPublicPort(8086)}";
  106. var options = new InfluxDBClientOptions.Builder()
  107. .Url(baseUrl)
  108. .Authenticate("my-user", "my-password".ToCharArray())
  109. .Bucket("mydb")
  110. .Org("myorg")
  111. .Build();
  112. var config = new Influx2Config(options);
  113. using var writer = new Influx2Writer(config, "my-pc");
  114. for (int attempts = 0; ; attempts++)
  115. {
  116. try
  117. {
  118. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  119. var influxDBClient = InfluxDBClientFactory.Create(options);
  120. var flux = "from(bucket:\"mydb\") |> range(start: -1h)";
  121. var queryApi = influxDBClient.GetQueryApi();
  122. var tables = await queryApi.QueryAsync(flux, "myorg");
  123. var fields = tables.SelectMany(x => x.Records).Select(x => x.GetValueByKey("identifier"));
  124. Assert.Contains("/intelcpu/0/temperature/0", fields);
  125. break;
  126. }
  127. catch (Exception)
  128. {
  129. if (attempts >= 10)
  130. {
  131. throw;
  132. }
  133. Thread.Sleep(TimeSpan.FromSeconds(1));
  134. }
  135. }
  136. }
  137. [Fact, Trait("Category", "integration")]
  138. public async void CanInsertIntoInflux2Token()
  139. {
  140. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  141. .WithImage("influxdb:2.0-alpine")
  142. .WithEnvironment("DOCKER_INFLUXDB_INIT_MODE", "setup")
  143. .WithEnvironment("DOCKER_INFLUXDB_INIT_USERNAME", "my-user")
  144. .WithEnvironment("DOCKER_INFLUXDB_INIT_PASSWORD", "my-password")
  145. .WithEnvironment("DOCKER_INFLUXDB_INIT_BUCKET", "mydb")
  146. .WithEnvironment("DOCKER_INFLUXDB_INIT_ORG", "myorg")
  147. .WithEnvironment("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", "thisistheinfluxdbtoken")
  148. .WithPortBinding(8086, assignRandomHostPort: true)
  149. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));
  150. await using var container = testContainersBuilder.Build();
  151. await container.StartAsync();
  152. var baseUrl = $"http://{container.Hostname}:{container.GetMappedPublicPort(8086)}";
  153. var configMap = new ExeConfigurationFileMap {ExeConfigFilename = "assets/influx2.config"};
  154. var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
  155. config.AppSettings.Settings["influx2_address"].Value = baseUrl;
  156. var customConfig = new CustomConfig(config);
  157. var results = MetricConfig.ParseAppSettings(customConfig);
  158. using var writer = new Influx2Writer(results.Influx2, "my-pc");
  159. for (int attempts = 0; ; attempts++)
  160. {
  161. try
  162. {
  163. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  164. var influxDBClient = InfluxDBClientFactory.Create(results.Influx2.Options);
  165. var flux = "from(bucket:\"mydb\") |> range(start: -1h)";
  166. var queryApi = influxDBClient.GetQueryApi();
  167. var tables = await queryApi.QueryAsync(flux, "myorg");
  168. var fields = tables.SelectMany(x => x.Records).Select(x => x.GetValueByKey("identifier"));
  169. Assert.Contains("/intelcpu/0/temperature/0", fields);
  170. break;
  171. }
  172. catch (Exception)
  173. {
  174. if (attempts >= 10)
  175. {
  176. throw;
  177. }
  178. Thread.Sleep(TimeSpan.FromSeconds(1));
  179. }
  180. }
  181. }
  182. }
  183. }