Skip to content

Commit fcbe81b

Browse files
committed
Added entrypoint and workdir, closes #22, #23.
1 parent a2c6aee commit fcbe81b

20 files changed

+205
-180
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ Choose from existing pre-configured configurations [^1] and start containers wit
99

1010
## Supported commands
1111
- `WithImage` specifies an `IMAGE[:TAG]` to derive the container from.
12+
- `WithName` set the container name e. g. `--name nginx`.
13+
- `WithWorkingDirectory` specifies and overrides the `WORKDIR` for the instruction sets.
14+
- `WithEntrypoint` specifies and overrides the `ENTRYPOINT` that will run as an executable.
1215
- `WithCommand` specifies and overrides the `[COMMAND]` instruction provided from the Dockerfile.
1316
- `WithEnvironment` set an environment variable in the container e. g. `-e "test=containers"`.
1417
- `WithLabel` applies metadata to a container e. g. `-l, --label dotnet.testcontainers=awesome`.
1518
- `WithExposedPort` exposes a port inside the container e. g. `--expose=80`.
1619
- `WithPortBinding` publishes a container port to the host e. g. `-p 80:80`.
1720
- `WithMount` mounts a volume into the container e. g. `-v, --volume .:/tmp`.
18-
- `WithName` set the container name e. g. `--name nginx`.
1921

2022
## Examples
2123
Pulls `nginx`, creates a new container with port binding `80:80` and hits the default site.

