1
0

InfluxTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 DotNet.Testcontainers.Containers.Builders;
  8. using DotNet.Testcontainers.Containers.Modules;
  9. using DotNet.Testcontainers.Containers.WaitStrategies;
  10. using InfluxDB.Client;
  11. using Xunit;
  12. namespace OhmGraphite.Test
  13. {
  14. public class InfluxTest
  15. {
  16. [Fact, Trait("Category", "integration")]
  17. public async void CanInsertIntoInflux()
  18. {
  19. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  20. .WithDockerEndpoint(DockerUtils.DockerEndpoint())
  21. .WithImage("influxdb:1.8-alpine")
  22. .WithEnvironment("INFLUXDB_DB", "mydb")
  23. .WithEnvironment("INFLUXDB_USER", "my_user")
  24. .WithEnvironment("INFLUXDB_USER_PASSWORD", "my_pass")
  25. .WithPortBinding(8086, assignRandomHostPort: true)
  26. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));
  27. await using var container = testContainersBuilder.Build();
  28. await container.StartAsync();
  29. var baseUrl = $"http://{container.Hostname}:{container.GetMappedPublicPort(8086)}";
  30. var config = new InfluxConfig(new Uri(baseUrl), "mydb", "my_user", "my_pass");
  31. using var writer = new InfluxWriter(config, "my-pc");
  32. using var client = new HttpClient();
  33. for (int attempts = 0;; attempts++)
  34. {
  35. try
  36. {
  37. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  38. var resp = await client.GetAsync(
  39. $"{baseUrl}/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");
  40. Assert.True(resp.IsSuccessStatusCode);
  41. var content = await resp.Content.ReadAsStringAsync();
  42. Assert.Contains("/intelcpu/0/temperature/0", content);
  43. break;
  44. }
  45. catch (Exception)
  46. {
  47. if (attempts >= 10)
  48. {
  49. throw;
  50. }
  51. Thread.Sleep(TimeSpan.FromSeconds(1));
  52. }
  53. }
  54. }
  55. [Fact, Trait("Category", "integration")]
  56. public async void CanInsertIntoPasswordLessInfluxdb()
  57. {
  58. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  59. .WithDockerEndpoint(DockerUtils.DockerEndpoint())
  60. .WithImage("influxdb:1.8-alpine")
  61. .WithEnvironment("INFLUXDB_DB", "mydb")
  62. .WithEnvironment("INFLUXDB_USER", "my_user")
  63. .WithEnvironment("INFLUXDB_HTTP_AUTH_ENABLED", "false")
  64. .WithPortBinding(8086, assignRandomHostPort: true)
  65. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));
  66. await using var container = testContainersBuilder.Build();
  67. await container.StartAsync();
  68. var baseUrl = $"http://{container.Hostname}:{container.GetMappedPublicPort(8086)}";
  69. var config = new InfluxConfig(new Uri(baseUrl), "mydb", "my_user", null);
  70. using var writer = new InfluxWriter(config, "my-pc");
  71. using var client = new HttpClient();
  72. for (int attempts = 0;; attempts++)
  73. {
  74. try
  75. {
  76. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  77. var resp = await client.GetAsync(
  78. $"{baseUrl}/query?pretty=true&db=mydb&q=SELECT%20*%20FROM%20Temperature");
  79. Assert.True(resp.IsSuccessStatusCode);
  80. var content = await resp.Content.ReadAsStringAsync();
  81. Assert.Contains("/intelcpu/0/temperature/0", content);
  82. break;
  83. }
  84. catch (Exception)
  85. {
  86. if (attempts >= 10)
  87. {
  88. throw;
  89. }
  90. Thread.Sleep(TimeSpan.FromSeconds(1));
  91. }
  92. }
  93. }
  94. [Fact, Trait("Category", "integration")]
  95. public async void CanInsertIntoInflux2()
  96. {
  97. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  98. .WithDockerEndpoint(DockerUtils.DockerEndpoint())
  99. .WithImage("influxdb:2.0-alpine")
  100. .WithEnvironment("DOCKER_INFLUXDB_INIT_MODE", "setup")
  101. .WithEnvironment("DOCKER_INFLUXDB_INIT_USERNAME", "my-user")
  102. .WithEnvironment("DOCKER_INFLUXDB_INIT_PASSWORD", "my-password")
  103. .WithEnvironment("DOCKER_INFLUXDB_INIT_BUCKET", "mydb")
  104. .WithEnvironment("DOCKER_INFLUXDB_INIT_ORG", "myorg")
  105. .WithPortBinding(8086, assignRandomHostPort: true)
  106. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));
  107. await using var container = testContainersBuilder.Build();
  108. await container.StartAsync();
  109. var baseUrl = $"http://{container.Hostname}:{container.GetMappedPublicPort(8086)}";
  110. var options = new InfluxDBClientOptions.Builder()
  111. .Url(baseUrl)
  112. .Authenticate("my-user", "my-password".ToCharArray())
  113. .Bucket("mydb")
  114. .Org("myorg")
  115. .Build();
  116. var config = new Influx2Config(options);
  117. using var writer = new Influx2Writer(config, "my-pc");
  118. for (int attempts = 0;; attempts++)
  119. {
  120. try
  121. {
  122. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  123. var influxDBClient = InfluxDBClientFactory.Create(options);
  124. var flux = "from(bucket:\"mydb\") |> range(start: -1h)";
  125. var queryApi = influxDBClient.GetQueryApi();
  126. var tables = await queryApi.QueryAsync(flux, "myorg");
  127. var fields = tables.SelectMany(x => x.Records).Select(x => x.GetValueByKey("identifier"));
  128. Assert.Contains("/intelcpu/0/temperature/0", fields);
  129. break;
  130. }
  131. catch (Exception)
  132. {
  133. if (attempts >= 10)
  134. {
  135. throw;
  136. }
  137. Thread.Sleep(TimeSpan.FromSeconds(1));
  138. }
  139. }
  140. }
  141. [Fact, Trait("Category", "integration")]
  142. public async void CanInsertIntoInflux2Token()
  143. {
  144. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  145. .WithDockerEndpoint(DockerUtils.DockerEndpoint())
  146. .WithImage("influxdb:2.0-alpine")
  147. .WithEnvironment("DOCKER_INFLUXDB_INIT_MODE", "setup")
  148. .WithEnvironment("DOCKER_INFLUXDB_INIT_USERNAME", "my-user")
  149. .WithEnvironment("DOCKER_INFLUXDB_INIT_PASSWORD", "my-password")
  150. .WithEnvironment("DOCKER_INFLUXDB_INIT_BUCKET", "mydb")
  151. .WithEnvironment("DOCKER_INFLUXDB_INIT_ORG", "myorg")
  152. .WithEnvironment("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", "thisistheinfluxdbtoken")
  153. .WithPortBinding(8086, assignRandomHostPort: true)
  154. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));
  155. await using var container = testContainersBuilder.Build();
  156. await container.StartAsync();
  157. var baseUrl = $"http://{container.Hostname}:{container.GetMappedPublicPort(8086)}";
  158. var configMap = new ExeConfigurationFileMap {ExeConfigFilename = "assets/influx2.config"};
  159. var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
  160. config.AppSettings.Settings["influx2_address"].Value = baseUrl;
  161. var customConfig = new CustomConfig(config);
  162. var results = MetricConfig.ParseAppSettings(customConfig);
  163. using var writer = new Influx2Writer(results.Influx2, "my-pc");
  164. for (int attempts = 0;; attempts++)
  165. {
  166. try
  167. {
  168. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  169. var influxDBClient = InfluxDBClientFactory.Create(results.Influx2.Options);
  170. var flux = "from(bucket:\"mydb\") |> range(start: -1h)";
  171. var queryApi = influxDBClient.GetQueryApi();
  172. var tables = await queryApi.QueryAsync(flux, "myorg");
  173. var fields = tables.SelectMany(x => x.Records).Select(x => x.GetValueByKey("identifier"));
  174. Assert.Contains("/intelcpu/0/temperature/0", fields);
  175. break;
  176. }
  177. catch (Exception)
  178. {
  179. if (attempts >= 10)
  180. {
  181. throw;
  182. }
  183. Thread.Sleep(TimeSpan.FromSeconds(1));
  184. }
  185. }
  186. }
  187. [Fact, Trait("Category", "integration")]
  188. public async void CanInsertIntoInflux2TokenTls()
  189. {
  190. // We do some fancy docker footwork where we informally connect
  191. // these two containers. In the future I believe test containers will
  192. // be able to natively handle adding these to a docker network
  193. var testContainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  194. .WithDockerEndpoint(DockerUtils.DockerEndpoint())
  195. .WithImage("influxdb:2.0-alpine")
  196. .WithEnvironment("DOCKER_INFLUXDB_INIT_MODE", "setup")
  197. .WithEnvironment("DOCKER_INFLUXDB_INIT_USERNAME", "my-user")
  198. .WithEnvironment("DOCKER_INFLUXDB_INIT_PASSWORD", "my-password")
  199. .WithEnvironment("DOCKER_INFLUXDB_INIT_BUCKET", "mydb")
  200. .WithEnvironment("DOCKER_INFLUXDB_INIT_ORG", "myorg")
  201. .WithEnvironment("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", "thisistheinfluxdbtoken")
  202. .WithPortBinding(8086, assignRandomHostPort: true)
  203. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8086));
  204. await using var container = testContainersBuilder.Build();
  205. await container.StartAsync();
  206. 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";
  207. var tlsContainerBuilder = new TestcontainersBuilder<TestcontainersContainer>()
  208. .WithDockerEndpoint(DockerUtils.DockerEndpoint())
  209. .WithImage("squareup/ghostunnel")
  210. .WithExposedPort(8087)
  211. .WithPortBinding(8087, assignRandomHostPort: true)
  212. .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8087))
  213. .WithEntrypoint("/bin/sh")
  214. .WithCommand("-c", cmd);
  215. await using var tlsContainer = tlsContainerBuilder.Build();
  216. await tlsContainer.StartAsync();
  217. var baseUrl = $"https://{tlsContainer.Hostname}:{tlsContainer.GetMappedPublicPort(8087)}";
  218. var configMap = new ExeConfigurationFileMap { ExeConfigFilename = "assets/influx2.config" };
  219. var config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
  220. config.AppSettings.Settings["influx2_address"].Value = baseUrl;
  221. var customConfig = new CustomConfig(config);
  222. var results = MetricConfig.ParseAppSettings(customConfig);
  223. MetricConfig.InstallCertificateVerification("false");
  224. try
  225. {
  226. using var writer = new Influx2Writer(results.Influx2, "my-pc");
  227. for (int attempts = 0;; attempts++)
  228. {
  229. try
  230. {
  231. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  232. var influxDbClient = InfluxDBClientFactory.Create(results.Influx2.Options);
  233. var flux = "from(bucket:\"mydb\") |> range(start: -1h)";
  234. var queryApi = influxDbClient.GetQueryApi();
  235. var tables = await queryApi.QueryAsync(flux, "myorg");
  236. var fields = tables.SelectMany(x => x.Records).Select(x => x.GetValueByKey("identifier"));
  237. Assert.Contains("/intelcpu/0/temperature/0", fields);
  238. break;
  239. }
  240. catch (Exception)
  241. {
  242. if (attempts >= 10)
  243. {
  244. throw;
  245. }
  246. Thread.Sleep(TimeSpan.FromSeconds(1));
  247. }
  248. }
  249. }
  250. finally
  251. {
  252. ServicePointManager.ServerCertificateValidationCallback = null;
  253. }
  254. }
  255. }
  256. }