Skip to content

Commit b37601b

Browse files
committed
First commit - Basic tests
0 parents  commit b37601b

14 files changed

+1882
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.ipynb_checkpoints

DockerInteraction.ipynb

+263
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"text/html": [
11+
"Installing package Docker.DotNet........................................done!"
12+
]
13+
},
14+
"metadata": {},
15+
"output_type": "display_data"
16+
},
17+
{
18+
"data": {
19+
"text/html": [
20+
"Successfully added reference to package Docker.DotNet, version 3.125.2"
21+
]
22+
},
23+
"metadata": {},
24+
"output_type": "display_data"
25+
},
26+
{
27+
"data": {
28+
"text/html": [
29+
"Installing package Npgsql.................done!"
30+
]
31+
},
32+
"metadata": {},
33+
"output_type": "display_data"
34+
},
35+
{
36+
"data": {
37+
"text/html": [
38+
"Successfully added reference to package Npgsql, version 4.1.1"
39+
]
40+
},
41+
"metadata": {},
42+
"output_type": "display_data"
43+
}
44+
],
45+
"source": [
46+
"#r \"nuget:Docker.DotNet\"\n",
47+
"#r \"nuget:Npgsql\"\n",
48+
"\n",
49+
"using Docker.DotNet;\n",
50+
"using Docker.DotNet.Models;\n",
51+
"using Npgsql;\n",
52+
"using System.Threading;\n",
53+
"using System.Threading.Tasks;\n",
54+
"using System.Data.Common;"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 2,
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"private async Task<(CreateContainerResponse, string)> GetContainer(DockerClient client, string image, string tag)\n",
64+
"{\n",
65+
" var hostPort = new Random((int)DateTime.UtcNow.Ticks).Next(10000, 12000);\n",
66+
" //look for image\n",
67+
" var images = await client.Images.ListImagesAsync(new ImagesListParameters()\n",
68+
" {\n",
69+
" MatchName = $\"{image}:{tag}\",\n",
70+
" }, CancellationToken.None);\n",
71+
"\n",
72+
" //check if container exists\n",
73+
" var pgImage = images.FirstOrDefault();\n",
74+
" if (pgImage == null)\n",
75+
" throw new Exception($\"Docker image for {image}:{tag} not found.\");\n",
76+
"\n",
77+
" //create container from image\n",
78+
" var container = await client.Containers.CreateContainerAsync(new CreateContainerParameters()\n",
79+
" {\n",
80+
" User = \"postgres\",\n",
81+
" Env = new List<string>()\n",
82+
" {\n",
83+
" \"POSTGRES_PASSWORD=password\",\n",
84+
" \"POSTGRES_DB=repotest\",\n",
85+
" \"POSTGRES_USER=postgres\"\n",
86+
" },\n",
87+
" ExposedPorts = new Dictionary<string, EmptyStruct>()\n",
88+
" {\n",
89+
" [\"5432\"] = new EmptyStruct()\n",
90+
" },\n",
91+
" HostConfig = new HostConfig()\n",
92+
" {\n",
93+
" PortBindings = new Dictionary<string, IList<PortBinding>>()\n",
94+
" {\n",
95+
" [\"5432\"] = new List<PortBinding>()\n",
96+
" {new PortBinding() {HostIP = \"0.0.0.0\", HostPort = $\"{hostPort}\"}}\n",
97+
" }\n",
98+
" },\n",
99+
" Image = $\"{image}:{tag}\",\n",
100+
" }, CancellationToken.None);\n",
101+
"\n",
102+
" if (!await client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters()\n",
103+
" {\n",
104+
" DetachKeys = $\"d={image}\"\n",
105+
" }, CancellationToken.None))\n",
106+
" {\n",
107+
" throw new Exception($\"Could not start container: {container.ID}\");\n",
108+
" }\n",
109+
"\n",
110+
" var count = 10;\n",
111+
" Thread.Sleep(5000);\n",
112+
" var containerStat = await client.Containers.InspectContainerAsync(container.ID, CancellationToken.None);\n",
113+
" while (!containerStat.State.Running && count-- > 0)\n",
114+
" {\n",
115+
" Thread.Sleep(1000);\n",
116+
" containerStat = await client.Containers.InspectContainerAsync(container.ID, CancellationToken.None);\n",
117+
"\n",
118+
" }\n",
119+
" Thread.Sleep(10000); //I need some time for the DB to finish starting up so that my tests don't report the DB is starting up\n",
120+
" return (container, $\"{hostPort}\");\n",
121+
"}"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": 3,
127+
"metadata": {},
128+
"outputs": [],
129+
"source": [
130+
"DbConnection Db;\n",
131+
"DockerClient _client;\n",
132+
"CreateContainerResponse _containerResponse;\n",
133+
"\n",
134+
"_client = new DockerClientConfiguration(new Uri(\"npipe://./pipe/docker_engine\")).CreateClient();\n",
135+
"\n"
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": 4,
141+
"metadata": {},
142+
"outputs": [],
143+
"source": [
144+
"//var (containerRespose, port) = await GetContainer(_client, \"postgres\", \"latest\");\n",
145+
"//_containerResponse = containerRespose;"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": 5,
151+
"metadata": {},
152+
"outputs": [],
153+
"source": [
154+
"var hostPort = new Random((int)DateTime.UtcNow.Ticks).Next(10000, 12000);\n",
155+
"\n",
156+
"var images = await _client.Images.ListImagesAsync(new ImagesListParameters()\n",
157+
" {\n",
158+
" MatchName = \"postgres:latest\",\n",
159+
" }, CancellationToken.None);\n",
160+
"\n",
161+
"//check if container exists\n",
162+
" var pgImage = images.FirstOrDefault();\n",
163+
" if (pgImage == null)\n",
164+
" throw new Exception($\"Docker image for postgres:latest not found.\");\n",
165+
"\n",
166+
" //create container from image\n",
167+
" var container = await _client.Containers.CreateContainerAsync(new CreateContainerParameters()\n",
168+
" {\n",
169+
" User = \"postgres\",\n",
170+
" Env = new List<string>()\n",
171+
" {\n",
172+
" \"POSTGRES_PASSWORD=password\",\n",
173+
" \"POSTGRES_DB=repotest\",\n",
174+
" \"POSTGRES_USER=postgres\"\n",
175+
" },\n",
176+
" ExposedPorts = new Dictionary<string, EmptyStruct>()\n",
177+
" {\n",
178+
" [\"5432\"] = new EmptyStruct()\n",
179+
" },\n",
180+
" HostConfig = new HostConfig()\n",
181+
" {\n",
182+
" PortBindings = new Dictionary<string, IList<PortBinding>>()\n",
183+
" {\n",
184+
" [\"5432\"] = new List<PortBinding>()\n",
185+
" {new PortBinding() {HostIP = \"0.0.0.0\", HostPort = $\"{hostPort}\"}}\n",
186+
" }\n",
187+
" },\n",
188+
" Image = \"postgres:latest\",\n",
189+
" }, CancellationToken.None);\n",
190+
" \n",
191+
" if (!await _client.Containers.StartContainerAsync(container.ID, new ContainerStartParameters()\n",
192+
" {\n",
193+
" DetachKeys = \"d=postgres\"\n",
194+
" }, CancellationToken.None))\n",
195+
" {\n",
196+
" throw new Exception($\"Could not start container: {container.ID}\");\n",
197+
" }\n",
198+
"\n",
199+
" var count = 10;\n",
200+
" Thread.Sleep(5000);\n",
201+
" var containerStat = await _client.Containers.InspectContainerAsync(container.ID, CancellationToken.None);\n",
202+
" while (!containerStat.State.Running && count-- > 0)\n",
203+
" {\n",
204+
" Thread.Sleep(1000);\n",
205+
" containerStat = await _client.Containers.InspectContainerAsync(container.ID, CancellationToken.None);\n",
206+
"\n",
207+
" }\n",
208+
" Thread.Sleep(10000); //I need some time for the DB to finish starting up so that my tests don't report the DB is starting up\n"
209+
]
210+
},
211+
{
212+
"cell_type": "code",
213+
"execution_count": 6,
214+
"metadata": {},
215+
"outputs": [
216+
{
217+
"data": {
218+
"text/plain": [
219+
"True"
220+
]
221+
},
222+
"execution_count": 6,
223+
"metadata": {},
224+
"output_type": "execute_result"
225+
}
226+
],
227+
"source": [
228+
"var connectionStringBuilder = new NpgsqlConnectionStringBuilder() { ConnectionString = $\"User ID=postgres;Password=password;Server=127.0.0.1;Port={hostPort};Database=repotest;Integrated Security=true;Pooling=false;CommandTimeout=300\" };\n",
229+
"Db = new NpgsqlConnection(connectionStringBuilder.ConnectionString);\n",
230+
"Db.Open();\n",
231+
"\n",
232+
"return String.Equals(\"repotest\", Db.Database);\n",
233+
"\n",
234+
"Db.Close(); \n",
235+
"Db.Dispose();\n",
236+
"//stop container\n",
237+
"if (await _client.Containers.StopContainerAsync(_containerResponse.ID, new ContainerStopParameters(), CancellationToken.None))\n",
238+
"{\n",
239+
" //delete container\n",
240+
" await _client.Containers.RemoveContainerAsync(_containerResponse.ID, new ContainerRemoveParameters(), CancellationToken.None);\n",
241+
"}\n",
242+
"\n",
243+
"_client?.Dispose();"
244+
]
245+
}
246+
],
247+
"metadata": {
248+
"kernelspec": {
249+
"display_name": ".NET (C#)",
250+
"language": "C#",
251+
"name": ".net-csharp"
252+
},
253+
"language_info": {
254+
"file_extension": ".cs",
255+
"mimetype": "text/x-csharp",
256+
"name": "C#",
257+
"pygments_lexer": "csharp",
258+
"version": "8.0"
259+
}
260+
},
261+
"nbformat": 4,
262+
"nbformat_minor": 2
263+
}

0 commit comments

Comments
 (0)