Skip to content

Commit e2391c2

Browse files
committed
refactor GetImageFromDockerfile helper
1 parent 44e7ddd commit e2391c2

File tree

21 files changed

+74
-38
lines changed

21 files changed

+74
-38
lines changed

src/Testcontainers.KurrentDb/KurrentDbBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public KurrentDbBuilder()
2222
/// </summary>
2323
/// <param name="image">Docker image tag. Available tags can be found here: <see href="https://hub.docker.com/r/kurrentplatform/kurrentdb/tags">https://hub.docker.com/r/kurrentplatform/kurrentdb/tags</see>.</param>
2424
public KurrentDbBuilder(string image)
25-
: this(new KurrentDbConfiguration())ed
25+
: this(new KurrentDbConfiguration())
2626
{
2727
DockerResourceConfiguration = Init().WithImage(image).DockerResourceConfiguration;
2828
}

tests/Testcontainers.CockroachDb.Tests/CockroachDbContainerTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public async Task ExecScriptReturnsSuccessful()
3535
public class CockroachDbDefaultFixture(IMessageSink messageSink)
3636
: DbContainerFixture<CockroachDbBuilder, CockroachDbContainer>(messageSink)
3737
{
38+
protected override CockroachDbBuilder Configure(CockroachDbBuilder builder)
39+
{
40+
return builder.WithImage(TestSession.GetImageFromDockerfile());
41+
}
42+
3843
public override DbProviderFactory DbProviderFactory
3944
=> NpgsqlFactory.Instance;
4045
}

tests/Testcontainers.Commons/TestSession.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,40 @@ static TestSession()
1212

1313
public static IImage GetImageFromDockerfile(
1414
string relativePath = "Dockerfile",
15-
int lineIndex = 0)
15+
string stage = "")
1616
{
1717
var fullpath = Path.GetFullPath(relativePath);
1818
if (!File.Exists(fullpath)) throw new Exception($"Dockerfile not found at '{fullpath}'.");
1919
var lines = File.ReadAllLines(fullpath);
20-
if (lines.Length == 0 || lines.Length <= lineIndex) throw new Exception($"Dockerfile located at '{fullpath}' is empty or shorter than {lineIndex + 1} line(s).");
21-
var imageLine = lines[lineIndex];
22-
var imageLineSplit = imageLine.Split(" ");
23-
if (imageLineSplit.Length < 2) throw new Exception($"Dockerfile located at '{fullpath}' has invalid image tag at line {lineIndex + 1}. The line should start with 'FROM' instruction, followed by space and image tag. For example: 'FROM postgres:17'.");
24-
var imageTag = imageLineSplit[1];
25-
var image = new DockerImage(imageTag);
26-
return image;
20+
if (lines.Length == 0) throw new Exception($"Dockerfile located at '{fullpath}' is empty.");
21+
if (!string.IsNullOrEmpty(stage)) return FindStage(lines, stage);
22+
else return FindFirstTag(lines);
23+
24+
DockerImage FindFirstTag(IEnumerable<string> lines)
25+
{
26+
foreach (var line in lines)
27+
{
28+
if (string.IsNullOrEmpty(line)) continue;
29+
var split = line.Split(" ", StringSplitOptions.RemoveEmptyEntries);
30+
if (split.Length < 2) continue;
31+
var imageTag = split[1];
32+
return new DockerImage(imageTag);
33+
}
34+
throw new Exception($"Failed to find any image tag in Dockerfile located at '{fullpath}'.");
35+
}
36+
37+
DockerImage FindStage(IEnumerable<string> lines, string stage)
38+
{
39+
foreach (var line in lines)
40+
{
41+
if (string.IsNullOrEmpty(line)) continue;
42+
if (!line.Trim().ToLowerInvariant().EndsWith($"as {stage}")) continue;
43+
var split = line.Split(" ", StringSplitOptions.RemoveEmptyEntries);
44+
if (split.Length < 2) continue;
45+
var imageTag = split[1];
46+
return new DockerImage(imageTag);
47+
}
48+
throw new Exception($"Failed to find image with stage {stage} in Dockerfile located at '{fullpath}'.");
49+
}
2750
}
2851
}

tests/Testcontainers.Commons/Usings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
global using System;
2+
global using System.Collections.Generic;
23
global using System.Diagnostics;
34
global using System.IO;
45
global using System.Text;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
FROM jacobalberty/firebird:v4.0
2-
#FROM firebirdsql/firebird:5
2+
FROM jacobalberty/firebird:2.5-sc AS fb2.5-sc
3+
FROM jacobalberty/firebird:2.5-ss AS fb2.5-ss
4+
FROM jacobalberty/firebird:v3.0 AS fb3.0

