@@ -14,7 +14,7 @@ const DEFAULT_SHARED_ACCESS_KEY_NAME = "RootManageSharedAccessKey";
1414const DEFAULT_SHARED_ACCESS_KEY = "SAS_KEY_VALUE" ;
1515const DEFAULT_MSSQL_ALIAS = "mssql" ;
1616const DEFAULT_MSSQL_IMAGE = "mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04" ;
17- const DEFAULT_MSSQL_PASSWORD = "P@ssword1!" ;
17+ const DEFAULT_MSSQL_PASSWORD = "P@ssword1!d3adb33f# " ;
1818const CONTAINER_CONFIG_FILE = "/ServiceBus_Emulator/ConfigFiles/Config.json" ;
1919
2020export class ServiceBusContainer extends GenericContainer {
@@ -28,6 +28,8 @@ export class ServiceBusContainer extends GenericContainer {
2828 super ( image ) ;
2929
3030 this . withExposedPorts ( DEFAULT_PORT , DEFAULT_HTTP_PORT )
31+ // The emulator image is minimal and does not include a shell, so the internal port checks will fail.
32+ // The HTTP health check should be sufficient to determine when the emulator is ready.
3133 . withWaitStrategy ( Wait . forHttp ( "/health" , DEFAULT_HTTP_PORT ) . forStatusCode ( 200 ) )
3234 . withEnvironment ( {
3335 SQL_WAIT_INTERVAL : "0" , // We start the MSSQL container before the emulator
@@ -40,16 +42,28 @@ export class ServiceBusContainer extends GenericContainer {
4042 }
4143
4244 public withMssqlContainer ( container : GenericContainer ) : this {
45+ if ( this . mssqlImage !== DEFAULT_MSSQL_IMAGE ) {
46+ throw new Error ( "Cannot use both withMssqlImage() and withMssqlContainer()" ) ;
47+ }
48+
4349 this . mssqlContainer = container ;
4450 return this ;
4551 }
4652
4753 public withMssqlImage ( image : string ) : this {
54+ if ( this . mssqlContainer ) {
55+ throw new Error ( "Cannot use both withMssqlImage() and withMssqlContainer()" ) ;
56+ }
57+
4858 this . mssqlImage = image ;
4959 return this ;
5060 }
5161
5262 public withMssqlPassword ( password : string ) : this {
63+ if ( this . mssqlPassword !== DEFAULT_MSSQL_PASSWORD ) {
64+ throw new Error ( "Cannot use both withMssqlPassword() and withMssqlContainer()" ) ;
65+ }
66+
5367 this . mssqlPassword = password ;
5468 return this ;
5569 }
@@ -67,7 +81,6 @@ export class ServiceBusContainer extends GenericContainer {
6781 // This should match the behaviour of @testcontainers /mssqlserver, we
6882 // create the container manually here to avoid module dependencies.
6983 this . mssqlContainer = new GenericContainer ( this . mssqlImage )
70- . withNetworkAliases ( DEFAULT_MSSQL_ALIAS )
7184 . withEnvironment ( {
7285 ACCEPT_EULA : "Y" ,
7386 MSSQL_SA_PASSWORD : this . mssqlPassword ,
@@ -76,7 +89,7 @@ export class ServiceBusContainer extends GenericContainer {
7689 . withWaitStrategy ( Wait . forLogMessage ( / .* R e c o v e r y i s c o m p l e t e .* / , 1 ) . withStartupTimeout ( 120_000 ) ) ;
7790 }
7891
79- const mssql = await this . mssqlContainer . withNetwork ( network ) . start ( ) ;
92+ const mssql = await this . mssqlContainer . withNetworkAliases ( DEFAULT_MSSQL_ALIAS ) . withNetwork ( network ) . start ( ) ;
8093
8194 if ( this . config ) {
8295 this . withCopyContentToContainer ( [
@@ -94,7 +107,16 @@ export class ServiceBusContainer extends GenericContainer {
94107 MSSQL_SA_PASSWORD : this . mssqlPassword ,
95108 } ) ;
96109
97- return new StartedServiceBusContainer ( await super . start ( ) , mssql , network ) ;
110+ try {
111+ const serviceBus = await super . start ( ) ;
112+
113+ return new StartedServiceBusContainer ( serviceBus , mssql , network ) ;
114+ } catch ( err ) {
115+ await mssql . stop ( ) ;
116+ await network . stop ( ) ;
117+
118+ throw err ;
119+ }
98120 }
99121}
100122
@@ -120,7 +142,10 @@ export class StartedServiceBusContainer extends AbstractStartedContainer {
120142 }
121143
122144 protected override async containerStopped ( ) : Promise < void > {
123- await this . mssql . stop ( ) ;
124- await this . network . stop ( ) ;
145+ try {
146+ await this . mssql . stop ( ) ;
147+ } finally {
148+ await this . network . stop ( ) ;
149+ }
125150 }
126151}
0 commit comments