-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bug (Issue #86) where hooks was not executed when container reused
- Loading branch information
Mario Toffia
committed
May 31, 2019
1 parent
4fe5c72
commit e62ea38
Showing
3 changed files
with
108 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using Ductus.FluentDocker.Common; | ||
using Ductus.FluentDocker.Services; | ||
using Ductus.FluentDocker.Services.Extensions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Npgsql; | ||
using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; | ||
|
||
namespace Ductus.FluentDocker.Tests.FluentApiTests | ||
{ | ||
[TestClass] | ||
public class WaitTests | ||
{ | ||
private static bool _checkConnectionInvoked; | ||
|
||
[TestMethod] | ||
public void SingleWaitLambdaShallGetInvoked() | ||
{ | ||
_checkConnectionInvoked = false; | ||
using (var container = Fd.UseContainer() | ||
.UseImage("postgres:9.6-alpine") | ||
.ExposePort(5432) | ||
.WithEnvironment("POSTGRES_PASSWORD=mysecretpassword") | ||
.Wait("", (service, count) => CheckConnection(count, service)) | ||
.Build() | ||
.Start()) | ||
{ | ||
IsTrue(_checkConnectionInvoked, "Invoked since container was created and thus run phase is executed"); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void WaitLambdaWithReusedContainerShallGetInvoked() | ||
{ | ||
_checkConnectionInvoked = false; | ||
using (var c1 = Fd.UseContainer() | ||
.UseImage("postgres:9.6-alpine") | ||
.WithName("postgres") | ||
.ExposePort(5432) | ||
.WithEnvironment("POSTGRES_PASSWORD=mysecretpassword") | ||
.ReuseIfExists() | ||
.WaitForPort("5432/tcp", TimeSpan.FromSeconds(30)) | ||
.Build() | ||
.Start()) | ||
{ | ||
// Make sure to have named container running | ||
var config = c1.GetConfiguration(); | ||
AreEqual(ServiceRunningState.Running, c1.State); | ||
|
||
using (var c2 = Fd.UseContainer() | ||
.UseImage("postgres:9.6-alpine") | ||
.WithName("postgres") | ||
.ExposePort(5432) | ||
.WithEnvironment("POSTGRES_PASSWORD=mysecretpassword") | ||
.ReuseIfExists() | ||
.Wait("", (service, count) => CheckConnection(count, service)) | ||
.Build() | ||
.Start()) | ||
{ | ||
IsTrue(_checkConnectionInvoked, | ||
"Is invoked even if reused container since Start drives the container state eventually to running"); | ||
} | ||
} | ||
} | ||
|
||
private static int CheckConnection(int count, IContainerService service) | ||
{ | ||
_checkConnectionInvoked = true; | ||
|
||
if (count > 10) throw new FluentDockerException("Failed to wait for sql server"); | ||
|
||
var ep = service.ToHostExposedEndpoint("5432/tcp"); | ||
var str = $"Server={ep.Address};Port={ep.Port};Userid=postgres;" + | ||
"Password=mysecretpassword;Pooling=false;MinPoolSize=1;" + | ||
"MaxPoolSize=20;Timeout=15;SslMode=Disable;Database=postgres"; | ||
|
||
try | ||
{ | ||
using (var conn = new NpgsqlConnection(str)) | ||
{ | ||
conn.Open(); | ||
return 0; | ||
} | ||
} | ||
catch | ||
{ | ||
return 500 /*ms*/; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters