Skip to content

Commit 46c05d7

Browse files
authored
refactor: Cache Docker image full and host name (#1043)
1 parent ceb9b05 commit 46c05d7

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

docs/modules/index.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ await moduleNameContainer.StartAsync();
2525
| Azure SQL Edge | `mcr.microsoft.com/azure-sql-edge:1.0.7` | [NuGet](https://www.nuget.org/packages/Testcontainers.SqlEdge) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.SqlEdge) |
2626
| Azurite | `mcr.microsoft.com/azure-storage/azurite:3.24.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.Azurite) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Azurite) |
2727
| ClickHouse | `clickhouse/clickhouse-server:23.6-alpine` | [NuGet](https://www.nuget.org/packages/Testcontainers.ClickHouse) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.ClickHouse) |
28+
| Consul | `consul:1.15` | [NuGet](https://www.nuget.org/packages/Testcontainers.Consul) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Consul) |
2829
| Couchbase | `couchbase:community-7.0.2` | [NuGet](https://www.nuget.org/packages/Testcontainers.Couchbase) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Couchbase) |
2930
| CouchDB | `couchdb:3.3` | [NuGet](https://www.nuget.org/packages/Testcontainers.CouchDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.CouchDb) |
3031
| DynamoDB | `amazon/dynamodb-local:1.21.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.DynamoDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.DynamoDb) |
3132
| Elasticsearch | `elasticsearch:8.6.1` | [NuGet](https://www.nuget.org/packages/Testcontainers.Elasticsearch) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Elasticsearch) |
3233
| EventStoreDB | `eventstore/eventstore:22.10.1-buster-slim` | [NuGet](https://www.nuget.org/packages/Testcontainers.EventStoreDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.EventStoreDb) |
34+
| FakeGcsServer | `fsouza/fake-gcs-server:1.47` | [NuGet](https://www.nuget.org/packages/Testcontainers.FakeGcsServer) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.FakeGcsServer) |
3335
| Firestore | `gcr.io/google.com/cloudsdktool/google-cloud-cli:446.0.1-emulators` | [NuGet](https://www.nuget.org/packages/Testcontainers.Firestore) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Firestore) |
3436
| InfluxDB | `influxdb:2.7` | [NuGet](https://www.nuget.org/packages/Testcontainers.InfluxDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.InfluxDb) |
3537
| K3s | `rancher/k3s:v1.26.2-k3s1` | [NuGet](https://www.nuget.org/packages/Testcontainers.K3s) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.K3s) |
@@ -41,10 +43,11 @@ await moduleNameContainer.StartAsync();
4143
| MinIO | `minio/minio:RELEASE.2023-01-31T02-24-19Z` | [NuGet](https://www.nuget.org/packages/Testcontainers.Minio) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Minio) |
4244
| MongoDB | `mongo:6.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.MongoDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.MongoDb) |
4345
| MySQL | `mysql:8.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.MySql) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.MySql) |
44-
| NATS | `nats:2.9` | [NuGet](https://www.nuget.org/packages/Testcontainers.Nats) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Nats) |
46+
| NATS | `nats:2.9` | [NuGet](https://www.nuget.org/packages/Testcontainers.Nats) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Nats) |
4547
| Neo4j | `neo4j:5.4` | [NuGet](https://www.nuget.org/packages/Testcontainers.Neo4j) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Neo4j) |
4648
| Oracle | `gvenzl/oracle-xe:21.3.0-slim-faststart` | [NuGet](https://www.nuget.org/packages/Testcontainers.Oracle) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Oracle) |
4749
| PostgreSQL | `postgres:15.1` | [NuGet](https://www.nuget.org/packages/Testcontainers.PostgreSql) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.PostgreSql) |
50+
| PubSub | `gcr.io/google.com/cloudsdktool/google-cloud-cli:446.0.1-emulators` | [NuGet](https://www.nuget.org/packages/Testcontainers.PubSub) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.PubSub) |
4851
| RabbitMQ | `rabbitmq:3.11` | [NuGet](https://www.nuget.org/packages/Testcontainers.RabbitMq) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.RabbitMq) |
4952
| RavenDB | `ravendb/ravendb:5.4-ubuntu-latest` | [NuGet](https://www.nuget.org/packages/Testcontainers.RavenDb) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.RavenDb) |
5053
| Redis | `redis:7.0` | [NuGet](https://www.nuget.org/packages/Testcontainers.Redis) | [Source](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Testcontainers.Redis) |

src/Testcontainers/Images/DockerImage.cs

+45-19
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ namespace DotNet.Testcontainers.Images
88
[PublicAPI]
99
public sealed class DockerImage : IImage
1010
{
11+
private const string LatestTag = "latest";
12+
1113
private static readonly Func<string, IImage> GetDockerImage = MatchImage.Match;
1214

15+
private static readonly char[] TrimChars = { ' ', ':', '/' };
16+
1317
private readonly string _hubImageNamePrefix;
1418

19+
private readonly Lazy<string> _lazyFullName;
20+
21+
private readonly Lazy<string> _lazyHostname;
22+
1523
/// <summary>
1624
/// Initializes a new instance of the <see cref="DockerImage" /> class.
1725
/// </summary>
@@ -44,7 +52,7 @@ public DockerImage(string image)
4452
public DockerImage(
4553
string repository,
4654
string name,
47-
string tag,
55+
string tag = null,
4856
string hubImageNamePrefix = null)
4957
{
5058
_ = Guard.Argument(repository, nameof(repository))
@@ -56,11 +64,38 @@ public DockerImage(
5664
.NotEmpty()
5765
.NotUppercase();
5866

59-
_hubImageNamePrefix = hubImageNamePrefix;
67+
_hubImageNamePrefix = TrimOrDefault(hubImageNamePrefix);
68+
69+
Repository = TrimOrDefault(repository, repository);
70+
Name = TrimOrDefault(name, name);
71+
Tag = TrimOrDefault(tag, LatestTag);
72+
73+
_lazyFullName = new Lazy<string>(() =>
74+
{
75+
var imageComponents = new[] { _hubImageNamePrefix, Repository, Name }
76+
.Where(imageComponent => !string.IsNullOrEmpty(imageComponent));
77+
78+
return string.Join("/", imageComponents) + ":" + Tag;
79+
});
80+
81+
_lazyHostname = new Lazy<string>(() =>
82+
{
83+
var firstSegmentOfRepository = new[] { _hubImageNamePrefix, Repository }
84+
.Where(imageComponent => !string.IsNullOrEmpty(imageComponent))
85+
.DefaultIfEmpty(string.Empty)
86+
.First()
87+
.Split('/')
88+
.First();
6089

61-
Repository = repository;
62-
Name = name;
63-
Tag = string.IsNullOrEmpty(tag) ? "latest" : tag;
90+
if (firstSegmentOfRepository.IndexOfAny(new[] { '.', ':' }) >= 0)
91+
{
92+
return firstSegmentOfRepository;
93+
}
94+
else
95+
{
96+
return null;
97+
}
98+
});
6499
}
65100

66101
/// <inheritdoc />
@@ -73,23 +108,14 @@ public DockerImage(
73108
public string Tag { get; }
74109

75110
/// <inheritdoc />
76-
public string FullName
77-
{
78-
get
79-
{
80-
var imageComponents = new[] { _hubImageNamePrefix, Repository, Name }
81-
.Where(imageComponent => !string.IsNullOrEmpty(imageComponent))
82-
.Select(imageComponent => imageComponent.Trim('/', ':'))
83-
.Where(imageComponent => !string.IsNullOrEmpty(imageComponent));
84-
return string.Join("/", imageComponents) + ":" + Tag;
85-
}
86-
}
111+
public string FullName => _lazyFullName.Value;
87112

88113
/// <inheritdoc />
89-
public string GetHostname()
114+
public string GetHostname() => _lazyHostname.Value;
115+
116+
private static string TrimOrDefault(string value, string defaultValue = default)
90117
{
91-
var firstSegmentOfRepository = (string.IsNullOrEmpty(_hubImageNamePrefix) ? Repository : _hubImageNamePrefix).Split('/')[0];
92-
return firstSegmentOfRepository.IndexOfAny(new[] { '.', ':' }) >= 0 ? firstSegmentOfRepository : null;
118+
return string.IsNullOrEmpty(value) ? defaultValue : value.Trim(TrimChars);
93119
}
94120
}
95121
}

tests/Testcontainers.Tests/Unit/Images/TestcontainersImageTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public sealed class TestcontainersImageTest
1111
public void ShouldThrowArgumentNullExceptionWhenInstantiateDockerImage()
1212
{
1313
Assert.Throws<ArgumentException>(() => new DockerImage((string)null));
14-
Assert.Throws<ArgumentException>(() => new DockerImage(null, null, null));
15-
Assert.Throws<ArgumentException>(() => new DockerImage("fedora", null, null));
14+
Assert.Throws<ArgumentException>(() => new DockerImage(null, null));
15+
Assert.Throws<ArgumentException>(() => new DockerImage("fedora", null));
1616
}
1717

1818
[Fact]

0 commit comments

Comments
 (0)