11//#if INFORMIX
2- using IBM . Data . DB2 . Core ;
32using Respawn ;
43using Shouldly ;
54using System ;
65using System . Data ;
6+ using System . IO ;
77using System . Linq ;
88using System . Threading . Tasks ;
9+ using DotNet . Testcontainers . Builders ;
10+ using DotNet . Testcontainers . Configurations ;
11+ using DotNet . Testcontainers . Containers ;
12+ using IBM . Data . Db2 ;
913using NPoco ;
1014using Respawn . Graph ;
1115using Xunit ;
@@ -18,24 +22,76 @@ public class InformixTests : IAsyncLifetime
1822 private DB2Connection _connection ;
1923 private readonly ITestOutputHelper _output ;
2024 private string _databaseName ;
25+ private IContainer _sqlContainer ;
2126
2227 public InformixTests ( ITestOutputHelper output )
2328 {
2429 _output = output ;
2530 }
2631
27- public Task DisposeAsync ( )
32+ public async Task DisposeAsync ( )
2833 {
2934 _connection ? . Close ( ) ;
3035 _connection ? . Dispose ( ) ;
3136 _connection = null ;
32- return Task . FromResult ( 0 ) ;
37+
38+ await _sqlContainer . StopAsync ( ) ;
39+ await _sqlContainer . DisposeAsync ( ) ;
40+ _sqlContainer = null ;
3341 }
3442
3543 public async Task InitializeAsync ( )
3644 {
37- const string connString = "Server=127.0.0.1:9089;Database=sysadmin;UID=informix;PWD=in4mix;Persist Security Info=True;Authentication=Server;" ;
38-
45+ //const string connString = "Server=127.0.0.1:9089;Database=sysadmin;UID=informix;PWD=in4mix;Persist Security Info=True;Authentication=Server;";
46+
47+ _sqlContainer = new ContainerBuilder ( )
48+ . WithImage ( "ibmcom/informix-developer-database:14.10.FC5DE" )
49+
50+ // = environment:
51+ . WithEnvironment ( "LICENSE" , "accept" )
52+ . WithEnvironment ( "ONCONFIG_FILE" , "onconfig" )
53+ . WithEnvironment ( "RUN_FILE_PRE_INIT" , "my_post.sh" )
54+
55+ // = ports:
56+ . WithPortBinding ( 9088 , 9088 )
57+ . WithPortBinding ( 9089 , 9089 )
58+ . WithPortBinding ( 27017 , 27017 )
59+ . WithPortBinding ( 27018 , 27018 )
60+ . WithPortBinding ( 27883 , 27883 )
61+
62+ // = volumes:
63+ . WithBindMount (
64+ source : Path . GetFullPath ( "./informix-server" ) ,
65+ destination : "/opt/ibm/config" ,
66+ AccessMode . ReadWrite )
67+
68+ // = privileged: true
69+ . WithPrivileged ( true )
70+
71+ // = user: root
72+ //.WithUser("root")
73+
74+ // = tty: true
75+ //.WithTty(true)
76+
77+ // optional: equivalent to "restart: always" but Testcontainers
78+ // does not automatically restart containers (it recreates instead)
79+ // .WithAutoRemove(false)
80+
81+ . WithWaitStrategy ( Wait . ForUnixContainer ( )
82+ . UntilExternalTcpPortIsAvailable ( 9088 )
83+ . UntilExternalTcpPortIsAvailable ( 9089 )
84+ . UntilInternalTcpPortIsAvailable ( 9088 )
85+ . UntilInternalTcpPortIsAvailable ( 9089 )
86+ // This is the last success message
87+ . UntilMessageIsLogged ( "starting mqtt listener on port 27883" )
88+ )
89+ . Build ( ) ;
90+
91+ await _sqlContainer . StartAsync ( ) ;
92+
93+ var connString = GetConnectionString ( _sqlContainer ) ;
94+
3995 await using ( var connection = new DB2Connection ( connString ) )
4096 {
4197 await connection . OpenAsync ( ) ;
@@ -47,13 +103,33 @@ public async Task InitializeAsync()
47103 await database . ExecuteAsync ( $ "CREATE DATABASE { _databaseName } WITH BUFFERED LOG;") ;
48104 }
49105
50- var testDbConnString = $ "Server=127.0.0.1:9089;Database={ _databaseName } ;UID=informix;PWD=in4mix;Persist Security Info=True;Authentication=Server;";
106+ var testDbConnString = new DB2ConnectionStringBuilder ( connString )
107+ {
108+ Database = _databaseName
109+ } . ToString ( ) ;
51110
52111 _connection = new DB2Connection ( testDbConnString ) ;
53112
54113 await _connection . OpenAsync ( ) ;
55114 }
56115
116+ private static string GetConnectionString (
117+ IContainer container ,
118+ string database = "sysadmin" ,
119+ string user = "informix" ,
120+ string password = "in4mix" )
121+ {
122+ var host = container . Hostname ;
123+ var port = container . GetMappedPublicPort ( 9089 ) ; // SQL port
124+
125+ return
126+ $ "Server={ host } :{ port } ;" +
127+ $ "Database={ database } ;" +
128+ $ "UID={ user } ;" +
129+ $ "Password={ password } ;" +
130+ $ "Persist Security Info=True;Authentication=Server;";
131+ }
132+
57133 [ SkipOnCI ]
58134 public async Task ShouldDeleteData ( )
59135 {
0 commit comments