44
55namespace EstateManagement . IntegrationTests . Common
66{
7- using System . Configuration ;
87 using System . Net . Http ;
9- using System . Threading ;
8+ using System . Resources ;
109 using System . Threading . Tasks ;
1110 using Client ;
12- using Ductus . FluentDocker . Builders ;
1311 using Ductus . FluentDocker . Common ;
14- using Ductus . FluentDocker . Model . Builders ;
1512 using Ductus . FluentDocker . Services ;
1613 using Ductus . FluentDocker . Services . Extensions ;
17- using global :: Shared . General ;
14+ using Gherkin ;
1815 using global ::Shared . Logger ;
19- using Microsoft . Extensions . Logging ;
16+ using global ::Shared . IntegrationTesting ;
17+ using Microsoft . EntityFrameworkCore . Internal ;
2018 using SecurityService . Client ;
2119
22- public class DockerHelper
20+ public class DockerHelper : global :: Shared . IntegrationTesting . DockerHelper
2321 {
2422 private readonly NlogLogger Logger ;
2523
26- protected INetworkService TestNetwork ;
27-
28- protected Int32 EstateManagementApiPort ;
29- protected Int32 EventStorePort ;
30-
31- public IContainerService EstateManagementApiContainer ;
32- protected IContainerService EventStoreContainer ;
33-
34- public IEstateClient EstateClient ;
35-
36- protected HttpClient HttpClient ;
37-
38- protected String EventStoreConnectionString ;
39-
40- protected String EstateManagementApiContainerName ;
41- protected String EventStoreContainerName ;
42-
4324 public DockerHelper ( NlogLogger logger )
4425 {
4526 this . Logger = logger ;
27+ this . Containers = new List < IContainerService > ( ) ;
28+ this . TestNetworks = new List < INetworkService > ( ) ;
4629 }
30+
31+ public Guid TestId ;
4732
48- private void SetupTestNetwork ( )
49- {
50- this . Logger . LogInformation ( "Starting Network Setup" ) ;
51- // Build a network
52- this . TestNetwork = new Ductus . FluentDocker . Builders . Builder ( ) . UseNetwork ( $ "testnetwork{ Guid . NewGuid ( ) } ") . Build ( ) ;
53-
54- this . Logger . LogInformation ( "Network Setup Complete" ) ;
55- }
56-
57- private void SetupEventStoreContainer ( String traceFolder )
58- {
59- this . Logger . LogInformation ( "About to Start Event Store Container" ) ;
60- // Event Store Container
61- this . EventStoreContainer = new Ductus . FluentDocker . Builders . Builder ( )
62- . UseContainer ( )
63- . UseImage ( "eventstore/eventstore:release-5.0.2" )
64- . ExposePort ( 2113 )
65- . ExposePort ( 1113 )
66- . WithName ( this . EventStoreContainerName )
67- . WithEnvironment ( "EVENTSTORE_RUN_PROJECTIONS=all" , "EVENTSTORE_START_STANDARD_PROJECTIONS=true" )
68- . UseNetwork ( this . TestNetwork )
69- . Mount ( traceFolder , "/var/log/eventstore" , MountType . ReadWrite )
70- . Build ( )
71- . Start ( ) . WaitForPort ( "2113/tcp" , 30000 ) ;
72-
73- this . Logger . LogInformation ( "Event Store Container Started" ) ;
74- }
33+ protected List < IContainerService > Containers ;
7534
76- public Guid TestId ;
77-
78- public ISecurityServiceClient SecurityServiceClient ;
35+ protected List < INetworkService > TestNetworks ;
7936
80- public async Task StartContainersForScenarioRun ( String scenarioName )
37+ public override async Task StartContainersForScenarioRun ( String scenarioName )
8138 {
8239 String traceFolder = FdOs . IsWindows ( ) ? $ "D:\\ home\\ txnproc\\ trace\\ { scenarioName } " : $ "//home//txnproc//trace//{ scenarioName } ";
8340
@@ -87,123 +44,87 @@ public async Task StartContainersForScenarioRun(String scenarioName)
8744 this . TestId = testGuid ;
8845
8946 this . Logger . LogInformation ( $ "Test Id is { testGuid } ") ;
90-
91- // Setup the container names
92- this . SecurityServiceContainerName = $ "securityservice{ testGuid : N} ";
93- this . EstateManagementApiContainerName = $ "estate{ testGuid : N} ";
94- this . EventStoreContainerName = $ "eventstore{ testGuid : N} ";
95-
96- this . EventStoreConnectionString =
97- $ "EventStoreSettings:ConnectionString=ConnectTo=tcp://admin:changeit@{ this . EventStoreContainerName } :1113;VerboseLogging=true;";
9847
99- this . SetupTestNetwork ( ) ;
100- this . SetupEventStoreContainer ( traceFolder ) ;
101- this . SetupEstateManagementContainer ( traceFolder ) ;
102- this . SetupSecurityServiceContainer ( traceFolder ) ;
48+ // Setup the container names
49+ String securityServiceContainerName = $ "securityservice { testGuid : N } " ;
50+ String estateManagementApiContainerName = $ "estate { testGuid : N } " ;
51+ String eventStoreContainerName = $ "eventstore { testGuid : N } " ;
10352
53+ ( String , String , String ) dockerCredentials = ( "https://www.docker.com" , "stuartferguson" , "Sc0tland" ) ;
54+
55+ INetworkService testNetwork = DockerHelper . SetupTestNetwork ( ) ;
56+ this . TestNetworks . Add ( testNetwork ) ;
57+ IContainerService eventStoreContainer = DockerHelper . SetupEventStoreContainer ( eventStoreContainerName , this . Logger ,
58+ "eventstore/eventstore:release-5.0.2" ,
59+ testNetwork , traceFolder ) ;
60+
61+
62+ IContainerService estateManagementContainer = DockerHelper . SetupEstateManagementContainer ( estateManagementApiContainerName , this . Logger ,
63+ "estatemanagement" , new List < INetworkService >
64+ {
65+ testNetwork
66+ } , traceFolder , null ,
67+ securityServiceContainerName ,
68+ eventStoreContainerName ) ;
69+
70+ IContainerService securityServiceContainer = DockerHelper . SetupSecurityServiceContainer ( securityServiceContainerName ,
71+ this . Logger ,
72+ "stuartferguson/securityservice" ,
73+ testNetwork ,
74+ traceFolder ,
75+ dockerCredentials ) ;
76+
77+ this . Containers . AddRange ( new List < IContainerService >
78+ {
79+ eventStoreContainer ,
80+ estateManagementContainer ,
81+ securityServiceContainer
82+ } ) ;
83+
10484 // Cache the ports
105- this . EstateManagementApiPort = this . EstateManagementApiContainer . ToHostExposedEndpoint ( "5000/tcp" ) . Port ;
106- this . SecurityServicePort = this . SecurityServiceContainer . ToHostExposedEndpoint ( "5001/tcp" ) . Port ;
107- this . EventStorePort = this . EventStoreContainer . ToHostExposedEndpoint ( "2113/tcp" ) . Port ;
85+ this . EstateManagementApiPort = estateManagementContainer . ToHostExposedEndpoint ( "5000/tcp" ) . Port ;
86+ this . SecurityServicePort = securityServiceContainer . ToHostExposedEndpoint ( "5001/tcp" ) . Port ;
87+ this . EventStoreHttpPort = eventStoreContainer . ToHostExposedEndpoint ( "2113/tcp" ) . Port ;
10888
10989 // Setup the base address resolvers
110- Func < String , String > baseAddressResolver = api => $ "http://127.0.0.1:{ this . EstateManagementApiPort } ";
111- Func < String , String > securityServiceBaseAddressResolver = api => $ "http://127.0.0.1:{ this . SecurityServicePort } ";
90+ String EstateManagementBaseAddressResolver ( String api ) => $ "http://127.0.0.1:{ this . EstateManagementApiPort } ";
91+ String SecurityServiceBaseAddressResolver ( String api ) => $ "http://127.0.0.1:{ this . SecurityServicePort } ";
11292
11393 HttpClient httpClient = new HttpClient ( ) ;
114- this . EstateClient = new EstateClient ( baseAddressResolver , httpClient ) ;
115- this . SecurityServiceClient = new SecurityServiceClient ( securityServiceBaseAddressResolver , httpClient ) ;
116-
117- //this.HttpClient = new HttpClient();
118- //this.HttpClient.BaseAddress = new Uri(baseAddressResolver(String.Empty));
94+ this . EstateClient = new EstateClient ( EstateManagementBaseAddressResolver , httpClient ) ;
95+ this . SecurityServiceClient = new SecurityServiceClient ( SecurityServiceBaseAddressResolver , httpClient ) ;
11996 }
12097
121- public Int32 SecurityServicePort ;
98+ public IEstateClient EstateClient ;
12299
123- public async Task StopContainersForScenarioRun ( )
124- {
125- try
126- {
127- if ( this . EstateManagementApiContainer != null )
128- {
129- this . EstateManagementApiContainer . StopOnDispose = true ;
130- this . EstateManagementApiContainer . RemoveOnDispose = true ;
131- this . EstateManagementApiContainer . Dispose ( ) ;
132- }
100+ public ISecurityServiceClient SecurityServiceClient ;
133101
134- if ( this . SecurityServiceContainer != null )
135- {
136- this . SecurityServiceContainer . StopOnDispose = true ;
137- this . SecurityServiceContainer . RemoveOnDispose = true ;
138- this . SecurityServiceContainer . Dispose ( ) ;
139- }
102+ protected Int32 EstateManagementApiPort ;
140103
141- if ( this . EventStoreContainer != null )
142- {
143- this . EventStoreContainer . StopOnDispose = true ;
144- this . EventStoreContainer . RemoveOnDispose = true ;
145- this . EventStoreContainer . Dispose ( ) ;
146- }
104+ protected Int32 SecurityServicePort ;
105+
106+ protected Int32 EventStoreHttpPort ;
147107
148- if ( this . TestNetwork != null )
108+ public override async Task StopContainersForScenarioRun ( )
109+ {
110+ if ( this . Containers . Any ( ) )
111+ {
112+ foreach ( IContainerService containerService in this . Containers )
149113 {
150- this . TestNetwork . Stop ( ) ;
151- this . TestNetwork . Remove ( true ) ;
114+ containerService . StopOnDispose = true ;
115+ containerService . RemoveOnDispose = true ;
116+ containerService . Dispose ( ) ;
152117 }
153118 }
154- catch ( Exception e )
119+
120+ if ( this . TestNetworks . Any ( ) )
155121 {
156- Console . WriteLine ( e ) ;
122+ foreach ( INetworkService networkService in this . TestNetworks )
123+ {
124+ networkService . Stop ( ) ;
125+ networkService . Remove ( true ) ;
126+ }
157127 }
158128 }
159-
160- public IContainerService SecurityServiceContainer ;
161-
162- public String SecurityServiceContainerName ;
163-
164- private void SetupSecurityServiceContainer ( String traceFolder )
165- {
166- this . Logger . LogInformation ( "About to Start Security Container" ) ;
167-
168- this . SecurityServiceContainer = new Builder ( ) . UseContainer ( ) . WithName ( this . SecurityServiceContainerName )
169- . WithEnvironment ( $ "ServiceOptions:PublicOrigin=http://{ this . SecurityServiceContainerName } :5001",
170- $ "ServiceOptions:IssuerUrl=http://{ this . SecurityServiceContainerName } :5001",
171- "ASPNETCORE_ENVIRONMENT=IntegrationTest" ,
172- "urls=http://*:5001" )
173- . WithCredential ( "https://www.docker.com" , "stuartferguson" , "Sc0tland" )
174- . UseImage ( "stuartferguson/securityservice" ) . ExposePort ( 5001 ) . UseNetwork ( new List < INetworkService >
175- {
176- this . TestNetwork
177- } . ToArray ( ) )
178- . Mount ( traceFolder , "/home/txnproc/trace" , MountType . ReadWrite ) . Build ( ) . Start ( ) . WaitForPort ( "5001/tcp" , 30000 ) ;
179- Thread . Sleep ( 20000 ) ;
180-
181- this . Logger . LogInformation ( "Security Service Container Started" ) ;
182-
183- }
184-
185- private void SetupEstateManagementContainer ( String traceFolder )
186- {
187- this . Logger . LogInformation ( "About to Start Estate Management Container" ) ;
188-
189- this . EstateManagementApiContainer = new Builder ( )
190- . UseContainer ( )
191- . WithName ( this . EstateManagementApiContainerName )
192- . WithEnvironment ( this . EventStoreConnectionString ,
193- $ "AppSettings:SecurityService=http://{ this . SecurityServiceContainerName } :5001",
194- $ "SecurityConfiguration:Authority=http://{ this . SecurityServiceContainerName } :5001",
195- "urls=http://*:5000" )
196- //"AppSettings:MigrateDatabase=true",
197- //"EventStoreSettings:START_PROJECTIONS=true",
198- //"EventStoreSettings:ContinuousProjectionsFolder=/app/projections/continuous")
199- . UseImage ( "estatemanagement" )
200- . ExposePort ( 5000 )
201- . UseNetwork ( new List < INetworkService > { this . TestNetwork , Setup . DatabaseServerNetwork } . ToArray ( ) )
202- . Mount ( traceFolder , "/home/txnproc/trace" , MountType . ReadWrite )
203- . Build ( )
204- . Start ( ) . WaitForPort ( "5000/tcp" , 30000 ) ;
205-
206- this . Logger . LogInformation ( "Estate Management Container Started" ) ;
207- }
208129 }
209130}
0 commit comments