src/DotNet.Testcontainers/Clients/TestcontainersClient.cs

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,16 @@
11
namespace DotNet.Testcontainers.Clients
22
{
33
using System;
4-
using System.Collections.Generic;
54
using System.Threading.Tasks;
65
using Docker.DotNet.Models;
76
using DotNet.Testcontainers.Core.Mapper;
8-
using DotNet.Testcontainers.Core.Mapper.Converters;
97
using DotNet.Testcontainers.Core.Models;
108
using DotNet.Testcontainers.Diagnostics;
119

1210
internal class TestcontainersClient : DockerApiClient, ITestcontainersClient
1311
{
1412
private static readonly Lazy<ITestcontainersClient> Testcontainers = new Lazy<ITestcontainersClient>(() => new TestcontainersClient());
1513

16-
private static readonly ConverterFactory ConverterFactory = new ConverterFactory();
17-
18-
private static readonly GenericConverter GenericConverter = new GenericConverter(ConverterFactory);
19-
20-
static TestcontainersClient()
21-
{
22-
ConverterFactory.Register<IReadOnlyCollection<string>, IList<string>>(
23-
() => new ConvertList());
24-
25-
ConverterFactory.Register<IReadOnlyDictionary<string, string>, IList<string>>(
26-
() => new ConvertList());
27-
28-
ConverterFactory.Register<IReadOnlyDictionary<string, string>, IDictionary<string, string>>(
29-
() => new ConvertDictionary());
30-
31-
ConverterFactory.Register<IReadOnlyDictionary<string, string>, IDictionary<string, EmptyStruct>>(
32-
() => new ConvertExposedPort(), "ExposedPorts");
33-
34-
ConverterFactory.Register<IReadOnlyDictionary<string, string>, IDictionary<string, IList<PortBinding>>>(
35-
() => new ConvertPortBinding(), "PortBindings");
36-
37-
ConverterFactory.Register<IReadOnlyDictionary<string, string>, IList<Mount>>(
38-
() => new ConvertMount(), "Mounts");
39-
}
40-
4114
internal static ITestcontainersClient Instance
4215
{
4316
get
@@ -83,8 +56,6 @@ public async Task<string> RunAsync(TestcontainersConfiguration configuration)
8356
{
8457
var image = configuration.Container.Image;
8558

86-
var name = configuration.Container.Name;
87-
8859
var pullImageTask = MetaDataClientImages.Instance.ExistsWithNameAsync(image).ContinueWith(async imageExists =>
8960
{
9061
if (!await imageExists)
@@ -94,23 +65,25 @@ public async Task<string> RunAsync(TestcontainersConfiguration configuration)
9465
}
9566
});
9667

97-
var cmd = GenericConverter.Convert<IReadOnlyCollection<string>,
98-
IList<string>>(configuration.Container.Command);
68+
var name = configuration.Container.Name;
69+
70+
var workingDir = configuration.Container.WorkingDirectory;
71+
72+
var converter = new TestcontainersConfigurationConverter(configuration);
73+
74+
var entrypoint = converter.Entrypoint;
75+
76+
var cmd = converter.Command;
9977

100-
var env = GenericConverter.Convert<IReadOnlyDictionary<string, string>,
101-
IList<string>>(configuration.Container.Environments);
78+
var env = converter.Environments;
10279

103-
var labels = GenericConverter.Convert<IReadOnlyDictionary<string, string>,
104-
IDictionary<string, string>>(configuration.Container.Labels);
80+
var labels = converter.Labels;
10581

106-
var exposedPorts = GenericConverter.Convert<IReadOnlyDictionary<string, string>,
107-
IDictionary<string, EmptyStruct>>(configuration.Container.ExposedPorts, "ExposedPorts");
82+
var exposedPorts = converter.ExposedPorts;
10883

109-
var portBindings = GenericConverter.Convert<IReadOnlyDictionary<string, string>,
110-
IDictionary<string, IList<PortBinding>>>(configuration.Host.PortBindings, "PortBindings");
84+
var portBindings = converter.PortBindings;
11185

112-
var mounts = GenericConverter.Convert<IReadOnlyDictionary<string, string>,
113-
IList<Mount>>(configuration.Host.Mounts, "Mounts");
86+
var mounts = converter.Mounts;
11487

11588
await pullImageTask;
11689

@@ -124,6 +97,8 @@ public async Task<string> RunAsync(TestcontainersConfiguration configuration)
12497
{
12598
Image = image,
12699
Name = name,
100+
WorkingDir = workingDir,
101+
Entrypoint = entrypoint,
127102
Env = env,
128103
Labels = labels,
129104
Cmd = cmd,

src/DotNet.Testcontainers/Core/Builder/ITestcontainersBuilder.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,21 @@ public interface ITestcontainersBuilder
2727
ITestcontainersBuilder WithName(string name);
2828

2929
/// <summary>
30-
/// Adds a command to the Testcontainer to provide defaults for an executing.
30+
/// Overrides the working directory of the Testcontainer for the instruction sets.
31+
/// </summary>
32+
/// <param name="workingDirectory">Working directory.</param>
33+
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
34+
ITestcontainersBuilder WithWorkingDirectory(string workingDirectory);
35+
36+
/// <summary>
37+
/// Overrides the entrypoint of the Testcontainer to configure an executable.
38+
/// </summary>
39+
/// <param name="entrypoint">Entrypoint executable.</param>
40+
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>
41+
ITestcontainersBuilder WithEntrypoint(params string[] entrypoint);
42+
43+
/// <summary>
44+
/// Overrides the command of the Testcontainer to provide defaults for an executing.
3145
/// </summary>
3246
/// <param name="command">List of commands, "executable", "param1", "param2" or "param1", "param2".</param>
3347
/// <returns>A configured instance of <see cref="ITestcontainersBuilder"/>.</returns>

src/DotNet.Testcontainers/Core/Builder/TestcontainersBuilder.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public class TestcontainersBuilder : ITestcontainersBuilder
1414

1515
private readonly string name;
1616

17+
private readonly string workingDirectory;
18+
19+
private readonly IReadOnlyCollection<string> entrypoint = new List<string>();
20+
1721
private readonly IReadOnlyCollection<string> command = new List<string>();
1822

1923
private readonly IReadOnlyDictionary<string, string> environments = new Dictionary<string, string>();
@@ -37,6 +41,8 @@ public TestcontainersBuilder()
3741
protected TestcontainersBuilder(
3842
IDockerImage image,
3943
string name,
44+
string workingDirectory,
45+
IReadOnlyCollection<string> entrypoint,
4046
IReadOnlyCollection<string> commands,
4147
IReadOnlyDictionary<string, string> environments,
4248
IReadOnlyDictionary<string, string> labels,
@@ -47,6 +53,8 @@ protected TestcontainersBuilder(
4753
{
4854
this.image = image;
4955
this.name = name;
56+
this.workingDirectory = workingDirectory;
57+
this.entrypoint = entrypoint;
5058
this.command = commands;
5159
this.environments = environments;
5260
this.labels = labels;
@@ -73,6 +81,16 @@ public ITestcontainersBuilder WithName(string name)
7381
return Build(this, name: name);
7482
}
7583

84+
public ITestcontainersBuilder WithWorkingDirectory(string workingDirectory)
85+
{
86+
return Build(this, workingDirectory: workingDirectory);
87+
}
88+
89+
public ITestcontainersBuilder WithEntrypoint(params string[] entrypoint)
90+
{
91+
return Build(this, entrypoint: entrypoint);
92+
}
93+
7694
public ITestcontainersBuilder WithCommand(params string[] command)
7795
{
7896
return Build(this, command: new ReadOnlyCollection<string>(command));
@@ -133,6 +151,8 @@ public IDockerContainer Build()
133151
var configuration = default(TestcontainersConfiguration);
134152
configuration.Container.Image = this.image.Image;
135153
configuration.Container.Name = this.name;
154+
configuration.Container.WorkingDirectory = this.workingDirectory;
155+
configuration.Container.Entrypoint = this.entrypoint;
136156
configuration.Container.Command = this.command;
137157
configuration.Container.Environments = this.environments;
138158
configuration.Container.ExposedPorts = this.exposedPorts;
@@ -149,6 +169,8 @@ private static ITestcontainersBuilder Build(
149169
TestcontainersBuilder old,
150170
IDockerImage image = null,
151171
string name = null,
172+
string workingDirectory = null,
173+
IReadOnlyCollection<string> entrypoint = null,
152174
IReadOnlyCollection<string> command = null,
153175
IReadOnlyDictionary<string, string> environments = null,
154176
IReadOnlyDictionary<string, string> exposedPorts = null,
@@ -161,12 +183,15 @@ private static ITestcontainersBuilder Build(
161183
Merge(old.exposedPorts, ref exposedPorts);
162184
Merge(old.labels, ref labels);
163185
Merge(old.portBindings, ref portBindings);
186+
Merge(old.entrypoint, ref entrypoint);
164187
Merge(old.command, ref command);
165188
Merge(old.mounts, ref mounts);
166189

167190
return new TestcontainersBuilder(
168191
image ?? old.image,
169192
name ?? old.name,
193+
workingDirectory ?? old.workingDirectory,
194+
entrypoint,
170195
command,
171196
environments,
172197
labels,

src/DotNet.Testcontainers/Core/Containers/IDockerContainer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ public interface IDockerContainer : IDisposable
2424
/// <summary>
2525
/// Starts the Testcontainer. If the image does not exist, it will be downloaded automatically. Non-existing containers are created at first start.
2626
/// </summary>
27+
/// <returns>A task that represents the asynchronous start operation of a Testcontainer.</returns>
2728
Task StartAsync();
2829

2930
/// <summary>
3031
/// Stops the Testcontainer and removes the container automatically.
3132
/// </summary>
33+
/// <returns>A task that represents the asynchronous stop operation of a Testcontainer.</returns>
3234
Task StopAsync();
3335
}
3436
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace DotNet.Testcontainers.Core.Mapper
2+
{
3+
internal abstract class BaseConverter<TSource, TTarget>
4+
{
5+
private readonly string name;
6+
7+
protected BaseConverter(string name)
8+
{
9+
this.name = name;
10+
}
11+
12+
public abstract TTarget Convert(TSource source);
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace DotNet.Testcontainers.Core.Mapper
2+
{
3+
using System.Collections.Generic;
4+
5+
internal abstract class CollectionConverter<T> : BaseConverter<IReadOnlyCollection<string>, T>
6+
{
7+
protected CollectionConverter() : base(string.Empty)
8+
{
9+
}
10+
11+
protected CollectionConverter(string name) : base(name)
12+
{
13+
}
14+
}
15+
}

src/DotNet.Testcontainers/Core/Mapper/ConvertDictionary.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/DotNet.Testcontainers/Core/Mapper/ConvertExposedPort.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/DotNet.Testcontainers/Core/Mapper/ConvertList.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)