-
-
Notifications
You must be signed in to change notification settings - Fork 340
feat: Add Grafana module #1509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
feat: Add Grafana module #1509
Changes from all commits
29415ee
66c10ec
9b2967e
c16798b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,159 @@ | ||||||
| namespace Testcontainers.Grafana; | ||||||
|
|
||||||
| /// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||||||
| [PublicAPI] | ||||||
| public sealed class GrafanaBuilder : ContainerBuilder<GrafanaBuilder, GrafanaContainer, GrafanaConfiguration> | ||||||
| { | ||||||
| public const string GrafanaImage = "grafana/grafana:11.0.0"; | ||||||
|
|
||||||
| public const ushort GrafanaPort = 3000; | ||||||
|
|
||||||
| public const string DefaultUsername = "admin"; | ||||||
|
|
||||||
| public const string DefaultPassword = "admin"; | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Initializes a new instance of the <see cref="GrafanaBuilder" /> class. | ||||||
| /// </summary> | ||||||
| public GrafanaBuilder() | ||||||
| : this(new GrafanaConfiguration()) | ||||||
| { | ||||||
| DockerResourceConfiguration = Init().DockerResourceConfiguration; | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Initializes a new instance of the <see cref="GrafanaBuilder" /> class. | ||||||
| /// </summary> | ||||||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||||||
| private GrafanaBuilder(GrafanaConfiguration resourceConfiguration) | ||||||
| : base(resourceConfiguration) | ||||||
| { | ||||||
| DockerResourceConfiguration = resourceConfiguration; | ||||||
| } | ||||||
|
|
||||||
| /// <inheritdoc /> | ||||||
| protected override GrafanaConfiguration DockerResourceConfiguration { get; } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Sets the Grafana username. | ||||||
| /// </summary> | ||||||
| /// <param name="username">The Grafana username.</param> | ||||||
| /// <returns>A configured instance of <see cref="GrafanaBuilder" />.</returns> | ||||||
| public GrafanaBuilder WithUsername(string username) | ||||||
| { | ||||||
| return Merge(DockerResourceConfiguration, new GrafanaConfiguration(username: username)) | ||||||
| .WithEnvironment("GF_SECURITY_ADMIN_USER", username); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Sets the Grafana password. | ||||||
| /// </summary> | ||||||
| /// <param name="password">The Grafana password.</param> | ||||||
| /// <returns>A configured instance of <see cref="GrafanaBuilder" />.</returns> | ||||||
| public GrafanaBuilder WithPassword(string password) | ||||||
| { | ||||||
| return Merge(DockerResourceConfiguration, new GrafanaConfiguration(password: password)) | ||||||
| .WithEnvironment("GF_SECURITY_ADMIN_PASSWORD", password); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Disables the anonymous access. | ||||||
| /// </summary> | ||||||
| /// <returns>A configured instance of <see cref="GrafanaBuilder" />.</returns> | ||||||
| public GrafanaBuilder WithAnonymousAccessDisabled() | ||||||
| { | ||||||
| return WithEnvironment("GF_AUTH_ANONYMOUS_ENABLED", "false"); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Enables the anonymous access. | ||||||
| /// </summary> | ||||||
| /// <returns>A configured instance of <see cref="GrafanaBuilder" />.</returns> | ||||||
| public GrafanaBuilder WithAnonymousAccessEnabled() | ||||||
| { | ||||||
| return WithEnvironment("GF_AUTH_ANONYMOUS_ENABLED", "true") | ||||||
| .WithEnvironment("GF_AUTH_ANONYMOUS_ORG_ROLE", "Admin"); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Mounts a datasource configuration file. | ||||||
| /// </summary> | ||||||
| /// <param name="datasourceFilePath">The path to the datasource configuration file.</param> | ||||||
| /// <returns>A configured instance of <see cref="GrafanaBuilder" />.</returns> | ||||||
| public GrafanaBuilder WithDataSource(string datasourceFilePath) | ||||||
| { | ||||||
| return WithBindMount(datasourceFilePath, "/etc/grafana/provisioning/datasources/"); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Mounts a dashboard configuration file. | ||||||
| /// </summary> | ||||||
| /// <param name="dashboardFilePath">The path to the dashboard configuration file.</param> | ||||||
| /// <returns>A configured instance of <see cref="GrafanaBuilder" />.</returns> | ||||||
| public GrafanaBuilder WithDashboard(string dashboardFilePath) | ||||||
| { | ||||||
| return WithBindMount(dashboardFilePath, "/etc/grafana/provisioning/dashboards/"); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Mounts a plugin configuration file. | ||||||
| /// </summary> | ||||||
| /// <param name="pluginFilePath">The path to the plugin configuration file.</param> | ||||||
| /// <returns>A configured instance of <see cref="GrafanaBuilder" />.</returns> | ||||||
| public GrafanaBuilder WithPlugin(string pluginFilePath) | ||||||
| { | ||||||
| return WithBindMount(pluginFilePath, "/etc/grafana/provisioning/plugins/"); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Mounts a notifier configuration file. | ||||||
| /// </summary> | ||||||
| /// <param name="notifierFilePath">The path to the notifier configuration file.</param> | ||||||
| /// <returns>A configured instance of <see cref="GrafanaBuilder" />.</returns> | ||||||
| public GrafanaBuilder WithNotifier(string notifierFilePath) | ||||||
| { | ||||||
| return WithBindMount(notifierFilePath, "/etc/grafana/provisioning/notifiers/"); | ||||||
| } | ||||||
|
Comment on lines
+78
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These builder APIs haven't been tested. We typically add builder APIs only for configurations that are essential to run an instance or required for specific use cases. |
||||||
|
|
||||||
| /// <inheritdoc /> | ||||||
| public override GrafanaContainer Build() | ||||||
| { | ||||||
| Validate(); | ||||||
| return new GrafanaContainer(DockerResourceConfiguration); | ||||||
| } | ||||||
|
|
||||||
| /// <inheritdoc /> | ||||||
| protected override GrafanaBuilder Init() | ||||||
| { | ||||||
| return base.Init() | ||||||
| .WithImage(GrafanaImage) | ||||||
| .WithPortBinding(GrafanaPort, true) | ||||||
| .WithUsername(DefaultUsername) | ||||||
| .WithPassword(DefaultPassword) | ||||||
| .WithEnvironment("GF_AUTH_ANONYMOUS_ENABLED", "false") | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| .WithEnvironment("GF_AUTH_BASIC_ENABLED", "true") | ||||||
| .WithWaitStrategy(Wait.ForUnixContainer() | ||||||
| .UntilHttpRequestIsSucceeded(request => request | ||||||
| .ForPort(GrafanaPort) | ||||||
| .ForPath("/api/health") | ||||||
| .ForStatusCode(HttpStatusCode.OK))); | ||||||
| } | ||||||
|
|
||||||
| /// <inheritdoc /> | ||||||
| protected override GrafanaBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||||||
| { | ||||||
| return Merge(DockerResourceConfiguration, new GrafanaConfiguration(resourceConfiguration)); | ||||||
| } | ||||||
|
|
||||||
| /// <inheritdoc /> | ||||||
| protected override GrafanaBuilder Clone(IContainerConfiguration resourceConfiguration) | ||||||
| { | ||||||
| return Merge(DockerResourceConfiguration, new GrafanaConfiguration(resourceConfiguration)); | ||||||
| } | ||||||
|
|
||||||
| /// <inheritdoc /> | ||||||
| protected override GrafanaBuilder Merge(GrafanaConfiguration oldValue, GrafanaConfiguration newValue) | ||||||
| { | ||||||
| return new GrafanaBuilder(new GrafanaConfiguration(oldValue, newValue)); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| namespace Testcontainers.Grafana; | ||
|
|
||
| /// <inheritdoc cref="ContainerConfiguration" /> | ||
| [PublicAPI] | ||
| public sealed class GrafanaConfiguration : ContainerConfiguration | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="username">The Grafana username.</param> | ||
| /// <param name="password">The Grafana password.</param> | ||
| public GrafanaConfiguration( | ||
| string username = null, | ||
| string password = null) | ||
| { | ||
| Username = username; | ||
| Password = password; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public GrafanaConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public GrafanaConfiguration(IContainerConfiguration resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public GrafanaConfiguration(GrafanaConfiguration resourceConfiguration) | ||
| : this(new GrafanaConfiguration(), resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="oldValue">The old Docker resource configuration.</param> | ||
| /// <param name="newValue">The new Docker resource configuration.</param> | ||
| public GrafanaConfiguration(GrafanaConfiguration oldValue, GrafanaConfiguration newValue) | ||
| : base(oldValue, newValue) | ||
| { | ||
| Username = newValue.Username ?? oldValue.Username; | ||
| Password = newValue.Password ?? oldValue.Password; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the Grafana username. | ||
| /// </summary> | ||
| public string Username { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the Grafana password. | ||
| /// </summary> | ||
| public string Password { get; } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| namespace Testcontainers.Grafana; | ||
|
|
||
| /// <inheritdoc cref="DockerContainer" /> | ||
| [PublicAPI] | ||
| public sealed class GrafanaContainer : DockerContainer | ||
| { | ||
| private readonly GrafanaConfiguration _configuration; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="GrafanaContainer" /> class. | ||
| /// </summary> | ||
| /// <param name="configuration">The container configuration.</param> | ||
| public GrafanaContainer(GrafanaConfiguration configuration) | ||
| : base(configuration) | ||
| { | ||
| _configuration = configuration; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the Grafana HTTP endpoint. | ||
| /// </summary> | ||
| /// <returns>The Grafana HTTP endpoint.</returns> | ||
| public string GetHttpEndpoint() | ||
| { | ||
| return new UriBuilder(Uri.UriSchemeHttp, Hostname, GetMappedPublicPort(GrafanaBuilder.GrafanaPort)).ToString(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the Grafana connection string. | ||
| /// </summary> | ||
| /// <returns>The Grafana connection string.</returns> | ||
| public string GetConnectionString() | ||
| { | ||
| var endpoint = GetHttpEndpoint(); | ||
| var username = _configuration.Username ?? GrafanaBuilder.DefaultUsername; | ||
| var password = _configuration.Password ?? GrafanaBuilder.DefaultPassword; | ||
| return new UriBuilder(endpoint) | ||
| { | ||
| UserName = username, | ||
| Password = password | ||
| }.ToString(); | ||
| } | ||
|
Comment on lines
+32
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose of this? The tests only use the HTTP header for authentication. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFrameworks>net8.0;net9.0;netstandard2.0;netstandard2.1</TargetFrameworks> | ||
| <LangVersion>latest</LangVersion> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="JetBrains.Annotations" VersionOverride="2023.3.0" PrivateAssets="All"/> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="../Testcontainers/Testcontainers.csproj"/> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| global using System; | ||
| global using System.Net; | ||
| global using Docker.DotNet.Models; | ||
| global using DotNet.Testcontainers; | ||
| global using DotNet.Testcontainers.Builders; | ||
| global using DotNet.Testcontainers.Configurations; | ||
| global using DotNet.Testcontainers.Containers; | ||
| global using JetBrains.Annotations; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ubuntu-24.04 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mounting a share isn't aligned with our best practices. It can make the module incompatible with remote container runtimes. Instead, we recommend using the
WithResourceMapping(FileInfo, FileInfo)API, which copies the files into the container.