Skip to content

Commit b2c79c1

Browse files
Merge pull request #33 from StuartFerguson/task/#32_updatedockerherlper
Updated to use Shared Docker Helper
2 parents 0d873be + d4833c4 commit b2c79c1

File tree

4 files changed

+132
-269
lines changed

4 files changed

+132
-269
lines changed

TransactionProcessor.IntegrationTests/Common/DockerHelper.cs

Lines changed: 101 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Linq;
56
using System.Net.Http;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Client;
910
using Ductus.FluentDocker.Builders;
11+
using Ductus.FluentDocker.Common;
1012
using Ductus.FluentDocker.Executors;
1113
using Ductus.FluentDocker.Extensions;
1214
using Ductus.FluentDocker.Model.Builders;
@@ -16,209 +18,139 @@
1618
using global::Shared.Logger;
1719
using SecurityService.Client;
1820

19-
public class DockerHelper
21+
public class DockerHelper : global::Shared.IntegrationTesting.DockerHelper
2022
{
2123
private readonly NlogLogger Logger;
2224

23-
protected INetworkService TestNetwork;
24-
25-
public Int32 SecurityServicePort;
26-
protected Int32 EstateManagementPort;
27-
protected Int32 TransactionProcessorPort;
28-
protected Int32 EventStorePort;
29-
30-
public IContainerService SecurityServiceContainer;
31-
public IContainerService EstateManagementContainer;
32-
public IContainerService TransactionProcessorContainer;
33-
protected IContainerService EventStoreContainer;
34-
35-
public IEstateClient EstateClient;
36-
public ITransactionProcessorClient TransactionProcessorClient;
37-
public ISecurityServiceClient SecurityServiceClient;
38-
39-
protected String EventStoreConnectionString;
40-
41-
public String SecurityServiceContainerName;
42-
protected String EstateManagementContainerName;
43-
protected String TransactionProcessorContainerName;
44-
protected String EventStoreContainerName;
45-
4625
public DockerHelper(NlogLogger logger)
4726
{
4827
this.Logger = logger;
28+
this.Containers = new List<IContainerService>();
29+
this.TestNetworks = new List<INetworkService>();
4930
}
5031

51-
private void SetupTestNetwork()
52-
{
53-
// Build a network
54-
this.TestNetwork = new Ductus.FluentDocker.Builders.Builder().UseNetwork($"testnetwork{Guid.NewGuid()}").Build();
55-
}
5632
public Guid TestId;
57-
private void SetupEventStoreContainer(String traceFolder)
58-
{
59-
// Event Store Container
60-
this.EventStoreContainer = new Ductus.FluentDocker.Builders.Builder()
61-
.UseContainer()
62-
.UseImage("eventstore/eventstore:release-5.0.2")
63-
.ExposePort(2113)
64-
.ExposePort(1113)
65-
.WithName(this.EventStoreContainerName)
66-
.WithEnvironment("EVENTSTORE_RUN_PROJECTIONS=all", "EVENTSTORE_START_STANDARD_PROJECTIONS=true")
67-
.UseNetwork(this.TestNetwork)
68-
.Mount(traceFolder, "/var/log/eventstore", MountType.ReadWrite)
69-
.Build()
70-
.Start().WaitForPort("2113/tcp", 30000);
71-
}
7233

73-
public async Task StartContainersForScenarioRun(String scenarioName)
34+
protected List<IContainerService> Containers;
35+
36+
protected List<INetworkService> TestNetworks;
37+
38+
public override async Task StartContainersForScenarioRun(String scenarioName)
7439
{
75-
String traceFolder = $"/home/ubuntu/estatemanagement/trace/{scenarioName}/";
40+
String traceFolder = FdOs.IsWindows() ? $"D:\\home\\txnproc\\trace\\{scenarioName}" : $"//home//txnproc//trace//{scenarioName}";
7641

7742
Logging.Enabled();
7843

7944
Guid testGuid = Guid.NewGuid();
8045
this.TestId = testGuid;
8146

47+
this.Logger.LogInformation($"Test Id is {testGuid}");
48+
8249
// Setup the container names
83-
this.SecurityServiceContainerName = $"securityservice{testGuid:N}";
84-
this.EstateManagementContainerName = $"estate{testGuid:N}";
85-
this.TransactionProcessorContainerName = $"txnprocessor{testGuid:N}";
86-
this.EventStoreContainerName = $"eventstore{testGuid:N}";
87-
88-
this.EventStoreConnectionString =
89-
$"EventStoreSettings:ConnectionString=ConnectTo=tcp://admin:changeit@{this.EventStoreContainerName}:1113;VerboseLogging=true;";
90-
91-
this.SetupTestNetwork();
92-
this.SetupSecurityServiceContainer(traceFolder);
93-
this.SetupEventStoreContainer(traceFolder);
94-
this.SetupEstateManagementContainer(traceFolder);
95-
this.SetupTransactionProcessorContainer(traceFolder);
50+
String securityServiceContainerName = $"securityservice{testGuid:N}";
51+
String estateManagementApiContainerName = $"estate{testGuid:N}";
52+
String transactionProcessorContainerName = $"txnprocessor{testGuid:N}";
53+
String eventStoreContainerName = $"eventstore{testGuid:N}";
54+
55+
(String, String, String) dockerCredentials = ("https://www.docker.com", "stuartferguson", "Sc0tland");
56+
57+
INetworkService testNetwork = DockerHelper.SetupTestNetwork();
58+
this.TestNetworks.Add(testNetwork);
59+
IContainerService eventStoreContainer =
60+
DockerHelper.SetupEventStoreContainer(eventStoreContainerName, this.Logger, "eventstore/eventstore:release-5.0.2", testNetwork, traceFolder);
61+
62+
63+
IContainerService estateManagementContainer = DockerHelper.SetupEstateManagementContainer(estateManagementApiContainerName,
64+
this.Logger,
65+
"stuartferguson/estatemanagement",
66+
new List<INetworkService>
67+
{
68+
testNetwork
69+
},
70+
traceFolder,
71+
dockerCredentials,
72+
securityServiceContainerName,
73+
eventStoreContainerName);
74+
75+
IContainerService securityServiceContainer = DockerHelper.SetupSecurityServiceContainer(securityServiceContainerName,
76+
this.Logger,
77+
"stuartferguson/securityservice",
78+
testNetwork,
79+
traceFolder,
80+
dockerCredentials);
81+
82+
IContainerService transactionProcessorContainer = DockerHelper.SetupTransactionProcessorContainer(transactionProcessorContainerName,
83+
this.Logger,
84+
"transactionprocessor",
85+
new List<INetworkService>
86+
{
87+
testNetwork
88+
},
89+
traceFolder,
90+
dockerCredentials,
91+
securityServiceContainerName,
92+
eventStoreContainerName);
93+
94+
95+
this.Containers.AddRange(new List<IContainerService>
96+
{
97+
eventStoreContainer,
98+
estateManagementContainer,
99+
securityServiceContainer,
100+
transactionProcessorContainer
101+
});
96102

97103
// Cache the ports
98-
this.EstateManagementPort = this.EstateManagementContainer.ToHostExposedEndpoint("5000/tcp").Port;
99-
this.TransactionProcessorPort = this.TransactionProcessorContainer.ToHostExposedEndpoint("5002/tcp").Port;
100-
this.EventStorePort = this.EventStoreContainer.ToHostExposedEndpoint("2113/tcp").Port;
101-
this.SecurityServicePort = this.SecurityServiceContainer.ToHostExposedEndpoint("5001/tcp").Port;
104+
this.EstateManagementApiPort = estateManagementContainer.ToHostExposedEndpoint("5000/tcp").Port;
105+
this.SecurityServicePort = securityServiceContainer.ToHostExposedEndpoint("5001/tcp").Port;
106+
this.EventStoreHttpPort = eventStoreContainer.ToHostExposedEndpoint("2113/tcp").Port;
107+
this.TransactionProcessorPort = transactionProcessorContainer.ToHostExposedEndpoint("5002/tcp").Port;
102108

103-
// Setup the base address resolver
104-
Func<String, String> estateManagementBaseAddressResolver = api => $"http://127.0.0.1:{this.EstateManagementPort}";
105-
Func<String, String> transactionProcessorBaseAddressResolver = api => $"http://127.0.0.1:{this.TransactionProcessorPort}";
106-
Func<String, String> securityServiceBaseAddressResolver = api => $"http://127.0.0.1:{this.SecurityServicePort}";
109+
// Setup the base address resolvers
110+
String EstateManagementBaseAddressResolver(String api) => $"http://127.0.0.1:{this.EstateManagementApiPort}";
111+
String SecurityServiceBaseAddressResolver(String api) => $"http://127.0.0.1:{this.SecurityServicePort}";
112+
String TransactionProcessorBaseAddressResolver(String api) => $"http://127.0.0.1:{this.TransactionProcessorPort}";
107113

108114
HttpClient httpClient = new HttpClient();
109-
this.EstateClient = new EstateClient(estateManagementBaseAddressResolver, httpClient);
110-
this.TransactionProcessorClient = new TransactionProcessorClient(transactionProcessorBaseAddressResolver, httpClient);
111-
this.SecurityServiceClient = new SecurityServiceClient(securityServiceBaseAddressResolver, httpClient);
112-
// TODO: Use this to talk to txn processor until we have a client
113-
//this.HttpClient = new HttpClient();
114-
//this.HttpClient.BaseAddress = new Uri(transactionProcessorBaseAddressResolver(String.Empty));
115+
this.EstateClient = new EstateClient(EstateManagementBaseAddressResolver, httpClient);
116+
this.SecurityServiceClient = new SecurityServiceClient(SecurityServiceBaseAddressResolver, httpClient);
117+
this.TransactionProcessorClient = new TransactionProcessorClient(TransactionProcessorBaseAddressResolver, httpClient);
115118
}
116119

117-
public async Task StopContainersForScenarioRun()
118-
{
119-
try
120-
{
121-
if (this.SecurityServiceContainer != null)
122-
{
123-
this.SecurityServiceContainer.StopOnDispose = true;
124-
this.SecurityServiceContainer.RemoveOnDispose = true;
125-
this.SecurityServiceContainer.Dispose();
126-
}
120+
public IEstateClient EstateClient;
127121

128-
if (this.TransactionProcessorContainer != null)
129-
{
130-
this.TransactionProcessorContainer.StopOnDispose = true;
131-
this.TransactionProcessorContainer.RemoveOnDispose = true;
132-
this.TransactionProcessorContainer.Dispose();
133-
}
122+
public ITransactionProcessorClient TransactionProcessorClient;
134123

135-
if (this.EstateManagementContainer != null)
136-
{
137-
this.EstateManagementContainer.StopOnDispose = true;
138-
this.EstateManagementContainer.RemoveOnDispose = true;
139-
this.EstateManagementContainer.Dispose();
140-
}
124+
public ISecurityServiceClient SecurityServiceClient;
141125

142-
if (this.EventStoreContainer != null)
143-
{
144-
this.EventStoreContainer.StopOnDispose = true;
145-
this.EventStoreContainer.RemoveOnDispose = true;
146-
this.EventStoreContainer.Dispose();
147-
}
126+
protected Int32 EstateManagementApiPort;
148127

149-
if (this.TestNetwork != null)
150-
{
151-
this.TestNetwork.Stop();
152-
this.TestNetwork.Remove(true);
153-
}
154-
}
155-
catch (Exception e)
156-
{
157-
Console.WriteLine(e);
158-
}
159-
}
128+
protected Int32 SecurityServicePort;
160129

161-
private void SetupSecurityServiceContainer(String traceFolder)
162-
{
163-
this.Logger.LogInformation("About to Start Security Container");
164-
165-
this.SecurityServiceContainer = new Builder().UseContainer().WithName(this.SecurityServiceContainerName)
166-
.WithEnvironment($"ServiceOptions:PublicOrigin=http://{this.SecurityServiceContainerName}:5001",
167-
$"ServiceOptions:IssuerUrl=http://{this.SecurityServiceContainerName}:5001",
168-
"ASPNETCORE_ENVIRONMENT=IntegrationTest",
169-
"urls=http://*:5001")
170-
.WithCredential("https://www.docker.com", "stuartferguson", "Sc0tland")
171-
.UseImage("stuartferguson/securityservice").ExposePort(5001).UseNetwork(new List<INetworkService>
172-
{
173-
this.TestNetwork
174-
}.ToArray())
175-
.Mount(traceFolder, "/home/txnproc/trace", MountType.ReadWrite).Build().Start().WaitForPort("5001/tcp", 30000);
176-
Thread.Sleep(20000);
177-
178-
this.Logger.LogInformation("Security Service Container Started");
130+
protected Int32 EventStoreHttpPort;
179131

180-
}
132+
protected Int32 TransactionProcessorPort;
181133

182-
private void SetupEstateManagementContainer(String traceFolder)
134+
public override async Task StopContainersForScenarioRun()
183135
{
184-
// Management API Container
185-
this.EstateManagementContainer = new Builder()
186-
.UseContainer()
187-
.WithName(this.EstateManagementContainerName)
188-
.WithEnvironment(this.EventStoreConnectionString,
189-
$"AppSettings:SecurityService=http://{this.SecurityServiceContainerName}:5001",
190-
$"SecurityConfiguration:Authority=http://{this.SecurityServiceContainerName}:5001",
191-
"urls=http://*:5000") //,
192-
//"AppSettings:MigrateDatabase=true",
193-
//"EventStoreSettings:START_PROJECTIONS=true",
194-
//"EventStoreSettings:ContinuousProjectionsFolder=/app/projections/continuous")
195-
.WithCredential("https://www.docker.com", "stuartferguson", "Sc0tland")
196-
.UseImage("stuartferguson/estatemanagement")
197-
.ExposePort(5000)
198-
.UseNetwork(new List<INetworkService> { this.TestNetwork, Setup.DatabaseServerNetwork }.ToArray())
199-
.Mount(traceFolder, "/home", MountType.ReadWrite)
200-
.Build()
201-
.Start().WaitForPort("5000/tcp", 30000);
202-
}
136+
if (this.Containers.Any())
137+
{
138+
foreach (IContainerService containerService in this.Containers)
139+
{
140+
containerService.StopOnDispose = true;
141+
containerService.RemoveOnDispose = true;
142+
containerService.Dispose();
143+
}
144+
}
203145

204-
private void SetupTransactionProcessorContainer(String traceFolder)
205-
{
206-
// Management API Container
207-
this.TransactionProcessorContainer = new Builder()
208-
.UseContainer()
209-
.WithName(this.TransactionProcessorContainerName)
210-
.WithEnvironment(this.EventStoreConnectionString,
211-
$"AppSettings:SecurityService=http://{this.SecurityServiceContainerName}:5001",
212-
$"SecurityConfiguration:Authority=http://{this.SecurityServiceContainerName}:5001") //,
213-
//"AppSettings:MigrateDatabase=true",
214-
//"EventStoreSettings:START_PROJECTIONS=true",
215-
//"EventStoreSettings:ContinuousProjectionsFolder=/app/projections/continuous")
216-
.UseImage("transactionprocessor")
217-
.ExposePort(5002)
218-
.UseNetwork(new List<INetworkService> { this.TestNetwork, Setup.DatabaseServerNetwork }.ToArray())
219-
.Mount(traceFolder, "/home", MountType.ReadWrite)
220-
.Build()
221-
.Start().WaitForPort("5002/tcp", 30000);
146+
if (this.TestNetworks.Any())
147+
{
148+
foreach (INetworkService networkService in this.TestNetworks)
149+
{
150+
networkService.Stop();
151+
networkService.Remove(true);
152+
}
153+
}
222154
}
223155
}
224156
}

0 commit comments

Comments
 (0)