tests/Testcontainers.FirebirdSql.Tests/FirebirdSqlContainerTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,23 @@ public class FirebirdSql25ScFixture(IMessageSink messageSink)
5757
: FirebirdSqlDefaultFixture(messageSink)
5858
{
5959
protected override FirebirdSqlBuilder Configure(FirebirdSqlBuilder builder)
60-
=> builder.WithImage("jacobalberty/firebird:2.5-sc");
60+
=> builder.WithImage(TestSession.GetImageFromDockerfile(stage: "fb2.5-sc"));
6161
}
6262

6363
[UsedImplicitly]
6464
public class FirebirdSql25SsFixture(IMessageSink messageSink)
6565
: FirebirdSqlDefaultFixture(messageSink)
6666
{
6767
protected override FirebirdSqlBuilder Configure(FirebirdSqlBuilder builder)
68-
=> builder.WithImage("jacobalberty/firebird:2.5-ss");
68+
=> builder.WithImage(TestSession.GetImageFromDockerfile(stage: "fb2.5-ss"));
6969
}
7070

7171
[UsedImplicitly]
7272
public class FirebirdSql30Fixture(IMessageSink messageSink)
7373
: FirebirdSqlDefaultFixture(messageSink)
7474
{
7575
protected override FirebirdSqlBuilder Configure(FirebirdSqlBuilder builder)
76-
=> builder.WithImage("jacobalberty/firebird:v3.0"); // firebirdsql/firebird:3
76+
=> builder.WithImage(TestSession.GetImageFromDockerfile(stage: "fb3.0"));
7777
}
7878

7979
[UsedImplicitly]
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
FROM confluentinc/cp-kafka:7.5.9
1+
FROM confluentinc/cp-kafka:7.5.9
2+
FROM apache/kafka:3.9.1 AS kafka3.9.1
3+
FROM apache/kafka-native:3.9.1 AS kafka-native3.9.1

tests/Testcontainers.Kafka.Tests/KafkaBuilderTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ public sealed class KafkaBuilderTests
66
public void KRaftWithConfluentPre7ThrowsArgumentException()
77
{
88
const string message = "KRaft is not supported for Confluent Platform images with versions earlier than 7.0.0.";
9-
ExpectArgEx(message, () => new KafkaBuilder().WithImage("confluentinc/cp-kafka:6.1.9").WithKRaft().Build());
9+
ExpectArgEx(message, () => new KafkaBuilder("confluentinc/cp-kafka:6.1.9").WithKRaft().Build());
1010
}
1111

1212
[Fact]
1313
public void ZooKeeperWithApacheKafkaImageThrowsArgumentException()
1414
{
1515
const string message = "Local ZooKeeper is not supported for Apache Kafka images. Configure an external ZooKeeper.";
16-
ExpectArgEx(message, () => new KafkaBuilder().WithImage("apache/kafka:3.9.1").WithZooKeeper().Build());
16+
ExpectArgEx(message, () => new KafkaBuilder("apache/kafka:3.9.1").WithZooKeeper().Build());
1717
}
1818

1919
private static void ExpectArgEx(string message, Action testCode)

tests/Testcontainers.Kafka.Tests/KafkaContainerTest.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ public KafkaZooKeeperConfiguration()
9898
public sealed class ApacheKafkaConfiguration : KafkaContainerTest
9999
{
100100
public ApacheKafkaConfiguration()
101-
: base(new KafkaBuilder(TestSession.GetImageFromDockerfile())
102-
.WithImage("apache/kafka:3.9.1")
101+
: base(new KafkaBuilder(TestSession.GetImageFromDockerfile(stage: "kafka3.9.1"))
103102
.Build())
104103
{
105104
}
@@ -109,8 +108,7 @@ public ApacheKafkaConfiguration()
109108
public sealed class ApacheKafkaNativeConfiguration : KafkaContainerTest
110109
{
111110
public ApacheKafkaNativeConfiguration()
112-
: base(new KafkaBuilder(TestSession.GetImageFromDockerfile())
113-
.WithImage("apache/kafka-native:3.9.1")
111+
: base(new KafkaBuilder(TestSession.GetImageFromDockerfile(stage: "kafka-native3.9.1"))
114112
.Build())
115113
{
116114
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
FROM quay.io/keycloak/keycloak:26.4
1+
FROM quay.io/keycloak/keycloak:26.4
2+
FROM quay.io/keycloak/keycloak:25.0 AS keycloak25.0
3+
FROM quay.io/keycloak/keycloak:26.0 AS keycloak26.0

0 commit comments

Comments
 (0)