1
+ using Docker . DotNet ;
2
+ using Docker . DotNet . Models ;
3
+ using System ;
4
+ using System . Collections . Generic ;
5
+ using System . Runtime . InteropServices ;
6
+ using System . Text ;
7
+ using System . Threading ;
8
+ using Xunit ;
9
+
10
+ namespace WorkflowCore . Tests . SqlServer
11
+ {
12
+ public class DockerSetup : IDisposable
13
+ {
14
+ //public static string ImageName = "microsoft/mssql-server-windows-express";
15
+ public static string ImageName = "microsoft/mssql-server-linux" ;
16
+ public static int Port = 1533 ;
17
+ public static string SqlPassword = "I@mJustT3st1ing" ;
18
+ DockerClient docker ;
19
+ string containerId ;
20
+
21
+ public string ConnectionString { get ; private set ; }
22
+
23
+ public DockerSetup ( )
24
+ {
25
+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
26
+ docker = new DockerClientConfiguration ( new Uri ( "npipe://./pipe/docker_engine" ) ) . CreateClient ( ) ;
27
+ else
28
+ docker = new DockerClientConfiguration ( new Uri ( "unix:///var/run/docker.sock" ) ) . CreateClient ( ) ;
29
+
30
+ HostConfig hostCfg = new HostConfig ( ) ;
31
+ PortBinding pb = new PortBinding ( ) ;
32
+ pb . HostIP = "0.0.0.0" ;
33
+ pb . HostPort = Port . ToString ( ) ;
34
+ hostCfg . PortBindings = new Dictionary < string , IList < PortBinding > > ( ) ;
35
+ hostCfg . PortBindings . Add ( "1433/tcp" , new PortBinding [ ] { pb } ) ;
36
+
37
+ var env = new List < string > ( ) ;
38
+ env . Add ( "ACCEPT_EULA=Y" ) ;
39
+ env . Add ( $ "SA_PASSWORD={ SqlPassword } ") ;
40
+
41
+ //docker.Images.PullImageAsync(new ImagesPullParameters() { Parent = ImageName, Tag = "latest" }, null).Wait();
42
+ docker . Images . CreateImageAsync ( new ImagesCreateParameters ( ) { Parent = ImageName , Tag = "latest" , } , null ) . Wait ( ) ;
43
+
44
+ var container = docker . Containers . CreateContainerAsync ( new CreateContainerParameters ( )
45
+ {
46
+ Image = $ "{ ImageName } :latest",
47
+ Name = "workflowcore-sqlserver-tests" ,
48
+ HostConfig = hostCfg ,
49
+ Env = env
50
+ } ) . Result ;
51
+
52
+ bool started = docker . Containers . StartContainerAsync ( container . ID , new ContainerStartParameters ( ) ) . Result ;
53
+ if ( started )
54
+ {
55
+ containerId = container . ID ;
56
+ Console . Write ( "Waiting 10 seconds for SQL Server to start in the docker container..." ) ;
57
+ Thread . Sleep ( 10000 ) ; //allow time for SQL to start
58
+ Console . WriteLine ( "10 seconds are up." ) ;
59
+
60
+ Console . WriteLine ( "Docker container started: " + containerId ) ;
61
+ ConnectionString = $ "Server=127.0.0.1,{ Port } ;Database=workflowcore-tests;User Id=sa;Password={ SqlPassword } ;";
62
+ }
63
+ else
64
+ {
65
+ Console . WriteLine ( "Docker container failed" ) ;
66
+ }
67
+ }
68
+
69
+ public void Dispose ( )
70
+ {
71
+ docker . Containers . KillContainerAsync ( containerId , new ContainerKillParameters ( ) ) . Wait ( ) ;
72
+ docker . Containers . RemoveContainerAsync ( containerId , new ContainerRemoveParameters ( ) { Force = true } ) . Wait ( ) ;
73
+ }
74
+ }
75
+
76
+ [ CollectionDefinition ( "SqlServer collection" ) ]
77
+ public class SqlServerCollection : ICollectionFixture < DockerSetup >
78
+ {
79
+ }
80
+
81
+ }
0 commit comments