1
0

InfluxTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System;
  2. using System.Configuration;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using DotNet.Testcontainers.Builders;
  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 Task CanInsertIntoInflux()
  17. {
  18. var testContainersBuilder = new ContainerBuilder()
  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 Task CanInsertIntoPasswordLessInfluxdb()
  56. {
  57. var testContainersBuilder = new ContainerBuilder()
  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 Task CanInsertIntoInflux2()
  95. {
  96. var testContainersBuilder = new ContainerBuilder()
  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 = new InfluxDBClient(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 Task CanInsertIntoInflux2Token()
  142. {
  143. var testContainersBuilder = new ContainerBuilder()
  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 = new InfluxDBClient(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. [Fact, Trait("Category", "integration")]
  187. public async Task CanInsertIntoInflux2TokenTls()
  188. {
  189. // We do some fancy docker footwork where we informally connect
  190. // these two containers. In the future I believe test containers will
  191. // be able to natively handle adding these to a docker network
  192. var testContainersBuilder = new ContainerBuilder()
  193. .WithDockerEndpoint(DockerUtils.DockerEndpoint())
  194. .WithImage("influxdb:2.0-alpine")
  195. .WithEnvironment("DOCKER_INFLUXDB_INIT_MODE", "setup")
  196. .WithEnvironment("DOCKER_INFLUXDB_INIT_USERNAME", "my-user")
  197. .WithEnvironment("DOCKER_INFLUXDB_INIT_PASSWORD", "my-password")
  198. .WithEnvironment("DOCKER_INFLUXDB_INIT_BUCKET", "mydb")
  199. .WithEnvironment("DOCKER_INFLUXDB_INIT_ORG", "myorg")
  200. .WithEnvironment("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", "thisistheinfluxdbtoken")
  201. .WithPortBinding(8086, assignRandomHostPort: true)
  202. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));
  203. await using var container = testContainersBuilder.Build();
  204. await container.StartAsync();
  205. var cmd = $"apk add openssl && openssl req -x509 -nodes -newkey rsa:4096 -keyout /tmp/key.pem -out /tmp/cert.pem -days 365 -subj '/C=US/ST=Oregon/L=Portland/O=Company Name/OU=Org/CN=www.example.com' && /usr/bin/ghostunnel server --listen=0.0.0.0:8087 --target={container.IpAddress}:8086 --unsafe-target --disable-authentication --key /tmp/key.pem --cert=/tmp/cert.pem";
  206. var tlsContainerBuilder = new ContainerBuilder()
  207. .WithDockerEndpoint(DockerUtils.DockerEndpoint())
  208. .WithImage("squareup/ghostunnel")
  209. .WithExposedPort(8087)
  210. .WithPortBinding(8087, assignRandomHostPort: true)
  211. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8087))
  212. .WithEntrypoint("/bin/sh")
  213. .WithCommand("-c", cmd);
  214. await using var tlsContainer = tlsContainerBuilder.Build();
  215. await tlsContainer.StartAsync();
  216. var baseUrl = $"https://{tlsContainer.Hostname}:{tlsContainer.GetMappedPublicPort(8087)}";
  217. var configMap = new ExeConfigurationFileMap { ExeConfigFilename = "assets/influx2-ssl.config" };
  218. var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
  219. config.AppSettings.Settings["influx2_address"].Value = baseUrl;
  220. var customConfig = new CustomConfig(config);
  221. var results = MetricConfig.ParseAppSettings(customConfig);
  222. try
  223. {
  224. using var writer = new Influx2Writer(results.Influx2, "my-pc");
  225. for (int attempts = 0; ; attempts++)
  226. {
  227. try
  228. {
  229. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  230. var influxDbClient = new InfluxDBClient(results.Influx2.Options);
  231. var flux = "from(bucket:\"mydb\") |> range(start: -1h)";
  232. var queryApi = influxDbClient.GetQueryApi();
  233. var tables = await queryApi.QueryAsync(flux, "myorg");
  234. var fields = tables.SelectMany(x => x.Records).Select(x => x.GetValueByKey("identifier"));
  235. Assert.Contains("/intelcpu/0/temperature/0", fields);
  236. break;
  237. }
  238. catch (Exception)
  239. {
  240. if (attempts >= 10)
  241. {
  242. throw;
  243. }
  244. Thread.Sleep(TimeSpan.FromSeconds(1));
  245. }
  246. }
  247. }
  248. finally
  249. {
  250. ServicePointManager.ServerCertificateValidationCallback = null;
  251. }
  252. }
  253. }
  254. }