Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion DockerComposeFixture/Compose/DockerCompose.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ namespace DockerComposeFixture.Compose
{
public class DockerCompose : IDockerCompose
{
public DockerCompose(ILogger[] logger)
private readonly IEnumerable<KeyValuePair<string, object>> environmentVariables;

public DockerCompose(ILogger[] logger, IEnumerable<KeyValuePair<string, object>> environmentVariables)
Copy link

@ghost ghost Jul 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this an IEnumerable<KeyValuePair<,>> instead of a IDictionary<,>? If it is so that the order of the env vars can be preserved then does the order matter?

{
this.environmentVariables = environmentVariables;
this.Logger = logger;
}
public DockerCompose(ILogger[] logger) : this(logger, Enumerable.Empty<KeyValuePair<string, object>>()) {}

private string dockerComposeArgs,dockerComposeUpArgs, dockerComposeDownArgs;

public void Init(string dockerComposeArgs, string dockerComposeUpArgs, string dockerComposeDownArgs)
Expand All @@ -33,6 +38,11 @@ public Task Up()

private void RunProcess(ProcessStartInfo processStartInfo)
{
foreach (var e in environmentVariables)
{
processStartInfo.EnvironmentVariables[e.Key] = e.Value.ToString();
}

var runner = new ProcessRunner(processStartInfo);
foreach (var logger in this.Logger)
{
Expand Down
33 changes: 16 additions & 17 deletions DockerComposeFixture/DockerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public DockerFixture(IMessageSink output)
/// If you call this multiple times on the same DockerFixture then it will be ignored.
/// </summary>
/// <param name="setupOptions">Options that control how docker-compose is executed.</param>
public void InitOnce(Func<IDockerFixtureOptions> setupOptions)
public async Task InitOnceAsync(Func<IDockerFixtureOptions> setupOptions)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst I have no problem with this being async, just remember that as a fixture people will be calling this from a constructor (which are inherently synchronous). So they will have to do a .GetAwaiter().GetResult(). However if you are firing off multiple async Tasks then this would still make sense.

{
InitOnce(setupOptions, null);
await InitOnceAsync(setupOptions, null);
}

/// <summary>
Expand All @@ -43,11 +43,11 @@ public void InitOnce(Func<IDockerFixtureOptions> setupOptions)
/// </summary>
/// <param name="setupOptions">Options that control how docker-compose is executed.</param>
/// <param name="dockerCompose"></param>
public void InitOnce(Func<IDockerFixtureOptions> setupOptions, IDockerCompose dockerCompose)
public async Task InitOnceAsync(Func<IDockerFixtureOptions> setupOptions, IDockerCompose dockerCompose)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

{
if (!this.initialised)
{
this.Init(setupOptions, dockerCompose);
await this.InitAsync(setupOptions, dockerCompose);
this.initialised = true;
}
}
Expand All @@ -57,26 +57,26 @@ public void InitOnce(Func<IDockerFixtureOptions> setupOptions, IDockerCompose do
/// Initialize docker compose services from file(s).
/// </summary>
/// <param name="setupOptions">Options that control how docker-compose is executed</param>
public void Init(Func<IDockerFixtureOptions> setupOptions)
public async Task InitAsync(Func<IDockerFixtureOptions> setupOptions)
{
Init(setupOptions, null);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

await InitAsync(setupOptions, null);
}

/// <summary>
/// Initialize docker compose services from file(s).
/// </summary>
/// <param name="setupOptions">Options that control how docker-compose is executed</param>
/// <param name="compose"></param>
public void Init(Func<IDockerFixtureOptions> setupOptions, IDockerCompose compose)
public async Task InitAsync(Func<IDockerFixtureOptions> setupOptions, IDockerCompose compose)
{
var options = setupOptions();
options.Validate();
string logFile = options.DebugLog
? Path.Combine(Path.GetTempPath(), $"docker-compose-{DateTime.Now.Ticks}.log")
: null;

this.Init(options.DockerComposeFiles, options.DockerComposeUpArgs, options.DockerComposeDownArgs,
options.StartupTimeoutSecs, options.CustomUpTest, compose, this.GetLoggers(logFile).ToArray());
await this.InitAsync(options.DockerComposeFiles, options.DockerComposeUpArgs, options.DockerComposeDownArgs,
options.StartupTimeoutSecs, options.CustomUpTest, compose, this.GetLoggers(logFile).ToArray(), options.EnvironmentVariables);
}

private IEnumerable<ILogger> GetLoggers(string file)
Expand All @@ -103,14 +103,15 @@ private IEnumerable<ILogger> GetLoggers(string file)
/// <param name="customUpTest">Checks whether the docker-compose services have come up correctly based upon the output of docker-compose</param>
/// <param name="dockerCompose"></param>
/// <param name="logger"></param>
public void Init(string[] dockerComposeFiles, string dockerComposeUpArgs, string dockerComposeDownArgs,
/// <param name="environmentVariables"></param>
public async Task InitAsync(string[] dockerComposeFiles, string dockerComposeUpArgs, string dockerComposeDownArgs,
int startupTimeoutSecs, Func<string[], bool> customUpTest = null,
IDockerCompose dockerCompose = null, ILogger[] logger = null)
IDockerCompose dockerCompose = null, ILogger[] logger = null, IEnumerable<KeyValuePair<string, object>> environmentVariables = null)
{
this.loggers = logger ?? GetLoggers(null).ToArray();

var dockerComposeFilePaths = dockerComposeFiles.Select(this.GetComposeFilePath);
this.dockerCompose = dockerCompose ?? new DockerCompose(this.loggers);
this.dockerCompose = dockerCompose ?? new DockerCompose(this.loggers, environmentVariables);
this.customUpTest = customUpTest;
this.startupTimeoutSecs = startupTimeoutSecs;

Expand All @@ -119,7 +120,7 @@ public void Init(string[] dockerComposeFiles, string dockerComposeUpArgs, string
dockerComposeFilePaths
.Select(f => $"-f \"{f}\""))
.Trim(), dockerComposeUpArgs, dockerComposeDownArgs);
this.Start();
await this.StartAsync();
}

private string GetComposeFilePath(string file)
Expand Down Expand Up @@ -195,7 +196,7 @@ public virtual void Dispose()
this.Stop();
}

private void Start()
private async Task StartAsync()
{
if (this.CheckIfRunning().hasContainers)
{
Expand All @@ -214,7 +215,7 @@ private void Start()
break;
}
this.loggers.Log($"---- checking docker services ({i + 1}/{this.startupTimeoutSecs}) ----");
Thread.Sleep(this.dockerCompose.PauseMs);
await Task.Delay(1000);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserve the use of PauseMs

if (this.customUpTest != null)
{
if (this.customUpTest(this.loggers.GetLoggedLines()))
Expand Down Expand Up @@ -252,7 +253,5 @@ private void Stop()
{
this.dockerCompose.Down();
}


}
}
2 changes: 2 additions & 0 deletions DockerComposeFixture/DockerFixtureOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class DockerFixtureOptions : IDockerFixtureOptions
/// Default is 'docker-compose -f file.yml down --remove-orphans' you can add '--rmi all' if you want to guarantee a fresh build on each test
/// </summary>
public string DockerComposeDownArgs { get; set; } = "--remove-orphans";

public IEnumerable<KeyValuePair<string, object>> EnvironmentVariables { get; set; }

/// <summary>
/// How many seconds to wait for the application to start before giving up. (Default is 120.)
Expand Down
2 changes: 2 additions & 0 deletions DockerComposeFixture/IDockerFixtureOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface IDockerFixtureOptions
string DockerComposeUpArgs { get; set; }
string DockerComposeDownArgs { get; set; }
int StartupTimeoutSecs { get; set; }

IEnumerable<KeyValuePair<string, object>> EnvironmentVariables { get; set; }

void Validate();

Expand Down