1+ using System ;
2+ using System . Data . Common ;
3+ using System . IO ;
4+ using Docker . DotNet . Models ;
5+ using DotNet . Testcontainers . Builders ;
6+ using DotNet . Testcontainers . Configurations ;
7+ using DotNet . Testcontainers . Containers ;
8+ using DotNet . Testcontainers . Images ;
9+ using IBM . Data . Db2 ;
10+ using NPoco ;
11+ using Xunit . Abstractions ;
12+
13+ namespace Respawn . DatabaseTests ;
14+
15+ public class InformixFixture ( IMessageSink messageSink ) : DbFixture < InformixBuilder , InformixContainer > ( messageSink )
16+ {
17+ public override DbProviderFactory DbProviderFactory => DB2Factory . Instance ;
18+
19+ protected override DatabaseType DbType => null ;
20+
21+ protected override InformixBuilder CreateBuilder ( )
22+ {
23+ return new InformixBuilder ( "ibmcom/informix-developer-database:14.10.FC5DE" ) ;
24+ }
25+ }
26+
27+ public sealed class InformixBuilder : ContainerBuilder < InformixBuilder , InformixContainer , ContainerConfiguration >
28+ {
29+ public InformixBuilder ( )
30+ : this ( "" )
31+ {
32+ throw new NotSupportedException ( ) ;
33+ }
34+
35+ public InformixBuilder ( string image )
36+ : this ( new DockerImage ( image ) )
37+ {
38+ }
39+
40+ public InformixBuilder ( IImage image )
41+ : this ( new ContainerConfiguration ( ) )
42+ {
43+ DockerResourceConfiguration = Init ( ) . WithImage ( image ) . DockerResourceConfiguration ;
44+ }
45+
46+ private InformixBuilder ( ContainerConfiguration configuration ) : base ( configuration )
47+ {
48+ DockerResourceConfiguration = configuration ;
49+ }
50+
51+ protected override ContainerConfiguration DockerResourceConfiguration { get ; }
52+
53+ protected override InformixBuilder Init ( )
54+ {
55+ return base . Init ( )
56+
57+ // = environment:
58+ . WithEnvironment ( "LICENSE" , "accept" )
59+ . WithEnvironment ( "ONCONFIG_FILE" , "onconfig" )
60+ . WithEnvironment ( "RUN_FILE_PRE_INIT" , "my_post.sh" )
61+
62+ // = ports:
63+ . WithPortBinding ( 9088 , assignRandomHostPort : true )
64+ . WithPortBinding ( 9089 , assignRandomHostPort : true )
65+ . WithPortBinding ( 27017 , assignRandomHostPort : true )
66+ . WithPortBinding ( 27018 , assignRandomHostPort : true )
67+ . WithPortBinding ( 27883 , assignRandomHostPort : true )
68+
69+ // = volumes:
70+ . WithBindMount (
71+ source : Path . GetFullPath ( "./informix-server" ) ,
72+ destination : "/opt/ibm/config" ,
73+ AccessMode . ReadWrite )
74+
75+ // = privileged: true
76+ . WithPrivileged ( true )
77+
78+ // = user: root
79+ //.WithUser("root")
80+
81+ // = tty: true
82+ //.WithTty(true)
83+
84+ // optional: equivalent to "restart: always" but Testcontainers
85+ // does not automatically restart containers (it recreates instead)
86+ // .WithAutoRemove(false)
87+
88+ . WithWaitStrategy ( Wait . ForUnixContainer ( )
89+ . UntilExternalTcpPortIsAvailable ( 9088 )
90+ . UntilExternalTcpPortIsAvailable ( 9089 )
91+ . UntilInternalTcpPortIsAvailable ( 9088 )
92+ . UntilInternalTcpPortIsAvailable ( 9089 )
93+ // This is the last success message
94+ . UntilMessageIsLogged ( "starting mqtt listener on port 27883" )
95+ ) ;
96+ }
97+
98+ public override InformixContainer Build ( )
99+ {
100+ Validate ( ) ;
101+ return new InformixContainer ( DockerResourceConfiguration ) ;
102+ }
103+
104+ protected override InformixBuilder Clone ( IResourceConfiguration < CreateContainerParameters > resourceConfiguration )
105+ {
106+ return Merge ( DockerResourceConfiguration , new ContainerConfiguration ( resourceConfiguration ) ) ;
107+ }
108+
109+ protected override InformixBuilder Clone ( IContainerConfiguration resourceConfiguration )
110+ {
111+ return Merge ( DockerResourceConfiguration , new ContainerConfiguration ( resourceConfiguration ) ) ;
112+ }
113+
114+ protected override InformixBuilder Merge ( ContainerConfiguration oldValue , ContainerConfiguration newValue )
115+ {
116+ return new InformixBuilder ( new ContainerConfiguration ( oldValue , newValue ) ) ;
117+ }
118+ }
119+
120+ public sealed class InformixContainer ( IContainerConfiguration configuration ) : DockerContainer ( configuration ) , IDatabaseContainer
121+ {
122+ public string GetConnectionString ( )
123+ {
124+ var host = Hostname ;
125+ var port = GetMappedPublicPort ( 9089 ) ; // SQL port
126+ return $ "Server={ host } :{ port } ;Database=sysadmin;UID=informix;Password=in4mix;Persist Security Info=True;Authentication=Server;";
127+ }
128+ }
0 commit comments