InfluxTest.cs 9.1 KB

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