From 199e2e2ad98d35c374fc6bacaf211afa3400ffa5 Mon Sep 17 00:00:00 2001 From: "joe.miyamoto" Date: Fri, 8 May 2020 22:15:22 +0900 Subject: [PATCH 1/4] enable to pass environment variables from outside --- DockerComposeFixture/Compose/DockerCompose.cs | 12 +++++++++++- DockerComposeFixture/DockerFixture.cs | 7 ++++--- DockerComposeFixture/DockerFixtureOptions.cs | 2 ++ DockerComposeFixture/IDockerFixtureOptions.cs | 2 ++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/DockerComposeFixture/Compose/DockerCompose.cs b/DockerComposeFixture/Compose/DockerCompose.cs index c53140f..41e7b6b 100644 --- a/DockerComposeFixture/Compose/DockerCompose.cs +++ b/DockerComposeFixture/Compose/DockerCompose.cs @@ -12,10 +12,15 @@ namespace DockerComposeFixture.Compose { public class DockerCompose : IDockerCompose { - public DockerCompose(ILogger[] logger) + private readonly IEnumerable> environmentVariables; + + public DockerCompose(ILogger[] logger, IEnumerable> environmentVariables) { + this.environmentVariables = environmentVariables; this.Logger = logger; } + public DockerCompose(ILogger[] logger) : this(logger, Enumerable.Empty>()) {} + private string dockerComposeArgs,dockerComposeUpArgs, dockerComposeDownArgs; public void Init(string dockerComposeArgs, string dockerComposeUpArgs, string dockerComposeDownArgs) @@ -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) { diff --git a/DockerComposeFixture/DockerFixture.cs b/DockerComposeFixture/DockerFixture.cs index 967f07f..ef13094 100644 --- a/DockerComposeFixture/DockerFixture.cs +++ b/DockerComposeFixture/DockerFixture.cs @@ -76,7 +76,7 @@ public void Init(Func setupOptions, IDockerCompose compos : null; this.Init(options.DockerComposeFiles, options.DockerComposeUpArgs, options.DockerComposeDownArgs, - options.StartupTimeoutSecs, options.CustomUpTest, compose, this.GetLoggers(logFile).ToArray()); + options.StartupTimeoutSecs, options.CustomUpTest, compose, this.GetLoggers(logFile).ToArray(), options.EnvironmentVariables); } private IEnumerable GetLoggers(string file) @@ -103,14 +103,15 @@ private IEnumerable GetLoggers(string file) /// Checks whether the docker-compose services have come up correctly based upon the output of docker-compose /// /// + /// public void Init(string[] dockerComposeFiles, string dockerComposeUpArgs, string dockerComposeDownArgs, int startupTimeoutSecs, Func customUpTest = null, - IDockerCompose dockerCompose = null, ILogger[] logger = null) + IDockerCompose dockerCompose = null, ILogger[] logger = null, IEnumerable> 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; diff --git a/DockerComposeFixture/DockerFixtureOptions.cs b/DockerComposeFixture/DockerFixtureOptions.cs index fd57b32..6d8c8fe 100644 --- a/DockerComposeFixture/DockerFixtureOptions.cs +++ b/DockerComposeFixture/DockerFixtureOptions.cs @@ -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 /// public string DockerComposeDownArgs { get; set; } = "--remove-orphans"; + + public IEnumerable> EnvironmentVariables { get; set; } /// /// How many seconds to wait for the application to start before giving up. (Default is 120.) diff --git a/DockerComposeFixture/IDockerFixtureOptions.cs b/DockerComposeFixture/IDockerFixtureOptions.cs index 130c6d5..9e28d03 100644 --- a/DockerComposeFixture/IDockerFixtureOptions.cs +++ b/DockerComposeFixture/IDockerFixtureOptions.cs @@ -11,6 +11,8 @@ public interface IDockerFixtureOptions string DockerComposeUpArgs { get; set; } string DockerComposeDownArgs { get; set; } int StartupTimeoutSecs { get; set; } + + IEnumerable> EnvironmentVariables { get; set; } void Validate(); From 3cc75b2f9a5f3bf8368450a6f63c8df7ff6631f3 Mon Sep 17 00:00:00 2001 From: "joe.miyamoto" Date: Mon, 11 May 2020 17:53:43 +0900 Subject: [PATCH 2/4] make initialization process async --- DockerComposeFixture/DockerFixture.cs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/DockerComposeFixture/DockerFixture.cs b/DockerComposeFixture/DockerFixture.cs index ef13094..f5f3a76 100644 --- a/DockerComposeFixture/DockerFixture.cs +++ b/DockerComposeFixture/DockerFixture.cs @@ -32,9 +32,9 @@ public DockerFixture(IMessageSink output) /// If you call this multiple times on the same DockerFixture then it will be ignored. /// /// Options that control how docker-compose is executed. - public void InitOnce(Func setupOptions) + public async Task InitOnceAsync(Func setupOptions) { - InitOnce(setupOptions, null); + await InitOnceAsync(setupOptions, null); } /// @@ -43,11 +43,11 @@ public void InitOnce(Func setupOptions) /// /// Options that control how docker-compose is executed. /// - public void InitOnce(Func setupOptions, IDockerCompose dockerCompose) + public async Task InitOnceAsync(Func setupOptions, IDockerCompose dockerCompose) { if (!this.initialised) { - this.Init(setupOptions, dockerCompose); + await this.InitAsync(setupOptions, dockerCompose); this.initialised = true; } } @@ -57,9 +57,9 @@ public void InitOnce(Func setupOptions, IDockerCompose do /// Initialize docker compose services from file(s). /// /// Options that control how docker-compose is executed - public void Init(Func setupOptions) + public async Task InitAsync(Func setupOptions) { - Init(setupOptions, null); + await InitAsync(setupOptions, null); } /// @@ -67,7 +67,7 @@ public void Init(Func setupOptions) /// /// Options that control how docker-compose is executed /// - public void Init(Func setupOptions, IDockerCompose compose) + public async Task InitAsync(Func setupOptions, IDockerCompose compose) { var options = setupOptions(); options.Validate(); @@ -75,7 +75,7 @@ public void Init(Func setupOptions, IDockerCompose compos ? Path.Combine(Path.GetTempPath(), $"docker-compose-{DateTime.Now.Ticks}.log") : null; - this.Init(options.DockerComposeFiles, options.DockerComposeUpArgs, options.DockerComposeDownArgs, + await this.InitAsync(options.DockerComposeFiles, options.DockerComposeUpArgs, options.DockerComposeDownArgs, options.StartupTimeoutSecs, options.CustomUpTest, compose, this.GetLoggers(logFile).ToArray(), options.EnvironmentVariables); } @@ -104,7 +104,7 @@ private IEnumerable GetLoggers(string file) /// /// /// - public void Init(string[] dockerComposeFiles, string dockerComposeUpArgs, string dockerComposeDownArgs, + public async Task InitAsync(string[] dockerComposeFiles, string dockerComposeUpArgs, string dockerComposeDownArgs, int startupTimeoutSecs, Func customUpTest = null, IDockerCompose dockerCompose = null, ILogger[] logger = null, IEnumerable> environmentVariables = null) { @@ -120,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) @@ -196,7 +196,7 @@ public virtual void Dispose() this.Stop(); } - private void Start() + private async Task StartAsync() { if (this.CheckIfRunning().hasContainers) { @@ -215,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); if (this.customUpTest != null) { if (this.customUpTest(this.loggers.GetLoggedLines())) @@ -253,7 +253,5 @@ private void Stop() { this.dockerCompose.Down(); } - - } } From 39b983299c277c8ce574406eb6396dac89c7ecb3 Mon Sep 17 00:00:00 2001 From: "joe.miyamoto" Date: Mon, 11 May 2020 20:13:58 +0900 Subject: [PATCH 3/4] add InitWithRetry --- DockerComposeFixture/DockerFixture.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/DockerComposeFixture/DockerFixture.cs b/DockerComposeFixture/DockerFixture.cs index f5f3a76..be27b02 100644 --- a/DockerComposeFixture/DockerFixture.cs +++ b/DockerComposeFixture/DockerFixture.cs @@ -27,6 +27,23 @@ public DockerFixture(IMessageSink output) this.output = output; } + public async Task InitWithRetry(Func setupOptions, int count) + { + try + { + await InitOnceAsync(setupOptions); + } + catch (DockerComposeException ex) + { + if (count == 0) + { + throw; + } + this.initialised = false; + await InitWithRetry(setupOptions, count - 1); + } + } + /// /// Initialize docker compose services from file(s) but only once. /// If you call this multiple times on the same DockerFixture then it will be ignored. From c9259bacb80fce76786008b8144476cfb94681a5 Mon Sep 17 00:00:00 2001 From: "joe.miyamoto" Date: Mon, 11 May 2020 20:28:00 +0900 Subject: [PATCH 4/4] stop using init with retry --- DockerComposeFixture/DockerFixture.cs | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/DockerComposeFixture/DockerFixture.cs b/DockerComposeFixture/DockerFixture.cs index be27b02..f5f3a76 100644 --- a/DockerComposeFixture/DockerFixture.cs +++ b/DockerComposeFixture/DockerFixture.cs @@ -27,23 +27,6 @@ public DockerFixture(IMessageSink output) this.output = output; } - public async Task InitWithRetry(Func setupOptions, int count) - { - try - { - await InitOnceAsync(setupOptions); - } - catch (DockerComposeException ex) - { - if (count == 0) - { - throw; - } - this.initialised = false; - await InitWithRetry(setupOptions, count - 1); - } - } - /// /// Initialize docker compose services from file(s) but only once. /// If you call this multiple times on the same DockerFixture then it will be ignored.